Applying Spatial Filter to Query FDO data using Geospatial Platform API in AutoCAD Map 3D
In AutoCAD Map 3D, using the UI tools, we can apply spatial filter to query FDO data. To give an example, in the screenshot below, if we want to see the Road feature (FDO Feature) inside of the highlighted parcel (FDO feature), we can apply the Map 3D Query Filter Data UI tools and see the road features inside of the parcel. “Inside” is just an example here, we can apply many other spatial conditions like intersects , Overlaps etc.
If we want to achieve the same using Map 3D API, we can use Map 3D Geospatial Platform API SetSpatialFilter and MgFeatureSpatialOperations.Intersects spatial operation.
Here is a C# .NET code snippet demonstrating this -
public void MapSpatialQueryFilterDemo()
{
try
{
// Selct using API
AcMapMap currentMap = AcMapMap.GetCurrentMap();
MgLayerCollection layers = currentMap.GetLayers();
MgLayerBase layer = layers.GetItem("Roads"); // specific to Roads SDF data
MgFeatureService fs = AcMapServiceFactory.GetService(MgServiceType.FeatureService) as MgFeatureService;
string fsId = layer.GetFeatureSourceId();
string className = layer.GetFeatureClassName();
// Selecting the Boundary feature
// In this case the Parcel boundary
PromptSelectionResult selResult = ed.GetSelection();
SelectionSet selSet = selResult.Value;
MgSelectionBase selection = AcMapFeatureEntityService.GetSelection(selSet);
MgLayerBase layerFrame = null;
foreach (MgLayerBase testlayer in selection.GetLayers())
{
if (testlayer.Name == "Parcels")
{
layerFrame = testlayer;
break;
}
}
//get the properties
MgFeatureReader reader = selection.GetSelectedFeatures(layerFrame, layerFrame.FeatureClassName, false);
MgAgfReaderWriter agfReadWrite = new MgAgfReaderWriter();
MgGeometry boundaryGeomtry = null;
try
{
if (reader.ReadNext())
{
boundaryGeomtry = agfReadWrite.Read(reader.GetGeometry(layerFrame.GetFeatureGeometryName()));
}
}
finally
{
reader.Close();
}
//spatial relationship inside filter
MgFeatureQueryOptions query = new MgFeatureQueryOptions();
query.SetSpatialFilter("Geometry", boundaryGeomtry, MgFeatureSpatialOperations.Inside);
//Get the features
MgResourceIdentifier resId = new MgResourceIdentifier(fsId);
MgFeatureReader ftrRdr = fs.SelectFeatures(resId, className, query);
//Display the ID and Other Property of the selected features
while (ftrRdr.ReadNext())
{
int id = ftrRdr.GetInt32("Autogenerated_SDF_ID");
string roadName = ftrRdr.GetString("ST_NAME");
ed.WriteMessage("\nID: " + id + " " + "Road Name : " + roadName.ToString());
}
ftrRdr.Close();
}
catch (MgException ex)
{
ed.WriteMessage(ex.Message.ToString());
}
}
And the result of the same :
Hope this is useful to you.