Avoiding a Given Area
In many instances it may be undesirable for a vehicle to be routed through a certain area (for example, a courier may be restricted to only traveling within a certain 'zone', or hazardous waste may not be permitted to travel through a residential area).
In this section of the routing tutorial we will use classes within the Telogis.GeoBase.GeoFence namespace to 'fence off' a geographical area.
Our example route starts at 17927 San Fernando Mission Boulevard and ends at 16567 Chatsworth Street. Both addresses are in Granada Hills, Los Angeles (USA). A screenshot of GeoBase's default route is shown below in green. We will create a GeoFence to exclude the area bounded by Index Street, Balboa Boulevard, Chatsworth Street and Louise Avenue (as shown in red).
The following code segment creates three objects: a GeoFenceCollection, a RectangleGeoFence and a BoundingBox. The bounding box is created by geocoding the street addresses on two opposite corners of the exclusion area.
/* define variables */ GeoFenceCollection allMyFences = new GeoFenceCollection(); RectangleFence myFence = new RectangleFence(); BoundingBox myBox = new BoundingBox(); /* add corner points to bounding box */ myBox.Add(GeoCoder.GeoCode("Louise & Chatsworth, Granada Hills, Los Angeles", Country.USA).Location); myBox.Add(GeoCoder.GeoCode("Balboa & Index, Granada Hills, Los Angeles", Country.USA).Location); /* add box to fence, and fence to collection */ myFence.Box = myBox; allMyFences.Add(myFence);
Now that the GeoFence has been defined we can instruct the routing engine to avoid the defined area:
/* instruct our route to exclude the area in allMyFences */ myRoute.Strategy.KeepOut = allMyFences;
A route can have only one KeepOut area (of type GeoFence). However by using a GeoFenceCollection we may define multiple exclusion areas for each route. Even if only one exclusion area is originally defined, it is still a good idea to use a GeoFenceCollection as it allows other exclusion areas to be easily added at a later stage.
After defining the keep-out area for the route the GeoBase routing engine creates the following route:
To fully exclude Chatsworth Street from our route we could increase the size of the bounding box to include addresses on the south side of Chatsworth Street (that is, inflate the bounding box to contain 1780 Chatsworth Street).