DEV Community

Cover image for Converting .shp files to CSV with GeoPandas
David Ostler
David Ostler

Posted on β€’ Edited on

13

Converting .shp files to CSV with GeoPandas

Working with geospatial data is really fun! We use this type of data all over the place in data science, and data warehouses have gotten really good at working with it (I'm looking at you, Snowflake!).

However, as you get started down the path of using geospatial data, you will likely run into a speed bump: .shp files

What is a .shp file?

A .shp file is a common way of encoding geospatial data in a binary format. This geospatial data will contain geospatial points, polygons (a collection of geospatial points that form an area), and multipolygons (more exotic polygons).

Image description

A .shp file will be bundled with other files with the same name but different file extensions (e.g., .shx, .dbf, .prj, etc.). In short, you will need all of them.

What is the problem with .shp files?

Unfortunately, most data warehouses don't read .shp files natively. If you are looking to load this dataset into your data warehouse, you are going to need to convert it to a compatible format like CSV.

Additionally, a .shp file might use a different coordinate system than the latitude / longitude system you are familiar with (WGS 84).

In this tutorial, we will convert a .shp file to CSV and transform a geospatial data to the latitude / longitude coordinate system.

The latitude / longitude coordinate system we are familiar with is called WGS 84 EPSG:4326.

Convert a .shp file to CSV

In this example, we'll use GeoPandas for the conversion.



import geopandas as gpd

# Import .shp file into a GeoPandas DataFrame
geopandas_df = gpd.read_file('Grid_100m.shp')

# Convert geospatial data to latitude/longitude coordinate system
converted_df = geopandas_df.to_crs('EPSG:4326')

# Write data to CSV
converted_df.to_csv('Grid_100m.csv', index=False)


Enter fullscreen mode Exit fullscreen mode

If all goes well, you should now have a well-formatted CSV file that you can inspect with Excel (and load to your data warehouse).

Cheers! πŸš€

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
flea_li_54e14fcae20effed2 profile image
Flea li β€’

thanks for the tutorial. For no-code users, shp converter csv offers quick conversion

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay