Wednesday, 15 May 2019

mongodb - How to find inner points near to corner of rectangular GeoJSON feature?


This question is in continuation of How to check if GeoJSON feature is in rectangular shape and find corner points?


I have a collection of GeoJSON features as Polygon & MultiPolygon which are saved in MongoDB. Many among them are in rectangular or square shape, while others are odd shaped. From above question's answer, I am able to find if the feature is rectangular or not also can find the corner points of it. But I actually need inner points which are near to corner (see attached image). I want to place label at that geolocation using Google map label library. How can I do that?


I tried to +/- some value from corner points but it is not working because some shapes are tilted towards left and some towards right.


near corner points of rectangular GeoJSON feature



Answer



So, to continue my answer in the linked question (thus, sticking to Turf.js as you suggested), one simple solution would be to scale down each polygon by, say, 10% and use those corner points.
Add Turf.js's turf.transformScale() function to the custom function in my linked answer like so:


...


var turfPolygon = turf.transformScale(turfInputPolygon, -0.1);

...

You'll get all four corners, and the point array order depends on the input polygon vertex order, so it is not guaranteed that the first point in the array is the NW point.
Also, scaling works in relative units; if you need a fixed, absolute offset, you'd either need to implement absolute measurements and calculate the relative value accordingly, or use a different approach.


Again, my function is thrown together and you could possibly optimize things in terms of speed (and style, I guess...), but this would do it's job.
However, as I said already, depending on your software stack, it might be easier and more versatile to use label placement options instead of constructing geometries.


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...