Ejemplo n.º 1
0
class TestWrite(TestCase):
    def setUp(self) -> None:
        # inputs
        self.predictors = [
            nc.band1, nc.band2, nc.band3, nc.band4, nc.band5, nc.band7
        ]

        # test results
        self.stack = None

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

    def test_write(self):
        # test writing to file
        self.stack = Raster(self.predictors)
        fp = NamedTemporaryFile(suffix=".tif").name

        result = self.stack.write(fp)

        self.assertIsInstance(result, Raster)
        self.assertEqual(result.count, self.stack.count)
Ejemplo n.º 2
0
stack.names
stack.drop(labels='lsat7_2000_50')
stack.names

# Modify a layer
# templayer = ps.from_files(band1, mode='r+')
# arr = templayer.lsat7_2000_10.read(window=Window(0, 0, 100, 100))
# arr[:] += 500
# templayer.lsat7_2000_10.write(arr, window=Window(0, 0, 100, 100))
# ras = templayer.lsat7_2000_10.read(masked=True)
# plt.imshow(ras)
# templayer = None

# Save a stack
tmp_tif = tempfile.NamedTemporaryFile().name + '.tif'
newstack = stack.write(file_path=tmp_tif, nodata=-99)
newstack.landsat_multiband_band1_1.read()
newstack=None

# Load some training data in the form of a shapefile of point feature locations:
training_py = geopandas.read_file(nc.polygons)
training_pt = geopandas.read_file(nc.points)
training_px = rasterio.open(os.path.join(nc.labelled_pixels))
training_lines = deepcopy(training_py)
training_lines['geometry'] = training_lines.geometry.boundary

# Plot some training data
plt.imshow(stack.lsat7_2000_70.read(masked=True),
           extent=rasterio.plot.plotting_extent(stack.lsat7_2000_70.ds))
plt.scatter(x=training_pt.bounds.iloc[:, 0],
            y=training_pt.bounds.iloc[:, 1],
Ejemplo n.º 3
0
# Drop a layer
stack.names
stack.drop(labels='lsat7_2000_50')
stack.names

# Modifify a layer
# templayer = ps.from_files(band1, mode='r+')
# arr = templayer.lsat7_2000_10.read(window=Window(0, 0, 100, 100))
# arr[:] += 500
# templayer.lsat7_2000_10.write(arr, window=Window(0, 0, 100, 100))
# ras = templayer.lsat7_2000_10.read(masked=True)
# plt.imshow(ras)
# templayer = None

# Save a stack
newstack = stack.write(file_path="/Users/steven/Downloads/test.tif",
                       nodata=-99)
newstack.landsat_multiband_band1_1.read()
newstack = None

# Load some training data in the form of a shapefile of point feature locations:
training_py = geopandas.read_file(
    os.path.join(basedir, 'pyspatialml', 'tests', 'landsat96_polygons.shp'))
training_pt = geopandas.read_file(
    os.path.join(basedir, 'pyspatialml', 'tests', 'landsat96_points.shp'))
training_px = rasterio.open(
    os.path.join(basedir, 'pyspatialml', 'tests',
                 'landsat96_labelled_pixels.tif'))
training_lines = deepcopy(training_py)
training_lines['geometry'] = training_lines.geometry.boundary

# Plot some training data
## Dado que los datos de reflectancia vienen reescalados por un factor de 10.000

evi = 2.5 * (stack.B8.read()*0.0001- stack.B4.read()*0.0001)/(
    (stack.B8.read()*0.0001 + 6 * (stack.B4.read()*0.0001) 
    -  7.5 * (stack.B4.read()*0.0001)) + 1
)

raster_ndvi = Raster(np.where(ndvi >= 0.3, 1, 0).astype('uint8'), 
                     transform=result_keras.transform, crs=result_keras.crs,
                     mode='w+', in_memory=False)

raster_evi = Raster(np.where(evi >= 0.3, 1, 0).astype('uint8'), 
                    transform=result_keras.transform, crs=result_keras.crs, 
                    mode='w+', in_memory=False)

raster_ndvi.write(ndvi_file)
raster_evi.write(evi_file)

# %%
# Despliege de los resultados de clasificación y NDVI

fig, axes = plt.subplots(3, 2, figsize=(8, 10))
# Plot verdadero color
ep.plot_rgb(rgb432, extent=extent, ax=axes[0,0], stretch=True, title='Verdadero color')
manzana.boundary.plot(ax=axes[0,0], color='white', alpha=0.5, linewidth=1)
# Plot falso color
ep.plot_rgb(rgb843, extent=extent, ax=axes[0,1], stretch=True, title='Falso color')
manzana.boundary.plot(ax=axes[0,1], color='white', alpha=0.5, linewidth=1)
# Plot Class Keras
ep.plot_bands(result_keras.read(), extent=extent, ax=axes[1,0], cmap='RdYlGn', 
              alpha=0.8, title="Clasificación ANN", cbar=False)