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.
#Set the state
state = 'Hawaii'
#Import modules
import os
import urllib
import zipfile #https://pymotw.com/2/zipfile/
import geopandas as gpd #http://geopandas.org/io.html
#Allow maps to be shown inline...
%matplotlib inline
The data we want is stored on an web server:https://rockyweb.usgs.gov/vdelivery/Datasets/Staged/Hydrography/NHD/State/HighResolution/Shape). Open the link and view the files. The zip files contain the shapefiles we want, and the all have a set name of NHD_H_***_State_Shape.zip
, where ***
is the state name.
So, what we want to do is create two variables. The first points to the FTP folder containing the file we want, and the second is the name of the actual file we want. Separating the name is a bit easier to manage and also gives us a proper output file name.
#Set the URLs
ftpFolder = 'https://rockyweb.usgs.gov/vdelivery/Datasets/Staged/Hydrography/NHD/State/HighResolution/Shape/'
stateFile = 'NHD_H_{}_State_Shape.zip'.format(state)
With these variables set, we construct the full URL, which is just the ftpFolder and the stateFile together with a forward slash between them.
Then we use the urllib.request.urlretrieve
command, passing it the URL we want to retrieve and a filename used to store the file locally. This take a few moments to complete since we are downloading the data in this step.
#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.request.urlretrieve(url,stateFile)
We just downloaded the zip file, so we can use the Python zipfile
package to unpack the contents into a folder we create.
#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()
Here we use GeoPandas
to read the shapefile's contents into a GeoDataFrame. (More on GeoPandas later...)
#HUC8
shp = outFolder + os.sep + 'Shape' + os.sep + 'WBDHU8.shp'
gdf = gpd.read_file(shp)
gdf.head()
The plot
method of a geodataframe plots our spatial data to the screen. The figsize
property sets how big we want to show it. More on the plot function is here.
GIS without any ArcGIS!
#Plot the geodataframe
gdf.plot(
column='HUC8',
figsize=(20,10),
legend=True,
);
The site https://rockyweb.usgs.gov/vdelivery/Datasets/Staged/LndCvr/Shape/ contains land cover shapes for all US states. Use the example above to write code blocks to download and display land cover for Delaware.
state = 'Delaware'
#Set the URLs
ftpFolder =
stateFile =
#Get the file (this can take a few minutes...)
url =
data =
#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()
#LandCover
shp = outFolder + os.sep + 'Shape' + os.sep + 'LandCover_Woodland.shp'
gdf = gpd.read_file(shp)
gdf.head()
#Plot
gdf.plot(figsize=(20,10));