Skip to content

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

@jeanetteclark

Description

@jeanetteclark

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions