def __init__(self, srs=None, ext=None, ds=None, **kwargs): """Create Domain from GDALDataset or string options or lat/lon grids""" # If too much information is given raise error if ds is not None and srs is not None and ext is not None: raise ValueError( 'Ambiguous specification of both dataset, srs- and ext-strings.' ) # choose between input opitons: # ds # ds and srs # srs and ext # if only a dataset is given: # copy geo-reference from the dataset if ds is not None and srs is None: self.vrt = VRT.from_gdal_dataset(ds) # If dataset and srs are given (but not ext): # use AutoCreateWarpedVRT to determine bounds and resolution elif ds is not None and srs is not None: srs = NSR(srs) tmp_vrt = gdal.AutoCreateWarpedVRT(ds, None, srs.wkt) if tmp_vrt is None: raise NansatProjectionError( 'Could not warp the given dataset to the given SRS.') else: self.vrt = VRT.from_gdal_dataset(tmp_vrt) # If SpatialRef and extent string are given (but not dataset) elif srs is not None and ext is not None: srs = NSR(srs) # create full dictionary of parameters extent_dict = Domain._create_extent_dict(ext) # convert -lle to -te if 'lle' in extent_dict.keys(): extent_dict = self._convert_extentDic(srs, extent_dict) # get size/extent from the created extent dictionary geo_transform, raster_x_size, raster_y_size = self._get_geotransform( extent_dict) # create VRT object with given geo-reference parameters self.vrt = VRT.from_dataset_params(x_size=raster_x_size, y_size=raster_y_size, geo_transform=geo_transform, projection=srs.wkt, gcps=[], gcp_projection='') elif 'lat' in kwargs and 'lon' in kwargs: warnings.warn( 'Domain(lon=lon, lat=lat) will be deprectaed!' 'Use Domain.from_lonlat()', NansatFutureWarning) # create self.vrt from given lat/lon self.vrt = VRT.from_lonlat(kwargs['lon'], kwargs['lat']) else: raise ValueError('"dataset" or "srsString and extentString" ' 'or "dataset and srsString" are required')
def __init__(self, srs=None, ext=None, ds=None, **kwargs): """Create Domain from GDALDataset or string options or lat/lon grids""" # If too much information is given raise error if ds is not None and srs is not None and ext is not None: raise ValueError('Ambiguous specification of both dataset, srs- and ext-strings.') # choose between input opitons: # ds # ds and srs # srs and ext # if only a dataset is given: # copy geo-reference from the dataset if ds is not None and srs is None: self.vrt = VRT.from_gdal_dataset(ds) # If dataset and srs are given (but not ext): # use AutoCreateWarpedVRT to determine bounds and resolution elif ds is not None and srs is not None: srs = NSR(srs) tmp_vrt = gdal.AutoCreateWarpedVRT(ds, None, srs.wkt) if tmp_vrt is None: raise NansatProjectionError('Could not warp the given dataset to the given SRS.') else: self.vrt = VRT.from_gdal_dataset(tmp_vrt) # If SpatialRef and extent string are given (but not dataset) elif srs is not None and ext is not None: srs = NSR(srs) # create full dictionary of parameters extent_dict = Domain._create_extent_dict(ext) # convert -lle to -te if 'lle' in extent_dict.keys(): extent_dict = self._convert_extentDic(srs, extent_dict) # get size/extent from the created extent dictionary geo_transform, raster_x_size, raster_y_size = self._get_geotransform(extent_dict) # create VRT object with given geo-reference parameters self.vrt = VRT.from_dataset_params(x_size=raster_x_size, y_size=raster_y_size, geo_transform=geo_transform, projection=srs.wkt, gcps=[], gcp_projection='') elif 'lat' in kwargs and 'lon' in kwargs: warnings.warn('Domain(lon=lon, lat=lat) will be deprectaed!' 'Use Domain.from_lonlat()', NansatFutureWarning) # create self.vrt from given lat/lon self.vrt = VRT.from_lonlat(kwargs['lon'], kwargs['lat']) else: raise ValueError('"dataset" or "srsString and extentString" ' 'or "dataset and srsString" are required')
def test_get_super_vrt(self): ds = gdal.Open(self.test_file_gcps) vrt1 = VRT.from_gdal_dataset(ds, metadata=ds.GetMetadata()) vrt2 = vrt1.get_super_vrt() self.assertIsInstance(vrt2.vrt, VRT) self.assertEqual(vrt2.dataset.GetMetadataItem(str('AREA_OR_POINT')), 'Area')
def test_transform_points(self): ds = gdal.Open(self.test_file_gcps) vrt1 = VRT.from_gdal_dataset(ds, metadata=ds.GetMetadata()) vrt1.tps = True lon, lat = vrt1.transform_points([1, 2, 3], [4, 5, 6]) self.assertTrue(np.allclose(lon, np.array([28.23549571, 28.24337106, 28.25126129]))) self.assertTrue(np.allclose(lat, np.array([71.52509848, 71.51913744, 71.51317568]))) lon, lat = vrt1.transform_points([], []) self.assertTrue(np.allclose(lon, np.array([]))) self.assertTrue(np.allclose(lat, np.array([])))
def test_transform_points(self): ds = gdal.Open(self.test_file_gcps) vrt1 = VRT.from_gdal_dataset(ds, metadata=ds.GetMetadata()) vrt1.tps = True lon, lat = vrt1.transform_points([1, 2, 3], [4, 5, 6]) self.assertTrue(np.allclose(lon, np.array([28.23549571, 28.24337106, 28.25126129]))) self.assertTrue(np.allclose(lat, np.array([71.52509848, 71.51913744, 71.51317568]))) lon, lat = vrt1.transform_points([], []) self.assertTrue(np.allclose(lon, np.array([]))) self.assertTrue(np.allclose(lat, np.array([])))
def test_find_complex_band(self): a = np.random.randn(100,100) vrt1 = VRT.from_array(a) vrt2 = VRT.from_array(a.astype(np.complex64)) vrt3 = VRT.from_gdal_dataset(vrt1.dataset) vrt3.create_bands([{'src': {'SourceFilename': vrt1.filename}}, {'src': {'SourceFilename': vrt2.filename}}]) self.assertEqual(vrt1._find_complex_band(), None) self.assertEqual(vrt2._find_complex_band(), 1) self.assertEqual(vrt3._find_complex_band(), 2)
def test_find_complex_band(self): a = np.random.randn(100,100) vrt1 = VRT.from_array(a) vrt2 = VRT.from_array(a.astype(np.complex64)) vrt3 = VRT.from_gdal_dataset(vrt1.dataset) vrt3.create_bands([{'src': {'SourceFilename': vrt1.filename}}, {'src': {'SourceFilename': vrt2.filename}}]) self.assertEqual(vrt1._find_complex_band(), None) self.assertEqual(vrt2._find_complex_band(), 1) self.assertEqual(vrt3._find_complex_band(), 2)
def test_split_complex_bands(self): a = np.random.randn(100, 100) vrt1 = VRT.from_array(a.astype(np.complex64)) vrt2 = VRT.from_array(a) vrt3 = VRT.from_array(a.astype(np.complex64)) vrt4 = VRT.from_gdal_dataset(vrt1.dataset) vrt4.create_bands([{ 'src': { 'SourceFilename': vrt1.filename }, 'dst': { 'name': 'vrt1' } }, { 'src': { 'SourceFilename': vrt2.filename }, 'dst': { 'name': 'vrt2' } }, { 'src': { 'SourceFilename': vrt3.filename }, 'dst': { 'name': 'vrt3' } }]) vrt4.split_complex_bands() self.assertEqual(vrt4.dataset.RasterCount, 5) self.assertEqual( vrt4.dataset.GetRasterBand(1).GetMetadataItem(str('name')), 'vrt2') self.assertEqual( vrt4.dataset.GetRasterBand(2).GetMetadataItem(str('name')), 'vrt1_real') self.assertEqual( vrt4.dataset.GetRasterBand(3).GetMetadataItem(str('name')), 'vrt1_imag') self.assertEqual( vrt4.dataset.GetRasterBand(4).GetMetadataItem(str('name')), 'vrt3_real') self.assertEqual( vrt4.dataset.GetRasterBand(5).GetMetadataItem(str('name')), 'vrt3_imag')
def test_split_complex_bands(self): a = np.random.randn(100,100) vrt1 = VRT.from_array(a.astype(np.complex64)) vrt2 = VRT.from_array(a) vrt3 = VRT.from_array(a.astype(np.complex64)) vrt4 = VRT.from_gdal_dataset(vrt1.dataset) vrt4.create_bands([{'src': {'SourceFilename': vrt1.filename}, 'dst': {'name': 'vrt1'}}, {'src': {'SourceFilename': vrt2.filename}, 'dst': {'name': 'vrt2'}}, {'src': {'SourceFilename': vrt3.filename}, 'dst': {'name': 'vrt3'}}]) vrt4.split_complex_bands() self.assertEqual(vrt4.dataset.RasterCount,5) self.assertEqual(vrt4.dataset.GetRasterBand(1).GetMetadataItem(str('name')), 'vrt2') self.assertEqual(vrt4.dataset.GetRasterBand(2).GetMetadataItem(str('name')), 'vrt1_real') self.assertEqual(vrt4.dataset.GetRasterBand(3).GetMetadataItem(str('name')), 'vrt1_imag') self.assertEqual(vrt4.dataset.GetRasterBand(4).GetMetadataItem(str('name')), 'vrt3_real') self.assertEqual(vrt4.dataset.GetRasterBand(5).GetMetadataItem(str('name')), 'vrt3_imag')
def test_from_gdal_dataset(self, _init_from_gdal_dataset): ds = gdal.Open(self.test_file_gcps) vrt = VRT.from_gdal_dataset(ds) self.assertIsInstance(vrt, VRT) self.assertTrue(_init_from_gdal_dataset.called_once())
def __init__(self, srs=None, ext=None, ds=None, lon=None, lat=None, name='', logLevel=None): """Create Domain from GDALDataset or string options or lat/lon grids""" # set default attributes self.logger = add_logger('Nansat', logLevel) self.name = name self.logger.debug('ds: %s' % str(ds)) self.logger.debug('srs: %s' % srs) self.logger.debug('ext: %s' % ext) # If too much information is given raise error if ds is not None and srs is not None and ext is not None: raise ValueError( 'Ambiguous specification of both dataset, srs- and ext-strings.' ) # choose between input opitons: # ds # ds and srs # srs and ext # lon and lat # if only a dataset is given: # copy geo-reference from the dataset if ds is not None and srs is None: self.vrt = VRT.from_gdal_dataset(ds) # If dataset and srs are given (but not ext): # use AutoCreateWarpedVRT to determine bounds and resolution elif ds is not None and srs is not None: srs = NSR(srs) tmp_vrt = gdal.AutoCreateWarpedVRT(ds, None, srs.wkt) if tmp_vrt is None: raise NansatProjectionError( 'Could not warp the given dataset to the given SRS.') else: self.vrt = VRT.from_gdal_dataset(tmp_vrt) # If SpatialRef and extent string are given (but not dataset) elif srs is not None and ext is not None: srs = NSR(srs) # create full dictionary of parameters extent_dict = Domain._create_extent_dict(ext) # convert -lle to -te if 'lle' in extent_dict.keys(): extent_dict = self._convert_extentDic(srs, extent_dict) # get size/extent from the created extent dictionary geo_transform, raster_x_size, raster_y_size = self._get_geotransform( extent_dict) # create VRT object with given geo-reference parameters self.vrt = VRT.from_dataset_params(x_size=raster_x_size, y_size=raster_y_size, geo_transform=geo_transform, projection=srs.wkt, gcps=[], gcp_projection='') elif lat is not None and lon is not None: # create self.vrt from given lat/lon self.vrt = VRT.from_lonlat(lon, lat) else: raise ValueError('"dataset" or "srsString and extentString" ' 'or "dataset and srsString" are required') self.logger.debug('vrt.dataset: %s' % str(self.vrt.dataset))
def test_from_gdal_dataset(self, _init_from_gdal_dataset): ds = gdal.Open(self.test_file_gcps) vrt = VRT.from_gdal_dataset(ds) self.assertIsInstance(vrt, VRT) self.assertTrue(_init_from_gdal_dataset.called_once())
def test_get_super_vrt(self): ds = gdal.Open(self.test_file_gcps) vrt1 = VRT.from_gdal_dataset(ds, metadata=ds.GetMetadata()) vrt2 = vrt1.get_super_vrt() self.assertIsInstance(vrt2.vrt, VRT) self.assertEqual(vrt2.dataset.GetMetadataItem(str('AREA_OR_POINT')), 'Area')