Farhan Ghumra
Farhan Ghumra

Reputation: 15296

How to check if GeoJSON feature is rectangular shape and find corner co-ordinates?

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

Answers (1)

ardilgulez
ardilgulez

Reputation: 1944

A basic algorithm will be:

  • Check the line between point 1 and point 2. Make it a reference line and note its length and slope
  • Check the line between point 2 and point 3. If it is not orthogonal or parallel to the reference line, return False. It's not a rectangle.
  • If it's parallel, then the reference line is between point 1 and point 3 now.
  • If it's orthogonal, the reference line is now between point 2 and point 3. Save the line between point 1 and point 2 as an edge.

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

Related Questions