function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John BrodrickJohn Brodrick 

Find Distance Between Two Polygons (Represented by Geolocation Fields)

I have a tricky requirement in my project.  We are currently storing sets of up to 8 geolocation fields in a custom object that can represent anything from a point, a line, up to a 8 sided polygon for display on a map.  The client would like the ability that given a single shape (call it the "Selected" shape) only display other polygons that are within a certain distance to the selected shape (e.g. 1 mile).

So in short I am going to need to find a way to calculate the distance between two polygons represented by up to 8 geocode fields.  Now obviously calculating distance between two points is easy using the DISTANCE function which is accessible even in SOQL.  I'm looking for some better suggestions on how to approach this though for lines and polygons?

Currently I found this on stack overflow:

http://gis.stackexchange.com/questions/11409/calculating-the-distance-between-a-point-and-a-virtual-line-of-two-lat-lngs

I'm thinking I could use that formula by interating over all points in the selected polygon and finding the distance between the point and each side of a potential target polygon.  The shortest distance would be the distance between the polygons.

This is a pretty challenging problem IMO so I am open to all suggestions - just wondering if someone else has a better way of doing this than I do.