def save(self, path: str): drv = gdal.GetDriverByName("GTiff") band_number = self.data.shape[2] dtype = self.io_options["dtype"] ds = drv.Create(path, self.data.shape[1], self.data.shape[0], band_number, dtype) gdal_raster = GdalImage(ds, self.data.crs) srs = osr.SpatialReference() # Establish its coordinate encoding srs.ImportFromEPSG(self.data.crs.code) transformed_ds = gdal_raster.transform(self.data.extent.origin, self.data.pixel, srs.ExportToWkt()) for band in range(self.data.shape[2]): transformed_ds.ds.GetRasterBand(band + 1).WriteArray(self.data[:, :, band])
def test_insert_polygon(self): gdal_in_memory = GdalImage.in_memory(100, 100) gdal_in_memory.transform(Origin(100.0, 100.0), Pixel(1.0, 1.0)) gdal_in_memory.insert_polygon( "Polygon((110.0 110.0, 110.0 120.0, 120.0 120.0, 120.0 110.0, 110.0 110.0))", 10) array = gdal_in_memory.array self.assertEqual(array[array == 10].size, 121)
def test_convertion_to_raster(self): gdal_in_memory = GdalImage.in_memory(100, 100) gdal_in_memory.transform(Origin(100.0, 100.0), Pixel(1.0, 1.0)) gdal_in_memory.insert_polygon( "Polygon((110.0 110.0, 110.0 120.0, 120.0 120.0, 120.0 110.0, 110.0 110.0))", 10) pixel, ref = gdal_in_memory.to_raster() return Raster(pixel=pixel, ref=ref)
def load(self): from gis.raster import Raster gdal_image = GdalImage.load_from_file(self.path, self.io_options["crs"]) pixel, ref = gdal_image.to_raster() return Raster(pixel=pixel, ref=ref)
def wkt_to_gdal_raster(cls, default_extent, options): from gis import Pixel extent = options.get("extent", default_extent.expand_percentage_equally(0.3)) try: assert extent.crs == default_extent.crs except AssertionError: raise CrsException( "Crs from extent does not match with Crs specified. Please make changes." ) pixel: Pixel = options.get("pixel", Pixel(0.5, 0.5)) gdal_in_memory, extent_new = GdalImage.from_extent(extent, pixel) return gdal_in_memory
def test_gdal_image_to_raster(self): gd = GdalImage.load_from_file(TEST_IMAGE_PATH, "epsg:4326") pixel, ref = gd.to_raster() raster = Raster(pixel=pixel, ref=ref) self.assertEqual(isinstance(raster, Raster), True)
def test_gdal_image_from_file_image_accuracy(self): gd = GdalImage.load_from_file(TEST_IMAGE_PATH, "epsg:4326") array = gd.array self.assertEqual(array[array == 255].__len__(), 25121) self.assertEqual(array.shape, (3, 1677, 1673))
def test_gdal_image_from_file(self): gd = GdalImage.load_from_file(TEST_IMAGE_PATH, "epsg:4326") self.assertTrue(isinstance(gd.array, np.ndarray))
def empty_raster(cls, extent: 'Extent', pixel: Pixel) -> Tuple[Pixel, ReferencedArray]: transformed_raster, extent_new = GdalImage.from_extent(extent, pixel) return cls.to_raster(transformed_raster, pixel)