I have a script that creates a lot of Polygons using Shapely and then exports them as .geojson files. See toy example below
JavaScript
x
8
1
from shapely.geometry import Polygon
2
import geopandas
3
4
roi = Polygon([(0,0), (0,1), (1,0), (1,1)])
5
rois = [roi, roi]
6
7
geopandas.GeoSeries(rois).to_file("detection_data.geojson", driver='GeoJSON')
8
However, I also have a list of numbers, each number is associated with one ploygon. Is there a way to export this with the GeoJSON file inside properties?
For example, if I have a list:
JavaScript
1
2
1
detection_prob = [0.8, 0.9]
2
In the .geojson file I would like the properties section for the first polygon to read
“properties”:{“detection_prob”:0.8}
and for the second polygon
“properties”:{“detection_prob”:0.9}
etc etc etc… in the outputted GeoJSON file.
Advertisement
Answer
If you call to_file
on a dataframe instead of a series, you can add extra attributes as columns:
JavaScript
1
9
1
import geopandas as gpd
2
import shapely.geometry as g
3
4
df = gpd.GeoDataFrame({
5
'geometry': [g.Point(0, 0), g.Point(1,1)],
6
'name': ['foo', 'bar']
7
})
8
df.to_file('out.json', driver='GeoJSON')
9