Sunday, 14 August 2016

labeling - Algorithm for finding irrregular polygon centroid (label point)


I need to find a centroid (or label point) for irregularly shaped polygons in Google Maps. I'm showing InfoWindows for parcels and need a place to anchor the the InfoWindow that is guaranteed to be on the surface. See images below.


alt text alt text


In reality I don't need anything Google Maps specific, just looking for an idea of how to automatically find this point.


My first idea was to find the "false" centroid by taking the average lat and lngs and the randomly placing points out from there until I find one that intersects the polygon. I already have the point-in-polygon code. This just seems awfully "hacky" to me.


I should note that I don't have access to any of the server side code outputting the geometry so I can't do anything like ST_PointOnSurface(the_geom).



Answer




Quick and dirty: If the "false" centroid is not in the polygon use the nearest vertex to that point.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...