Skip to content

Commit a895d07

Browse files
committed
Changing reprojected CRS code to be more robust instead of hard-coding EPSG:3338. Needed to be done because the CRS is now defined as WKT in ships_meta with rasterio.
1 parent 25b6fed commit a895d07

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

sections/geopandas.qmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ comm.plot(figsize=(9,9))
174174
This plot doesn't look so good. Turns out, these data are in WGS 84 (EPSG 4326), as opposed to Alaska Albers (EPSG 3338), which is what our raster data are in. To make pretty plots, and allow our raster data and vector data to be analyzed together, we'll need to reproject the vector data into 3338. To to this, we'll use the `to_crs` method on our `comm` object, and specify as an argument the projection we want to transform to.
175175

176176
```{python}
177-
comm_3338 = comm.to_crs("EPSG:3338")
177+
comm_3338 = comm.to_crs(ships_meta["crs])
178178
179179
comm_3338.plot()
180180
```
181181

182-
Much better!
182+
Much better! Additionally, we could have reprojected the data using `comm_3338 = comm.to_crs("EPSG:3338")`.
183183

184184
## Crop data to area of interest
185185

@@ -194,7 +194,7 @@ coord_box = box(-159.5, 55, -144.5, 62)
194194
195195
coord_box_df = gpd.GeoDataFrame(
196196
crs = 'EPSG:4326',
197-
geometry = [coord_box]).to_crs("EPSG:3338")
197+
geometry = [coord_box]).to_crs(ships_meta["crs"])
198198
```
199199

200200
Now, we can read in raster again cropped to bounding box. We use the `mask` function from `rasterio.mask`. Note that we apply this to the connection to the raster file (`with rasterio.open(...)`), then update the metadata associated with the raster, because the `mask` function requires as its first `dataset` argument a dataset object opened in `r` mode.

0 commit comments

Comments
 (0)