diff options
author | Guilhem Moulin <guilhem@fripost.org> | 2023-10-01 20:30:50 +0200 |
---|---|---|
committer | Guilhem Moulin <guilhem@fripost.org> | 2023-10-01 20:32:14 +0200 |
commit | 9abfea72dfa8f97accd208fc6c01605af7bafe7b (patch) | |
tree | bb5f4508aebcd1c8d728b8597316696529c6a174 /gis-observation-map | |
parent | 17833f47adcb9813400aa4dfe7f4783a15047ee5 (diff) |
New option --geometry-layername to make layer name configurable.
Diffstat (limited to 'gis-observation-map')
-rwxr-xr-x | gis-observation-map | 108 |
1 files changed, 59 insertions, 49 deletions
diff --git a/gis-observation-map b/gis-observation-map index 5c1c94b..bc1ab73 100755 --- a/gis-observation-map +++ b/gis-observation-map @@ -72,7 +72,8 @@ class geometryAction(argparse.Action): items = getattr(namespace, self.dest, None) items = items.copy() style = getattr(namespace, 'geometry_style', None) # get current style - items.append({'path': values, 'style': style}) + layername = getattr(namespace, 'geometry_layername', None) # get current layer name + items.append({'path': values, 'style': style, 'layername': layername}) setattr(namespace, self.dest, items) parser = argparse.ArgumentParser( @@ -90,6 +91,8 @@ parser.add_argument('--topo-basedir', default=config['QGIS']['topo-basedir'], ty parser_geom = parser.add_argument_group('Geographic area of interest') parser_geom.add_argument('--geometry-style', type=ePath, dest='geometry_style', metavar='STYLE_FILE', help='QGIS Layer Style File (*.qml) to apply to subsequent geometry files') +parser_geom.add_argument('--geometry-layername', dest='geometry_layername', metavar='NAME', + help='Layer names for subsequent geometry files') parser_geom.add_argument('--geometry', nargs='*', default=[], type=ePath, metavar='GEOMETRY_FILE', action=geometryAction, help='Geometry file of interest') parser_geom.add_argument('--point', nargs='*', metavar='Y,X', default=[], @@ -171,11 +174,15 @@ if projectInstance is not None: sourceGroup.setItemVisibilityChecked(False) for i, geom in enumerate(args.geometry): - path = geom['path'] - ds = ogr.Open(geom['path'].as_posix(), update=0) - layername = ds.GetLayer().GetName() - ds = None - layer = QgsVectorLayer(path.as_posix(), layername, 'ogr') + path = geom['path'].as_posix() + layername = geom['layername'] + if layername is None: + ds = ogr.Open(path, update=0) + layername = ds.GetLayer().GetName() + ds = None + else: + path = f'{path}|layername={layername}' + layer = QgsVectorLayer(path, layername.title(), 'ogr') if not layer.isValid(): raise Exception(f'ERROR: {path}: failed to load in QGIS') @@ -290,52 +297,55 @@ for pt in args.point: for geom in args.geometry: ds = ogr.Open(geom['path'].as_posix(), update=0) - for i in range(ds.GetLayerCount()): - layer = ds.GetLayerByIndex(i) - source_srs = layer.GetSpatialRef() - transform_srs = osr.CoordinateTransformation(source_srs, target_srs) + if geom['layername'] is None: + layer = ds.GetLayer() + else: + layer = ds.GetLayerByName(geom['layername']) - feature = layer.GetNextFeature() - while feature is not None: - geometry = feature.GetGeometryRef() - if not geometry.IsValid(): - raise Exception('Invalid geometry') - geometryType = geometry.GetGeometryType() - geometry = geometry.Clone() - geometry.FlattenTo2D() - geometry.Transform(transform_srs) + source_srs = layer.GetSpatialRef() + transform_srs = osr.CoordinateTransformation(source_srs, target_srs) - if args.margin is not None: - minX, maxX, minY, maxY = geometry.GetEnvelope() - minX -= args.margin - maxX += args.margin - minY -= args.margin - maxY += args.margin - - ring = ogr.Geometry(ogr.wkbLinearRing) - ring.AddPoint_2D(minX, maxY) - ring.AddPoint_2D(maxX, maxY) - ring.AddPoint_2D(maxX, minY) - ring.AddPoint_2D(minX, minY) - ring.AddPoint_2D(minX, maxY) - - bbox = ogr.Geometry(ogr.wkbPolygon) - bbox.AddGeometry(ring) - clipGeometry = clipGeometry.Union(bbox) - elif geometryType == ogr.wkbPoint or geometryType == ogr.wkbMultiPoint: - parser.error('Point geometry requires positive --margin') - elif geometryType == ogr.wkbLineString or geometryType == ogr.wkbMultiLineString: - parser.error('Line geometry requires positive --margin') - elif geometryType == ogr.wkbPolygon: - clipGeometry = clipGeometry.Union(geometry) - elif geometryType == ogr.wkbMultiPolygon: - for i in range(geometry.GetGeometryCount()): - geom2 = geometry.GetGeometryRef(i) - clipGeometry = clipGeometry.Union(geom2) - else: - raise Exception('Unsuported geometry (try --margin=0)') + feature = layer.GetNextFeature() + while feature is not None: + geometry = feature.GetGeometryRef() + if not geometry.IsValid(): + raise Exception('Invalid geometry') + geometryType = geometry.GetGeometryType() + geometry = geometry.Clone() + geometry.FlattenTo2D() + geometry.Transform(transform_srs) - feature = layer.GetNextFeature() + if args.margin is not None: + minX, maxX, minY, maxY = geometry.GetEnvelope() + minX -= args.margin + maxX += args.margin + minY -= args.margin + maxY += args.margin + + ring = ogr.Geometry(ogr.wkbLinearRing) + ring.AddPoint_2D(minX, maxY) + ring.AddPoint_2D(maxX, maxY) + ring.AddPoint_2D(maxX, minY) + ring.AddPoint_2D(minX, minY) + ring.AddPoint_2D(minX, maxY) + + bbox = ogr.Geometry(ogr.wkbPolygon) + bbox.AddGeometry(ring) + clipGeometry = clipGeometry.Union(bbox) + elif geometryType == ogr.wkbPoint or geometryType == ogr.wkbMultiPoint: + parser.error('Point geometry requires positive --margin') + elif geometryType == ogr.wkbLineString or geometryType == ogr.wkbMultiLineString: + parser.error('Line geometry requires positive --margin') + elif geometryType == ogr.wkbPolygon: + clipGeometry = clipGeometry.Union(geometry) + elif geometryType == ogr.wkbMultiPolygon: + for i in range(geometry.GetGeometryCount()): + geom2 = geometry.GetGeometryRef(i) + clipGeometry = clipGeometry.Union(geom2) + else: + raise Exception('Unsuported geometry (try --margin=0)') + + feature = layer.GetNextFeature() if not clipGeometry.IsValid(): raise Exception('Invalid geometry') |