| 1 |
############################################################################### |
| 2 |
# $Id$ |
| 3 |
# |
| 4 |
# Project: GDAL/OGR Test Suite |
| 5 |
# Purpose: Support functions for OGR tests. |
| 6 |
# Author: Frank Warmerdam <[email protected]> |
| 7 |
# |
| 8 |
############################################################################### |
| 9 |
# Copyright (c) 2003, Frank Warmerdam <[email protected]> |
| 10 |
# |
| 11 |
# This library is free software; you can redistribute it and/or |
| 12 |
# modify it under the terms of the GNU Library General Public |
| 13 |
# License as published by the Free Software Foundation; either |
| 14 |
# version 2 of the License, or (at your option) any later version. |
| 15 |
# |
| 16 |
# This library is distributed in the hope that it will be useful, |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
# Library General Public License for more details. |
| 20 |
# |
| 21 |
# You should have received a copy of the GNU Library General Public |
| 22 |
# License along with this library; if not, write to the |
| 23 |
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 24 |
# Boston, MA 02111-1307, USA. |
| 25 |
############################################################################### |
| 26 |
|
| 27 |
import contextlib |
| 28 |
import sys |
| 29 |
|
| 30 |
sys.path.append("../pymod") |
| 31 |
|
| 32 |
import gdaltest |
| 33 |
import pytest |
| 34 |
|
| 35 |
from osgeo import ogr |
| 36 |
|
| 37 |
geos_flag = None |
| 38 |
sfcgal_flag = None |
| 39 |
|
| 40 |
############################################################################### |
| 41 |
|
| 42 |
|
| 43 |
def check_features_against_list(layer, field_name, value_list): |
| 44 |
__tracebackhide__ = True |
| 45 |
|
| 46 |
field_index = layer.GetLayerDefn().GetFieldIndex(field_name) |
| 47 |
assert field_index >= 0, f"did not find required field {field_name}" |
| 48 |
|
| 49 |
for i, value in enumerate(value_list): |
| 50 |
feat = layer.GetNextFeature() |
| 51 |
|
| 52 |
assert ( |
| 53 |
feat is not None |
| 54 |
), f"Got only {i} features, not the expected {len(value_list)} features." |
| 55 |
|
| 56 |
failure_message = ( |
| 57 |
"field %s feature %d did not match expected value %s, got %s." |
| 58 |
% (field_name, i, str(value), str(feat.GetField(field_index))) |
| 59 |
) |
| 60 |
|
| 61 |
if isinstance(value, type("str")): |
| 62 |
assert feat.GetFieldAsString(field_index) == value, failure_message |
| 63 |
else: |
| 64 |
assert feat.GetField(field_index) == value, failure_message |
| 65 |
|
| 66 |
feat = layer.GetNextFeature() |
| 67 |
assert feat is None, "got more features than expected" |
| 68 |
|
| 69 |
|
| 70 |
############################################################################### |
| 71 |
|
| 72 |
|
| 73 |
@gdaltest.disable_exceptions() |
| 74 |
def check_feature_geometry( |
| 75 |
actual, |
| 76 |
expected, |
| 77 |
max_error=0.0001, |
| 78 |
*, |
| 79 |
pointwise=False, |
| 80 |
context=None, |
| 81 |
_root_actual=None, |
| 82 |
_root_expected=None, |
| 83 |
): |
| 84 |
""" |
| 85 |
Check that a geometry matches an expected value. |
| 86 |
|
| 87 |
:param actual: ogr.Feature or ogr.Geometry to test |
| 88 |
:param expected: ogr.Geometry or WKT string representing expected value |
| 89 |
:param max_error: maximum error in any single ordinate (applies to pointwise comparison only) |
| 90 |
:param pointwise: if True, a pointwise comparison will be used to check structural equality (ring ordering, orientation, etc.) |
| 91 |
if False, the pointwise comparison will be done only if a topological equality check (ST_Equals semantics) fails. |
| 92 |
:param context: Optional context information to include in assertion failure message (e.g., "i = 5") |
| 93 |
""" |
| 94 |
__tracebackhide__ = True |
| 95 |
if type(actual) is ogr.Feature: |
| 96 |
actual = actual.GetGeometryRef() |
| 97 |
|
| 98 |
if context: |
| 99 |
context_msg = f" ({context})" |
| 100 |
else: |
| 101 |
context_msg = "" |
| 102 |
|
| 103 |
if expected is not None and isinstance(expected, type("a")): |
| 104 |
expected = ogr.CreateGeometryFromWkt(expected) |
| 105 |
assert expected is not None, ( |
| 106 |
"failed to parse expected geometry as WKT" + context_msg |
| 107 |
) |
| 108 |
|
| 109 |
if expected is None: |
| 110 |
assert actual is None, "expected NULL geometry but got one" + context_msg |
| 111 |
return |
| 112 |
else: |
| 113 |
assert actual is not None, "expected geometry but got NULL" + context_msg |
| 114 |
|
| 115 |
assert expected.GetGeometryName() == expected.GetGeometryName() |
| 116 |
|
| 117 |
assert actual.GetGeometryName() == expected.GetGeometryName(), ( |
| 118 |
"geometry types do not match" + context_msg |
| 119 |
) |
| 120 |
|
| 121 |
assert actual.GetGeometryCount() == expected.GetGeometryCount(), ( |
| 122 |
"sub-geometry counts do not match" + context_msg |
| 123 |
) |
| 124 |
|
| 125 |
assert actual.GetPointCount() == expected.GetPointCount(), ( |
| 126 |
"point counts do not match" + context_msg |
| 127 |
) |
| 128 |
|
| 129 |
if expected.Is3D(): |
| 130 |
assert actual.Is3D(), "expected Z dimension not found" |
| 131 |
if actual.Is3D(): |
| 132 |
assert expected.Is3D(), "unexpected Z dimension" |
| 133 |
|
| 134 |
if expected.IsMeasured(): |
| 135 |
assert actual.IsMeasured(), "expected M dimension not found" |
| 136 |
if actual.IsMeasured(): |
| 137 |
assert expected.IsMeasured(), "unexpected M dimension" |
| 138 |
|
| 139 |
# ST_Equals(a,b) <==> ST_Within(a,b) && ST_Within(b,a) |
| 140 |
# We can't use OGRGeometry::Equals() because it doesn't test spatial |
| 141 |
# equality, but structural one |
| 142 |
# Within does not take into account Z or M values, so we skip to the |
| 143 |
# pointwise check if they are present. |
| 144 |
if ( |
| 145 |
(not pointwise) |
| 146 |
and have_geos() |
| 147 |
and actual.Within(expected) |
| 148 |
and expected.Within(actual) |
| 149 |
and (not actual.Is3D()) |
| 150 |
and (not actual.IsMeasured()) |
| 151 |
): |
| 152 |
return |
| 153 |
|
| 154 |
if _root_actual is None: |
| 155 |
_root_actual = actual |
| 156 |
if _root_expected is None: |
| 157 |
_root_expected = expected |
| 158 |
|
| 159 |
if actual.GetGeometryCount() > 0: |
| 160 |
count = actual.GetGeometryCount() |
| 161 |
for i in range(count): |
| 162 |
check_feature_geometry( |
| 163 |
actual.GetGeometryRef(i), |
| 164 |
expected.GetGeometryRef(i), |
| 165 |
max_error, |
| 166 |
context=context, |
| 167 |
_root_actual=_root_actual, |
| 168 |
_root_expected=_root_expected, |
| 169 |
) |
| 170 |
else: |
| 171 |
count = actual.GetPointCount() |
| 172 |
if ogr.GT_Flatten(actual.GetGeometryType()) == ogr.wkbPoint: |
| 173 |
count = 1 |
| 174 |
|
| 175 |
for i in range(count): |
| 176 |
actual_pt = [actual.GetX(i), actual.GetY(i)] |
| 177 |
expected_pt = [expected.GetX(i), expected.GetY(i)] |
| 178 |
|
| 179 |
if actual.Is3D() or expected.Is3D(): |
| 180 |
actual_pt.append(actual.GetZ(i)) |
| 181 |
expected_pt.append(expected.GetZ(i)) |
| 182 |
|
| 183 |
if actual.IsMeasured() or expected.IsMeasured(): |
| 184 |
# Hack to deal with shapefile not-a-number M values that equal to -1.79769313486232e+308 |
| 185 |
if actual.GetM(i) >= -1.7e308 and expected.GetM(i) >= -1.7e308: |
| 186 |
actual_pt.append(actual.GetM(i)) |
| 187 |
expected_pt.append(expected.GetM(i)) |
| 188 |
|
| 189 |
assert actual_pt == pytest.approx( |
| 190 |
expected_pt, abs=max_error |
| 191 |
), f"Error in vertex {i+1}/{count} exceeds tolerance. {context_msg}\n Expected: {_root_expected.ExportToWkt()}\n Actual: {_root_actual.ExportToWkt()}" |
| 192 |
|
| 193 |
|
| 194 |
############################################################################### |
| 195 |
|
| 196 |
|
| 197 |
def check_feature(feat, feat_ref, max_error=0.0001, excluded_fields=None): |
| 198 |
__tracebackhide__ = True |
| 199 |
|
| 200 |
for i in range(feat.GetGeomFieldCount()): |
| 201 |
check_feature_geometry( |
| 202 |
feat.GetGeomFieldRef(i), feat_ref.GetGeomFieldRef(i), max_error=max_error |
| 203 |
) |
| 204 |
|
| 205 |
for i in range(feat.GetFieldCount()): |
| 206 |
if excluded_fields is not None: |
| 207 |
if feat.GetDefnRef().GetFieldDefn(i).GetName() in excluded_fields: |
| 208 |
continue |
| 209 |
|
| 210 |
assert feat.GetField(i) == feat_ref.GetField( |
| 211 |
i |
| 212 |
), f"Field {i}, expected {feat.GetField(i)}, got {feat_ref.GetField(i)}" |
| 213 |
|
| 214 |
|
| 215 |
############################################################################### |
| 216 |
|
| 217 |
|
| 218 |
def compare_layers(lyr, lyr_ref, excluded_fields=None): |
| 219 |
|
| 220 |
for f_ref in lyr_ref: |
| 221 |
f = lyr.GetNextFeature() |
| 222 |
assert f is not None, "not enough features" |
| 223 |
check_feature(f, f_ref, excluded_fields=excluded_fields) |
| 224 |
f = lyr.GetNextFeature() |
| 225 |
assert f is None, "more features than expected" |
| 226 |
|
| 227 |
|
| 228 |
############################################################################### |
| 229 |
|
| 230 |
|
| 231 |
def get_wkt_data_series(with_z, with_m, with_gc, with_circular, with_surface): |
| 232 |
basic_wkts = [ |
| 233 |
"POINT (1 1)", |
| 234 |
"POINT (1.1234 1.4321)", |
| 235 |
"POINT (1.12345678901234 1.4321)", |
| 236 |
"POINT (1.2 -2.1)", |
| 237 |
"MULTIPOINT ((10 40),(40 30),(20 20),(30 10))", |
| 238 |
"LINESTRING (1.2 -2.1,2.4 -4.8)", |
| 239 |
"MULTILINESTRING ((10 10,20 20,10 40),(40 40,30 30,40 20,30 10),(50 50,60 60,50 90))", |
| 240 |
"MULTILINESTRING ((1.2 -2.1,2.4 -4.8))", |
| 241 |
"POLYGON ((30 10,40 40,20 40,10 20,30 10))", |
| 242 |
"POLYGON ((35 10,45 45,15 40,10 20,35 10),(20 30,35 35,30 20,20 30))", |
| 243 |
"MULTIPOLYGON (((30 20,45 40,10 40,30 20)),((15 5,40 10,10 20,5 10,15 5)))", |
| 244 |
"MULTIPOLYGON (((40 40,20 45,45 30,40 40)),((20 35,10 30,10 10,30 5,45 20,20 35),(30 20,20 15,20 25,30 20)))", |
| 245 |
"MULTIPOLYGON (((30 20,45 40,10 40,30 20)))", |
| 246 |
"MULTIPOLYGON (((35 10,45 45,15 40,10 20,35 10),(20 30,35 35,30 20,20 30)))", |
| 247 |
] |
| 248 |
gc_wkts = [ |
| 249 |
"GEOMETRYCOLLECTION (POINT (4 6),LINESTRING (4 6,7 10))", |
| 250 |
"GEOMETRYCOLLECTION (POINT (4 6),GEOMETRYCOLLECTION (POINT (4 6),LINESTRING (4 6,7 10)))", |
| 251 |
] |
| 252 |
circular_wkts = [ |
| 253 |
"CIRCULARSTRING (0 0,1 1,1 0)", |
| 254 |
"COMPOUNDCURVE (CIRCULARSTRING (0 0,1 1,1 0),(1 0,0 1))", |
| 255 |
"CURVEPOLYGON (CIRCULARSTRING (0 0,4 0,4 4,0 4,0 0),(1 1,3 3,3 1,1 1))", |
| 256 |
"MULTICURVE ((0 0,5 5),CIRCULARSTRING (4 0,4 4,8 4))", |
| 257 |
"MULTISURFACE (CURVEPOLYGON (CIRCULARSTRING (0 0,4 0,4 4,0 4,0 0),(1 1,3 3,3 1,1 1)),((10 10,14 12,11 10,10 10),(11 11,11.5 11.0,11.0 11.5,11 11)))", |
| 258 |
] |
| 259 |
surface_wkts = [ |
| 260 |
"POLYHEDRALSURFACE Z (((0 0 0,0 0 1,0 1 1,0 1 0,0 0 0)),((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),((0 0 0,1 0 0,1 0 1,0 0 1,0 0 0)),((1 1 0,1 1 1,1 0 1,1 0 0,1 1 0)),((0 1 0,0 1 1,1 1 1,1 1 0,0 1 0)),((0 0 1,1 0 1,1 1 1,0 1 1,0 0 1)))", |
| 261 |
"TRIANGLE ((0 0,0 9,9 0,0 0))", |
| 262 |
"TIN Z (((0 0 0,0 0 1,0 1 0,0 0 0)))", |
| 263 |
"TIN Z (((0 0 0,0 0 1,0 1 0,0 0 0)),((0 0 0,0 1 0,1 1 0,0 0 0)))", |
| 264 |
] |
| 265 |
|
| 266 |
wkts = basic_wkts |
| 267 |
wkts_with_z = [] |
| 268 |
wkts_with_m = [] |
| 269 |
wkts_with_zm = [] |
| 270 |
if with_gc: |
| 271 |
wkts.extend(gc_wkts) |
| 272 |
if with_circular: |
| 273 |
wkts.extend(circular_wkts) |
| 274 |
if with_z or with_m: |
| 275 |
for i, wkt in enumerate(wkts): |
| 276 |
if with_z: |
| 277 |
g = ogr.CreateGeometryFromWkt(wkt) |
| 278 |
g.Set3D(True) |
| 279 |
wkts_with_z.extend([g.ExportToIsoWkt()]) |
| 280 |
if with_z: |
| 281 |
g = ogr.CreateGeometryFromWkt(wkt) |
| 282 |
g.SetMeasured(True) |
| 283 |
wkts_with_m.extend([g.ExportToIsoWkt()]) |
| 284 |
if with_z and with_m: |
| 285 |
g = ogr.CreateGeometryFromWkt(wkt) |
| 286 |
g.Set3D(True) |
| 287 |
g.SetMeasured(True) |
| 288 |
wkts_with_zm.extend([g.ExportToIsoWkt()]) |
| 289 |
wkts.extend(wkts_with_z) |
| 290 |
wkts.extend(wkts_with_m) |
| 291 |
wkts.extend(wkts_with_zm) |
| 292 |
if with_surface: |
| 293 |
wkts.extend(surface_wkts) |
| 294 |
return wkts |
| 295 |
|
| 296 |
|
| 297 |
############################################################################### |
| 298 |
|
| 299 |
|
| 300 |
def quick_create_layer_def(lyr, field_list): |
| 301 |
# Each field is a tuple of (name, type, width, precision) |
| 302 |
# Any of type, width and precision can be skipped. Default type is string. |
| 303 |
|
| 304 |
for field in field_list: |
| 305 |
name = field[0] |
| 306 |
if len(field) > 1: |
| 307 |
field_type = field[1] |
| 308 |
else: |
| 309 |
field_type = ogr.OFTString |
| 310 |
|
| 311 |
field_defn = ogr.FieldDefn(name, field_type) |
| 312 |
|
| 313 |
if len(field) > 2: |
| 314 |
field_defn.SetWidth(int(field[2])) |
| 315 |
|
| 316 |
if len(field) > 3: |
| 317 |
field_defn.SetPrecision(int(field[3])) |
| 318 |
|
| 319 |
lyr.CreateField(field_defn) |
| 320 |
|
| 321 |
|
| 322 |
############################################################################### |
| 323 |
|
| 324 |
|
| 325 |
def quick_create_feature(layer, field_values, wkt_geometry): |
| 326 |
feature = ogr.Feature(feature_def=layer.GetLayerDefn()) |
| 327 |
|
| 328 |
for i, field_value in enumerate(field_values): |
| 329 |
feature.SetField(i, field_value) |
| 330 |
|
| 331 |
if wkt_geometry is not None: |
| 332 |
geom = ogr.CreateGeometryFromWkt(wkt_geometry) |
| 333 |
if geom is None: |
| 334 |
raise ValueError("Failed to create geometry from: " + wkt_geometry) |
| 335 |
feature.SetGeometryDirectly(geom) |
| 336 |
|
| 337 |
result = layer.CreateFeature(feature) |
| 338 |
|
| 339 |
if result != 0: |
| 340 |
raise ValueError("CreateFeature() failed in ogrtest.quick_create_feature()") |
| 341 |
|
| 342 |
|
| 343 |
############################################################################### |
| 344 |
|
| 345 |
|
| 346 |
def have_geos(): |
| 347 |
return ogr.GetGEOSVersionMajor() > 0 |
| 348 |
|
| 349 |
|
| 350 |
############################################################################### |
| 351 |
|
| 352 |
|
| 353 |
def have_sfcgal(): |
| 354 |
global sfcgal_flag |
| 355 |
|
| 356 |
if sfcgal_flag is None: |
| 357 |
with gdaltest.disable_exceptions(): |
| 358 |
pnt1 = ogr.CreateGeometryFromWkt("POINT(10 20 30)") |
| 359 |
pnt2 = ogr.CreateGeometryFromWkt("POINT(40 50 60)") |
| 360 |
sfcgal_flag = pnt1.Distance3D(pnt2) >= 0 |
| 361 |
|
| 362 |
return sfcgal_flag |
| 363 |
|
| 364 |
|
| 365 |
############################################################################### |
| 366 |
# Temporarily set an attribute filter |
| 367 |
|
| 368 |
|
| 369 |
@contextlib.contextmanager |
| 370 |
def attribute_filter(lyr, filter_txt): |
| 371 |
lyr.SetAttributeFilter(filter_txt) |
| 372 |
try: |
| 373 |
yield |
| 374 |
finally: |
| 375 |
lyr.SetAttributeFilter(None) |
| 376 |
|
| 377 |
|
| 378 |
############################################################################### |
| 379 |
# Temporarily set a spatial filter |
| 380 |
# Single argument is parsed as WKT or assumed to be an OGRGeometry |
| 381 |
# Four arguments are interpreted as bounding rectangle |
| 382 |
|
| 383 |
|
| 384 |
@contextlib.contextmanager |
| 385 |
def spatial_filter(lyr, *args): |
| 386 |
|
| 387 |
if len(args) == 1: |
| 388 |
if type(args[0]) is str: |
| 389 |
geom = ogr.CreateGeometryFromWkt(args[0]) |
| 390 |
lyr.SetSpatialFilter(geom) |
| 391 |
else: |
| 392 |
lyr.SetSpatialFilter(args[0]) |
| 393 |
elif len(args) == 4: |
| 394 |
lyr.SetSpatialFilterRect(*args) |
| 395 |
else: |
| 396 |
raise Exception("Unknown spatial filter type") |
| 397 |
try: |
| 398 |
yield |
| 399 |
finally: |
| 400 |
lyr.SetSpatialFilter(None) |