Skip to content

8 tips using GeoPandas and Python for Geospatial People

  • blog

GeoPandas is great. If you are learning Geospatial Programming and work with vector data then you could do alot worse than giving GeoPandas a go. I’ve written a little about GeoPandas before; so first a couple of links.

Installing a Python Geospatial work environment that includes GeoPandas:

Python for Geospatial work flows part 1: Use anaconda

And if you want a simple guide to plotting then have a look here

Using GeoPandas to display Shapefiles in Jupyter Notebooks

In this post I have written 8 tips & tricks to downloading and manipulating a Shapefile using GeoPandas. These are the topics covered – more detail in the Notebook here

1. Downloading data and extracting it

import requests, zipfile, io ## Python imports

# set the url to the data
url = 'https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip'

# Use requests to download that url and zipfile to extract it
r = requests.get(url)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall()

2. Loading and plotting in GeoPandas

import geopandas as gpd shape_data = “ne_50m_admin_0_countries.shp” # refer to z.namelist() gdf = gpd.read_file(shape_data) gdf.plot()

Or 1 and 2 combined! Or you can read directly into geopandas from a URL. Thanks to @James_O_Connor1

gdf = gpd.GeoDataFrame.from_file('https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip')
gdf.plot()

3. Removing columns (in this case NAME_ZH or all columns with NAME in)

gdf.drop('NAME_ZH', axis=1, inplace=True)
## or
gdf.drop(list(gdf.filter(regex = 'NAME')), axis = 1, inplace = True)

4. Renaning columns (change AMDIN to C_Name)

gdf.rename(columns={"ADMIN": "C_Name"}, inplace=True)

5. Changing a column’s datatype (change MIN_ZOOM to an integer)

gdf = gdf.astype({'MIN_ZOOM': 'int32'})

6. Parsing an attribute query (country name = Belgium)

gdf_Bel = gdf.loc[gdf['C_Name'] == 'Belgium']

7. Converting a Pandas object (Dataframe) to a GeoPandas object (Dataframe)

projection = {'init': 'epsg:4326'}

gdf_Bel = gpd.GeoDataFrame(gdf_Bel, crs = projection, geometry = 'geometry')

8. Saving your work

gdf_Bel.to_file("Belgium.shp")

For more context all the code with more detailed comments is in a easy to use Juypter Notebook here

 

I run training courses in the UK please do check out my training page for a Geospatial Python course near you. Or contact me for any training need on info@acgeospatial.co.uk – I’d be happy to help.


I am a freelancer able to help you with your projects. I offer consultancy, training and writing. I’d be delighted to hear from you. Please check out the books I have written on QGIS 3.4

https://www.packtpub.com/application-development/learn-qgis-fourth-edition

https://www.packtpub.com/application-development/qgis-quick-start-guide


I am @map_andrew on twitter

 

Image credit https://unsplash.com/photos/pZQQ_UyphtA