|
1 | 1 | # System imports
|
2 | 2 | import os
|
3 | 3 | import sys
|
4 |
| -from pathlib import Path |
5 | 4 |
|
6 | 5 | # Third-party imports
|
7 | 6 | from osgeo import osr
|
8 | 7 | from osgeo import gdal
|
9 | 8 | from osgeo import ogr
|
10 | 9 | from datetime import datetime
|
11 | 10 |
|
12 |
| -# Rtree installation |
| 11 | +# Rtree should be installed by the plugin for QGIS |
| 12 | +# For ArcGIS Pro the following is needed |
| 13 | +import sys |
| 14 | +from pathlib import Path |
13 | 15 | from .rtree_installer import unpack_rtree
|
| 16 | +if not str(Path(__file__).parent) in sys.path: # bgt_inlooptool\\core |
| 17 | + rtree_path = unpack_rtree() |
| 18 | + sys.path.append(str(rtree_path)) |
14 | 19 |
|
15 |
| -unpack_rtree() |
16 |
| -sys.path.append(str(Path(__file__).parent.parent)) |
17 | 20 | import rtree
|
18 | 21 |
|
19 | 22 | # Local imports
|
@@ -744,60 +747,44 @@ def clean_surfaces(self):
|
744 | 747 | Multipolygons, multisurfaces, curved polygons are forced to polygon.
|
745 | 748 |
|
746 | 749 | """
|
747 |
| - for stype in ALL_USED_SURFACE_TYPES: |
748 |
| - lyr = self.mem_database.GetLayerByName(stype) |
749 |
| - if ( |
750 |
| - lyr is None |
751 |
| - ): # this happens if this particular layer in the bgt input has no features |
| 750 | + for surface_type in ALL_USED_SURFACE_TYPES: |
| 751 | + layer = self.mem_database.GetLayerByName(surface_type) |
| 752 | + if layer is None: # this happens if this particular layer in the bgt input has no features |
752 | 753 | continue
|
753 |
| - lyr.ResetReading() |
| 754 | + layer.ResetReading() |
754 | 755 | delete_fids = []
|
755 |
| - for f in lyr: |
756 |
| - geom = f.GetGeometryRef() |
| 756 | + for feature in layer: |
| 757 | + geom = feature.GetGeometryRef() |
757 | 758 | geom_type = geom.GetGeometryType()
|
758 | 759 | if geom_type == ogr.wkbPolygon:
|
759 | 760 | pass
|
760 |
| - elif geom_type == ogr.wkbCurvePolygon: |
| 761 | + elif geom_type in [ogr.wkbCurvePolygon, ogr.wkbMultiSurface]: |
761 | 762 | # print('Fixing Curve Polygon feature {}'.format(f.GetFID()))
|
762 | 763 | geom_linear = geom.GetLinearGeometry()
|
763 |
| - f.SetGeometry(geom_linear) |
764 |
| - lyr.SetFeature(f) |
765 |
| - elif geom_type in [ogr.wkbMultiSurface, ogr.wkbMultiPolygon]: |
766 |
| - # print('Fixing MultiSurface or MultiPolygon feature {}'.format(f.GetFID())) |
767 |
| - geom_fixed = ogr.ForceToPolygon(geom) |
768 |
| - geom_fixed = geom_fixed.MakeValid() |
769 |
| - if geom_fixed.GetGeometryType() == ogr.wkbMultiPolygon: |
770 |
| - for subgeom in geom_fixed: |
771 |
| - if subgeom.GetGeometryType() == ogr.wkbPolygon: |
772 |
| - cf = f.Clone() |
773 |
| - cf.SetGeometry(subgeom) |
774 |
| - lyr.SetFeature(cf) |
775 |
| - else: |
776 |
| - f.SetGeometry(geom_fixed) |
777 |
| - lyr.SetFeature(f) |
778 |
| - |
| 764 | + feature.SetGeometry(geom_linear) |
| 765 | + layer.SetFeature(feature) |
779 | 766 | elif geom_type in (
|
780 | 767 | ogr.wkbLineString,
|
781 | 768 | ogr.wkbCompoundCurve,
|
782 | 769 | ogr.wkbCircularString,
|
783 | 770 | ):
|
784 | 771 | # print('Deleting feature {} because it is a Linestring'.format(f.GetFID()))
|
785 |
| - delete_fids.append(f.GetFID()) |
| 772 | + delete_fids.append(feature.GetFID()) |
786 | 773 | else:
|
787 | 774 | print(
|
788 | 775 | "Warning: Fixing feature {fid} in {stype} failed! No procedure defined to clean up geometry "
|
789 | 776 | "type {geom_type}. Continuing anyway.".format(
|
790 |
| - fid=f.GetFID(), stype=stype, geom_type=str(geom_type) |
| 777 | + fid=feature.GetFID(), stype=surface_type, geom_type=str(geom_type) |
791 | 778 | )
|
792 | 779 | )
|
793 | 780 | continue
|
794 | 781 | for fid in delete_fids:
|
795 |
| - lyr.DeleteFeature(fid) |
| 782 | + layer.DeleteFeature(fid) |
796 | 783 | print(
|
797 |
| - f"cleaned import of {stype} layer has {lyr.GetFeatureCount()} features" |
| 784 | + f"cleaned import of {surface_type} layer has {layer.GetFeatureCount()} features" |
798 | 785 | )
|
799 | 786 |
|
800 |
| - lyr = None |
| 787 | + layer = None |
801 | 788 |
|
802 | 789 | def classify_pipes(self, delete=True):
|
803 | 790 | """Assign pipe type based on GWSW pipe type. Optionally, delete pipes of type INTERNAL_PIPE_TYPE_IGNORE"""
|
|
0 commit comments