From 0cf03cb4cbf8a705b62afde1ee045b594b585abf Mon Sep 17 00:00:00 2001 From: "benjamin.jakimow@geo.hu-berlin.de" <q8DTkxUg-BB> Date: Wed, 7 Mar 2018 18:41:34 +0100 Subject: [PATCH] made SpatialPoint and SpatialExtent hashable --- test/test_utils.py | 11 ++++++++++- timeseriesviewer/utils.py | 22 +++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index 37305dcb..5cc2797d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -31,7 +31,8 @@ class testclassUtilityTests(unittest.TestCase): pass - def test_spatialObjection(self): + + def test_spatialObjects(self): """Test we can click OK.""" import example pathRE = file_search(os.path.dirname(example.__file__), 're*', recursive=True)[0] @@ -45,6 +46,14 @@ class testclassUtilityTests(unittest.TestCase): self.assertIsInstance(pt1, SpatialPoint) + d = {} + for t in [pt1, se]: + try: + d[t] = '{}'.format(t) + except: + self.fail('Unable to use {} as dictionary key.'.format(type(t))) + + if __name__ == "__main__": unittest.main() diff --git a/timeseriesviewer/utils.py b/timeseriesviewer/utils.py index 4a81e1ab..1cd414de 100644 --- a/timeseriesviewer/utils.py +++ b/timeseriesviewer/utils.py @@ -99,7 +99,7 @@ def scaledUnitString(num, infix=' ', suffix='B', div=1000): return "{:.1f}{}{}{}".format(num, infix, unit, suffix) -class SpatialPoint(QgsPoint): +class SpatialPoint(QgsPointXY): """ Object to keep QgsPoint and QgsCoordinateReferenceSystem together """ @@ -123,6 +123,9 @@ class SpatialPoint(QgsPoint): super(SpatialPoint, self).__init__(*args) self.mCrs = crs + def __hash__(self): + return hash(str(self)) + def setCrs(self, crs): assert isinstance(crs, QgsCoordinateReferenceSystem) self.mCrs = crs @@ -155,7 +158,7 @@ class SpatialPoint(QgsPoint): def toCrs(self, crs): assert isinstance(crs, QgsCoordinateReferenceSystem) - pt = QgsPoint(self) + pt = QgsPointXY(self) if self.mCrs != crs: pt = saveTransform(pt, self.mCrs, crs) @@ -291,7 +294,7 @@ def px2geo(px, gt): #see http://www.gdal.org/gdal_datamodel.html gx = gt[0] + px.x()*gt[1]+px.y()*gt[2] gy = gt[3] + px.x()*gt[4]+px.y()*gt[5] - return QgsPoint(gx,gy) + return QgsPointXY(gx,gy) class SpatialExtent(QgsRectangle): @@ -400,16 +403,16 @@ class SpatialExtent(QgsRectangle): s = "" def upperRightPt(self): - return QgsPoint(*self.upperRight()) + return QgsPointXY(*self.upperRight()) def upperLeftPt(self): - return QgsPoint(*self.upperLeft()) + return QgsPointXY(*self.upperLeft()) def lowerRightPt(self): - return QgsPoint(*self.lowerRight()) + return QgsPointXY(*self.lowerRight()) def lowerLeftPt(self): - return QgsPoint(*self.lowerLeft()) + return QgsPointXY(*self.lowerLeft()) def upperRight(self): @@ -442,6 +445,11 @@ class SpatialExtent(QgsRectangle): self.xMinimum(), self.yMinimum(), self.xMaximum(), self.yMaximum() ), {} + + + def __hash__(self): + return hash(str(self)) + def __str__(self): return self.__repr__() -- GitLab