def test_identity_gcps(): """Define an identity transform using GCPs""" # Tile: [53, 96, 8] src_crs = dst_crs = 'EPSG:3857' width = height = 1000 left, bottom, right, top = (-11740727.544603072, 4852834.0517692715, -11584184.510675032, 5009377.085697309) # For comparison only, these are not used to calculate the transform. transform = from_bounds(left, bottom, right, top, width, height) # Define 4 ground control points at the corners of the image. gcps = [ GroundControlPoint(row=0, col=0, x=left, y=top, z=0.0), GroundControlPoint(row=0, col=1000, x=right, y=top, z=0.0), GroundControlPoint(row=1000, col=1000, x=right, y=bottom, z=0.0), GroundControlPoint(row=1000, col=0, x=left, y=bottom, z=0.0) ] # Compute an output transform. res_transform, res_width, res_height = _calculate_default_transform( src_crs, dst_crs, height=height, width=width, gcps=gcps) assert res_width == width assert res_height == height for res, exp in zip(res_transform, transform): assert round(res, 3) == round(exp, 3)
def test_reproject_gcps(rgb_byte_profile): """Reproject using ground control points for the source""" source = np.ones((3, 800, 800), dtype=np.uint8) * 255 out = np.zeros( (3, rgb_byte_profile["height"], rgb_byte_profile["height"]), dtype=np.uint8 ) 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), ] reproject( source, out, src_crs="EPSG:32618", gcps=src_gcps, dst_transform=rgb_byte_profile["transform"], dst_crs=rgb_byte_profile["crs"], resampling=Resampling.nearest, ) assert not out.all() assert not out[:, 0, 0].any() assert not out[:, 0, -1].any() assert not out[:, -1, -1].any() assert not out[:, -1, 0].any()
def test_warpedvrt_gcps__width_height(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=numpy.uint8) as source: source.gcps = (src_gcps, crs) with rasterio.open(tiffname) as src: with WarpedVRT(src, width=10, height=10) as vrt: assert vrt.height == 10 assert vrt.width == 10 assert vrt.crs == crs assert vrt.dst_transform.almost_equals( affine.Affine(22271.389322449897, 0.0, 115698.25, 0.0, -20016.05875815117, 2818720.0))
def test_write_read_gcps(tmpdir): tiffname = str(tmpdir.join('test.tif')) gcps = [GroundControlPoint(1, 1, 100.0, 1000.0, z=0.0)] with rasterio.open(tiffname, 'w', driver='GTiff', dtype='uint8', count=1, width=10, height=10, crs='epsg:4326', gcps=gcps) as dst: pass with rasterio.open(tiffname, 'r+') as dst: gcps, crs = dst.gcps assert crs['init'] == 'epsg:4326' assert len(gcps) == 1 point = gcps[0] assert (1, 1) == (point.row, point.col) assert (100.0, 1000.0, 0.0) == (point.x, point.y, point.z) dst.gcps = [ GroundControlPoint(1, 1, 100.0, 1000.0, z=0.0), GroundControlPoint(2, 2, 200.0, 2000.0, z=0.0)], crs gcps, crs = dst.gcps assert crs['init'] == 'epsg:4326' assert len(gcps) == 2 point = gcps[1] assert (2, 2) == (point.row, point.col) assert (200.0, 2000.0, 0.0) == (point.x, point.y, point.z)
def gcps(): return [ GroundControlPoint(row=11521.5, col=0.5, x=-123.6185142817931, y=48.99561141948625, z=89.13533782958984, id='217', info=''), GroundControlPoint(row=11521.5, col=7448.5, x=-122.8802747777599, y=48.91210259315549, z=89.13533782958984, id='234', info=''), GroundControlPoint(row=0.5, col=0.5, x=-123.4809665720148, y=49.52809729106944, z=89.13533782958984, id='1', info=''), GroundControlPoint(row=0.5, col=7448.5, x=-122.7345733674704, y=49.44455878004666, z=89.13533782958984, id='18', info='') ]
def test_write_read_gcps_buffereddatasetwriter(tmpdir): filename = str(tmpdir.join('test.jpg')) gcps = [GroundControlPoint(1, 1, 100.0, 1000.0, z=0.0)] with rasterio.open(filename, 'w', driver='JPEG', dtype='uint8', count=3, width=10, height=10, crs='epsg:4326', gcps=gcps) as dst: dst.write(numpy.ones((3, 10, 10), dtype='uint8')) with rasterio.open(filename, 'r+') as dst: gcps, crs = dst.gcps assert crs.to_epsg() == 4326 assert len(gcps) == 1 point = gcps[0] assert (1, 1) == (point.row, point.col) assert (100.0, 1000.0, 0.0) == (point.x, point.y, point.z) dst.gcps = [ GroundControlPoint(1, 1, 100.0, 1000.0, z=0.0), GroundControlPoint(2, 2, 200.0, 2000.0, z=0.0) ], crs gcps, crs = dst.gcps assert crs.to_epsg() == 4326 assert len(gcps) == 2 point = gcps[1] assert (2, 2) == (point.row, point.col) assert (200.0, 2000.0, 0.0) == (point.x, point.y, point.z)
def test_gcps_calculate_transform(): 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)] _, width, height = calculate_default_transform( 'epsg:3857', 'epsg:4326', width=800, height=800, gcps=src_gcps) assert width == 1087 assert height == 895
def get_gcps(self): """ Get the GroundControlPoints from the dataset. https://rasterio.readthedocs.io/en/latest/topics/georeferencing.html#ground-control-points Returns ------- list of :obj:`rasterio.control.GroundControlPoints` or None The Ground Control Points from the dataset or None if not applicable """ try: geojson_gcps = self._obj.coords[self.grid_mapping].attrs["gcps"] except (KeyError, AttributeError): return None gcps = [ GroundControlPoint( x=gcp["geometry"]["coordinates"][0], y=gcp["geometry"]["coordinates"][1], z=gcp["geometry"]["coordinates"][2], row=gcp["properties"]["row"], col=gcp["properties"]["col"], id=gcp["properties"]["id"], info=gcp["properties"]["info"], ) for gcp in geojson_gcps["features"] ] return gcps
def test_gcp_geo_interface(): gcp = GroundControlPoint(1.0, 1.5, 100.0, 1000.0, id='foo', info='bar') assert gcp.__geo_interface__['geometry']['coordinates'] == (100.0, 1000.0) assert gcp.__geo_interface__['type'] == 'Feature' assert gcp.__geo_interface__['id'] == 'foo' assert gcp.__geo_interface__['properties']['info'] == 'bar' assert gcp.__geo_interface__['properties']['row'] == 1.0 assert gcp.__geo_interface__['properties']['col'] == 1.5
def test_gcp(): gcp = GroundControlPoint(1.0, 1.5, 100.0, 1000.0, z=0.0) assert gcp.row == 1.0 assert gcp.col == 1.5 assert gcp.x == 100.0 assert gcp.y == 1000.0 assert gcp.z == 0.0 assert isinstance(gcp.id, str)
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, driver="GTiff", ) 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, ) )
def test_gcp_empty(): with pytest.raises(ValueError): GroundControlPoint()
def test_gcp_geo_interface_z(): gcp = GroundControlPoint(1.0, 1.5, 100.0, 1000.0, z=0.0) assert gcp.__geo_interface__['geometry']['coordinates'] == (100.0, 1000.0, 0.0)
def test_gcp_dict(): gcp = GroundControlPoint(1.0, 1.5, 100.0, 1000.0, id='foo', info='bar') assert gcp.asdict()['row'] == 1.0 assert gcp.asdict()['col'] == 1.5 assert gcp.asdict()['x'] == 100.0
def test_gcp_repr(): gcp = GroundControlPoint(1.0, 1.5, 100.0, 1000.0, id='foo', info='bar') copy = eval(repr(gcp)) for attr in ('id', 'info', 'row', 'col', 'x', 'y', 'z'): assert getattr(copy, attr) == getattr(gcp, attr)