diff options
author | Guilhem Moulin <guilhem@fripost.org> | 2023-09-28 20:58:28 +0200 |
---|---|---|
committer | Guilhem Moulin <guilhem@fripost.org> | 2023-09-28 21:15:26 +0200 |
commit | e6279c5edc9a2610719b246f7d3034f225940991 (patch) | |
tree | 24d88e6ce4c356a63092263f9b6d6b950b9006e2 | |
parent | 554374bb368eff5f40648901971972409dea2e11 (diff) |
Merge topographic tiles if possible.
QGIS struggles (eps. memory-wise) when a projects has many small layers.
Merging all ‘Topografi 10’ tiles of interest yields a much smoother
experience.
-rwxr-xr-x | gis-observation-map | 73 |
1 files changed, 54 insertions, 19 deletions
diff --git a/gis-observation-map b/gis-observation-map index 96eacfa..11f4473 100755 --- a/gis-observation-map +++ b/gis-observation-map @@ -819,26 +819,55 @@ if args.topo_basedir is not None and projectInstance is not None: idxContains = {} for topo in topo_maps: qlr_paths = [] + qlr_merged = None d = args.topo_basedir.joinpath(topo) if not d.is_dir(): continue - if args.project_home is not None: - l = args.project_home.joinpath(topo) - l.symlink_to(d, target_is_directory=True) - d = l - idx = d.joinpath('index.geojson') if idx.is_file(): tiles, idxContains[topo] = idx_intersects(idx.as_posix(), geometries) - for tile in tiles: - t = d.joinpath(tile) - qlr_path = find_qlr(t) + if len(tiles) == 0: + continue + + if args.project_home is None: + for tile in tiles: + t = d.joinpath(tile) + qlr_path = find_qlr(t) + if qlr_path is None: + continue + qlr_paths.append(qlr_path) + else: + t0 = d.joinpath(tiles[0]) + qlr_path = find_qlr(d) if qlr_path is None: continue - qlr_paths.append(qlr_path) + + d2 = args.project_home.joinpath(topo) + d2.mkdir(mode=0o755, exist_ok=True) + d2.joinpath(qlr_path.name).symlink_to(qlr_path, target_is_directory=False) + qlr_merged = d2.joinpath(qlr_path.name) + + gpkgs = set() + for tile in tiles: + for gpkg in d.joinpath(tile).glob('*.gpkg'): + gpkgs.add(gpkg.name) + + for gpkg in gpkgs: + dst = d2.joinpath(gpkg) + if dst.exists(): + raise Exception(f'{dst}: file exists') + for tile in tiles: + src = d.joinpath(tile).joinpath(gpkg) + if src.is_file(): + gdal.VectorTranslate(dst.as_posix(), src.as_posix(), format='GPKG', accessMode='upsert') else: + if args.project_home is not None: + l = args.project_home.joinpath(topo) + l.symlink_to(d, target_is_directory=True) + d = l + for t in sorted(d.glob('*')): qlr_path = find_qlr(t) if qlr_path is None: @@ -855,15 +884,22 @@ if args.topo_basedir is not None and projectInstance is not None: qlr_paths.append(qlr_path) - if projectInstance is not None and len(qlr_paths) > 0: - rootGroup = layerTreeRoot.addGroup(topo) - for qlr_path in qlr_paths: - QgsLayerDefinition.loadLayerDefinition(qlr_path.as_posix(), projectInstance, rootGroup) - lyrSubTree = rootGroup.children() - if len(lyrSubTree) == len(qlr_paths): - for i, lt in enumerate(lyrSubTree): - name = qlr_paths[i].parent.stem - lt.setName(name) + if projectInstance is not None: + if qlr_merged is not None: + QgsLayerDefinition.loadLayerDefinition(qlr_merged.as_posix(), projectInstance, layerTreeRoot) + lt = layerTreeRoot.children()[-1] + lt.setName(topo) + lyrSubTree = [lt] + elif len(qlr_paths) > 0: + rootGroup = layerTreeRoot.addGroup(topo) + for qlr_path in qlr_paths: + QgsLayerDefinition.loadLayerDefinition(qlr_path.as_posix(), projectInstance, rootGroup) + lyrSubTree = rootGroup.children() + rootGroup.setExpanded(False) + if len(lyrSubTree) == len(qlr_paths): + for i, lt in enumerate(lyrSubTree): + name = qlr_paths[i].parent.stem + lt.setName(name) for lt in lyrSubTree: lt.setExpanded(False) lt.setItemVisibilityChecked(True) @@ -872,7 +908,6 @@ if args.topo_basedir is not None and projectInstance is not None: lyr = lyr.layer() lyr.setReadOnly(True) lyr.setFlags(QgsMapLayer.Removable) - rootGroup.setExpanded(False) if projectInstance is not None: maxScale = 1 |