Finding Entities with the MapPoint Web Service Find APIs - Finding Polygons (Page 3 of 4 )
With the find methods, you have seen how to find places, addresses, and points around a place or address, but all you have been finding so far are points (latitude and longitude coordinates). You may have a requirement to find polygons in situations with queries such as: “find all polygons that contain a point (latitude/longitude)” or “find all polygons that have spatial relationship with a rectangle.” In order to accomplish such tasks, use the FindServiceSoap.FindPolygon method.
To learn more about polygons, refer to Appendix B.
Like any find method in Find Service, theFindPolygonmethod takes theFindPolygonSpecificationobject as an argument and returns a validFindResultsobject. TheFindPolygonSpecificationobject provides a way for you to specify arguments such as data source name and spatial filter. Table 6-8 shows the fields exposed by theFindPolygonSpecificationclass.
Table 6-8. Fields of the FindPolygonSpecification class
| Field | Description |
| DataSourceName | Name of the data source as a string |
| Filter | The filter (FindFilterobject) to apply to the results, including the specific entity type, properties, and values that the returned results must match |
| Options | The search options (FindOptionsobject), which may include the range of results and a flag to identify which objects are desired in the returned results |
| SpatialFilter | The spatial filter (SpatialFilterobject) to apply to the results |
One interesting field from Table 6-8 is theSpatialFilter field; this field is of typeSpatialFilterclass, and it defines the spatial relationship between polygons, points, and rectangles. TheSpatialFilterclass is an abstract class, and there are two classes that derive this abstract class to define two specific spatial relationships:
LatLongSpatialFilter
Defines a spatial filter that returns only polygons that include the point specified by theLatLongobject. This is used in specifying a spatial filter to find polygons that contain a certain point. This class has only one field that takes the target point as aLatLongobject. The following code shows how to specify aLatLongSpatialFilterto find polygons that contain a given set of latitude and longitude coordinates:
//Create a new instance of LatLongSpatialFilter
LatLongSpatialFilter filter = new LatLongSpatialFilter();
//Assign the given latitude and longitude values
Filter.LatLong = new LatLong();
Filter.LatLong.Latitude = 47.44;
Filter.LatLong.Longitude = -122.55;
LatLongRectangleSpatialFilter
Defines a spatial filter that returns polygons related to theLatLongRectanglespecified via theBoundingRectanglefield. The relation between the polygons and the rectangle is determined by thePolygonRectangleRelationfield. This field is of typeSpatialRelationenumeration and has two values that are shown in Table 6-10. TheLatLongRectangleSpatialFilterclass is used in defining a spatial filter to find polygons that fall within or touch a rectangle. The following code shows how to define this spatial filter to find all polygons that fall within a rectangle:
//Define a new instance of LatLongRectanglSpatialFilter
LatLongRectangleSpatialFilter rectangleFilter =
new LatLongRectangleSpatialFilter();
//Define a bounding rectangle with north east and south west
//corners
LatLongRectangle boundingRectangle = new LatLongRectangle();
boundingRectangle.Northeast = new LatLong();
boundingRectangle.Northeast.Latitude = 47.44;
boundingRectangle.Northeast.Latitude =
-122.56;
boundingRectangle.Southwest = new LatLong();
boundingRectangle.Southwest.Latitude = 41.44;
boundingRectangle.Southwest.Latitude =
-119.56;
//Now assign bounding rectangle to the filter
rectangleFilter.BoundingRectangle = boundingRectangle;
//Define the spatial relationship to be
//"find polygons inside the rectangle"
rectangleFilter.PolygonRectangleRelation =
SpatialRelation.WithinArea;
Now that you know how to define spatial filters, let’s look at theFindPolygonmethod in action, using the relations shown in Table 6-9.
Table 6-9. SpatialRelation enumeration
| Item | Description |
| WithinArea | Returns all polygons contained entirely within the specified rectangle |
| TouchesArea | Returns all polygons that come into contact with the specified rectangle |
Use theFindPolygonmethod to findPolygonsthat either contain a specified point or are spatially related to a rectangle. Theymethod takes theFindPolygonSpecificationobject as an argument, as shown in the following code:
//Create an instance of FindServiceSoap and assign
//Credentials
FindServiceSoap findService
= new FindserviceSoap();
//Assign your credentials
. . .
//Create an instance of FindPlygonSpecification
FindPolygonSpecification findPolySpec
= new FindPolygonSpecification();
//Create a new instance of LatLongSpatialFilter
LatLongSpatialFilter filter = new LatLongSpatialFilter();
//Assign the given latitude and longitude values
Filter.LatLong = new LatLong();
Filter.LatLong.Latitude = 47.44;
Filter.LatLong.Longitude = -122.55;
//Assign the spatial filter to the find polygon specification
findPolySpec.SpatialFilter=filter;
//Assign your polygon data source
findPolySpec.DataSourceName="your polygon data source";
//Define what kind of entities you are looking for
FindFilter findfilter = new FindFilter();
findfilter.EntityTypeName = "your entity name";
findPolySpec.Filter = findfilter;
//Call Find Polygon
FindResults findResults = findService.FindPolygon(findPolySpec);
//Now get the polygon entities
foreach(FindResult findResult in findResults.Results)
{
//Get polygons that matched the query
Console.WriteLine(String.Format(
"Polygon Entity Matched with ID: {0}",
findResult.FoundLocation.Entity.ID)
);
}
Now that you have the entity IDs of polygons that match your spatial filter criteria, you can use that information either to render the polygons (covered more in Chapter 8) or to perform any other processing to suit your business needs.
Next: Getting Entities from Latitude/Longitude >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter six of the book Programming MapPoint in .NET, written by Chandu Thota (O'Reilly; ISBN: 0596009062). Check it out today at your favorite bookstore. Buy this book now.
|
|