SHP to GeoJSON
A command-line converter that turns ESRI Shapefiles into GeoJSON, reprojecting to WGS 84
along the way.
Why the reprojection matters
The GeoJSON specification (RFC 7946)
defines coordinates as WGS 84 longitude/latitude. Shapefiles, by contrast, are commonly
distributed in a local projected system — State Plane, UTM, or a national grid — measured
in feet or metres.
Converting the file format without converting the coordinate system produces a technically
valid GeoJSON whose coordinates are meaningless to any web map. This script always
reprojects to EPSG:4326 and prints both the input and output CRS so you can see what
changed.
Requirements
pip install -r requirements.txt
Usage
Put your shapefile — along with its .dbf, .shx and .prj sidecar files — in in_SHP/,
then run:
python convert.py
The script prompts for two names:
Enter the name of the input shapefile: Roads
Enter the name of the new GeoJSON file: roads_wgs84
File extensions are optional; Roads and Roads.shp both work. Output is written to
out_GeoJSON/, which is created if it doesn’t exist.
Repository layout
├── convert.py # the converter
├── in_SHP/ # input shapefiles
├── out_GeoJSON/ # generated GeoJSON output
└── L2_data/ # additional sample data
Sample shapefiles are included in in_SHP/ — creeks, roads and boundary data — so the
script can be run immediately after cloning.
A note on the .prj file
Reprojection depends on the input’s coordinate system being known, which comes from the
.prj sidecar file. If it’s missing, GeoPandas reports the CRS as None and the
reprojection will fail. Shapefiles are multi-file formats — copy all sidecar files, not
just the .shp.
评论