def envelope(self): envelope = Envelope() envelope.addPoint(self._ulx, self._uly, 0, self.srs()) envelope.addPoint(self._lrx, self._lry, 0, self.srs()) return envelope
def testConsistentResults(self): ulx = 94.2 uly = 19.4 lrx = 94.6 lry = 19.1 srs = SpatialReference() srs.ImportFromEPSG(4326) srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) fpq = FootprintsQuery(FootprintsQueryTestCase._logger) fpq.addAoI(env) numScenes = 27 fpq.setMaximumScenes(numScenes) fpScenes1 = fpq.getScenes() fpScenes2 = fpq.getScenes() self.assertEqual(len(fpScenes1), numScenes) self.assertEqual(len(fpScenes1), len(fpScenes2)) for i in range(len(fpScenes1)): self.assertEqual(fpScenes1[i].fileName(), fpScenes2[i].fileName())
def envelope(self): if not self._envelope: self._envelope = Envelope() for obs in self._observations: self._envelope.addOgrPoint(obs[0].Clone()) return self._envelope
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 main(): # Process command-line args. desc = 'Use this application to request MERRA files.' parser = argparse.ArgumentParser(description=desc) parser.add_argument('-e', required=True, nargs='+', help='ulx uly lrx lry') parser.add_argument('--epsg', required=True, type=int, help='EPSG code') parser.add_argument('--start_date', required=True, help='YYYY-MM-DD') parser.add_argument('--end_date', required=True, help='YYYY-MM-DD') parser.add_argument('-c', required=True, help='Name of collection of MERRA2') parser.add_argument('--vars', required=True, nargs='+', help='List of variables in M2 collection') parser.add_argument('--op', required=True, help='Type of analysis') parser.add_argument('-o', default='.', help='Path to output directory') args = parser.parse_args() # Envelope srs = SpatialReference() srs.ImportFromEPSG(args.epsg) env = Envelope() env.addPoint(float(args.e[0]), float(args.e[1]), 0, srs) env.addPoint(float(args.e[2]), float(args.e[3]), 0, srs) # Date Range dateRange = pandas.date_range(args.start_date, args.end_date) # Mas Request MerraRequest.run(env, dateRange, MerraRequest.MONTHLY, [args.c], args.vars, [args.op], args.o)
def testAddAoI(self): ulx = 626002.2463251714 uly = 2145525.859757114 lrx = 668316.2848759613 lry = 2112661.4394026464 srs = SpatialReference() srs.ImportFromEPSG(32646) env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) fpq = FootprintsQuery(FootprintsQueryTestCase._logger) fpq.addAoI(env) numScenes = 27 fpq.setMaximumScenes(numScenes) fpScenes1 = fpq.getScenes() self.assertEqual(len(fpScenes1), numScenes)
def testRun(self): ulx = -71 uly = 40 lrx = -70 lry = 39 srs = SpatialReference() srs.ImportFromEPSG(4326) srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) dateRange = pandas.date_range('2010-11-11', '2011-01-12') outDir = tempfile.mkdtemp() files = MerraRequest.run(env, dateRange, MerraRequest.MONTHLY, ['m2t1nxslv'], ['QV2M', 'TS'], ['avg'], outDir) expected = [ os.path.join(outDir, 'm2t1nxslv_avg_2010_month11_QV2M.nc'), os.path.join(outDir, 'm2t1nxslv_avg_2010_month11_TS.nc'), os.path.join(outDir, 'm2t1nxslv_avg_2010_month12_QV2M.nc'), os.path.join(outDir, 'm2t1nxslv_avg_2010_month12_TS.nc'), os.path.join(outDir, 'm2t1nxslv_avg_2011_month01_QV2M.nc'), os.path.join(outDir, 'm2t1nxslv_avg_2011_month01_TS.nc') ] self.assertEqual(len(expected), len(files)) self.assertEqual(expected, sorted(files)) clipped = GeospatialImageFile(os.path.join( outDir, 'm2t1nxslv_avg_2010_month11_QV2M.nc'), spatialReference=srs) self.assertTrue(clipped.envelope().Equal(env)) # Delete the clipped files. for f in files: os.remove(f)
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 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() # --- # If this file is in 4326, we must swap the x-y to conform with GDAL # 3's strict conformity to the 4326 definition. # --- srs4326 = SpatialReference() srs4326.ImportFromEPSG(4326) if srs4326.IsSame(self._dataset.GetSpatialRef()): envelope.addPoint(uly, ulx, 0, self._dataset.GetSpatialRef()) envelope.addPoint(lry, lrx, 0, self._dataset.GetSpatialRef()) else: envelope.addPoint(ulx, uly, 0, self._dataset.GetSpatialRef()) envelope.addPoint(lrx, lry, 0, self._dataset.GetSpatialRef()) return envelope
def testAddOgrPoint(self): # --- # Several of the tests are covered by testAddPoint because addPoint # uses addOgrPoint. # --- env = Envelope() with self.assertRaises(AttributeError): env.addOgrPoint('abc') with self.assertRaisesRegex(RuntimeError, 'must be of type wkbPoint'): env.addOgrPoint(ogr.Geometry(ogr.wkbPolygon)) srs = SpatialReference() srs.ImportFromEPSG(4326) ogrPt = ogr.Geometry(ogr.wkbPoint) ogrPt.AddPoint(20.0, 30.0, 40) ogrPt.AssignSpatialReference(srs) env.addOgrPoint(ogrPt) self.assertEqual(env.ulx(), 20.0) self.assertEqual(env.uly(), 30.0)
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 testGeographicOrdinateOrder(self): ulx = -148 uly = 65 lrx = -147 lry = 64 srs = SpatialReference() srs.ImportFromEPSG(4326) 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 testTransformTo(self): ulx = 626002.2463251714 # 94.19 uly = 2145525.859757114 # 19.40 lrx = 668316.2848759613 # 94.59 lry = 2112661.4394026464 # 19.10 srs = SpatialReference() srs.ImportFromEPSG(32646) env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) targetSrs = SpatialReference() targetSrs.ImportFromEPSG(4326) targetSrs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) env.TransformTo(targetSrs) self.assertAlmostEqual(94.199, env.ulx(), places=2) self.assertAlmostEqual(19.400, env.uly(), places=2) self.assertAlmostEqual(94.599, env.lrx(), places=2) self.assertAlmostEqual(19.100, env.lry(), places=2)
def testEnvelope(self): # Create a test image. imageFile = self._createTestFile() # Test envelope. srs = SpatialReference() srs.ImportFromEPSG(4326) # https://github.com/OSGeo/gdal/blob/release/3.0/gdal/MIGRATION_GUIDE.TXT srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) 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 testGetSetState(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) state = env.__reduce__() env2 = Envelope() env2.__setstate__(state[2]) self.assertEqual(env.ulx(), env2.ulx()) self.assertEqual(env.uly(), env2.uly()) self.assertEqual(env.lrx(), env2.lrx()) self.assertEqual(env.lry(), env2.lry())
def testClipReproject(self): # --- # Test only clipping. # --- imageFile = self._createTestFile() # https://github.com/OSGeo/gdal/blob/release/3.0/gdal/MIGRATION_GUIDE.TXT srs = SpatialReference() srs.ImportFromEPSG(4326) srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) ulx = -120 uly = 48 lrx = -119 lry = 47 env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) imageFile.clipReproject(env) # Check the result. xform = imageFile.getDataset().GetGeoTransform() xScale = xform[1] yScale = xform[5] width = imageFile.getDataset().RasterXSize height = imageFile.getDataset().RasterYSize clippedUlx = xform[0] clippedUly = xform[3] clippedLrx = clippedUlx + width * xScale clippedLry = clippedUly + height * yScale self.assertAlmostEqual(clippedUlx, ulx) self.assertAlmostEqual(clippedUly, uly) self.assertAlmostEqual(clippedLrx, lrx) self.assertAlmostEqual(clippedLry, lry) os.remove(imageFile.fileName()) # --- # Test only reprojection. # --- imageFile = self._createTestFile(createUTM=True) targetSRS = SpatialReference() targetSRS.ImportFromEPSG(4326) targetSRS.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) imageFile.clipReproject(None, targetSRS) # gdaltransform confirms these expected values. self.assertTrue(imageFile.srs().IsSame(targetSRS)) self.assertAlmostEqual(imageFile.envelope().ulx(), -112.5144042, places=7) self.assertAlmostEqual(imageFile.envelope().uly(), 38.0308143, places=7) self.assertAlmostEqual(imageFile.envelope().lrx(), -110.8938763, places=7) self.assertAlmostEqual(imageFile.envelope().lry(), 36.9934700, places=7) os.remove(imageFile.fileName()) # --- # Test clipping and reprojection. # --- imageFile = self._createTestFile() # Build the envelope. srs = SpatialReference() srs.ImportFromEPSG(4326) srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) ulx = -120 uly = 48 lrx = -119 lry = 47 env = Envelope() env.addPoint(ulx, uly, 0, srs) env.addPoint(lrx, lry, 0, srs) # Reprojection parameter targetSRS = SpatialReference() targetSRS.ImportFromEPSG(32611) # Clip, reproject and resample. imageFile.clipReproject(env, targetSRS) # Check the results. xform = imageFile.getDataset().GetGeoTransform() xScale = xform[1] yScale = xform[5] width = imageFile.getDataset().RasterXSize height = imageFile.getDataset().RasterYSize clippedUlx = xform[0] clippedUly = xform[3] clippedLrx = clippedUlx + width * xScale clippedLry = clippedUly + height * yScale self.assertTrue(imageFile.srs().IsSame(targetSRS)) self.assertAlmostEqual(clippedUlx, 271930.435, places=3) self.assertAlmostEqual(clippedUly, 5318235.614, places=3) self.assertAlmostEqual(clippedLrx, 350812.125, places=3) self.assertAlmostEqual(clippedLry, 5209532.848, places=3) # Delete the test file. os.remove(imageFile.fileName())
class ObservationFile(BaseFile): FILE_KEY = 'PathToFile' SPECIES_KEY = 'Species' # ------------------------------------------------------------------------- # __init__ # ------------------------------------------------------------------------- def __init__(self, pathToFile, species): if not species: raise RuntimeError('A species must be specified.') # Initialize the base class. super(ObservationFile, self).__init__(pathToFile) # Initialize the data members. self._species = species self._srs = None self._envelope = None self._observations = [] self._parse() # ------------------------------------------------------------------------- # envelope # ------------------------------------------------------------------------- def envelope(self): if not self._envelope: self._envelope = Envelope() for obs in self._observations: self._envelope.addOgrPoint(obs[0].Clone()) return self._envelope # ------------------------------------------------------------------------- # numObservations # ------------------------------------------------------------------------- def numObservations(self): return len(self._observations) # ------------------------------------------------------------------------- # observation # ------------------------------------------------------------------------- def observation(self, index): if index >= self.numObservations(): raise IndexError return self._observations[index] # ------------------------------------------------------------------------- # _parse # # This parser requires a header and understands the following formats: # - x,y,response:binary,epsg:nnnnn # ------------------------------------------------------------------------- def _parse(self): with open(self._filePath) as csvFile: first = True fdReader = csv.reader(csvFile, delimiter=',') for row in fdReader: if first: first = False # If the first element is a float, there is no header row. try: float(row[0]) raise RuntimeError('The observation file, ' + str(self.fileName()) + ' must have a header.') except ValueError: if ':' not in row[3]: raise RuntimeError('EPSG in header field ' + 'must contain a colon ' + 'then integer EPSG code.') epsg = row[3].split(':')[1] self._srs = SpatialReference() self._srs.ImportFromEPSG(int(epsg)) else: # Parse a row. ogrPt = ogr.Geometry(ogr.wkbPoint) ogrPt.AddPoint(float(row[0]), float(row[1]), 0) ogrPt.AssignSpatialReference(self._srs) # self._envelope.addOgrPoint(ogrPt) self._observations.append((ogrPt, float(row[2]))) # ------------------------------------------------------------------------- # species # ------------------------------------------------------------------------- def species(self): return self._species # ------------------------------------------------------------------------- # srs # ------------------------------------------------------------------------- def srs(self): return self._srs # ------------------------------------------------------------------------- # transformTo # ------------------------------------------------------------------------- def transformTo(self, newSRS): if newSRS.IsSame(self._srs): return self._envelope = None self._srs = newSRS for obs in self._observations: obs[0].TransformTo(newSRS) # ------------------------------------------------------------------------- # __getstate__ # ------------------------------------------------------------------------- def __getstate__(self): state = {ObservationFile.FILE_KEY: self._filePath, ObservationFile.SPECIES_KEY: self._species} return state # ------------------------------------------------------------------------- # __setstate__ # # e2 = pickle.loads(pickle.dumps(env)) # ------------------------------------------------------------------------- def __setstate__(self, state): self.__init__(state[ObservationFile.FILE_KEY], state[ObservationFile.SPECIES_KEY])
def testExpandByPercentage(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) env.expandByPercentage(0.0) self.assertEqual(ulx, env.ulx()) self.assertEqual(uly, env.uly()) self.assertEqual(lrx, env.lrx()) self.assertEqual(lry, env.lry()) percentage = 10 width = 127411.0 height = 163224.55529116935 exWidth = width * percentage / 100 / 2.0 exHeight = height * percentage / 100 / 2.0 exUlx = abs(ulx - exWidth) exUly = abs(uly + exHeight) newUlx, newUly, newLrx, newLry = env.expandByPercentage(percentage) self.assertEqual(exUlx, newUlx) self.assertEqual(exUly, newUly)
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.assertRaisesRegex(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)