-
Notifications
You must be signed in to change notification settings - Fork 109
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
[XARRAY] add env settings to fallback to WGS84 when no CRS is provided #725
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
from __future__ import annotations | ||
|
||
import os | ||
import warnings | ||
from typing import Any, Dict, List, Optional | ||
|
||
|
@@ -15,7 +16,12 @@ | |
from rasterio.warp import transform as transform_coords | ||
|
||
from rio_tiler.constants import WEB_MERCATOR_TMS, WGS84_CRS | ||
from rio_tiler.errors import PointOutsideBounds, TileOutsideBounds | ||
from rio_tiler.errors import ( | ||
MissingCRSWarning, | ||
OverwritingCRSWarning, | ||
PointOutsideBounds, | ||
TileOutsideBounds, | ||
) | ||
from rio_tiler.io.base import BaseReader | ||
from rio_tiler.models import BandStatistics, ImageData, Info, PointData | ||
from rio_tiler.types import BBox, NoData, WarpResampling | ||
|
@@ -32,6 +38,11 @@ | |
rioxarray = None # type: ignore | ||
|
||
|
||
default_to_wgs84_crs = os.environ.get( | ||
"RIO_TILER_XARRAY_DEFAULT_WGS84", "FALSE" | ||
).upper() in ["TRUE", "YES"] | ||
|
||
|
||
@attr.s | ||
class XarrayReader(BaseReader): | ||
"""Xarray Reader. | ||
|
@@ -71,7 +82,20 @@ def __attrs_post_init__(self): | |
assert rioxarray is not None, "rioxarray must be installed to use XarrayReader" | ||
|
||
self.bounds = tuple(self.input.rio.bounds()) | ||
|
||
if not self.input.rio.crs and default_to_wgs84_crs: | ||
warnings.warn( | ||
"Dataset doesn't have a valid CRS set. Automatically setting CRS to WGS84", | ||
OverwritingCRSWarning, | ||
) | ||
self.input.rio.write_crs("epsg:4326", inplace=True) | ||
|
||
self.crs = self.input.rio.crs | ||
if not self.crs: | ||
warnings.warn( | ||
"Dataset doesn't have a valid CRS set. rio-tiler might not work properly", | ||
MissingCRSWarning, | ||
) | ||
Comment on lines
+94
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we not throw an exception in this case? Using an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually don't fail for this because you can still access the dataset 🤷 Let me think about it |
||
|
||
self._dims = [ | ||
d | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mutates the
self.input
Xarray object (DataArray).If we do not copy what the user passed in earlier, this behaviour could be unexpected for a user.
If there is a chance that the XarrayReader mutates the input, should we perhaps always have it create a copy up-front?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 good point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬 sadly rio-tiler gets the input directly from the user so we can't really avoid
editing
the origin data array