Open alexgleith opened 2 months ago
This is the more explicit code illustrating the issue:
transformer_to = Transformer.from_crs(src.crs, dst.crs, always_xy=True)
t_to_wgs = Transformer.from_crs(src.crs, "EPSG:4326", always_xy=True)
# xy_pix_src is in pixel space of source
xx, yy = np.asarray([pt.xy for pt in xy_pix_src]).T
# Convert to world coordinates
xx, yy = src.pix2wld(xx, yy)
# Transform to destination crs, which results in some coords that are on the wrong
# side of the antimeridian
xx_p, yy_p = transformer_to.transform(xx, yy)
# print(f"This should be negative {xx_p[0]}")
# Uncommenting this fixes the whole thing, but it's not the right way to do it
# for i, n in enumerate(xx_p):
# if n > 0:
# xx_p[i] = n * -1
# Convert back to pixel space of destination
xx, yy = dst.wld2pix(*(xx_p, yy_p))
xys_new = [xy_(x, y) for x, y in zip(xx, yy)]
# Coords are borked
xys_new
Another example of unloadable data at the antimeridian:
from pystac import Item
from affine import Affine
from odc.geo.geobox import GeoBox
from odc.stac import load
url = 'https://landsatlook.usgs.gov/stac-server/collections/landsat-c2l2-sr/items/LC09_L2SR_073073_20240128_20240130_02_T1_SR'
item = Item.from_file(url)
src_geobox = GeoBox(item.properties["proj:shape"], Affine(*item.properties["proj:transform"]), crs=item.properties["proj:epsg"])
# This is wrong... it's world spanning.
src_geobox.geographic_extent
shape = (5000, 5000)
transform = Affine(0.0003, 0.0, 180.0, 0.0, -0.0003, -18.0)
crs = 'EPSG:4326'
dst_geobox = GeoBox(shape, transform, crs)
# Can't load data in the target geobox
data = load([item], chunks={}, geobox=dst_geobox, bands=["red"]).compute()
data # Empty
The issue is documented in detail in https://github.com/opendatacube/odc-stac/issues/172
The short reproduction of the cause of the issue is this code:
which results in:
Noting that the large value coords like
262143.xxx
should be something like0.xxx
.The main issue is transforming from Web Mercator to UTM and back to to Web Mercator, which results in ambiguity, and therefore, coordinates that are one on side or the other of the antimeridian.