Query Example
 
 
 

In the No label chapter, we created an instance of the FdoFeatureClass SampleFeatureClass and assigned values to its integer, string, and geometry properties (see Example: Inserting an Integer, a String, and a Geometry Value). The sample code in the following query example selects this instance and retrieves the values of its properties. Specifically, the sample code does the following:

  1. Creates the select command, and
  2. Points the select command at the target FdoFeatureClass SampleFeatureClass, and
  3. Creates a filter to identify which instance of SampleFeatureClass to select, and
  4. Points the select command at the filter, and
  5. Executes the command, which returns an FdoIFeatureReader object, and
  6. Loops through the feature reader object, which contains one or more query results depending on the filter arguments. In the sample code provided, there is only one result.
  7. Finally, the code extracts the property values from each query result.
// we have one FdoFeatureClass object in the DataStore
// create a query that returns this object
// create the select command
GisPtr<FdoISelect> sampleSelect;
sampleSelect = (FdoISelect *)
  connection->CreateCommand(FdoCommandType_Select);
// point the select command at the target FdoFeatureClass
// SampleFeatureClass
sampleSelect->SetFeatureClassName(L"SampleFeatureClass");
// create the filter by
// 1. creating an FdoIdentifier object containing the name of 
//   the identity property
GisPtr<FdoIdentifier> queryPropertyName;
queryPropertyName =
  FdoIdentifier::Create(L"SampleIdentityDataProperty");
// 2. creating an FdoDataValue object containing the value of the
//   identity property
GisPtr<FdoDataValue> queryPropertyValue;
queryPropertyValue = FdoDataValue::Create(101);
// 3. calling FdoComparisonCondition::Create() passing in the
//   the queryPropertyName, an enumeration constant signifying an
//   equals comparison operation, and the queryPropertyValue
GisPtr<FdoFilter> filter;
filter = FdoComparisonCondition::Create(queryPropertyName,
  FdoComparisonOperations_EqualTo, queryPropertyValue);
// point the select command at the filter
sampleSelect->SetFilter(filter);
// execute the select command
GisPtr<FdoIFeatureReader> queryResults;
queryResults = sampleSelect->Execute();
// declare variables needed to capture query results
GisPtr<FdoClassDefinition> classDef;
GisPtr<FdoPropertyDefinitionCollection> properties;
GisInt32 numProperties = 0;
FdoPropertyDefinition * propertyDef;
FdoPropertyType propertyType;
FdoDataType dataType;
FdoDataPropertyDefinition * dataPropertyDef;
GisString * propertyName = NULL;
GisPtr<GisByteArray> byteArray;
GisIGeometry * geometry = NULL;
GisGeometryType geometryType = GisGeometryType_None;
GisIPolygon * polygon = NULL;
GisILinearRing * exteriorRing = NULL;
GisILinearRing * interiorRing = NULL;
GisIDirectPosition * position = NULL;
GisInt32 dimensionality = GisDimensionality_XY;
GisInt32 numPositions = 0;
GisInt32 numInteriorRings = 0;
// loop through the query results
while (queryResults->ReadNext()) {
  // get the feature class object and its properties
  classDef = queryResults->GetClassDefinition();
  properties = classDef->GetProperties();
  // loop through the properties
  numProperties = properties->GetCount();
  for(int i = 0; i < numProperties; i++) {
    propertyDef = properties->GetItem(i);
    // get the property name and property type
    propertyName = propertyDef->GetName();
    propertyType = propertyDef->GetPropertyType();
    switch (propertyType) {
      // it’s a data property
      case FdoPropertyType_DataProperty:
        dataPropertyDef =
          dynamic_cast<FdoDataPropertyDefinition *>
          (propertyDef);
        dataType = dataPropertyDef->GetDataType();
        switch (dataType) {
          case FdoDataType_Boolean:
            break;
          case FdoDataType_Int32:
            break;
          case FdoDataType_String:
            break;
          default:
        }
        break;
      // it’s a geometric property
      // convert the byte array to a geometry
      // and determine the derived type of the geometry
      case FdoPropertyType_GeometricProperty:
        byteArray = queryResults->GetGeometry(propertyName);
        geometry =
          sampleGeometryFactory->CreateGeometryFromAgf 
          (byteArray);
        geometryType = geometry->GetDerivedType();
        // resolve the derived type into a list of ordinates
        switch (geometryType) {
          case GisGeometryType_None:
            break;
          case GisGeometryType_Point:
            break;
          case GisGeometryType_LineString:
            break;
          case GisGeometryType_Polygon:
            polygon = dynamic_cast<GisIPolygon *>(geometry);
            exteriorRing = polygon->GetExteriorRing();
            dimensionality = exteriorRing-
              >GetDimensionality();
            numPositions = exteriorRing->GetCount();
            double X, Y, Z, M;
            for(int i=0; i<numPositions; i++) {
              position = exteriorRing->GetItem(i);
              if (dimensionality & GisDimensionality_Z && 
                dimensionality & GisDimensionality_M) {
                X = position->GetX();
                Y = position->GetY();
                Z = position->GetZ();
                M = position->GetM();
              else if (dimensionality & GisDimensionality_Z 
              && !(dimensionality & GisDimensionality_M)) {
                X = position->GetX();
                Y = position->GetY();
                Z = position->GetZ();
              else {
                X = position->GetX();
                Y = position->GetY();
              }
            }
            numInteriorRings = polygon-
              >GetInteriorRingCount();
            for(int i=0; i<numInteriorRings; i++) {
              interiorRing = polygon->GetInteriorRing(i);
              // do same for interior ring as exterior ring
            }
            break;
          case GisGeometryType_MultiPoint:
            break;
          case GisGeometryType_MultiLineString:
            break;
          case GisGeometryType_MultiPolygon:
            break;
          case GisGeometryType_MultiGeometry:
            break;
          case GisGeometryType_CurveString:
            break;
          case GisGeometryType_CurvePolygon:
            break;
          case GisGeometryType_MultiCurveString:
            break;
          case GisGeometryType_MultiCurvePolygon:
            break;
          default:
        }
        break;
      default:
    }
  }
}