Creating Geometry Objects From Features

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 the MgFeatureService::SelectFeatures() method.
- 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 District 1 in the Sheboygan sample data.

    $districtQuery = new MgFeatureQueryOptions();

    $districtQuery->SetFilter("Autogenerated_SDF_ID = 1");

    $layer = $map->GetLayers()->GetItem('Districts');

    $featureReader = $layer->SelectFeatures($districtQuery);

    $featureReader->ReadNext();

    $districtGeometryData = $featureReader->GetGeometry('Data');

    $agfReaderWriter = new MgAgfReaderWriter();

    $districtGeometry = $agfReaderWriter->Read($districtGeometryData);

To convert an MgGeometry object into its WKT representation, use the MgWktReaderWriter::Write() method, as in the following example:

    $wktReaderWriter = new MgWktReaderWriter();

    $districtWkt = $wktReaderWriter->Write($districtGeometry);