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

grid.catchment ..... TypeError: '<' not supported between instances of 'AxesSubplot' and 'float' #174

Closed
fluviotect opened this issue Jan 10, 2022 · 5 comments

Comments

@fluviotect
Copy link

# Specify outlet
x, y = 1824187, 5456997

# specify crs
crs = 'EPSG:2193'

# new_crs = pyproj.Proj('+init=epsg:2193') # pyproj syntax has changed
crs_proj = pyproj.CRS(crs)

# Resolve flats in DEM
inflated_dem = grid.resolve_flats(dem)

# compute flow directions...as_crs kwarg added per https://github.com/mdbartos/pysheds/issues/20#issuecomment-416018819
fdir = grid.flowdir(inflated_dem, as_crs=crs_proj)

# Delineate a catchment
catch = grid.catchment(x=x, y=y, fdir=fdir, xytype='coordinate')

TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6164/1784417402.py in
1 # Delineate a catchment
----> 2 catch = grid.catchment(x=x, y=y, fdir=fdir, xytype='coordinate')
3
4 # Clip the view to the catchment
5 grid.clip_to(catch)

~\miniconda3\envs\CatchDelin\lib\site-packages\pysheds\sgrid.py in catchment(self, x, y, fdir, pour_value, dirmap, nodata_out, xytype, routing, snap, **kwargs)
685 xmin, ymin, xmax, ymax = fdir.bbox
686 if xytype in {'label', 'coordinate'}:
--> 687 if (x < xmin) or (x > xmax) or (y < ymin) or (y > ymax):
688 raise ValueError('Pour point ({}, {}) is out of bounds for dataset with bbox {}.'
689 .format(x, y, (xmin, ymin, xmax, ymax)))

TypeError: '<' not supported between instances of 'AxesSubplot' and 'float'

Based on the plotting:
image

the coordinate values seem to be within the graphical space.

@mdbartos
Copy link
Owner

The way that projections are handled was changed in v0.3. See subheading "Converting the Raster coordinate reference system" in the docs: https://mattbartos.com/pysheds/raster.html

The as_crs keyword argument was removed from all functions. It is now probably being swallowed by **kwargs.

For your case, consider something like this:

# Specify outlet
x, y = 1824187., 5456997.

# specify crs
crs = 'EPSG:2193'

# new_crs = pyproj.Proj('+init=epsg:2193') # pyproj syntax has changed
crs_proj = pyproj.CRS(crs)

# Resolve flats in DEM
inflated_dem = grid.resolve_flats(dem)

# Project DEM to new CRS
proj_dem = inflated_dem.to_crs(crs_proj)

# Set grid's viewfinder to projected viewfinder
grid.viewfinder = proj_dem.viewfinder

# Compute flow directions
fdir = grid.flowdir(proj_dem)

# Delineate a catchment
catch = grid.catchment(x=x, y=y, fdir=fdir, xytype='coordinate')

The error message you are getting makes me think that a matplotlib plot was passed in as one of the arguments though.

@fluviotect
Copy link
Author

Cheers!

I was getting a type error (TypeError: crs must be a pyproj.Proj object.) on the reproj and had to edit one bit of the above:

crs_proj = pyproj.CRS(crs)

needs to be:

crs_proj = pyproj.Proj(crs)

your link to the docs got me sorted. Cheers

@fluviotect
Copy link
Author

and good thought on the matplotlib

If I run this block of code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors

fig, ax = plt.subplots(figsize=(8,6))
fig.patch.set_alpha(0)

ax = plt.subplot(1,2,1)
plt.imshow(dem, extent=grid.extent, cmap='terrain', zorder=1)
plt.colorbar(label='Elevation (m)')
plt.grid(zorder=0)
plt.title('Digital elevation map', size=14)
plt.xlabel('NZTM - Easting')
plt.ylabel('NZTM - Northing')

x = plt.subplot(1,2,2)
plt.imshow(pit_filled_dem, extent=grid.extent, cmap='terrain', zorder=1)
plt.colorbar(label='Elevation (m)')
plt.grid(zorder=0)
plt.title('Flow Direction Map', size=14)
plt.xlabel('NZTM - Easting')
plt.ylabel('NZTM - Northing') 

plt.tight_layout()

I get the error. If I skip it, then grid.catchment runs.

This is made even more strange by the fact that I don't even call fdir in the block.

Maybe it's a jupyter issue?

@mdbartos
Copy link
Owner

It's because x is being set to plt.subplot

@fluviotect
Copy link
Author

classic...good catch

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

2 participants