To create a buffer around a feature, use the MgGeometry.Buffer() method. This returns an MgGeometry object that you can use for further analysis. For example, you could display the buffer by creating a feature in a temporary feature source and adding a new layer to the map. You could also use the buffer geometry as part of a spatial filter. For example, you might want to find all the features within the buffer zone that match certain criteria, or you might want to find all roads that cross the buffer zone.
To create a buffer, get the geometry of the feature to be buffered. If the feature is being processed in an MgFeatureReader as part of a selection, this requires getting the geometry data from the feature reader and converting it to an MgGeometry object. For example:
MgByteReader geometryData = featureReader.GetGeometry(geometryName); MgGeometry featureGeometry = agfReaderWriter.Read(geometryData);
string mapWktSrs = currentMap.GetMapSRS(); MgCoordinateSystemFactory coordSysFactory = new MgCoordinateSystemFactory(); MgCoordinateSystem srs = coordSysFactory.Create(mapWktSrs); MgCoordinateSystemMeasure srsMeasure = srs.GetMeasure();
double srsDist = srs.ConvertMetersToCoordinateSystemUnits(bufferDist); MgGeometry bufferGeometry = featureGeometry.Buffer(srsDist, srsMeasure);
MgPropertyCollection properties = new MgPropertyCollection(); properties.Add(new MgGeometryProperty("Geometry", agfReaderWriter.Write(bufferGeometry))); MgFeatureCommandCollection commands = new MgFeatureCommandCollection(); commands.Add(new MgInsertFeatures(resFeatureClassName, properties)); resLayer.UpdateFeatures(commands);
To use the buffer as part of a query, create a spatial filter using the buffer geometry, and use this in a call to AcMapLayer.SelectFeatures(). For example, the following code uses a basic filter and a spatial filter to select parcels inside the buffer area that are of type “MFG”. You can use the MgFeatureReader to perform tasks like generating a report of the parcels, or creating a new layer that puts point markers on each parcel.
MgFeatureQueryOptions queryOptions = new MgFeatureQueryOptions(); queryOptions.SetFilter("PARCELTYPE = 'MFG'"); queryOptions.SetSpatialFilter('SHPGEOM', bufferGeometry, MgFeatureSpatialOperations.Inside); MgResourceIdentifier featureResId = new MgResourceIdentifier( "Library://Data/Parcels.FeatureSource"); MgFeatureReader featureReader = layer.SelectFeatures(queryOptions);