Replies: 4 comments 3 replies
-
Cool, @hanried ! This looks great. I also put a direct link in the assignment description if you prefer to download a single shapefile with all the grasslands rather than to go through the API (or if this doesn't end up working out for you) |
Beta Was this translation helpful? Give feedback.
-
Thanks! A bit ahead of where I am, but nice to have a guidepost. |
Beta Was this translation helpful? Give feedback.
-
@eculler - good to know it is ok to download a single shapefile. I'm doing this a bit differently because I want to compare two areas which can support native rivercane which both the Eastern band of the Cherokee & the Cherokee Nation use for a variety of purposes. It's actually pretty cool that rivercane can grow as far west as Oklahoma. However, when I looked at the grassland data the area in Oklahoma was not anywhere near the area where Cherokee people live. So - I hope it's ok if I got the shape files for Cherokee County OK and Jackson County NC from a US Census Source. I downloaded the zip files and was going to use python code to extract the .shp files, but it became a mess with different directories for data and another for the project, so I just used my file manager to extract them and moved them into the correct directory. I'm sure I could have made it work, but this was much easier. |
Beta Was this translation helpful? Give feedback.
-
OK - after many tries, I finally have a shapefile for US Counties from the US Census. I'm doing this because I want to compare a county in Oklahoma with one in North Carolina as my two locations. Here's the code if you want to use it: Define download URL and file pathsbase_url = "https://www2.census.gov/geo/tiger/TIGER2024/COUNTY/tl_2024_us_county.zip" Create the 'counties' directory if it doesn't existos.makedirs(county_dir, exist_ok=True) Download county data (if not already downloaded)if not os.path.exists(os.path.join(county_dir, "tl_2024_us_county.zip")): Save the downloaded data (compressed) to the specified pathwith open(os.path.join(county_dir, "tl_2024_us_county.zip"), "wb") as county_file: Extract the downloaded shapefilewith zipfile.ZipFile(os.path.join(county_dir, "tl_2024_us_county.zip"), "r") as county_zip: print("Shapefile extracted successfully!") |
Beta Was this translation helpful? Give feedback.
-
In case anyone isn't sure what API to use to get the grassland data, I used this one: https://data-usfs.hub.arcgis.com/datasets/usfs::national-grassland-units-feature-layer/api
And here's the code I used to use said API:
Set up the grassland URL
grasslands_url = ("https://apps.fs.usda.gov/arcx/rest/services"
"/EDW/EDW_NationalGrassland_01/MapServer/0"
"/query?where=1%3D1&outFields=*&geometry=&geometry"
"Type=esriGeometryEnvelope&inSR=4326&spatialRel=esriSpatialRelIntersects&outSR=4326&f=json")
Set up a path to save the data on your machine
grasslands_dir = os.path.join(data_dir, 'grasslands_dir')
Make the grasslands directory
os.makedirs(grasslands_dir, exist_ok=True)
Join grasslands shapefile path
grasslands_path = os.path.join(grasslands_dir, 'S_USA.NationalGrassland.shp')
Only download once
if not os.path.exists(grasslands_path):
grasslands_gdf = gpd.read_file(grasslands_url)
grasslands_gdf.to_file(grasslands_path)
Check that the URL is correct
grasslands_gdf = gpd.read_file(grasslands_path)
Display grasslands_gdf and plot grasslands to check download
display(grasslands_gdf)
grasslands_gdf.plot()
I haven't gotten past this step of the project yet but hopefully it's the right API!
Beta Was this translation helpful? Give feedback.
All reactions