Feature and FeatureCollection to work nice with Shapely geometry structures and GeoJson.
- Feature & FeatureCollection classes as in GeoJson spec.
- dump & dumps functions as in json, to serialize your shapely geometries.
I use shapely all the time and recently more frequently I use GeoJSON to show my data on maps. Main thing I was missing is fast way to create features and dump them to geojson.
Feature
>>> from shapely.geometry import Point
>>> from shapely_geojson import dumps, Feature
>>> feature = Feature(Point(1, 2), properties={'key': 'value'})
>>> print(dumps(feature, indent=2))
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
1.0,
2.0
]
},
"properties": {
"key": "value"
}
}
FeatureCollection
>>> feature1 = Feature(Point(1, 2), {'index': 1})
>>> feature2 = Feature(Point(3, 4), {'index': 2})
>>> features = [feature1, feature2]
>>> feature_collection = FeatureCollection(features)
>>> for feature in feature_collection:
... print(feature.properties['index'])
...
1
2
>>> print(dumps(feature_collection, indent=2))
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
1.0,
2.0
]
},
"properties": {
"index": 1
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
3.0,
4.0
]
},
"properties": {
"index": 2
}
}
]
}
You should also consider alternatives: