Multi polygons are used to represent complex areas. Each polygon in the set is topologically closed and each polygon may or may not intersect. This article attempts to answer on how-to handle such complex areas in Dynamics CRM.
Multi polygons are usually created from software such as QGIS or Google Earth and are output into a shape/ wkt file. For the purposes of this article, I have converted a Shape file into a KML using Google Earth. The advantage of having converted this to KML format is that it is an open standard and we can take advantage of its interoperability. This article serves as a framework for those who have been looking to do this in C# in general and D365 plugins in specific.
Step 1
Address fields
First thing you would obviously do before venturing into the complexity such as determining whether an address is within the polygon we are drawing is to make sure you have the correct address. The right address gives you right spatial information (latitude and longitude). If you use Field Services, this would automatically update from Microsoft's services. Otherwise there are several other third party providers to choose from for you to geocode your address.
Step 2
Multi Polygon Processing
The next step would involve converting the Multi Polygon into an array of geo-coordinates. There are quite a lot of cloud services that do this automatically for you. You will then need to either store this information in an entity or better still to have a web service that returns the coordinates when queried filtered by, let us say, area.
Step 3
Closed Polygon -> GeographicCoordinates
Convert your string array of closed polygon coordinates to GeographicCoordinates. You have to do this by splitting your comma separated coordinates information to a list of GeographicCoordinates in a loop and doing the required parsing.
Step 4
Looping
Convert the GeographicCoordinates that are obtained from the previous step into a single polygon and also making sure it is a valid polygon.
Step 5
Handy DBGeography
Convert the geo-coordinates of the address in consideration (from Step 1) to DBGeography.
Step 6
Handy DBGeography (again)
Use the Intersects function of DBGeography to find if the coordinates lay inside the polygon.
Repeat steps 3 - 6 until all polygons in our multi polygon array are processed.
Do feel free to drop in an email should you have any questions!
Till then, take care :)