@@ -104,6 +104,11 @@ def checkLayersEqual(self, layer_expected, layer_result, use_asserts=False, **kw
104104 except KeyError :
105105 precision = 14
106106
107+ try :
108+ topo_equal_check = compare ['geometry' ]['topo_equal_check' ]
109+ except KeyError :
110+ topo_equal_check = False
111+
107112 def sort_by_pk_or_fid (f ):
108113 if 'pk' in kwargs and kwargs ['pk' ] is not None :
109114 key = kwargs ['pk' ]
@@ -118,27 +123,13 @@ def sort_by_pk_or_fid(f):
118123 result_features = sorted (layer_result .getFeatures (request ), key = sort_by_pk_or_fid )
119124
120125 for feats in zip (expected_features , result_features ):
121- if feats [0 ].hasGeometry ():
122- geom0 = feats [0 ].geometry ().constGet ().asWkt (precision )
123- else :
124- geom0 = None
125- if feats [1 ].hasGeometry ():
126- geom1 = feats [1 ].geometry ().constGet ().asWkt (precision )
127- else :
128- geom1 = None
129- if use_asserts :
130- _TestCase .assertEqual (
131- self ,
132- geom0 ,
133- geom1 ,
134- 'Features (Expected fid: {}, Result fid: {}) differ in geometry: \n \n Expected geometry:\n {}\n \n Result geometry:\n {}' .format (
135- feats [0 ].id (),
136- feats [1 ].id (),
137- geom0 ,
138- geom1
139- )
140- )
141- elif geom0 != geom1 :
126+
127+ eq = self .checkGeometriesEqual (feats [0 ].geometry (),
128+ feats [1 ].geometry (),
129+ feats [0 ].id (),
130+ feats [1 ].id (),
131+ use_asserts , precision , topo_equal_check )
132+ if not eq and use_asserts :
142133 return False
143134
144135 for attr_expected , field_expected in zip (feats [0 ].attributes (), layer_expected .fields ().toList ()):
@@ -215,6 +206,34 @@ def assertFilesEqual(self, filepath_expected, filepath_result):
215206 diff = list (diff )
216207 self .assertEqual (0 , len (diff ), '' .join (diff ))
217208
209+ def checkGeometriesEqual (self , geom0 , geom1 , geom0_id , geom1_id , use_asserts = False , precision = 14 , topo_equal_check = False ):
210+ """ Checks whether two geometries are the same - using either a strict check of coordinates (up to given precision)
211+ or by using topological equality (where e.g. a polygon with clockwise is equal to a polygon with counter-clockwise
212+ order of vertices) """
213+ if not geom0 .isNull () and not geom1 .isNull ():
214+ if topo_equal_check :
215+ equal = geom0 .isGeosEqual (geom1 )
216+ else :
217+ equal = geom0 .constGet ().asWkt (precision ) == geom1 .constGet ().asWkt (precision )
218+ elif geom0 .isNull () and geom1 .isNull ():
219+ equal = True
220+ else :
221+ equal = False
222+
223+ if use_asserts :
224+ _TestCase .assertTrue (
225+ self ,
226+ equal ,
227+ 'Features (Expected fid: {}, Result fid: {}) differ in geometry: \n \n Expected geometry:\n {}\n \n Result geometry:\n {}' .format (
228+ geom0_id ,
229+ geom1_id ,
230+ geom0 .constGet ().asWkt (precision ) if geom0 is not None else 'NULL' ,
231+ geom1 .constGet ().asWkt (precision ) if geom1 is not None else 'NULL'
232+ )
233+ )
234+ else :
235+ return equal
236+
218237
219238class _UnexpectedSuccess (Exception ):
220239
0 commit comments