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:
$geometryData = $featureReader->GetGeometry($geometryName); $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:
$mapWktSrs = $currentMap->GetMapSRS(); $coordSysFactory = new MgCoordinateSystemFactory(); $srs = $coordSysFactory->Create($mapWktSrs); $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.
$srsDist = $srs->ConvertMetersToCoordinateSystemUnits($bufferDist); $bufferGeometry = $featureGeometry->Buffer($srsDist, $srsMeasure);
To display the buffer in the map, perform the following steps:
To use the buffer as part of a query, create a spatial filter using the buffer geometry, and use this in a call to MgFeatureService::SelectFeatures(). For example, the following code selects 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.
$queryOptions = new MgFeatureQueryOptions(); $queryOptions->SetFilter("RTYPE = 'MFG'"); $queryOptions->SetSpatialFilter('SHPGEOM', $bufferGeometry, MgFeatureSpatialOperations::Inside); $featureResId = new MgResourceIdentifier( "Library://Samples/Sheboygan/Data/Parcels.FeatureSource"); $featureReader = $featureService->SelectFeatures($featureResId, "Parcels", $queryOptions);