Reading Shapefile ZIPs from a URL in Python 3

Lauren Oldja
3 min readFeb 12, 2018

This article takes inspiration from Andrew Gadius’ blog on the same topic, using updated libraries and Python 3 to achieve a similar effect. If you do not already have a working environment, check out Part One of this two-part blog post. UPDATE June 4, 2019: Or get started using this notebook in Google Colab.

Prepare your workspace

In > jupyter notebook or your preferred Python 3 IDE, first import the relevant libraries. In Python:

import geopandas as gpd
import requests
import zipfile
import io
import matplotlib.pyplot as plt
%matplotlib inline # jupyter "magic" to display plots in notebook

Load the data

For this example we’ll download and graph a shapefile from the Census FTP of my home state, Florida. In Python:

url = 'http://www2.census.gov/geo/tiger/GENZ2015/shp/cb_2015_us_county_500k.zip'local_path = 'tmp/'print('Downloading shapefile...')
r = requests.get(url)
z = zipfile.ZipFile(io.BytesIO(r.content))
print("Done")
z.extractall(path=local_path) # extract to folderfilenames = [y for y in sorted(z.namelist()) for ending in ['dbf', 'prj', 'shp', 'shx'] if y.endswith(ending)]
print(filenames)

Output:

Downloading shapefile...
Done
['cb_2015_us_county_500k.dbf', 'cb_2015_us_county_500k.prj', 'cb_2015_us_county_500k.shp'…

--

--