def test_crop_vector_null(self): """Test case where vector intersection is null""" source_path = os.path.join(testfile_path, '2states.geojson') source = gaia.create(source_path) tool_path = os.path.join(testfile_path, 'iraq_hospitals.geojson') tool = gaia.create(tool_path) cropped = crop(source, tool) self.assertIsNone(cropped)
def test_crop_rgb_null(self): """Test with geometry that does not intersect""" input_path = os.path.join(testfile_path, 'simplergb.tif') input_raster = gaia.create(input_path) input_path = os.path.join(testfile_path, 'iraq_hospitals.geojson') input_vector = gaia.create(input_path) cropped = crop(input_raster, input_vector) self.assertIsNone(cropped)
def test_create_api(self): """ Test cropping (within process) for vector inputs """ path1 = os.path.join(testfile_path, 'iraq_hospitals.geojson') path2 = os.path.join(testfile_path, 'baghdad_districts.geojson') data1 = gaia.create(uri=path1) data2 = gaia.create(uri=path2) output = crop(data1, data2) self.assertEquals(len(output.get_data()), 19)
def test_create_api(self): """ Test cropping (within process) for vector inputs """ path1 = os.path.join(testfile_path, 'iraq_hospitals.geojson') path2 = os.path.join(testfile_path, 'baghdad_districts.geojson') data1 = gaia.create(path1) data2 = gaia.create(path2) output = crop(data1, data2) self.assertEqual(len(output.get_data()), 19)
def test_crop_rgb_bigger(self): """Test with geometry partially outside raster bounds""" input_path = os.path.join(testfile_path, 'simplergb.tif') input_raster = gaia.create(input_path) bounds = input_raster.get_metadata().get('bounds').get('coordinates') bounds = bounds[0] x = (bounds[0][0] + bounds[2][0]) / 2.0 y = (bounds[0][1] + bounds[2][1]) / 2.0 dx = 1.0 * (bounds[2][0] - bounds[0][0]) dy = 1.0 * (bounds[2][1] - bounds[0][1]) poly = [[ [x, y], [x, y-dy], [x+dx, y-dy], [x+dx, y] ]] geometry = geojson.Polygon(poly) crop_geom = gaia.create(geometry) cropped_raster = crop(input_raster, crop_geom) self.assertIsNotNone(cropped_raster)
def test_crop_rgb(self): """ Test cropping raster data with RGB bands """ input_path = os.path.join(testfile_path, 'simplergb.tif') input_raster = gaia.create(input_path) # Generate crop geometry from raster bounds bounds = input_raster.get_metadata().get('bounds').get('coordinates') bounds = bounds[0] x = (bounds[0][0] + bounds[2][0]) / 2.0 y = (bounds[0][1] + bounds[2][1]) / 2.0 dx = 0.12 * (bounds[2][0] - bounds[0][0]) dy = 0.16 * (bounds[2][1] - bounds[0][1]) poly = [[[x, y], [x + dx, y + dy], [x - dx, y + dy], [x - dx, y - dy], [x + dx, y - dy]]] geometry = geojson.Polygon(poly) crop_geom = gaia.create(geometry) cropped_raster = crop(input_raster, crop_geom) self.assertIsNotNone(cropped_raster)
def crop_task(self, target_file, target_driver, by_file, name): import gaia from gaia import preprocess target_file = copyfile(target_file, getTempFileNameWithExtention(target_driver)) by_file = copyfile(by_file, getTempFileNameWithExtention('GeoJSON')) target = gaia.create(target_file) by_file = gaia.create(by_file) cropped = preprocess.crop(target, by_file) tempName = getTempFileNameWithExtention(target_driver, name) # A bug with geopandas and fiona that throws an error like # fiona.errors.GeometryTypeValidationError: Record's geometry type does not match collection schema's geometry type: 'MultiPolygon' != 'Polygon' if (target_driver == 'GeoJSON'): with open(tempName, 'w') as f: f.write(cropped.get_data().to_json()) else: gaia.save(cropped, tempName) return tempName
def test_crop_rgb(self): """ Test cropping raster data with RGB bands """ input_path = os.path.join(testfile_path, 'simplergb.tif') input_raster = gaia.create(input_path) # Generate crop geometry from raster bounds bounds = input_raster.get_metadata().get('bounds').get('coordinates') bounds = bounds[0] x = (bounds[0][0] + bounds[2][0]) / 2.0 y = (bounds[0][1] + bounds[2][1]) / 2.0 dx = 0.12 * (bounds[2][0] - bounds[0][0]) dy = 0.16 * (bounds[2][1] - bounds[0][1]) poly = [[ [x, y], [x+dx, y+dy], [x-dx, y+dy], [x-dx, y-dy], [x+dx, y-dy] ]] geometry = geojson.Polygon(poly) crop_geom = gaia.create(geometry) cropped_raster = crop(input_raster, crop_geom) self.assertIsNotNone(cropped_raster)
from __future__ import print_function import os from zipfile import ZipFile import gaia from gaia import preprocess src_folder = os.path.dirname(__file__) data_folder = os.path.join(src_folder, os.pardir, os.pardir, 'tests', 'data') ## Vector crop print('VECTOR') # Load 2 datasets hospitals = gaia.create(os.path.join(data_folder, 'iraq_hospitals.geojson')) districts = gaia.create(os.path.join(data_folder, 'baghdad_districts.geojson')) # Apply crop print('Before crop (vector)', districts.get_data().shape) vector_crop = preprocess.crop(hospitals, districts) print('After crop (vector)', vector_crop.get_data().shape) #print(cropped.get_data().head()) vector_filename = 'cropped.geojson' gaia.save(vector_crop, vector_filename) print('Wrote file {}'.format(vector_filename)) # Readback file and print some info readback = gaia.create(vector_filename) print('After readback (vector)', readback.get_data().shape) #print(readback.get_data().head())
# except StopIteration: # raise GaiaException('User/Private folder not found') # gaia_list = gc.listFolder( # private_folder['_id'], parentFolderType='folder', name='gaia') # try: # gaia_folder = next(gaia_list) # except StopIteration: # # Create folder # gaia_folder = TBD() # API option 1: create internal url datastore_url = datastore.lookup_url('Public/DEM_bare_earth.tif') print(datastore_url) dataset = gaia.create(datastore_url) # API option 2: pass in tuple as the data source # data_source = (datastore, 'Public/DEM_bare_earth.tif') # dataset = gaia.create(data_source) meta = dataset.get_metadata() print() print(json.dumps(meta, sort_keys=True, indent=2)) print('Input dataset width {}, height {}'.format(meta['width'], meta['height'])) # Generate crop geometry (small!) bounds = meta.get('bounds', {}).get('coordinates')[0] assert bounds, 'Dataset bounds missing' # print()
# raise GaiaException('User/Private folder not found') # gaia_list = gc.listFolder( # private_folder['_id'], parentFolderType='folder', name='gaia') # try: # gaia_folder = next(gaia_list) # except StopIteration: # # Create folder # gaia_folder = TBD() # API option 1: create internal url datastore_url = datastore.lookup_url('Public/DEM_bare_earth.tif') print(datastore_url) dataset = gaia.create(datastore_url) # API option 2: pass in tuple as the data source # data_source = (datastore, 'Public/DEM_bare_earth.tif') # dataset = gaia.create(data_source) meta = dataset.get_metadata() print() print(json.dumps(meta, sort_keys=True, indent=2)) print('Input dataset width {}, height {}'.format(meta['width'], meta['height'])) # Generate crop geometry (small!) bounds = meta.get('bounds',{}).get('coordinates')[0] assert bounds, 'Dataset bounds missing' # print() # print(bounds)
from __future__ import print_function import os from zipfile import ZipFile import gaia from gaia import preprocess src_folder = os.path.dirname(__file__) data_folder = os.path.join(src_folder, os.pardir, os.pardir, 'tests', 'data') ## Vector crop print('VECTOR') # Load 2 datasets hospitals = gaia.create(os.path.join(data_folder, 'iraq_hospitals.geojson')) districts = gaia.create(os.path.join(data_folder, 'baghdad_districts.geojson')) # Apply crop print('Before crop (vector)', districts.get_data().shape) vector_crop = preprocess.crop(hospitals, districts) print('After crop (vector)', vector_crop.get_data().shape) #print(cropped.get_data().head()) vector_filename = 'cropped.geojson' gaia.save(vector_crop, vector_filename) print('Wrote file {}'.format(vector_filename)) # Readback file and print some info readback = gaia.create(vector_filename) print('After readback (vector)', readback.get_data().shape)