Creating a Buffer

AutoCAD Map 3D Geospatial Platform API

 
Creating a Buffer
 
 
 

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);

If the buffer is to be calculated using coordinate system units, create an MgCoordinateSystemMeasure object from the coordinate system for the map. For example:

string mapWktSrs = currentMap.GetMapSRS();
MgCoordinateSystemFactory coordSysFactory = 
  new MgCoordinateSystemFactory();
MgCoordinateSystem srs = coordSysFactory.Create(mapWktSrs);
MgCoordinateSystemMeasure srsMeasure = srs.GetMeasure();

Use the coordinate system measure to determine the buffer size in the coordinate system, and create the buffer object from the geometry to be buffered.

double srsDist = 
  srs.ConvertMetersToCoordinateSystemUnits(bufferDist);
MgGeometry bufferGeometry = 
  featureGeometry.Buffer(srsDist, srsMeasure);

To display the buffer in the map, perform the following steps:

  • Create a feature source for the buffer. This may require creating a new file for the feature source. See Creating SDF files.
  • Create a layer that references the feature source. Add it to the map and make it visible.
  • Create a new feature using the buffer geometry and insert it into the feature source.

For example, the following code assumes that the layer resLayer has been created using a feature source with a geometry property of Geometry. It adds a new feature using the buffer geometry.

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);