34

I created a circle using geopandas and it returned a shapely polygon:

POLYGON: ((...))

I want this same polygon as a geojson object. I ran across this:

shapely.geometry.mapping(shapelyObject)

which returns this:

{'type': 'Polygon', 'coordinates': (((570909.9247264927, 125477.71811034005)...}

But when I try to map this in mapbox it does not show anything. I think maybe it is not fully a geojson object.

conv3d
  • 2,668
  • 6
  • 25
  • 45

8 Answers8

48

If you don't want to create this dict manually, you can also rely on geopandas creating it:

In [1]: import shapely.geometry

In [2]: import geopandas

In [3]: shapely_polygon = shapely.geometry.Polygon([(0, 0), (0, 1), (1, 0)])

In [4]: geopandas.GeoSeries([shapely_polygon]).__geo_interface__
Out[4]: 
{'bbox': (0.0, 0.0, 1.0, 1.0),
 'features': [{'bbox': (0.0, 0.0, 1.0, 1.0),
   'geometry': {'coordinates': (((0.0, 0.0),
      (0.0, 1.0),
      (1.0, 0.0),
      (0.0, 0.0)),),
    'type': 'Polygon'},
   'id': '0',
   'properties': {},
   'type': 'Feature'}],
 'type': 'FeatureCollection'}

(Note that this gives a FeatureCollection and not a single feature.)

Or to a string (or file):

In [4]: geopandas.GeoSeries([shapely_polygon]).to_json()
Out[4]: '{"features": [{"bbox": [0.0, 0.0, 1.0, 1.0], "geometry": {"coordinates": [[[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 0.0]]], "type": "Polygon"}, "properties": {}, "id": "0", "type": "Feature"}], "bbox": [0.0, 0.0, 1.0, 1.0], "type": "FeatureCollection"}'
joris
  • 133,120
  • 36
  • 247
  • 202
28

Shapely returns a python dict where all the coordinates are in tuples. You need to convert to JSON in order for mapbox, etc... to properly accept it.

json.dumps(shapely.geometry.mapping(shapelyObject))
Alex Carruthers
  • 395
  • 6
  • 10
11

Something like this should do the trick:

features = [{'type': 'Feature', 'properties': {}, 'geometry': shapely.geometry.mapping(shapelyObject)}]

Now you can try to map features in mapbox. Hope this helps.

Reference: https://gis.stackexchange.com/questions/213717/geometry-workflow-from-shapely-to-geojson

Paul Varghese
  • 1,635
  • 1
  • 15
  • 30
9

To write a standard geojson object using pandas you shall use the driver provided by fiona as recommended in the documentation

gdf.to_file('path/to/file.geojson', driver='GeoJSON')

See import fiona; fiona.supported_drivers for a list of fully supported drivers

MCMZL
  • 1,078
  • 9
  • 21
4

Assume polygon_list is a list of shapely.geometry.Polygon.

geo_dict = {}
geo_dict["type"] = "FeatureCollection"
geo_dict["features"] = [{"type": "Feature", "geometry": a} for a in [geometry.mapping(b) for b in polygon_list]]
my_geojson = json.dumps(geo_dict) # str in json format
xitong
  • 352
  • 2
  • 9
1

You can also use PyShp

import shapefile

with shapefile.Reader("shapefile.shp") as shp:
    geojson_data = shp.__geo_interface__

or

geojson_data = shapefile.Reader("shapefile.shp").__geo_interface__

example usage:

>>> geojson_data["type"]

'MultiPolygon'
bwl1289
  • 1,655
  • 1
  • 12
  • 10
0

Use the driver provided by fiona:

data=shapefile.to_file("file.geojson",driver='GeoJSON')

data=geopandas.read_file("file.geojson")

data
  • 2
    Hi, could you provide more details about your answer? For instance, you could add how dnd why your solutions works, what does the user that posted the question was missing, etc.. This will help other people that reach the post in the future to understand more about your solution. Thanks – EnriqueBet Jul 26 '20 at 16:45
  • geopandas uses fiona for data conversion ,fiona has drivers to do so e.g creating shapefile,geojson etc from geodataframe.if you need to chack drivers in fiona just type >import fiona >fiona.supported_drivers the results should be {'ESRI Shapefile': 'raw', 'ARCGEN': 'r', 'PCIDSK': 'r', 'SUA': 'r', 'DGN': 'raw', 'SEGY': 'r', 'MapInfo File': 'raw', 'GeoJSON': 'rw', 'PDS': 'r', 'FileGDB': 'raw', 'GPX': 'raw', 'DXF': 'raw', 'GMT': 'raw', 'Idrisi': 'r', 'GPKG': 'rw', 'OpenFileGDB': 'r', 'BNA': 'raw', 'AeronavFAA': 'r', 'GPSTrackMaker': 'raw'} – Francis Odero Jul 30 '20 at 20:14
0

With geopandas, you can do:

with open("out.geojson","w") as f:
    f.write(gpd.GeoSeries([shapely_object]).to_json())
zabop
  • 6,750
  • 3
  • 39
  • 84