Coverage for pyguymer3/geo/_debug.py: 4%

24 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-08 18:47 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def _debug( 

5 badGeoms, 

6 /, 

7 *, 

8 prefix = ".", 

9): 

10 """Save CSVs for debugging 

11 

12 Parameters 

13 ---------- 

14 badGeoms : shapely.geometry.multilinestring.LineString, shapely.geometry.multilinestring.MultiLineString, shapely.geometry.polygon.Polygon, shapely.geometry.multipolygon.MultiPolygon 

15 the bad [Multi]LineString or [Multi]Polygon 

16 prefix : str, optional 

17 change the name of the output debugging CSVs 

18 

19 Notes 

20 ----- 

21 Copyright 2017 Thomas Guymer [1]_ 

22 

23 References 

24 ---------- 

25 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

26 """ 

27 

28 # Import sub-functions ... 

29 from .extract_lines import extract_lines 

30 from .extract_points import extract_points 

31 from .extract_polys import extract_polys 

32 

33 # ************************************************************************** 

34 

35 # Loop over all the bad Points in this [Multi]Point ... 

36 for i, badGeom in enumerate(extract_points(badGeoms, onlyValid = False)): 

37 # Open output file ... 

38 print(f"ERROR: Writing \"debug{prefix}Point{i:d}.csv\" ...") 

39 with open(f"debug{prefix}Point{i:d}.csv", "wt", encoding = "utf-8") as fObj: 

40 # Loop over bad coordinates of this bad Point ... 

41 for badCoord in badGeom.coords: 

42 # Write output ... 

43 fObj.write(f"{badCoord[0]:.15e},{badCoord[1]:.15e}\n") 

44 

45 # Loop over all the bad LineStrings in this [Multi]LineString ... 

46 for i, badGeom in enumerate(extract_lines(badGeoms, onlyValid = False)): 

47 # Open output file ... 

48 print(f"ERROR: Writing \"debug{prefix}LineString{i:d}.csv\" ...") 

49 with open(f"debug{prefix}LineString{i:d}.csv", "wt", encoding = "utf-8") as fObj: 

50 # Loop over bad coordinates of this bad LineString ... 

51 for badCoord in badGeom.coords: 

52 # Write output ... 

53 fObj.write(f"{badCoord[0]:.15e},{badCoord[1]:.15e}\n") 

54 

55 # Loop over all the bad Polygons in this [Multi]Polygon ... 

56 for i, badGeom in enumerate(extract_polys(badGeoms, repair = False, onlyValid = False)): 

57 # Open output file ... 

58 print(f"ERROR: Writing \"debug{prefix}Polygon{i:d}.exterior.csv\" ...") 

59 with open(f"debug{prefix}Polygon{i:d}.exterior.csv", "wt", encoding = "utf-8") as fObj: 

60 # Loop over bad coordinates in the exterior ring of this bad Polygon ... 

61 for badCoord in badGeom.exterior.coords: 

62 # Write output ... 

63 fObj.write(f"{badCoord[0]:.15e},{badCoord[1]:.15e}\n") 

64 

65 # Loop over bad interior rings of this bad Polygon ... 

66 for j, interior in enumerate(badGeom.interiors): 

67 # Open output file ... 

68 print(f"ERROR: Writing \"debug{prefix}Polygon{i:d}.interior{j:d}.csv\" ...") 

69 with open(f"debug{prefix}Polygon{i:d}.interior{j:d}.csv", "wt", encoding = "utf-8") as fObj: 

70 # Loop over bad coordinates in this interior ring of this bad 

71 # Polygon ... 

72 for badCoord in interior.coords: 

73 # Write output ... 

74 fObj.write(f"{badCoord[0]:.15e},{badCoord[1]:.15e}\n")