def polygonEnvelope(polygon): # 多边形的顶点序列 partList = polygon.partList # 两个顶点 rt = Point() lb = Point() rt.x = lb.x = partList[0][0].x rt.y = lb.y = partList[0][0].y for i in range(len(partList)): for k in range(len(partList[i])): if partList[i][k].x < lb.x: lb.x = partList[i][k].x if partList[i][k].x > rt.x: rt.x = partList[i][k].x if partList[i][k].y < lb.y: lb.y = partList[i][k].y if partList[i][k].y > rt.y: rt.y = partList[i][k].y envelope = Envelope() envelope.rtPoint = rt envelope.lbPoint = lb return envelope
def getEnvelopeGravity(envelopeList): # rtPoint = Point() lbPoint = Point() rtPoint.x = lbPoint.x = envelopeList[0].rtPoint.x rtPoint.y = lbPoint.y = envelopeList[0].lbPoint.y for envelope in envelopeList: rt = envelope.rtPoint lb = envelope.lbPoint if lb.x < lbPoint.x: lbPoint.x = lb.x if lb.y < lbPoint.y: lbPoint.y = lb.y if rt.x > rtPoint.x: rtPoint.x = rt.x if rt.y > rtPoint.y: rtPoint.y = rt.y envelope = Envelope() envelope.lbPoint = lbPoint envelope.rtPoint = rtPoint gravity = Point() gravity.x = (rtPoint.x + lbPoint.x) / 2 gravity.y = (rtPoint.y + lbPoint.y) / 2 return gravity, envelope pass
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 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 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
class SimilarScene(object): # 找到的相似场景中的polygon的id polygonList = [] # type:list[str] # 相似度 md = 0 # 外包矩形 envelope = Envelope() # 重心坐标 gravity = Point()
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 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.assertRaisesRegexp(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 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())
class ObservationFile(BaseFile): # ------------------------------------------------------------------------- # __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, '.csv') # 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)
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)