Monday, February 4, 2013

GeoMedia code snippet to apply a workspace spatial filter

In GeoMedia, a Spatial filter is a means to improve the application performance by reducing the amount of spatial data for display and processing. Typically, this is done using an arbitrary polygon as the spatial filter boundary.

The following C# code snippet can be used as a method in a driving GeoMedia type custom application to apply a rectangular spatial filter to the GeoMedia workspace.

using GMService = Intergraph.GeoMedia.GMService;
using PService = Intergraph.GeoMedia.PService;
using PBasic = Intergraph.GeoMedia.PBasic;
using PClient = Intergraph.GeoMedia.PClient;
private GeoMedia.Application _application = null;

public void ApplyWorkspaceSpatialFilter(PBasic.point lowerLeftPoint, PBasic.point upperRightPoint)
//Declare variables
PBasic.PolygonGeometry polygon;
PBasic.point pnt;
object objBlob;
PClient.GeometryStorageService storageSvc;
GMService.ApplySpatialFilterService spatFilterSvc;
//Create a new instance of the ApplySpatialFilterService
spatFilterSvc = new GMService.ApplySpatialFilterService(); 
//Create a new polygonGeometry object for the spatial filter polygon
polygon = (PBasic.PolygonGeometry) _application.CreateService("GeoMedia.PolygonGeometry");

//Create a new GeoMedia point object
pnt = (PBasic.point)_application.CreateService("GeoMedia.point");

//Populate the spatial filter polygon object with the bounding rectangle
pnt.X = lowerLeftPoint.X;
pnt.Y = lowerLeftPoint.Y;            
polygon.Points.Add ( pnt, null);
pnt.X = lowerLeftPoint.X;
pnt.Y = upperRightPoint.Y;
polygon.Points.Add (pnt, null);
pnt.X = upperRightPoint.X;
pnt.Y = upperRightPoint.Y;
polygon.Points.Add (pnt, null);
pnt.X = upperRightPoint.X;
pnt.Y = lowerLeftPoint.Y;
polygon.Points.Add (pnt, null);
pnt.X = lowerLeftPoint.X;
pnt.Y = lowerLeftPoint.Y;
polygon.Points.Add (pnt, null);
//Create a new instance of the GeometryStorageService object
storageSvc = (PClient.GeometryStorageService) _application.CreateService("GeoMedia.GeometryStorageService");

//Convert the polygonGeometry object into a blob
storageSvc.GeometryToStorage(polygon, out objBlob);
//Assign the polygonGeometry to the SpatialFilterService object and apply the spatial filtering to
//the GeoMedia application workspace
spatFilterSvc.Application = this._application;
spatFilterSvc.SpatialFilterOperator = PClient.GConstants.gdbTouches;
spatFilterSvc.SpatialFilterGeometry = objBlob;
spatFilterSvc.SpatialFilterName = "SpatialFilter1";
spatFilterSvc.UseGeometryMBR = false;
//Free up resources
objBlob = null;
if (spatFilterSvc != null) Marshal.FinalReleaseComObject(spatFilterSvc);
if (storageSvc != null) Marshal.FinalReleaseComObject(storageSvc);
if (polygon != null) Marshal.FinalReleaseComObject(polygon);
if (pnt != null) Marshal.FinalReleaseComObject(pnt);

No comments:

Related Posts with Thumbnails