Title: How to Read and Write Shapefiles with GeoPandas (Python GIS Tutorial)
๐Introduction
In GIS, shapefiles are one of the most common formats you'll work with. With Python and GeoPandas, you can read, edit, and write shapefiles with just a few lines of code. It’s a powerful way to automate tasks and reduce your dependency on desktop tools.
Let’s look at how to load shapefiles, view their structure, filter features, and save new versions.
๐ Step 1: Read a Shapefile
๐ง What’s Inside a GeoDataFrame?
You can check out the column names and geometry type like this:
๐ Step 2: Filter Features
Let’s say we only want to keep major roads:
๐ Optional: Plot the Filtered Data
๐พ Step 3: Write a New Shapefile
Save the filtered features to a new shapefile:
Or export it as GeoJSON:
๐ Bonus: Reproject the Data
Need your data in WGS84 (EPSG:4326)? Use:
๐งน Tips for Working with Shapefiles
-
Always keep all
.shp,.shx,.dbf,.prjfiles together. -
For non-English column names or long field names, consider simplifying them before exporting.
-
Use
.to_crs()before exporting if your tools expect a specific projection (e.g., WGS84 for web maps).
๐ฏ Conclusion
In just a few lines of Python, you can read, filter, and save shapefiles—perfect for automating spatial workflows or cleaning data before analysis.
๐ Next Up:
➡️ Spatial Joins and Attribute Queries in Python GIS
No comments:
Post a Comment