Skip to content

Commit

Permalink
BUG: Improve WarpedVRT support for gcps
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed Jun 3, 2021
1 parent 5293cc2 commit ccf3dc7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ History

Latest
------
- BUG: Improve WarpedVRT support for gcps (issue #339)

0.4.1
------
Expand Down
4 changes: 2 additions & 2 deletions rioxarray/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,8 @@ def open_rasterio(
vrt = filename
filename = vrt.src_dataset.name
vrt_params = dict(
src_crs=vrt.src_crs.to_string(),
crs=vrt.crs.to_string(),
src_crs=vrt.src_crs.to_string() if vrt.src_crs else None,
crs=vrt.crs.to_string() if vrt.crs else None,
resampling=vrt.resampling,
tolerance=vrt.tolerance,
src_nodata=vrt.src_nodata,
Expand Down
36 changes: 36 additions & 0 deletions test/integration/test_integration__io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import xarray as xr
from affine import Affine
from numpy.testing import assert_almost_equal, assert_array_equal
from rasterio.control import GroundControlPoint
from rasterio.crs import CRS
from rasterio.errors import NotGeoreferencedWarning
from rasterio.transform import from_origin
from rasterio.warp import calculate_default_transform
Expand Down Expand Up @@ -872,6 +874,40 @@ def test_rasterio_vrt_with_src_crs():
assert rds.rio.crs == src_crs


def test_rasterio_vrt_gcps(tmp_path):
tiffname = tmp_path / "test.tif"
src_gcps = [
GroundControlPoint(row=0, col=0, x=156113, y=2818720, z=0),
GroundControlPoint(row=0, col=800, x=338353, y=2785790, z=0),
GroundControlPoint(row=800, col=800, x=297939, y=2618518, z=0),
GroundControlPoint(row=800, col=0, x=115698, y=2651448, z=0),
]
crs = CRS.from_epsg(32618)
with rasterio.open(
tiffname, mode="w", height=800, width=800, count=3, dtype=np.uint8
) as source:
source.gcps = (src_gcps, crs)

with rasterio.open(tiffname) as src:
# NOTE: Eventually src_crs will not need to be provided
# https://github.com/mapbox/rasterio/pull/2193
with rasterio.vrt.WarpedVRT(src, src_crs=crs) as vrt:
with rioxarray.open_rasterio(vrt) as rds:
assert rds.rio.height == 923
assert rds.rio.width == 1027
assert rds.rio.crs == crs
assert rds.rio.transform().almost_equals(
Affine(
216.8587081056465,
0.0,
115698.25,
0.0,
-216.8587081056465,
2818720.0,
)
)


@pytest.mark.parametrize("lock", [True, False])
def test_open_cog(lock):
cog_file = os.path.join(TEST_INPUT_DATA_DIR, "cog.tif")
Expand Down

0 comments on commit ccf3dc7

Please sign in to comment.