alekzvik / shapely-geojson

Set of classes to work better with GeoJson spec with Shapely classes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status Coverage Status license PyPI

shapely-geojson

Feature and FeatureCollection to work nice with Shapely geometry structures and GeoJson.

What?

  • Feature & FeatureCollection classes as in GeoJson spec.
  • dump & dumps functions as in json, to serialize your shapely geometries.

Why?

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.

Examples

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
      }
    }
  ]
}

Existing alternatives

You should also consider alternatives:

  • PyGeoIf provides a GeoJSON-like protocol for geo-spatial (GIS) vector data.
  • geojson Python bindings and utilities for GeoJSON

About

Set of classes to work better with GeoJson spec with Shapely classes.

License:MIT License


Languages

Language:Python 100.0%