Reputation: 15296
I have a collection of GeoJSON features as Polygon & MultiPolygon. Many among them are in rectangular or square shape, while others are odd shaped. Is there any algorithm/3rd party library to find all the four corner co-ordinates if that feature is in rectangular or square shape?
I first tried to filter those features which has only five co-ordinates, so that those points will be corner co-ordinates essentially. But some of them are having more than five co-ordinates but as shape they are rectangle or square. Check below given feature examples.
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-100.89361997899994,
32.26379776400006
],
[
-100.91045627599999,
32.26071261200008
],
[
-100.91412950899996,
32.27488019800006
],
[
-100.89716524599999,
32.277952922000054
],
[
-100.89535669999998,
32.270942981000076
],
[
-100.89361997899994,
32.26379776400006
]
]
]
]
},
"properties": {}
}, {
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-100.94608104399998,
32.26182869300004
],
[
-100.95445864399994,
32.26032213600007
],
[
-100.95620274599997,
32.26742597700007
],
[
-100.94798489999994,
32.26894866500004
],
[
-100.94608104399998,
32.26182869300004
]
]
]
]
},
"properties": {}
}
]
}
Upvotes: 1
Views: 826
Reputation: 1944
A basic algorithm will be:
Find all edges like this. If you can find 4 orthogonal edges and both pairs of parallel edges have the same length, you have a rectangle. If all edges have equal length, you have a square.
As a third party implementation, please check turf (javascript library) and/or geos (C++ library with Python bindings). If a "check rectangle" implementation exists, it must be in one of these two.
EDIT: A better algorithm is:
Compare the area of your geometry with the area of its bbox. If areas are equal, rectangle. If areas are equal and bbox is a square, square. If areas are not equal, not a rectangle.
Upvotes: 0