-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_gpkgs.py
More file actions
61 lines (47 loc) · 1.68 KB
/
merge_gpkgs.py
File metadata and controls
61 lines (47 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Reads a directory of geopackages and
merges all of the files into a single output geopackage.
usage: merge_gpkgs.py /path/to/gpkgs output.gpkg
"""
# Requires Python 3.5+
# This is just doing an ogr2ogr call again.
# It could be a shell script, but this is cross-platform.
# standard imports
import argparse
import os
import glob
import subprocess
# set some overall ogr2ogr options
opts_ogr2ogr = ["-f", "GPKG",
"-progress",
"-append"]
# geopackage driver options
opts_gpkg = ["-dsco", "VERSION=1.2",
"-lco", "OVERWRITE=YES",
"-gt", "65536"]
# parse the arguments from the command line
parser = argparse.ArgumentParser(
description='Merge a directory of geopackages to a single output.')
parser.add_argument('src_dir', type=str,
help='Source directory of geopackages')
# this output should either be a directory or a PostgreSQL endpoint
parser.add_argument('dest_gpkg', type=str, help="""Path to output
geopackage file.
""")
args = parser.parse_args()
# clean this up so they're standardized
src = os.path.abspath(args.src_dir)
dest = os.path.abspath(args.dest_gpkg)
if not os.path.isdir(src):
raise ValueError('Source directory does not exist.')
gpkg_paths = glob.glob(os.path.join(src, "*.gpkg"))
if not gpkg_paths:
raise ValueError('No geopackages found in source directory.')
if os.path.exists(dest):
print('Destination geopackage exists.')
for gpkg in gpkg_paths:
print('Merging {}...'.format(os.path.split(gpkg)[1]))
subprocess.run(["ogr2ogr"] + opts_ogr2ogr + opts_gpkg
+ [dest, gpkg])