How to add your knowledge

Creating Geometry Objects From Features

    Table of contents
    No headers

    You may want to use an existing feature as part of a spatial query. To retrieve the feature’s geometry and convert it into an appropriate format for a query, perform the following steps:

    • Create a query that will select the feature.
    • Query the feature class containing the feature using AcMapLayer.SelectFeatures() or MgFeatureService.SelectFeatures().
    • Obtain the feature from the query using the MgFeatureReader.ReadNext() method.
    • Get the geometry data from the feature using the MgFeatureReader.GetGeometry() method. This data is in AGF binary format.
    • Convert the AGF data to an MgGeometry object using the MgAgfReaderWriter.Read() method.

    For example, the following sequence creates an MgGeometry object representing the boundaries of zone 1 in the layer named “zones”. It creates an MgGeometry object and the WKT representation of that object.

    MgLayerCollection layers = currentMap.GetLayers();
    MgLayerBase layer = layers.GetItem("zones");
    string fsId = layer.GetFeatureSourceId();
    string className = layer.GetFeatureClassName();
    MgFeatureQueryOptions query =
      new MgFeatureQueryOptions();
    query.SetFilter("ZONE_ID = 1");
    MgResourceIdentifier resId =
      new MgResourceIdentifier(fsId);
     
    MgFeatureReader featureReader =
      fs.SelectFeatures(resId,
       className, query);
    if (featureReader.ReadNext())
    {
      string geometryName = layer.GetFeatureGeometryName();
      MgByteReader geometryData = 
        featureReader.GetGeometry(geometryName);
      MgAgfReaderWriter agfReaderWriter = new MgAgfReaderWriter();
      MgGeometry geometry = agfReaderWriter.Read(geometryData);
     
      MgWktReaderWriter wktReaderWriter = new MgWktReaderWriter();
      string wkt = wktReaderWriter.Write(geometry);
    }
    

    The following assumes that another feature class has a geometry property SHPGEOM. It uses the WKT string in a query to find features in the other feature class that intersect the zone:

    MgFeatureQueryOptions queryOpts = new MgFeatureQueryOptions();
    queryOpts.SetFilter("SHPGEOM intersects GEOMFROMTEXT(" + 
      wkt + ")");