← Back to list

How to Convert a Shapefile into GeoJSON

Why Convert to GeoJSON?

Kawintida P · 2026-05-27 04:15 · 4 claps · 1.3 min read
#shapefile #geojson #gis #programming
Open on Medium ↗
Wiki topics: 💻 · Programming

How to Convert a Shapefile into GeoJSON

Why Convert to GeoJSON?

GeoJSON is much easier to use in modern applications because everything is stored in one file:

  • geometry
  • attributes
  • coordinates
  • feature structure

all together.

This makes it ideal for interactive web maps!

Installing the Python Libraries

pip install pyshp pyproj

The Conversion Script

import shapefile
from pyproj import Transformer
import json

# input / output
input_shp = "large_dam.shp"
output_geojson = "large_dam.geojson"

# read shapefile
sf = shapefile.Reader(input_shp)

# read fields from .dbf
fields = [field[0] for field in sf.fields[1:]]

# projection: UTM Zone 47N -> WGS84
transformer = Transformer.from_crs(
    "EPSG:32647",
    "EPSG:4326",
    always_xy=True
)

features = []

for shape_record in sf.shapeRecords():

    shape = shape_record.shape
    record = shape_record.record

    properties = dict(zip(fields, record))

    # geometry
    x, y = shape.points[0]

    # convert coordinates
    lon, lat = transformer.transform(x, y)

    feature = {
        "type": "Feature",
        "properties": properties,
        "geometry": {
            "type": "Point",
            "coordinates": [lon, lat]
        }
    }

    features.append(feature)

geojson = {
    "type": "FeatureCollection",
    "features": features
}

with open(output_geojson, "w", encoding="utf-8") as f:
    json.dump(geojson, f, ensure_ascii=False, indent=2)

print(f"Done: {output_geojson}")

What the Script Is Actually Doing

The workflow is surprisingly simple:

  1. Read geometry from .shp
  2. Read attributes from .dbf
  3. Read projection from .prj
  4. Convert coordinates to WGS84
  5. Merge everything into GeoJSON
  6. Save one .geojson file

Even though we only opened:

shapefile.Reader("large_dam.shp")

the library automatically loaded .dbf .shx .prj

behind the scenes because they share the same filename.

Why WGS84 Matters

Most web maps use: EPSG:4326

which is standard latitude/longitude coordinates.

Many shapefiles come in projected coordinate systems like: UTM Zone 47N

So the script reprojects the coordinates before exporting.

Without this step, your data may appear in the wrong place on the map.

GeoJSON is the modern web-native version of that same geographic information simpler, portable, and much easier to integrate into modern applications.


메타데이터
post_id
d18970d8b713
slug
how-to-convert-a-shapefile-into-geojson-d18970d8b713
url
https://medium.com/@lovelyfortran/how-to-convert-a-shapefile-into-geojson-d18970d8b713
canonical_url
https://medium.com/@lovelyfortran/how-to-convert-a-shapefile-into-geojson-d18970d8b713
author_url
https://medium.com/@lovelyfortran
status
ok
fetched_at
2026-07-30 14:02:18