Dear Stackoverflow team, I'm impressed that after a bunch of hours digging the forum I still can't find any question/answer similar to my problem :
I have a GeoJson with a lot of Points features. I collect all Points (green in my example, see figure below) that follow some specification (distance between each of them)
and I want to link all of them to build a Polygon (which represent an area).
So I collect all coordinates from these Points, and to be sure the Polygon follows the GeoJson requirements, I'm using the "rewind" function
from geojson_rewind import rewind
But at the end, whatever I've tried I only get that kind of Polygon:
I probably don't use correctly the "rewind" function?
I'm looking for an (easy) automatic way to link all points together in a "convexion hull"
Thanks a lot for any help !
My initial coordinates are collected in a list :
[[4.3556672, 50.8538851], [4.3542534, 50.8546955], [4.3567798, 50.8547854], [4.3566527, 50.8541356], [4.3574286, 50.8552813], [4.3572234, 50.8551264], [4.3547752, 50.8545063], [4.3572736, 50.8560176], [4.3571226, 50.8546104]]
and the Polygon GeoJson I've managed to build, with the rewind function (recopying the last coordinates to get a Polygon) looks like that :
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
4.357223,
50.855126
],
[
4.35678,
50.854785
],
[
4.355667,
50.853885
],
[
4.356653,
50.854136
],
[
4.357123,
50.85461
],
[
4.354253,
50.854695
],
[
4.354775,
50.854506
],
[
4.357429,
50.855281
],
[
4.357274,
50.856018
],
[
4.357223,
50.855126
]
]
]
}
}
]
}
The shapely library is very useful for doing these kinds of geometric manipulations.
For generating a polygon of the convex hull of a set of geometries you can use object.convex_hull
:
import shapely.geometry as sg
points = [[4.3556672, 50.8538851], [4.3542534, 50.8546955], [4.3567798, 50.8547854], [4.3566527, 50.8541356], [4.3574286, 50.8552813], [4.3572234, 50.8551264], [4.3547752, 50.8545063], [4.3572736, 50.8560176], [4.3571226, 50.8546104]]
polygon = sg.MultiPoint(points).convex_hull
Which results in the following shape:
Converting it into a GeoJSON with the help of shapely.geometry.mapping
:
feature_collection = {
"type": "FeatureCollection",
"features": [
{"type": "Feature", "properties": {}, "geometry": sg.mapping(polygon)}
],
}
import json
geojson = json.dumps(feature_collection)