Skip to content

Commit

Permalink
Re-write Rasterize and tutorial to depend on pyogrio
Browse files Browse the repository at this point in the history
  • Loading branch information
coroa committed Mar 12, 2024
1 parent f51a06c commit 8bb6944
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 78 deletions.
120 changes: 57 additions & 63 deletions docs/notebooks/introduction.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies = [
# https://stackoverflow.com/questions/14399534
'pandas>=1.2',
'deprecated',
'fiona',
"pyogrio",
'xarray[io]',
'affine',
'rasterio',
Expand Down
25 changes: 11 additions & 14 deletions src/ptolemy/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from typing import Optional, Union

import dask
import fiona as fio
import geopandas as gpd
import numpy as np
import pandas as pd
import pyogrio as pio
import xarray as xr
from affine import Affine
from cf_xarray import decode_compress_to_multi_index, encode_multi_index_as_compress
Expand Down Expand Up @@ -279,17 +279,16 @@ def __init__(self, shape=None, coords=None, like=None):
self.shape = shape
self.coords = coords

def read_shpf(self, shpf, idxkey=None, flatten=None):
with fio.open(shpf, "r") as n:
geoms_idxs = tuple((c["geometry"], i) for i, c in enumerate(n))
self.tags = tuple(
(str(i), c["properties"][idxkey] if idxkey is not None else "")
for i, c in enumerate(n)
)
if flatten:
def read_shpf(self, shpf, idxkey=None, flatten=None, where=None):
df = pio.read_dataframe(shpf, where=where)
idx = df[idxkey] if idxkey is not None else np.full(len(df), "")
self.tags = tuple((str(i), s) for i, s in enumerate(idx))

if not flatten:
geoms_idxs = tuple((s, i) for i, s in enumerate(df["geometry"]))
else:
logger.info("Flatting geometries to a single feature")
geoms = [shape(geom) for geom, i in geoms_idxs]
geoms_idxs = [(unary_union(geoms), 0)]
geoms_idxs = [(df.unary_union, 0)]
self.geoms_idxs = geoms_idxs
self.idxkey = idxkey
return self
Expand Down Expand Up @@ -813,9 +812,7 @@ def aggregate(
self.indicator,
expected_groups=pd.RangeIndex(len(self.index) + 1),
func=func,
).isel(
{self.dim: slice(1, None)}
) # skip the "outside of all"-element
).isel({self.dim: slice(1, None)}) # skip the "outside of all"-element

if interior_only:
return weight_indicator.assign_coords({self.dim: self.index})
Expand Down

0 comments on commit 8bb6944

Please sign in to comment.