Ogr2ogr is the standard way to convert files from Shapefile to GeoJSON on the command line. But it has a few limitations for me:

  • The output format is not condensed (ie, it contains a lot of needless whitespace, which adds up for huge files)
  • There doesn't seem to be an option to suppress null values (again, very inefficient on space)
  • GDAL is an enormous package that does a lot of stuff I don't need (so it's a very heavy dependency for this one tiny job).

Is there any better tool for the job?

  • 1
    GeoTools can do it in Java but it is not small either. OpenJUMP has native drivers for both formats but it is probably difficult to run without using the UI. I guess that both will create uncondensed result with nulls. – user30184 Dec 20 '17 at 6:04

Pyshp reads and writes shapefiles, no other formats, in Python: pip install pyshp.

You can use its Python geo protocol to get GeoJSON-like representations of features and then use Python's json module to serialize them to JSON. See https://docs.python.org/3.6/library/json.html for examples of compact encoding.

Mike Bostock's https://www.npmjs.com/package/shapefile also does command line conversion of shapefiles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.