Download watershed boundaries for a specific HUC

Here we use the urllib and zipfile packages to download and unzip HUCs for a state specified in the first code box. Once downloaded, we see how the geopandas package is used to analyze and visualize the data.


In [ ]:
#Set the state
state = 'Maryland'

In [ ]:
#Import modules
import os
import urllib
import zipfile #https://pymotw.com/2/zipfile/
import geopandas as gpd #http://geopandas.org/io.html

In [ ]:
#Allow maps to be shown inline...
%matplotlib inline

In [ ]:
#Set the URLs
ftpFolder = 'ftp://rockyftp.cr.usgs.gov/vdelivery/Datasets/Staged/Hydrography/NHD/State/HighResolution/Shape/'
stateFile = 'NHD_H_{}_Shape.zip'.format(state)

In [ ]:
#Get the file (this can take a few minutes...)
url = ftpFolder + "/" + stateFile
if os.path.exists(stateFile):
    print("{} already downloaded".format(stateFile))
else:
    print("Downloading {}".format(statefile))
    data = urllib.urlretrieve(url,stateFile)

In [ ]:
#Unzip the file
outFolder = stateFile[:-4]
if not os.path.exists(outFolder): os.mkdir(outFolder)
zip_ref = zipfile.ZipFile(stateFile)
zip_ref.extractall(outFolder)
zip_ref.close()

In [ ]:
#HUC8
shp = outFolder + os.sep + 'Shape' + os.sep + 'WBDHU8.shp'
gdf = gpd.read_file(shp)
gdf.head()

In [ ]:
gdf.plot();