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_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 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_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_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)