I have the following folium markers and would like to save them to a geojson file in python.
neb1 = folium.Marker([nebraska[0]+1.73, nebraska[1]-3.46], icon = folium.Icon()).add_to(m)
neb2 = folium.Marker([nebraska[0]+2.73, nebraska[1]-3.46], icon = folium.Icon()).add_to(m)
neb3 = folium.Marker([nebraska[0]+2.73, nebraska[1]-5.46], icon = folium.Icon()).add_to(m)
neb4 = folium.Marker([nebraska[0]+4.73, nebraska[1]-3.46], icon = folium.Icon()).add_to(m)
neb5 = folium.Marker([nebraska[0]+4.73, nebraska[1]], icon = folium.Icon()).add_to(m)
neb5 = folium.Marker([nebraska[0]+1.73, nebraska[1]+3.2], icon = folium.Icon()).add_to(m)
How do I achieve this?
Thanks!
You can create a GeoJSON file using the geojson library.
Note folium.Marker() uses coordinates in latitude, longitude order and geojson Point() coordinates are in longitude, latitude order so remember to use the correct order.
from geojson import Feature, Point, FeatureCollection, dumps
nebraska = [38.2, -98.5] # lat, lon
points = [
Point((nebraska[1]-3.46, nebraska[0]+1.73)),
Point((nebraska[1]-3.46, nebraska[0]+2.73)),
Point((nebraska[1]-5.46, nebraska[0]+2.73)),
Point((nebraska[1]-3.46, nebraska[0]+4.73)),
Point((nebraska[1], nebraska[0]+4.73)),
Point((nebraska[1]+3.2, nebraska[0]+1.73))
]
features = [Feature(geometry=p) for p in points]
feature_collection = FeatureCollection(features)
with open("out.geojson", "w") as fout:
fout.write(dumps(feature_collection))
This outputs a GeoJSON file with this structure:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-101.96, 39.93
]
},
"properties": {}
},
...
}
You can then load the GeoJSON file into a folium map using the folium.GeoJson() function.