def setUp(self): """ Creates a normal and a rotated raster geometry with a random coordinate extent. """ # set up spatial reference system self.sref = SpatialRef(4326) # define region of interest/extent ll_x = random.randrange(-50., 50., 10.) ll_y = random.randrange(-50., 50., 10.) ur_x = ll_x + random.randrange(10., 50., 10.) ur_y = ll_y + random.randrange(10., 50., 10.) self.sh_geom = Polygon(((ll_x, ll_y), (ll_x, ur_y), (ur_x, ur_y), (ur_x, ll_y))) # Polygon in clock-wise order self.ogr_geom = ogr.CreateGeometryFromWkt(self.sh_geom.wkt) self.ogr_geom.AssignSpatialReference(self.sref.osr_sref) self.extent = tuple(map(float, (ll_x, ll_y, ur_x, ur_y))) self.x_pixel_size = 0.5 self.y_pixel_size = 0.5 self.raster_geom = RasterGeometry.from_extent(self.extent, self.sref, self.x_pixel_size, self.y_pixel_size) # create rotated raster geometry geom_nap = affinity.rotate( shapely.wkt.loads(self.ogr_geom.ExportToWkt()), 45, 'center') geom_nap = ogr.CreateGeometryFromWkt(geom_nap.wkt) geom_nap.AssignSpatialReference(self.sref.osr_sref) self.raster_geom_rot = RasterGeometry.from_geometry( geom_nap, self.x_pixel_size, self.y_pixel_size)
def test_intersection(self): """ Tests intersection with different geometries. """ # test intersection with own geometry raster_geom_intsct = self.raster_geom.slice_by_geom( self.raster_geom.boundary, inplace=False) assert self.raster_geom == raster_geom_intsct # test intersection with scaled geometry raster_geom_scaled = self.raster_geom.scale(0.5, inplace=False) raster_geom_intsct = self.raster_geom.slice_by_geom(raster_geom_scaled, inplace=False) assert raster_geom_scaled == raster_geom_intsct # test intersection with partial overlap extent_shifted = tuple(np.array(self.extent) + 2) extent_intsct = (extent_shifted[0], extent_shifted[1], self.extent[2], self.extent[3]) raster_geom_shifted = RasterGeometry.from_extent( extent_shifted, self.sref, self.x_pixel_size, self.y_pixel_size) raster_geom_intsct = self.raster_geom.slice_by_geom( raster_geom_shifted, inplace=False) assert raster_geom_intsct.outer_boundary_extent == extent_intsct # test intersection with no overlap extent_no_ovlp = (self.extent[2] + 1., self.extent[3] + 1., self.extent[2] + 5., self.extent[3] + 5.) raster_geom_no_ovlp = RasterGeometry.from_extent( extent_no_ovlp, self.sref, self.x_pixel_size, self.y_pixel_size) raster_geom_intsct = self.raster_geom.slice_by_geom( raster_geom_no_ovlp, inplace=False) assert raster_geom_intsct is None
def test_get_common_geom(self): """ Tests the creation of an encasing raster geometry from multiple raster geometries. """ # create different raster geometries extent_a = self.extent extent_b = tuple(np.array(extent_a) - 2) # shift extent negatively extent_c = tuple(np.array(extent_a) + 2) # shift extent positively # create raster geometries from different extents raster_geom_a = self.raster_geom raster_geom_b = RasterGeometry.from_extent(extent_b, self.sref, self.x_pixel_size, self.y_pixel_size) raster_geom_c = RasterGeometry.from_extent(extent_c, self.sref, self.x_pixel_size, self.y_pixel_size) raster_geom_d = RasterGeometry.from_extent(extent_a, self.sref, self.x_pixel_size*2, self.y_pixel_size*2) # resize the main geometry raster_geom_a.scale(0.5) # create common raster geometry from the first three raster geometries raster_geom = RasterGeometry.from_raster_geometries([raster_geom_a, raster_geom_b, raster_geom_c]) ll_x, ll_y = raster_geom_b.rc2xy(raster_geom_b.n_rows - 1, 0, px_origin="ll") ur_x, ur_y = raster_geom_c.rc2xy(0, raster_geom_b.n_cols - 1, px_origin="ur") new_extent = (ll_x, ll_y, ur_x, ur_y) self.assertTupleEqual(raster_geom.outer_boundary_extent, new_extent) # test if error is raised, when raster geometries with different resolutions are joined try: raster_geom = RasterGeometry.from_raster_geometries([raster_geom_a, raster_geom_b, raster_geom_c, raster_geom_d]) except ValueError: assert True
def test_and(self): """ Tests AND operation, which is an intersection between both raster geometries. """ raster_geom_a = self.raster_geom raster_geom_b = RasterGeometry.from_geometry(self.ogr_geom, self.x_pixel_size, self.y_pixel_size) self.assertEqual(raster_geom_a & raster_geom_b, raster_geom_b & raster_geom_a)
def test_intersection_by_geom(self): """ Tests intersection of a geometry with the regular mosaic geometry. """ geom = any_geom2ogr_geom(self._get_roi(), sref=self.mosaic_geom.sref) mosaic_intsct = self.mosaic_geom.slice_by_geom(geom) outer_boundary_extent = RasterGeometry.from_raster_geometries(mosaic_intsct.tiles).outer_boundary_extent self.assertTupleEqual(outer_boundary_extent, self._get_roi())
def test_equal(self): """ Tests if two raster geometries are equal (one created from an extent, one from a geometry). """ raster_geom_a = self.raster_geom raster_geom_b = RasterGeometry.from_geometry(self.ogr_geom, self.x_pixel_size, self.y_pixel_size) assert raster_geom_a == raster_geom_b
def test_touches(self): """ Tests if raster geometries touch each other. """ # create raster geometry which touches the previous one extent_tchs = (self.extent[2], self.extent[3], self.extent[2] + 5., self.extent[3] + 5.) raster_geom_tchs = RasterGeometry.from_extent(extent_tchs, self.sref, self.x_pixel_size, self.y_pixel_size) assert self.raster_geom.touches(raster_geom_tchs) # create raster geometry which does not touch the previous one extent_no_tchs = (self.extent[2] + 1., self.extent[3] + 1., self.extent[2] + 5., self.extent[3] + 5.) raster_geom_no_tchs = RasterGeometry.from_extent(extent_no_tchs, self.sref, self.x_pixel_size, self.y_pixel_size) assert not self.raster_geom.touches(raster_geom_no_tchs)
def test_from_json(self): """ Tests the creation of a raster geometry from a JSON file containing a raster geometry definition. """ tmp_filepath = "raster_geom.json" self.raster_geom.to_json(tmp_filepath) raster_geom = RasterGeometry.from_json(tmp_filepath) os.remove(tmp_filepath) assert raster_geom == self.raster_geom
def test_slice_tiles_by_geom(self): """ Tests intersection of a geometry with the tiles contained in the regular mosaic geometry. """ geom = any_geom2ogr_geom(self._get_roi(), sref=self.mosaic_geom.sref) intscted_tiles = list( self.mosaic_geom.slice_tiles_by_geom(geom).values()) outer_boundary_extent = RasterGeometry.from_raster_geometries( intscted_tiles).outer_boundary_extent self.assertTupleEqual(outer_boundary_extent, self._get_roi())
def test_from_geom(self): """ Tests setting up a raster geometry from a given geometry. """ raster_geom = RasterGeometry.from_geometry(self.ogr_geom, self.x_pixel_size, self.y_pixel_size) self.assertListEqual(raster_geom.outer_boundary_corners, list(self.sh_geom.exterior.coords)[:-1])
def test_not_equal(self): """ Tests if two raster geometries are not equal. """ # create raster geometries from the given extent raster_geom_a = self.raster_geom raster_geom_b = RasterGeometry.from_extent(self.extent, self.sref, self.x_pixel_size, self.y_pixel_size) # resize second raster geometry raster_geom_b.scale(0.5, inplace=True) assert raster_geom_a != raster_geom_b
def test_different_sref(self): """ Test topological operation if the spatial reference systems are different. """ # create raster geometry which touches the previous one extent_tchs = (self.extent[2], self.extent[3], self.extent[2] + 5., self.extent[3] + 5.) raster_geom_tchs = RasterGeometry.from_extent(extent_tchs, self.sref, self.x_pixel_size, self.y_pixel_size) # reproject to different system sref_other = SpatialRef(3857) geom = raster_geom_tchs.boundary_ogr geom.TransformTo(sref_other.osr_sref) assert self.raster_geom.touches(geom)
def test_plot(self): """ Tests plotting function of a raster geometry. """ self.raster_geom.plot(add_country_borders=True) # test plotting with labelling and different output projection self.raster_geom.id = "E048N018T1" self.raster_geom.plot(proj=ccrs.EckertI(), label_geom=True, add_country_borders=True) # test plotting with different input projection sref = SpatialRef(31259) extent = [527798, 94878, 956835, 535687] raster_geom = RasterGeometry.from_extent(extent, sref, x_pixel_size=500, y_pixel_size=500) raster_geom.plot(add_country_borders=True)
def test_plot(self): """ Tests the `plot` function of a raster geometry. This test is only performed, if matplotlib is installed. """ if 'matplotlib' in sys.modules: self.raster_geom.plot(add_country_borders=True) # test plotting with labelling and different output projection self.raster_geom.name = "E048N018T1" self.raster_geom.plot(proj=ccrs.EckertI(), label_geom=True, add_country_borders=True) # test plotting with different input projection sref = SpatialRef(31259) extent = [527798, 94878, 956835, 535687] raster_geom = RasterGeometry.from_extent(extent, sref, x_pixel_size=500, y_pixel_size=500) raster_geom.plot(add_country_borders=True)