Example #1
0
class TestToCrs(TestCase):
    def setUp(self) -> None:
        # test inputs
        predictors = [
            nc.band1, nc.band2, nc.band3, nc.band4, nc.band5, nc.band7
        ]
        self.stack = Raster(predictors)

        # test results
        self.stack_prj = None

    def tearDown(self) -> None:
        self.stack.close()
        self.stack_prj.close()

    def test_to_crs_defaults(self):
        self.stack_prj = self.stack.to_crs({"init": "EPSG:4326"})

        # check raster object
        self.assertIsInstance(self.stack_prj, Raster)
        self.assertEqual(self.stack_prj.count, self.stack.count)
        self.assertEqual(self.stack_prj.read(masked=True).count(), 1012061)

        # test nodata value is recognized
        self.assertEqual(
            self.stack_prj.read(masked=True).min(),
            self.stack.read(masked=True).min())
        self.assertEqual(
            self.stack_prj.read(masked=True).max(),
            self.stack.read(masked=True).max())

    def test_to_crs_custom_nodata(self):
        self.stack_prj = self.stack.to_crs({"init": "EPSG:4326"}, nodata=-999)

        # check raster object
        self.assertIsInstance(self.stack_prj, Raster)
        self.assertEqual(self.stack_prj.count, self.stack.count)
        self.assertEqual(self.stack_prj.read(masked=True).count(), 1012061)

        # test nodata value is recognized
        self.assertEqual(
            self.stack_prj.read(masked=True).min(),
            self.stack.read(masked=True).min())
        self.assertEqual(
            self.stack_prj.read(masked=True).max(),
            self.stack.read(masked=True).max())

    def test_to_crs_in_memory(self):
        self.stack_prj = self.stack.to_crs({"init": "EPSG:4326"},
                                           in_memory=True)

        # check raster object
        self.assertIsInstance(self.stack_prj, Raster)
Example #2
0
os.chdir(os.path.join('.', 'examples'))
band1 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_10.tif')
band2 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_20.tif')
band3 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_30.tif')
band4 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_40.tif')
band5 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_50.tif')
band7 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_70.tif')
multiband = os.path.join(basedir, 'pyspatialml', 'tests',
                         'landsat_multiband.tif')
predictors = [band1, band2, band3, band4, band5, band7]

# Create a RasterStack instance
stack = Raster([band1, band2, band3, band4, band5, band7])
stack.count

stack_rs = stack.to_crs(crs={'init': 'EPSG:4326'}, progress=False)
stack_rs.plot()

# Aggregate a raster to a coarser cell size
stack_new = stack.aggregate(out_shape=(100, 100))
stack_new.iloc[0].plot()

# Plot a RasterLayer
stack.lsat7_2000_10.plot()

# Plot a Raster
stack.lsat7_2000_10.cmap = 'plasma'
stack.plot(label_fontsize=8, title_fontsize=8)

# Iterate through RasterLayers
for name, layer in stack:
Example #3
0
# masking
training_py = geopandas.read_file(nc.polygons)
mask_py = training_py.iloc[0:1, :]
mask_py.plot()
masked_object = stack.mask(mask_py, invert=False, pad=True)
masked_object.plot()

# cropping
training_py = geopandas.read_file(nc.polygons)
crop_bounds = training_py.loc[0, 'geometry'].bounds
stack_cropped = stack.crop(crop_bounds)
stack_cropped.plot()

# reprojection
stack_prj = stack.to_crs({'init': 'EPSG:4326'})
stack_prj.plot()

# Perform band math
ndvi = (stack.iloc[3] - stack.iloc[2]) / (stack.iloc[3] + stack.iloc[2])
ndvi = Raster(ndvi)
ndvi.plot()

tmp = ndvi.aggregate(out_shape=(50, 50))
tmp.plot()

# Perform OR operation (union two layers)
(stack.iloc[5] | stack.iloc[0]).plot()
plt.show()

# Perform AND operation (intersect two layers)