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', 'cb_2015_us_county_500k.shx']

From here, we can use a list comprehension to assign variable names to each of the four files, then read the shapefile into a geopandas dataframe. From here, check the number of records in the file and preview the data. In Python:

dbf, prj, shp, shx = [filename for filename in filenames]
usa = gpd.read_file(local_path + shp)
print("Shape of the dataframe: {}".format(usa.shape))
print("Projection of dataframe: {}".format(usa.crs))
usa.tail() #last 5 records in dataframe

Output:

Shape of the dataframe: (3233, 10)
Projection of dataframe: {'init': 'epsg:4269'}
Screencap of usa.tail()

Note: EPSG:4269 is the same map projection as WGS84

Plotting your data

--

--