def testEnvelope(self): testEnv = Envelope() srs = SpatialReference() srs.ImportFromEPSG(32612) testEnv.addPoint(374187, 4202663, 0, srs) testEnv.addPoint(501598, 4100640, 0, srs) obs = ObservationFile(ObservationFileTestCase._testObsFile, ObservationFileTestCase._species) self.assertTrue(testEnv.Equals(obs.envelope()))
def testClipReproject(self): # Build the test file. imageFile = self._createTestFile() # Build the envelope. ulx = 367080 uly = 4209230 lrx = 509200 lry = 4095100 srs = SpatialReference() srs.ImportFromEPSG(32612) env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) # Reprojection parameter targetSRS = SpatialReference() targetSRS.ImportFromEPSG(4326) # Clip, reproject and resample. imageFile.clipReproject(env, targetSRS,) # Check the results. dataset = gdal.Open(imageFile.fileName(), gdalconst.GA_ReadOnly) if not dataset: raise RuntimeError('Unable to read ' + imageFile.fileName() + '.') xform = dataset.GetGeoTransform() xScale = xform[1] yScale = xform[5] width = dataset.RasterXSize height = dataset.RasterYSize clippedUlx = xform[0] clippedUly = xform[3] clippedLrx = clippedUlx + width * xScale clippedLry = clippedUly + height * yScale self.assertAlmostEqual(clippedUlx, -112.49369402670872, places=12) self.assertAlmostEqual(clippedUly, 38.03073206024332, places=11) self.assertAlmostEqual(clippedLrx, -110.89516946364738, places=12) self.assertAlmostEqual(clippedLry, 36.99265291293727, places=11) outSRS = SpatialReference() outSRS.ImportFromWkt(dataset.GetProjection()) self.assertTrue(outSRS.IsSame(targetSRS)) # Delete the test file. os.remove(imageFile.fileName())
def testAccessors(self): ulx = 374187 uly = 4202663 lrx = 501598 lry = 4100640 srs = SpatialReference() srs.ImportFromEPSG(32612) env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) self.assertEqual(ulx, env.ulx()) self.assertEqual(uly, env.uly()) self.assertEqual(lrx, env.lrx()) self.assertEqual(lry, env.lry())
def testEnvelope(self): # Create a test image. imageFile = self._createTestFile() # Test envelope. srs = SpatialReference() srs.ImportFromEPSG(4326) expectedEnvelope = Envelope() expectedEnvelope.addPoint(-125.3125000, 50.25, 0, srs) expectedEnvelope.addPoint(-65.9375000, 23.75, 0, srs) self.assertTrue(imageFile.envelope().Equals(expectedEnvelope)) # Delete the test file. os.remove(imageFile.fileName())
def envelope(self): dataset = self._getDataset() xform = dataset.GetGeoTransform() xScale = xform[1] yScale = xform[5] width = dataset.RasterXSize height = dataset.RasterYSize ulx = xform[0] uly = xform[3] lrx = ulx + width * xScale lry = uly + height * yScale envelope = Envelope() envelope.addPoint(ulx, uly, 0, self.srs()) envelope.addPoint(lrx, lry, 0, self.srs()) return envelope
def testClip(self): # Build the test file. imageFile = self._createTestFile() # Build the envelope and clip. ulx = -100 uly = 40 lrx = -70 lry = 30 srs = SpatialReference() srs.ImportFromEPSG(4326) env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) imageFile.clipReproject(env) # Check the corners. dataset = gdal.Open(imageFile.fileName(), gdalconst.GA_ReadOnly) if not dataset: raise RuntimeError('Unable to read ' + imageFile.fileName() + '.') xform = dataset.GetGeoTransform() xScale = xform[1] yScale = xform[5] width = dataset.RasterXSize height = dataset.RasterYSize clippedUlx = xform[0] clippedUly = xform[3] clippedLrx = ulx + width * xScale clippedLry = uly + height * yScale self.assertEqual(clippedUlx, ulx) self.assertEqual(clippedUly, uly) self.assertEqual(clippedLrx, lrx) self.assertEqual(clippedLry, lry) # Delete the test file. os.remove(imageFile.fileName())
def testAddPoint(self): env = Envelope() # Invalid x. Invalid ordinates are undetected by GDAL, so no error. srs = SpatialReference() srs.ImportFromEPSG(4326) env.addPoint(100, 100, 0, srs) # Invalid ordinate type. with self.assertRaises(TypeError): env.addPoint('abc', 100, 0, srs) # Add a second point with a different SRS than the first. with self.assertRaisesRegexp(RuntimeError, 'must be in the SRS'): utm = SpatialReference() utm.ImportFromEPSG(32612) env.addPoint(374187, 4202663, 0, utm) # Add a couple valid points. env.addPoint(80, 10, 10, srs) env.addPoint(43.5, 79.3, 0, srs)