27
output_buffer = []
for features in range(0,layer.GetFeatureCount()):
    feat = layer.GetNextFeature()
    geom = feat.GetGeometryRef()
    result = feat.ExportToJson()
    output_buffer.append(result)

When I convert into geojson, I get output, but only one feature is getting formatted as JSON

I got output like this:

{"geometry": {"coordinates": [488081.726322771, 2360837.62927308], "type": "Point"}, "type": "Feature", "id": 0, "properties": {"EntityHand": null, "Layer": "pipe", "Linetype": null, "Text": "BB_D2", "ExtendedEn": null, "SubClasses": null}}{"geometry": {"coordinates": [487523.119248441, 2361228.95273474], "type": "Point"}, "type": "Feature", "id": 1, "properties": {"EntityHand": null, "Layer": "pipe", "Linetype": null, "Text": "Mil_D2", "ExtendedEn": null, "SubClasses": null}}..................

I would like to get output like this:

{"geometry": {"coordinates": [488081.726322771, 2360837.62927308], "type": "Point"}, "type": "Feature", "id": 0, "properties": {"EntityHand": null, "Layer": "pipe", "Linetype": null, "Text": "BB_D2", "ExtendedEn": null, "SubClasses": null}}**,**    
{"geometry": {"coordinates": [487523.119248441, 2361228.95273474], "type": "Point"}, "type": "Feature", "id": 1, "properties": {"EntityHand": null, "Layer": "pipe", "Linetype": null, "Text": "Mil_D2", "ExtendedEn": null, "SubClasses": null}}**,**
6
  • Please format your question into something legible... Commented Mar 31, 2017 at 18:46
  • 1
    It would help if you provide a working example. Its impossible to answer if your problem cannot be replicated. Commented Apr 3, 2017 at 12:18
  • @RutgerKassies. The correct procedure is to make the appropriate close vote in that case. Commented Apr 3, 2017 at 15:50
  • What libraries are you using? Commented Apr 5, 2017 at 17:36
  • 1
    Hey there Srinuvas Bathula: This answer is getting a lot of views, it's been nearly 3 years. As this is the sole answer and the question and answer are getting quite some positive feedback, would you mind marking it as correct ? :) Commented Dec 10, 2019 at 10:57

3 Answers 3

43

For conversion between shapefile and geojson I would definitely use geopandas:

    import geopandas
    myshpfile = geopandas.read_file('myshpfile.shp')
    myshpfile.to_file('myJson.geojson', driver='GeoJSON')
Sign up to request clarification or add additional context in comments.

Comments

37

Please check out the following library: https://pypi.python.org/pypi/pyshp/1.1.7

import shapefile
from json import dumps

# read the shapefile
reader = shapefile.Reader("my.shp")
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
    atr = dict(zip(field_names, sr.record))
    geom = sr.shape.__geo_interface__
    buffer.append(dict(type="Feature", \
    geometry=geom, properties=atr)) 
   
    # write the GeoJSON file
   
geojson = open("pyshp-demo.json", "w")
geojson.write(dumps({"type": "FeatureCollection", "features": buffer}, indent=2) + "\n")
geojson.close()

As noted in other answers, you could use geopandas:

import geopandas

shp_file = geopandas.read_file('myshpfile.shp')
shp_file.to_file('myshpfile.geojson', driver='GeoJSON')

Comments

6

To add onto @alexisdevarennes.

You can now convert to geojson using PyShp in 1 or two lines:

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'

1 Comment

This worked great but did not work with the .shp file for what I was doing; instead required the .dbf file. I was using files provided from Census: census.gov/geographies/mapping-files/time-series/geo/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.