Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

see if we can mostly replace rasterio with rioxarray in geospatial lesson #20

Open
jeanetteclark opened this issue Mar 27, 2024 · 1 comment

Comments

@jeanetteclark
Copy link
Collaborator

It would be cool if we could carry forward the xarray momentum and use rioxarray instead of rasterio in the geospatial lesson. In googling around I'm seeing some chatter about geocube, which could be helpful but might be slow (other out of the box tools I tried while writing this lesson were very inefficient).

Here is the code to read in the file using rioxarray and crop it

import urllib
from shapely.geometry import box
import geopandas as gpd
import rioxarray as rioxr
import os

url = 'https://arcticdata.io/metacat/d1/mn/v2/object/urn%3Auuid%3A6b847ab0-9a3d-4534-bf28-3a96c5fa8d72'
msg = urllib.request.urlretrieve(url, "Coastal_2020_08.tif")

# get coordinates in correct projection for bounding box
coord_box = box(-159.5, 55, -144.5, 62)

coord_box_df = gpd.GeoDataFrame(
    crs = 'EPSG:4326',
    geometry = [coord_box]).to_crs("EPSG:3338")
# extract max and min for the select method
c = coord_box_df.get_coordinates()

max_x = c.x.max()
min_x = c.x.min()
max_y = c.y.max()
min_y = c.y.min()

# read in the file
fp = os.path.join(os.getcwd(),"Coastal_2020_08.tif")
ships = rioxr.open_rasterio(fp, mask_and_scale=True).squeeze()

# crop using the sel method
shipsc = ships.sel(x = slice(min_x,max_x), y = slice(max_y,min_y))

I think the only thing you would need to do from here is extract the shape and transform to pass to the rasterize method

@jeanetteclark jeanetteclark changed the title see if we can replace rasterio with rioxarray in geospatial lesson see if we can mostly replace rasterio with rioxarray in geospatial lesson Mar 27, 2024
@jeanetteclark
Copy link
Collaborator Author

also consider wrapping up the tasks into functions and then demonstrating how we can run them in parallel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant