Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
    def test_crop_pandas(self):
        """
        Test cropping (within process) for vector inputs
        """
        reader1 = readers.GaiaReader(
            os.path.join(testfile_path, 'iraq_hospitals.geojson'))

        reader2 = readers.GaiaReader(
            os.path.join(testfile_path, 'baghdad_districts.geojson'))

        output = crop(reader1.read(), reader2.read())

        self.assertEqual(len(output.get_data()), 19)
Example #5
0
    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)
Example #6
0
    def test_crop_pandas(self):
        """
        Test cropping (within process) for vector inputs
        """
        reader1 = readers.GaiaReader(
            os.path.join(testfile_path, 'iraq_hospitals.geojson'))

        reader2 = readers.GaiaReader(
            os.path.join(testfile_path, 'baghdad_districts.geojson'))

        output = crop(reader1.read(), reader2.read())

        self.assertEqual(len(output.get_data()), 19)
Example #7
0
    def test_crop_gdal(self):
        """
        Test cropping (subset process) for vector & raster inputs
        """
        try:
            reader1 = readers.GaiaReader(
                os.path.join(testfile_path, 'globalairtemp.tif'))
            rasterData = reader1.read()

            reader2 = readers.GaiaReader(
                os.path.join(testfile_path, '2states.geojson'))
            vectorData = reader2.read()

            output = crop(rasterData, vectorData)

            self.assertEqual(type(output.get_data()).__name__, 'Dataset')
        except Exception:
            raise
Example #8
0
    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)
Example #9
0
    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)
Example #10
0
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
Example #11
0
    def test_crop_gdal(self):
        """
        Test cropping (subset process) for vector & raster inputs
        """
        zipfile = ZipFile(os.path.join(testfile_path, '2states.zip'), 'r')
        zipfile.extract('2states.geojson', testfile_path)

        try:
            reader1 = readers.GaiaReader(
                os.path.join(testfile_path, 'globalairtemp.tif'))
            rasterData = reader1.read()

            reader2 = readers.GaiaReader(
                os.path.join(testfile_path, '2states.geojson'))
            vectorData = reader2.read()

            output = crop(rasterData, vectorData)

            self.assertEqual(type(output.get_data()).__name__, 'Dataset')
        finally:
            testfile = os.path.join(testfile_path, '2states.geojson')
            if os.path.exists(testfile):
                os.remove(testfile)
Example #12
0
    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)
Example #13
0
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())

## Raster Crop
print()
print('RASTER')
Example #14
0
assert bounds, 'Dataset bounds missing'
# print()
# print(bounds)

# Compute center coordinates
x = (bounds[0][0] + bounds[2][0]) / 2.0
y = (bounds[0][1] + bounds[2][1]) / 2.0

# Use smll percentage of height & width
dx = 0.005 * (bounds[2][0] - bounds[0][0])
dy = 0.005 * (bounds[2][1] - bounds[0][1])
rect = [[x, y], [x, y - dy], [x - dx, y - dy], [x - dx, y], [x, y]]
# print()
# print(rect)

# Must pass rectangle in as a LIST, in order to get geom formatted the way resgeodata uses
crop_geom = geojson.Polygon([rect])
print()
print(crop_geom)
sys.exit(0)
# Perform crop operation
from gaia import preprocess
cropped_dataset = preprocess.crop(dataset, crop_geom, name='crop100m.tif')
print()
cropped_meta = cropped_dataset.get_metadata()
print('Cropped dataset width {}, height {}'.format(cropped_meta['width'],
                                                   cropped_meta['height']))
print(cropped_meta)

print('finis', dataset)
Example #15
0
# print(bounds)

# Compute center coordinates
x = (bounds[0][0] + bounds[2][0]) / 2.0
y = (bounds[0][1] + bounds[2][1]) / 2.0

# Use smll percentage of height & width
dx = 0.005 * (bounds[2][0] - bounds[0][0])
dy = 0.005 * (bounds[2][1] - bounds[0][1])
rect = [
    [x,y], [x, y-dy], [x-dx, y-dy], [x-dx, y], [x,y]
]
# print()
# print(rect)

# Must pass rectangle in as a LIST, in order to get geom formatted the way resgeodata uses
crop_geom = geojson.Polygon([rect])
print()
print(crop_geom)
sys.exit(0)
# Perform crop operation
from gaia import preprocess
cropped_dataset = preprocess.crop(dataset, crop_geom, name='crop100m.tif')
print()
cropped_meta = cropped_dataset.get_metadata()
print('Cropped dataset width {}, height {}'.format(
    cropped_meta['width'], cropped_meta['height']))
print(cropped_meta)

print('finis', dataset)
Example #16
0
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())


## Raster Crop
print()