def mask_to_objects_2d(mask, background=0, offset=None, flatten_collection=True): """Convert 2D (binary or label) mask to polygons. Generates borders fitting in the objects. Parameters ---------- mask: ndarray 2D mask array. Expected shape: (height, width). background: int Value used for encoding background pixels. offset: tuple (optional, default: None) (y, x) coordinate offset to apply to all the extracted polygons. flatten_collection: bool True for flattening geometry collections into individual geometries. Returns ------- extracted: list of AnnotationSlice Each object slice represent an object from the image. Fields time and depth of AnnotationSlice are set to None. """ if mask.ndim != 2: raise ValueError("Cannot handle image with ndim different from 2 ({} dim. given).".format(mask.ndim)) if offset is None: offset = (0, 0) exclusion = np.logical_not(mask == background) affine = Affine(1, 0, offset[1], 0, 1, offset[0]) slices = list() for gjson, label in shapes(mask.copy(), mask=exclusion, transform=affine): polygon = shape(gjson) # fixing polygon if not polygon.is_valid: # attempt to fix polygon = fix_geometry(polygon) if not polygon.is_valid: # could not be fixed continue if not hasattr(polygon, "geoms") or not flatten_collection: slices.append(AnnotationSlice(polygon=polygon, label=int(label))) else: for curr in flatten_geoms(polygon.geoms): slices.append(AnnotationSlice(polygon=curr, label=int(label))) return slices
def extract_by_coordinate_mxd11c3(MYD_DIR, ul=(123.652796, 46.327), lr=(128, 42.203257), res=0.05): """Extracet modis LST monthly data by coordinate extent. Args: MYD_DIR (str): Directory holding the myd(mod)11c3 product. ul (:tuple: lon,lat): Upper left coordinate. lr (:tuple: lon,lat): Lower right coordinate. res (float): Resolution in map units. """ for mxd11c3 in glob.glob(os.path.join(MYD_DIR, "*.hdf")): lst_arr, trans = open_eos_hdf_gdal_lst(mxd11c3) fwd = Affine.from_gdal(trans[0], trans[1], trans[2], trans[3], trans[4], trans[5]) ul_c, ul_r = ~fwd * ul # or (123.652796+180)*20, (90-46.700438)*20 lr_c, lr_r = ~fwd * lr ur_lon, ur_lat = fwd * (int(ul_c), int(ul_r)) trans_old = Affine(res, 0, ur_lon, 0, -res, ur_lat) width_old = int(lr_c) - int(ul_c) + 1 height_old = int(lr_r) - int(ul_r) + 1 kargs = { "driver": "GTIFF", "count": 1, "transform": trans_old, "width": width_old, "height": height_old, "crs": 'epsg:4326', "dtype": 'float32', } src_arr = lst_arr[int(ul_r):int(ul_r) + height_old, int(ul_c):int(ul_c)+width_old].astype('float32') with rasterio.open(mxd11c3.replace('.hdf', '.tif'), 'w', **kargs) as dst: dst.write(src_arr, 1)
def __init__( self, shape=(1, 1), origin=(0.0, 0.0), pixelsize=(0.00025, 0.00025), crs="EPSG:4326", ): """ Create a new GriddedGeoBox. :param shape: (ySize, xSize) 2-tuple defining the shape of the GGB. * Use get_shape_xy() to get (xSize, ySize). :param origin: (xPos, yPos) 2-tuple difining the upper left GGB corner. :param pixelsize: Pixel (xSize, ySize) in CRS units. :param crs: The SpatialReferenceSystem in which both origin and pixelsize are expressed (supports various text formats). """ self.pixelsize = pixelsize self.shape = tuple([int(v) for v in shape]) self.origin = origin if isinstance(crs, osr.SpatialReference): self.crs = crs else: self.crs = osr.SpatialReference() if self.crs == self.crs.SetFromUserInput(crs): raise ValueError("Invalid crs: %s" % (crs, )) # enforce x,y axis ordering self.crs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) self.transform = Affine(self.pixelsize[0], 0, self.origin[0], 0, -self.pixelsize[1], self.origin[1]) self.corner = self.transform * self.get_shape_xy()
def make_tifs_pngs(folder_path, tif_folder, save_folder, new_res=3.0): ''' Turns pngs into tifs with a new resolution. ''' pngs = gl.glob(folder_path + '/*.png') for png in pngs: name = os.path.basename(png)[:-17] tif = rio.open(tif_folder + '//' + name + '.tif') profile = tif.profile old_affine = profile['transform'] new_affine = Affine(new_res, old_affine.b, old_affine.c, old_affine.d, -new_res, old_affine.f) profile.update({'transform': new_affine}) profile.update({'dtype': 'uint8'}) profile.update({'width': 480, 'height': 480}) with rio.open(save_folder + '//' + name + '.tif', "w", **profile) as dest: dest.write(rio.open(png).read())
def interpolate_clouds_to_10m(file_clouds): print('interpolate_clouds_to_10m:\n') print('Open files, reserve memory:\n') dataset_clouds = rio.open(file_clouds) clouds = dataset_clouds.read(1) clouds10 = np.empty(shape=(int(round(clouds.shape[0] * 2)), int(round(clouds.shape[1] * 2)))) print('define transformation:\n') aff = dataset_clouds.transform newaff = Affine(aff[0] / 2, aff[1], aff[2], aff[3], aff[4] / 2, aff[5]) print('Reproject: \n') reproject(clouds, clouds10, src_transform=aff, dst_transform=newaff, src_crs=dataset_clouds.crs, dst_crs=dataset_clouds.crs, resampling=Resampling.nearest) return (clouds10)
def test_calculate_default_transform_single_resolution(): with rasterio.drivers(): with rasterio.open('tests/data/RGB.byte.tif') as src: l, b, r, t = src.bounds target_resolution = 0.1 target_transform = Affine(target_resolution, 0.0, -78.95864996545055, 0.0, -target_resolution, 25.550873767433984) dst_transform, width, height = calculate_default_transform( l, b, r, t, src.width, src.height, src.crs, {'init': 'EPSG:4326'}, resolution=target_resolution) assert dst_transform.almost_equals(target_transform) assert width == 24 assert height == 20
def __or__(self, other): """ Given two affine descriptions of image grids, create a grid that encompasses both images. """ assert numpy.allclose(self.linear_part(), other.linear_part()) translation = other.shift_part() - self.shift_part() linear = self.linear_part() steps = assert_ints(numpy.linalg.solve(linear, translation)) shift = numpy.array(self.affine * numpy.where(steps > 0, 0, steps)) affine = Affine(linear[0, 0], linear[0, 1], shift[0], linear[1, 0], linear[1, 1], shift[1]) self_end = ~affine * self.corner_coords() other_end = ~affine * other.corner_coords() shape = (int(max(self_end[1], other_end[1])), int(max(self_end[0], other_end[0]))) return GeoBox(affine, shape)
def test_geobox_simple(): from affine import Affine t = geometry.GeoBox(4000, 4000, Affine(0.00025, 0.0, 151.0, 0.0, -0.00025, -29.0), epsg4326) expect_lon = np.asarray([151.000125, 151.000375, 151.000625, 151.000875, 151.001125, 151.001375, 151.001625, 151.001875, 151.002125, 151.002375]) expect_lat = np.asarray([-29.000125, -29.000375, -29.000625, -29.000875, -29.001125, -29.001375, -29.001625, -29.001875, -29.002125, -29.002375]) expect_resolution = np.asarray([-0.00025, 0.00025]) assert t.coordinates['latitude'].values.shape == (4000,) assert t.coordinates['longitude'].values.shape == (4000,) np.testing.assert_almost_equal(t.resolution, expect_resolution) np.testing.assert_almost_equal(t.coords['latitude'].values[:10], expect_lat) np.testing.assert_almost_equal(t.coords['longitude'].values[:10], expect_lon) assert (t == "some random thing") is False
def _get_bounds(self) -> tuple: if self.aoi is None: return self.raster_bounds, self.input_transform overlay = gpd.overlay( self._get_gpdf_from_bounds(self.raster_bounds), self._get_gpdf_from_bounds(self.aoi.total_bounds), ) xmin, _, _, ymax = overlay.total_bounds aoi_transform = Affine( self.input_transform.a, self.input_transform.b, xmin, self.input_transform.d, self.input_transform.e, ymax, ) return overlay.total_bounds, aoi_transform
def test_rasterization_of_line_simple(): resolution = 1 pixels_width = 1 line = GeoFeature.from_shape(LineString([(2.5, 0), (2.5, 3)])) roi = GeoVector.from_bounds(xmin=0, ymin=0, xmax=5, ymax=5, crs=DEFAULT_CRS) fc = FeatureCollection([line]) expected_image = np.zeros((5, 5), dtype=np.uint8) expected_image[2:, 2] = 1 expected_affine = Affine(1.0, 0.0, 0.0, 0.0, -1.0, 5.0) expected_crs = DEFAULT_CRS expected_result = GeoRaster2(expected_image, expected_affine, expected_crs) result = fc.rasterize(resolution, polygonize_width=pixels_width, crs=DEFAULT_CRS, bounds=roi) assert result == expected_result
def test_when_constructor_argument_is_a_gdal_dataset_properties_are_correct( valid_gdal_dataset: gdal.Dataset): raster_dataset = RasterDataset(valid_gdal_dataset) assert raster_dataset._gdal_dataset == valid_gdal_dataset # pylint: disable=protected-access assert raster_dataset.filename is None assert raster_dataset.shape == (4, 3, 2) assert raster_dataset.bands == 4 assert raster_dataset.rows == 3 assert raster_dataset.cols == 2 assert raster_dataset.affine == Affine(1.0, 0.0, -123.0, 0.0, -2.0, 45.0) assert raster_dataset.pixel_size == (1.0, 2.0) assert raster_dataset.pixel_size_x == 1.0 assert raster_dataset.pixel_size_y == 2.0 assert raster_dataset.origin == (-123, 45) assert raster_dataset.origin_long == -123 assert raster_dataset.origin_lat == 45 assert raster_dataset.northwest_corner == (-123, 45) assert raster_dataset.southwest_corner == (-123, 39) assert raster_dataset.northeast_corner == (-121, 45) assert raster_dataset.southeast_corner == (-121, 39) assert raster_dataset.center == (-122, 42)
def test_calculate_default_transform_single_resolution(): with rasterio.open("tests/data/RGB.byte.tif") as src: target_resolution = 0.1 target_transform = Affine( target_resolution, 0.0, -78.95864996545055, 0.0, -target_resolution, 25.550873767433984, ) dst_transform, width, height = calculate_default_transform( src.crs, {"init": "EPSG:4326"}, src.width, src.height, *src.bounds, resolution=target_resolution) assert dst_transform.almost_equals(target_transform) assert width == 24 assert height == 20
def test_rasterization_of_line_has_correct_pixel_width(resolution): xmax, ymax = 11, 5 pixels_width = 1 line = GeoFeature.from_shape(LineString([(xmax / 2, 0), (xmax / 2, ymax * 4 / 5)])) roi = GeoVector.from_bounds(xmin=0, ymin=0, xmax=xmax, ymax=ymax, crs=DEFAULT_CRS) fc = FeatureCollection([line]) expected_image = np.zeros((int(ymax // resolution), int(xmax // resolution)), dtype=np.uint8) expected_image[int(1 // resolution):, expected_image.shape[1] // 2] = 1 expected_affine = Affine(resolution, 0.0, 0.0, 0.0, -resolution, 5.0) expected_crs = DEFAULT_CRS expected_result = GeoRaster2(expected_image, expected_affine, expected_crs) result = fc.rasterize(resolution, polygonize_width=pixels_width, crs=DEFAULT_CRS, bounds=roi) assert result == expected_result
def test_read_raster_with_custom_crs_and_transform(example_gdal_path): with rasterio.open(example_gdal_path) as src: band = rasterio.band(src, 1) crs = geometry.CRS('EPSG:3577') nodata = -999 transform = Affine(25.0, 0.0, 1000000.0, 0.0, -25.0, -900000.0) # Read all raw data from source file band_data_source = OverrideBandDataSource(band, nodata, crs, transform) dest1 = band_data_source.read() assert dest1.shape # Attempt to read with the same transform parameters dest2 = np.full(shape=(4000, 4000), fill_value=nodata, dtype=np.float32) dst_transform = transform dst_crs = crs dst_nodata = nodata resampling = datacube.storage.storage.RESAMPLING_METHODS['nearest'] band_data_source.reproject(dest2, dst_transform, dst_crs, dst_nodata, resampling) assert (dest1 == dest2).all()
def _get_transform(self): bbox = self.raster.bounds extent = [[bbox.left, bbox.top], [bbox.left, bbox.bottom], [bbox.right, bbox.bottom], [bbox.right, bbox.top]] raster_boundary = shape({'coordinates': [extent], 'type': 'Polygon'}) if not self.geometry.intersects(raster_boundary): print('shape do not intersect with rs image') return # get pixel coordinates of the geometry's bounding box, ll = self.raster.index( *self.geometry.bounds[0:2]) # lowerleft bounds[0:2] xmin, ymin ur = self.raster.index( *self.geometry.bounds[2:4]) # upperright bounds[2:4] xmax, ymax # create an affine transform for the subset data t = self.raster.transform shifted_affine = Affine(t.a, t.b, t.c + ll[1] * t.a, t.d, t.e, t.f + ur[0] * t.e) return shifted_affine
def test_percent_cover_zonal_stats(): polygon = Polygon([[0, 0], [0, 1.5], [1, 1.5], [1, 2], [2, 2], [2, 0]]) arr = np.array([ [100, 1], [100, 1] ]) affine = Affine(1, 0, 0, 0, -1, 2) stats_options = 'min max mean count sum nodata' # run base case # includes cell that is only 50% covered stats_a = zonal_stats(polygon, arr, affine=affine, stats=stats_options) assert stats_a[0]['mean'] == 50.5 # test selection # does not include cell that is only 50% covered stats_b = zonal_stats(polygon, arr, affine=affine, percent_cover_selection=0.75, percent_cover_scale=10, stats=stats_options) assert round(stats_b[0]['count'], 2) == 3 assert round(stats_b[0]['mean'], 2) == 34 # test weighting stats_c = zonal_stats(polygon, arr, affine=affine, percent_cover_weighting=True, percent_cover_scale=10, stats=stats_options) assert round(stats_c[0]['count'], 1) == 3.5 assert round(stats_c[0]['mean'], 1) == 43.4 assert round(stats_c[0]['sum'], 1) == 152 # check that percent_cover_scale is set to 10 when not provided by user stats_d = zonal_stats(polygon, arr, affine=affine, percent_cover_weighting=True, stats=stats_options) assert round(stats_d[0]['mean'], 2) == round(stats_c[0]['mean'], 2) # check invalid percent_cover_scale value with pytest.raises(Exception): zonal_stats(polygon, arr, affine=affine, percent_cover_selection=0.75, percent_cover_scale=0.5) # check invalid percent_cover_selection value with pytest.raises(Exception): zonal_stats(polygon, arr, affine=affine, percent_cover_selection='one', percent_cover_scale=10)
def game_loop(): """Prompt the user for input to encrypt or decrypt and, if applicable, any additional input settings required to perform the cipher process.""" menu() while True: cipher = input("\nWhich cipher would you like to use? ").title() available_ciphers = ["Caesar", "Affine", "Atbash", "Keyword"] if cipher in available_ciphers: if cipher.lower() == 'caesar': klass = Caesar() elif cipher.lower() == 'affine': klass = Affine() elif cipher.lower() == 'atbash': klass = AtBash() elif cipher.lower() == 'keyword': print("\n{} is a good cipher!\n".format(cipher.title())) elif cipher not in available_ciphers: print("\nThat's not an availble cipher, please select again!\n") continue else: pass user_choice = input("Are we going to encrypt or decrypt? ") if user_choice.lower() == 'encrypt': cipher_text = input("\nWhat cipher text do you want to encrypt? ") print('\n', klass.encrypt(str(cipher_text))) elif user_choice.lower() == 'decrypt': cipher_text = input("\nWhat cipher text do you want to decrypt? ") print('\n', klass.decrypt(str(cipher_text))) else: continue value = input("\nEncrypt/decrypt something else? (Y/N) ") if value.lower() == 'y': menu() continue elif value.lower() == 'n': print("\nThanks for using the Secret Messages app!!!\n") break else: pass
def test_testutils_geobox(): from datacube.testutils.io import dc_crs_from_rio, rio_geobox from rasterio.crs import CRS from affine import Affine assert rio_geobox({}) is None transform = Affine(10, 0, 4676, 0, -10, 171878) shape = (100, 640) h, w = shape crs = CRS.from_epsg(3578) meta = dict(width=w, height=h, transform=transform, crs=crs) gbox = rio_geobox(meta) assert gbox.shape == shape assert gbox.crs.epsg == 3578 assert gbox.transform == transform wkt = '''PROJCS["unnamed", GEOGCS["NAD83", DATUM["North_American_Datum_1983", SPHEROID["GRS 1980",6378137,298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]], UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]], ], PROJECTION["Albers_Conic_Equal_Area"], PARAMETER["standard_parallel_1",61.66666666666666], PARAMETER["standard_parallel_2",68], PARAMETER["latitude_of_center",59], PARAMETER["longitude_of_center",-132.5], PARAMETER["false_easting",500000], PARAMETER["false_northing",500000], UNIT["Meter",1]] ''' crs_ = dc_crs_from_rio(CRS.from_wkt(wkt)) assert crs_.epsg is None
def aligned_target(transform, width, height, resolution): """Aligns target to specified resolution Parameters ---------- transform : Affine Input affine transformation matrix width, height: int Input dimensions resolution: tuple (x resolution, y resolution) or float Target resolution, in units of target coordinate reference system. Returns ------- transform: Affine Output affine transformation matrix width, height: int Output dimensions """ if isinstance(resolution, (float, int)): res = (float(resolution), float(resolution)) else: res = resolution xmin = transform.xoff ymin = transform.yoff + height * transform.e xmax = transform.xoff + width * transform.a ymax = transform.yoff xmin = floor(xmin / res[0]) * res[0] xmax = ceil(xmax / res[0]) * res[0] ymin = floor(ymin / res[1]) * res[1] ymax = ceil(ymax / res[1]) * res[1] dst_transform = Affine(res[0], 0, xmin, 0, -res[1], ymax) dst_width = max(int(ceil((xmax - xmin) / res[0])), 1) dst_height = max(int(ceil((ymax - ymin) / res[1])), 1) return dst_transform, dst_width, dst_height
def test_reproject_epsg__simple_array_dst(): with rasterio.open("tests/data/RGB.byte.tif") as src: source = src.read(1) dst_crs = {"init": "EPSG:3857"} dst_out = np.empty(src.shape, dtype=np.uint8) out, dst_transform = reproject( source, dst_out, src_transform=src.transform, src_crs=src.crs, dst_crs=dst_crs, resampling=Resampling.nearest, ) assert (out > 0).sum() == 368123 assert_almost_equal(tuple(dst_transform), tuple( Affine(335.3101519032594, 0.0, -8789636.707871985, 0.0, -338.579773957742, 2943560.2346221623)), decimal=5)
def test_get_ij(): # upper left corner xul, yul = 434955., 1342785. spacing = 500. transform = Affine(spacing, 0.0, xul, 0.0, -spacing, yul) # get the i, j location for a point just past the first cell center x = xul + spacing / 2 + 1 y = yul - spacing / 2 - 1 i, j = get_ij(transform, x, y) assert (i, j) == (0, 0) # test the upper left corner i, j = get_ij(transform, xul, yul) assert (i, j) == (0, 0) # test a lower left corner expected_i, expected_j = 1000, 1000 i, j = get_ij(transform, xul + spacing * expected_i, yul - spacing * expected_j) assert (i, j) == (expected_i, expected_j)
def image_profile(lon, lat, crs, resolution, im, driver=GTIFF_DRIVER): count, height, width = im.shape x, y = Transformer.from_crs("epsg:4326", crs).transform(lat, lon) xmin = round(x - (width * resolution / 2)) ymin = round(y - (height * resolution / 2)) profile = { 'count': count, 'crs': CRS.from_string(crs), 'driver': GTIFF_DRIVER, 'dtype': im.dtype, 'height': height, 'nodata': None, 'transform': Affine(resolution, 0, xmin, 0, -resolution, ymin), 'width': width } if driver == GTIFF_DRIVER: profile.update({ 'compress': 'lzw', 'interleave': 'pixel', 'tiled': False }) return profile
def test_testutils_geobox(): from datacube.testutils.io import dc_crs_from_rio, rio_geobox from rasterio.crs import CRS from affine import Affine assert rio_geobox({}) is None A = Affine(10, 0, 4676, 0, -10, 171878) shape = (100, 640) h, w = shape crs = CRS.from_epsg(3578) meta = dict(width=w, height=h, transform=A, crs=crs) gbox = rio_geobox(meta) assert gbox.shape == shape assert gbox.crs.epsg == 3578 assert gbox.transform == A crs_ = dc_crs_from_rio(CRS.from_wkt(crs.wkt)) assert crs_.epsg is None
def test_get_area_def_from_raster(self): from rasterio.crs import CRS from affine import Affine x_size = 791 y_size = 718 transform = Affine(300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805, 2826915.0) crs = CRS(init='epsg:3857') source = tmptiff(x_size, y_size, transform, crs=crs) area_id = 'area_id' proj_id = 'proj_id' name = 'name' area_def = pyresample.utils._rasterio.get_area_def_from_raster( source, area_id=area_id, name=name, proj_id=proj_id) self.assertEqual(area_def.area_id, area_id) self.assertEqual(area_def.proj_id, proj_id) self.assertEqual(area_def.name, name) self.assertEqual(area_def.width, x_size) self.assertEqual(area_def.height, y_size) self.assertDictEqual(crs.to_dict(), area_def.proj_dict) self.assertTupleEqual(area_def.area_extent, (transform.c, transform.f + transform.e * y_size, transform.c + transform.a * x_size, transform.f))
def test_affine_identity(tmpdir): """ Setting a transform with absolute values equivalent to Affine.identity() should result in a warning (not captured here) and read with affine that matches Affine.identity(). """ output = str(tmpdir.join('test.tif')) out_affine = Affine(1, 0, 0, 0, -1, 0) with rasterio.open(output, 'w', driver='GTiff', count=1, dtype=rasterio.uint8, width=1, height=1, transform=out_affine) as out: assert out.transform == out_affine with rasterio.open(output) as out: assert out.transform == Affine.identity()
def coords_to_indices(x, y, transform): """ Converts map coordinates to array indices Args: x (float or 1d array): The x coordinates. y (float or 1d array): The y coordinates. transform (object): The affine transform. Returns: ``tuple``: (col_index, row_index) Example: >>> import geowombat as gw >>> from geowombat.core import coords_to_indices >>> >>> with gw.open('image.tif') as src: >>> j, i = coords_to_indices(x, y, src) """ if not isinstance(transform, Affine): if isinstance(transform, tuple): transform = Affine(*transform) elif isinstance(transform, xr.DataArray): transform = transform.gw.affine else: logger.exception( ' The transform must be an instance of affine.Affine, an xarray.DataArray, or a tuple' ) raise TypeError col_index, row_index = ~transform * (x, y) return np.int64(col_index), np.int64(row_index)
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 rasterize_pctcover_geom(geom, shape, affine, scale=None, all_touched=False): """ Parameters ---------- geom: GeoJSON geometry shape: desired shape affine: desired transform scale: scale at which to generate percent cover estimate Returns ------- ndarray: float32 """ min_dtype = min_scalar_type(scale**2) pixel_size_lon = affine[0] / scale pixel_size_lat = affine[4] / scale topleftlon = affine[2] topleftlat = affine[5] new_affine = Affine(pixel_size_lon, 0, topleftlon, 0, pixel_size_lat, topleftlat) new_shape = (shape[0] * scale, shape[1] * scale) rv_array = rasterize_geom(geom, new_shape, new_affine, all_touched=all_touched) # print rv_array rv_array = rebin_sum(rv_array, shape, min_dtype) return rv_array.astype('float32') / (scale**2)
def check_data_with_api(index, time_slices): """Chek retrieved data for specific values. We scale down by 100 and check for predefined values in the corners. """ from datacube import Datacube dc = Datacube(index=index) # TODO: this test needs to change, it tests that results are exactly the # same as some time before, but with the current zoom out factor it's # hard to verify that results are as expected even with human # judgement. What it should test is that reading native from the # ingested product gives exactly the same results as reading into the # same GeoBox from the original product. Separate to that there # should be a read test that confirms that what you read from native # product while changing projection is of expected value # Make the retrieved data lower res ss = 100 shape_x = int(GEOTIFF['shape']['x'] / ss) shape_y = int(GEOTIFF['shape']['y'] / ss) pixel_x = int(GEOTIFF['pixel_size']['x'] * ss) pixel_y = int(GEOTIFF['pixel_size']['y'] * ss) input_type_name = 'ls5_nbar_albers' input_type = dc.index.products.get_by_name(input_type_name) geobox = geometry.GeoBox(shape_x + 2, shape_y + 2, Affine(pixel_x, 0.0, GEOTIFF['ul']['x'], 0.0, pixel_y, GEOTIFF['ul']['y']), geometry.CRS(GEOTIFF['crs'])) observations = dc.find_datasets(product='ls5_nbar_albers', geopolygon=geobox.extent) group_by = query_group_by('time') sources = dc.group_datasets(observations, group_by) data = dc.load_data(sources, geobox, input_type.measurements.values()) assert hashlib.md5(data.green.data).hexdigest() == '0f64647bad54db4389fb065b2128025e' assert hashlib.md5(data.blue.data).hexdigest() == '41a7b50dfe5c4c1a1befbc378225beeb' for time_slice in range(time_slices): assert data.blue.values[time_slice][-1, -1] == -999
def test_reproject(generator): src_crs = epsg4326 dst_crs = sinusoidal ds = generator(crs=src_crs) src_bounds = warp.get_bounds(ds) dst_bounds_latlon = BoundingBox( left=src_bounds.left - 1, bottom=src_bounds.bottom - 1, right=src_bounds.right + 1, top=src_bounds.top + 1, ) dst_bounds = BoundingBox(*rasterio.warp.transform_bounds( src_crs, dst_crs, **dst_bounds_latlon._asdict() )) dst_width, dst_height = 35, 21 resx = (dst_bounds.right - dst_bounds.left) / (dst_width - 1) resy = (dst_bounds.bottom - dst_bounds.top) / (dst_height - 1) res = (abs(resx), abs(resy)) xoff = dst_bounds.left yoff = dst_bounds.top dst_transform = Affine(resx, 0, xoff, 0, resy, yoff) projected = [ warp._reproject(ds, dst_crs=dst_crs, dst_transform=dst_transform, width=dst_width, height=dst_height), warp._reproject(ds, dst_crs=dst_crs, dst_transform=dst_transform, extent=dst_bounds), warp._reproject(ds, dst_crs=dst_crs, extent=dst_bounds, res=res), warp._reproject(ds, dst_crs=dst_crs, extent=dst_bounds, width=dst_width, height=dst_height), ] for proj in projected[1:]: xr_assert_equal(proj, projected[0]) assert_almost_equal(warp.get_resolution(proj), res) assert_almost_equal(warp.get_bounds(proj), dst_bounds) assert_almost_equal(warp.get_transform(proj), dst_transform) assert_equal_crs(warp.get_crs(proj), dst_crs)