This example builds on the example from Working With the Active Selection. Instead of listing the parcels in the selection, it creates a series of concentric buffers around the selection, showing increasing distance. The code sections below contain the significant additions in this example. The complete source code is available with the Developer’s Guide samples.
Because this example modifies the map, it must refresh the map when it loads, by executing a JavaScript function. Add the function to the page.
<script language="javascript"> function OnPageLoad() { parent.parent.Refresh(); } </script>
Add an OnLoad command to the <body> element:
<body onLoad="OnPageLoad()">
The example uses a temporary map layer named Buffer to store the buffer feature. It creates a feature source and the layer if it does not exist. Otherwise, it deletes any existing features before creating the new buffer. The functions CreateBufferFeatureSource() and CreateBufferLayer() are in bufferfunctions.php, which is described below.
include 'bufferfunctions.php'; $bufferRingSize = 100; // measured in metres $bufferRingCount = 5; // Set up some objects for coordinate conversion $mapWktSrs = $map->GetMapSRS(); $agfReaderWriter = new MgAgfReaderWriter(); $wktReaderWriter = new MgWktReaderWriter(); $coordinateSystemFactory = new MgCoordinateSystemFactory(); $srs = $coordinateSystemFactory->Create($mapWktSrs); $srsMeasure = $srs->GetMeasure(); // Check for a buffer layer. If it exists, delete // the current features. // If it does not exist, create a feature source and // a layer to hold the buffer. try { $bufferLayer = $map->GetLayers()->GetItem('Buffer'); $bufferFeatureResId = new MgResourceIdentifier( $bufferLayer->GetFeatureSourceId()); $commands = new MgFeatureCommandCollection(); $commands->Add(new MgDeleteFeatures('BufferClass', "ID >= 0")); $featureService->UpdateFeatures($bufferFeatureResId, $commands, false); } catch (MgObjectNotFoundException $e) { // When an MgObjectNotFoundException is thrown, the layer // does not exist and must be created. $bufferFeatureResId = new MgResourceIdentifier("Session:" . $mgSessionId . "//Buffer.FeatureSource"); CreateBufferFeatureSource($featureService, $mapWktSrs, $bufferFeatureResId); $bufferLayer = CreateBufferLayer($resourceService, $bufferFeatureResId, $mgSessionId); $map->GetLayers()->Insert(0, $bufferLayer); }
The geometries for the selected features are merged into a single multi-geometry. Then a series of concentric buffers is created and added to the feature source. The style for the layer, which is set when function CreateBufferLayer() processes bufferlayerdefinition.xml, should define the buffer features to be partly transparent. When they are drawn on the map, the rings get progressively darker towards the center of the buffer area.
// Process each item in the MgFeatureReader. // Merge them into a single feature. $inputGeometries = new MgGeometryCollection(); while ($featureReader->ReadNext()) { $featureGeometryData = $featureReader->GetGeometry('SHPGEOM'); $featureGeometry = $agfReaderWriter->Read($featureGeometryData); $inputGeometries->Add($featureGeometry); } $geometryFactory = new MgGeometryFactory(); $mergedFeatures = $geometryFactory-> CreateMultiGeometry($inputGeometries); // Add buffer features to the temporary feature source. // Create multiple concentric buffers to show area. $commands = new MgFeatureCommandCollection(); for ($bufferRing = 0; $bufferRing < $bufferRingCount; $bufferRing++) { $bufferDist = $srs-> ConvertMetersToCoordinateSystemUnits($bufferRingSize * ($bufferRing + 1)); $bufferGeometry = $mergedFeatures->Buffer($bufferDist, $srsMeasure); $properties = new MgPropertyCollection(); $properties->Add(new MgGeometryProperty('BufferGeometry', $agfReaderWriter->Write($bufferGeometry))); $commands->Add(new MgInsertFeatures('BufferClass', $properties)); } $results = $featureService->UpdateFeatures($bufferFeatureResId, $commands, false); $bufferLayer->SetVisible(true); $bufferLayer->ForceRefresh(); $bufferLayer->SetDisplayInLegend(true); $map->Save($resourceService);
The functions CreateBufferFeatureSource() and CreateBufferLayer() are in bufferfunctions.php. CreateBufferFeatureSource() creates a temporary feature source, with a single feature class, BufferClass. The feature class has two properties, ID and BufferGeometry. ID is autogenerated, so it does not need to be added with a new feature. CreateBufferLayer() modifies a layer definition from an external file and saves it to the repository. For more details, see Modifying Maps and Layers.
function CreateBufferFeatureSource($featureService, $wkt, $bufferFeatureResId) { $bufferClass = new MgClassDefinition(); $bufferClass->SetName('BufferClass'); $properties = $bufferClass->GetProperties(); $idProperty = new MgDataPropertyDefinition('ID'); $idProperty->SetDataType(MgPropertyType::Int32); $idProperty->SetReadOnly(true); $idProperty->SetNullable(false); $idProperty->SetAutoGeneration(true); $properties->Add($idProperty); $polygonProperty = new MgGeometricPropertyDefinition('BufferGeometry'); $polygonProperty-> SetGeometryTypes(MgFeatureGeometricType::Surface); $polygonProperty->SetHasElevation(false); $polygonProperty->SetHasMeasure(false); $polygonProperty->SetReadOnly(false); $polygonProperty->SetSpatialContextAssociation('defaultSrs'); $properties->Add($polygonProperty); $idProperties = $bufferClass->GetIdentityProperties(); $idProperties->Add($idProperty); $bufferClass-> SetDefaultGeometryPropertyName('BufferGeometry'); $bufferSchema = new MgFeatureSchema('BufferLayerSchema', 'temporary schema to hold a buffer'); $bufferSchema->GetClasses()->Add($bufferClass); $sdfParams = new MgCreateSdfParams('defaultSrs', $wkt, $bufferSchema); $featureService->CreateFeatureSource($bufferFeatureResId, $sdfParams); } function CreateBufferLayer($resourceService, $bufferFeatureResId, $sessionId) { // Load the layer definition template into // a PHP DOM object, find the "ResourceId" element, and // modify its content to reference the temporary // feature source. $doc = DOMDocument::load('bufferlayerdefinition.xml'); $featureSourceNode = $doc->getElementsByTagName( 'ResourceId')->item(0); $featureSourceNode->nodeValue = $bufferFeatureResId->ToString(); // Get the updated layer definition from the DOM object // and save it to the session repository using the // ResourceService object. $layerDefinition = $doc->saveXML(); $byteSource = new MgByteSource($layerDefinition, strlen($layerDefinition)); $byteSource->SetMimeType(MgMimeType::Xml); $tempLayerResId = new MgResourceIdentifier("Session:" . $sessionId . "//Buffer.LayerDefinition"); $resourceService->SetResource($tempLayerResId, $byteSource->GetReader(), null); // Create an MgLayer object based on the new layer definition // and return it to the caller. $bufferLayer = new MgLayer($tempLayerResId, $resourceService); $bufferLayer->SetName("Buffer"); $bufferLayer->SetLegendLabel("Buffer"); $bufferLayer->SetDisplayInLegend(true); $bufferLayer->SetSelectable(false); return $bufferLayer; }
There is an additional example in the Developer’s Guide samples. It queries the parcels in the buffer area and selects parcels that match certain criteria. The selection is done using a query that combines a basic filter and a spatial filter.
$bufferDist = $srs-> ConvertMetersToCoordinateSystemUnits($bufferRingSize); $bufferGeometry = $mergedGeometries->Buffer($bufferDist, $srsMeasure); // Create a filter to select parcels within the buffer. Combine // a basic filter and a spatial filter to select all parcels // within the buffer that are of type "MFG". $queryOptions = new MgFeatureQueryOptions(); $queryOptions->SetFilter("RTYPE = 'MFG'"); $queryOptions->SetSpatialFilter('SHPGEOM', $bufferGeometry, MgFeatureSpatialOperations::Inside);
It creates an additional feature source that contains point markers for each of the selected parcels.
// Get the features from the feature source, // determine the centroid of each selected feature, and // add a point to the ParcelMarker layer to mark the // centroid. // Collect all the points into an MgFeatureCommandCollection, // so they can all be added in one operation. $featureResId = new MgResourceIdentifier( "Library://Samples/Sheboygan/Data/Parcels.FeatureSource"); $featureReader = $featureService->SelectFeatures($featureResId, "Parcels", $queryOptions); $parcelMarkerCommands = new MgFeatureCommandCollection(); while ($featureReader->ReadNext()) { $byteReader = $featureReader->GetGeometry('SHPGEOM'); $geometry = $agfReaderWriter->Read($byteReader); $point = $geometry->GetCentroid(); // Create an insert command for this parcel. $properties = new MgPropertyCollection(); $properties->Add(new MgGeometryProperty('ParcelLocation', $agfReaderWriter->Write($point))); $parcelMarkerCommands->Add( new MgInsertFeatures('ParcelMarkerClass', $properties)); } $featureReader->Close(); if ($parcelMarkerCommands->GetCount() > 0) { $featureService->UpdateFeatures($parcelFeatureResId, $parcelMarkerCommands, false); } else { echo '</p><p>No parcels within the buffer area match.'; }