def test_read(self): """ Read multiband image """ geoimg = gpt.get_test_image() arr = geoimg.read() self.assertEqual(geoimg.nbands(), arr.shape[0]) # make sure x, y dimensions are same when reading single bands self.assertEqual(arr.shape[1:3], geoimg[0].read().shape)
def test_scale(self): """ Scale image to byte range """ geoimg = gpt.get_test_image() for band in geoimg: band = band.autoscale(minout=1, maxout=255, percent=2.0) self.assertTrue(band.min() == 1) self.assertTrue(band.max() == 255)
def test_select(self): """ Selection of bands from GeoImage """ img1 = gpt.get_test_image() img2 = img1.select(['red', 'green', 'blue']) self.assertTrue(np.array_equal(img1['red'].read(), img2[0].read())) self.assertTrue(np.array_equal(img1['green'].read(), img2[1].read())) self.assertTrue(np.array_equal(img1['blue'].read(), img2[2].read()))
def test_pansharpen(self): """ Pansharpen multispectral image with panchromatic image """ geoimg = gpt.get_test_image().select(['red', 'green', 'blue', 'nir']) panimg = gpt.get_test_image(bands=['pan']) fout = 'test-pansharpen.tif' imgout = alg.pansharp_brovey(geoimg, panimg, filename=fout) self.assertAlmostEqual(imgout.resolution().x(), panimg.resolution().x(), places=1) self.assertAlmostEqual(imgout.resolution().y(), panimg.resolution().y(), places=1) self.assertEqual(imgout.nbands(), 4) os.remove(fout) geoimg = gpt.get_test_image().select(['red', 'green', 'blue']) imgout = alg.pansharp_brovey(geoimg, panimg, filename=fout) self.assertEqual(imgout.nbands(), 3) self.assertAlmostEqual(imgout.resolution().x(), panimg.resolution().x(), places=1) self.assertAlmostEqual(imgout.resolution().y(), panimg.resolution().y(), places=1) os.remove(fout)
def test_real_warp(self): """ Warp real image to another projection """ geoimg = gpt.get_test_image() fout = 'test-realwarp.tif' imgout = geoimg.warp(fout, proj='EPSG:4326', xres=0.0003, yres=0.0003) self.assertEqual(imgout.xsize(), 653) self.assertEqual(imgout.ysize(), 547) os.remove(fout)
def test_save_with_gain(self): """ Save image with a gain, which should copy through """ geoimg = gpt.get_test_image().select([2]) geoimg.set_gain(0.0001) fout = 'test-savegain.tif' imgout = geoimg.save(fout) assert_array_equal(imgout.read(), geoimg.read()) os.remove(fout)
def test_warp_into(self): """ Warp real image into an existing image """ geoimg = gpt.get_test_image().select([1]) ext = geoimg.extent() bbox = np.array([ext.x0(), ext.y0(), ext.width(), ext.height()]) imgout = gp.GeoImage.create('', geoimg.xsize(), geoimg.ysize(), 1, geoimg.srs(), bbox, geoimg.type().string()); geoimg.warp_into(imgout) self.assertEqual(imgout.read().sum(), geoimg.read().sum())
def test_sqrt(self): """ Calculate sqrt of image """ geoimg = gpt.get_test_image().select(['red', 'green', 'swir1', 'nir']) for band in geoimg: vals = band.sqrt().read() mask = band.data_mask() == 1 # check against numpy arr = band.read() self.assertTrue((vals[mask] == np.sqrt(arr[mask])).any())
def test_save(self): """ Save image as new image with different datatype """ fout = 'test-byte.tif' geoimg = gpt.get_test_image().autoscale(1.0, 255.0).save(fout, 'uint8') geoimg = None geoimg = gp.GeoImage(fout) self.assertEqual(geoimg.type().string(), 'uint8') self.assertEqual(geoimg[0].min(), 1.0) self.assertEqual(geoimg[0].max(), 255.0) os.remove(fout)
def test_autoscale(self): """ Auto scale each band in image """ geoimg = gpt.get_test_image() for band in geoimg: self.assertTrue(band.min() != 1.0) self.assertTrue(band.max() != 255.0) geoimg2 = geoimg.autoscale(minout=1.0, maxout=255.0) for band in geoimg2: self.assertTrue(band.min() == 1) self.assertTrue(band.max() == 255)
def test_rxd(self): """ RX anamoly detector """ geoimg = gpt.get_test_image().select(['red', 'green', 'blue']) fout = 'test-rxd.tif' rxd = alg.rxd(geoimg, filename=fout) self.assertEqual(rxd.bandnames()[0], "RXD") self.assertEqual(rxd.xsize(), geoimg.xsize()) self.assertEqual(rxd.ysize(), geoimg.ysize()) self.assertEqual(rxd.nbands(), 1) rxd = None os.remove(fout)
def test_stats(self): """ Calculate statistics using gippy """ geoimg = gpt.get_test_image() for band in geoimg: stats = band.stats() mask = band.data_mask() == 1 # check against numpy arr = band.read() self.assertAlmostEqual(arr[mask].min(), stats[0]) self.assertAlmostEqual(arr[mask].max(), stats[1]) self.assertAlmostEqual(arr[mask].mean(), stats[2], places=2)
def test_cookiecutter_real_crop(self): """ Test cookie cutter with cropping """ geoimg = gpt.get_test_image().select(['red', 'green', 'blue']) vpath = os.path.join(os.path.dirname(__file__), 'vectors') feature = gp.GeoVector(os.path.join(vpath, 'aoi1_epsg32416.shp')) imgout = alg.cookie_cutter([geoimg], feature=feature[0], xres=30.0, yres=30.0, crop=True) extin = feature.extent() extout = imgout.extent() self.assertTrue(extout.x0() >= extin.x0()) self.assertTrue(extout.y0() >= extin.y0()) self.assertTrue(extout.x1() <= extin.x1()) self.assertTrue(extout.y1() <= extin.y1())
def test_cookiecutter_real_reproj(self): """ Test with different projection """ geoimg = gpt.get_test_image().select(['red', 'green', 'blue']) vpath = os.path.join(os.path.dirname(__file__), 'vectors') feature = gp.GeoVector(os.path.join(vpath, 'aoi1_epsg32416.shp')) extin = feature.extent() # test extent matches feature imgout = alg.cookie_cutter([geoimg], feature=feature[0], xres=30.0, yres=30.0) extout = imgout.extent() self.assertAlmostEqual(extout.x0(), extin.x0()) self.assertAlmostEqual(extout.y0(), extin.y0()) self.assertAlmostEqual(extout.x1(), extin.x1()) self.assertAlmostEqual(extout.y1(), extin.y1())
def test_cookiecutter_real(self): """ Cookie cutter on single real image """ geoimg = gpt.get_test_image().select(['red']) #, 'green', 'blue']) vpath = os.path.join(os.path.dirname(__file__), 'vectors') # test with feature of different projection feature = gp.GeoVector(os.path.join(vpath, 'aoi1_epsg4326.shp')) extin = feature.extent() imgout = alg.cookie_cutter([geoimg], feature=feature[0], xres=0.0003, yres=0.0003) extout = imgout.extent() self.assertAlmostEqual(extout.x0(), extin.x0()) self.assertAlmostEqual(extout.y0(), extin.y0()) self.assertAlmostEqual(extout.x1(), extin.x1()) self.assertAlmostEqual(extout.y1(), extin.y1())
def test_ndvi_numpy(self): """ Calculate NDVI using numpy (for speed comparison) """ geoimg = gpt.get_test_image() nodata = geoimg[0].nodata() red = geoimg['RED'].read().astype('double') nir = geoimg['NIR'].read().astype('double') ndvi = np.zeros(red.shape) + nodata inds = np.logical_and(red != nodata, nir != nodata) ndvi[inds] = (nir[inds] - red[inds]) / (nir[inds] + red[inds]) fout = 'test-ndvi2.tif' geoimgout = gp.GeoImage.create_from(geoimg, fout, dtype="float64") geoimgout[0].write(ndvi) geoimgout = None geoimg = None os.remove(fout)
def test_ndvi_numpy(self): """ Calculate NDVI using numpy (for speed comparison) """ geoimg = gpt.get_test_image() nodata = geoimg[0].nodata() red = geoimg['RED'].read().astype('double') nir = geoimg['NIR'].read().astype('double') ndvi = np.zeros(red.shape) + nodata inds = np.logical_and(red != nodata, nir != nodata) ndvi[inds] = (nir[inds] - red[inds])/(nir[inds] + red[inds]) fout = 'test-ndvi2.tif' geoimgout = gp.GeoImage.create_from(geoimg, fout, dtype="float64") geoimgout[0].write(ndvi) geoimgout = None geoimg = None os.remove(fout)
def test_ndvi(self): """ Calculate NDVI using gippy and apply colortable """ geoimg = gpt.get_test_image() fout = 'test-ndvi.tif' imgout = alg.indices(geoimg, ['ndvi']) # add colorramp red = np.array([255, 0, 0]) green = np.array([0, 255, 0]) white = np.array([255, 255, 255]) imgout[0] = imgout[0].scale(-1.0, 1.0, 1, 255) imgout = imgout.save(fout, dtype='byte') # add color ramp for negative values imgout[0].add_colortable(red, white, value1=0, value2=128) # add color ramp for positive values imgout[0].add_colortable(white, green, value1=128, value2=255) # TODO - actually test something here os.remove(fout)
def test_cookiecutter_real_reproj(self): """ Test with different projection """ geoimg = gpt.get_test_image().select(['red', 'green', 'blue']) vpath = os.path.join(os.path.dirname(__file__), 'vectors') feature = gp.GeoVector(os.path.join(vpath, 'aoi1_epsg32416.shp')) extin = feature.extent() # test extent matches feature imgout = alg.cookie_cutter([geoimg], feature=feature[0], xres=30.0, yres=30.0) extout = imgout.extent() self.assertAlmostEqual(extout.x0() + 15, extin.x0()) self.assertAlmostEqual(extout.y0() + 15, extin.y0()) # cookie cutter will never add more than a pixel and a half in width self.assertTrue(extout.x1() - extin.x1() < 45.0) self.assertTrue(extout.y1() - extin.y1() < 45.0) self.assertEqual(imgout.resolution().x(), 30.0) self.assertEqual(imgout.resolution().y(), -30.0)
def test_cookiecutter_real(self): """ Cookie cutter on single real image """ geoimg = gpt.get_test_image().select(['red']) #, 'green', 'blue']) iext = geoimg.extent() vpath = os.path.join(os.path.dirname(__file__), 'vectors') # test with feature of different projection feature = gp.GeoVector(os.path.join(vpath, 'aoi1_epsg4326.shp')) extin = feature.extent() imgout = alg.cookie_cutter([geoimg], feature=feature[0], xres=0.0003, yres=0.0003) extout = imgout.extent() self.assertAlmostEqual(extout.x0() + 0.00015, extin.x0()) self.assertAlmostEqual(extout.y0() + 0.00015, extin.y0()) # cookie cutter will never add more than a pixel and a half in width self.assertTrue(extout.x1() - extin.x1() < 0.0045) self.assertTrue(extout.y1() - extin.y1() < 0.0045) self.assertAlmostEqual(imgout.resolution().x(), 0.0003) self.assertAlmostEqual(imgout.resolution().y(), -0.0003)
def test_affine(self): """ Compare affine vs coordinates and resolution """ geoimg = gp.GeoImage.create(xsz=100, ysz=100) aff = geoimg.affine() self.assertEqual(len(aff), 6) self.assertEqual(aff[0], geoimg.minxy().x()) self.assertEqual(aff[1], geoimg.resolution().x()) self.assertEqual(aff[2], 0.0) self.assertEqual(aff[3], geoimg.maxxy().y()) self.assertEqual(aff[4], 0.0) self.assertEqual(aff[5], geoimg.resolution().y()) # test with real image geoimg = gpt.get_test_image() aff = geoimg.affine() self.assertEqual(len(aff), 6) self.assertEqual(aff[0], geoimg.minxy().x()) self.assertEqual(aff[1], geoimg.resolution().x()) self.assertEqual(aff[2], 0.0) self.assertEqual(aff[3], geoimg.maxxy().y()) self.assertEqual(aff[4], 0.0) self.assertEqual(aff[5], geoimg.resolution().y())
def test_loop_through_bands(self): """ Check that GeoImage is iterable """ geoimg = gpt.get_test_image() for band in geoimg: self.assertEqual(band.xsize(), geoimg.xsize())
def test0_open(self): """ Open existing image """ geoimg = gpt.get_test_image() self.assertEqual(geoimg.xsize(), 627) self.assertEqual(geoimg.ysize(), 603)
def test_real_histogram(self): """ Calculate histogram of real data """ geoimg = gpt.get_test_image() hist = geoimg[0].histogram(normalize=False) self.assertEqual(len(hist), 100) self.assertEqual(hist.sum(), geoimg.size())
def test_read_random_pixels(self): """ Read random pixels """ geoimg = gpt.get_test_image() arr = geoimg.read_random_pixels(1000)