Convert N-sided polygon to nearest fitting rectangle?
In QGis, the Processing toolbox has a "Oriented minimum bounding box" algorithm, which does exactly what you want (your first choice). Be careful, you need to have the data saved in the correct coordinate system (your example data is saved in EPSG:4326, even though you visualize it in EPSG:3857, so the stored data is not a rectangle and the algorithm will not give the expected result).
- Open QGis and load the data you want (I had to convert your example geojson to EPSG:3857 first)
- Open the Processing toolbox (menu Processing -> Toolbox), then search for "Oriented minimum bounding box"
- In the small wizard window, choose the layer to apply the algorithm to and where to save the results, there are no other settings
You can use minimum_bounding_rectangle()
function in Finding minimum-area-rectangle for given points?
For your GeoJSON text, to get "minimum area bounding rectangle (MABR)":
import numpy as np
data = {"type":"FeatureCollection",
"features":[{"type":"Feature",
"properties":{"osm_id":"1269601",
"type":"multipolygon",
"leisure":"pitch",
"sport":"soccer"},
"geometry":{
"type":"Polygon",
"coordinates":[[
[6.6131123,46.5124914],
[6.6129421,46.5125385],
[6.6127998,46.5125783],
[6.6126291,46.512626],
[6.6125308,46.5124593],
[6.6127016,46.5124121],
[6.6128452,46.5123724],
[6.6130153,46.5123244],
[6.6131123,46.5124914]]
]
}
}
]
}
# polygons can have holes, so, ["coordinates"][0] gives you boundary of polygon.
# If you have multipolygon, ["coordinates"][0][0] gives you the first polygon boundary.
geom = data["features"][0]["geometry"]["coordinates"][0]
mabr = minimum_bounding_rectangle(np.array(geom))
# OUT:
#array[[ 6.6131123 , 46.5124914 ],
# [ 6.61306213, 46.51231129],
# [ 6.6125308 , 46.5124593 ],
# [ 6.61258097, 46.51263941]]
data2 = dict(data) # copy data to data2
data2["features"][0]["geometry"]["coordinates"][0] = mabr.tolist()
Now, data2
is GeoJSON text with MABR of polygon. But it is always 'great equal' than source polygon. So, you can think of scaling down polygon by rate of source_polygon_area/mabr_area
.