This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshp2gpkg.py
62 lines (47 loc) · 1.65 KB
/
shp2gpkg.py
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
62
#!/usr/bin/env python
# coding: utf-8
"""Grovel directories containing ESRI ShapeFile to create GeoPackage layers"""
import argparse
import os
import re
from os import walk
from pathlib import Path
import pandas as pd
from pyogrio import read_dataframe, write_dataframe
pd.set_option("display.max_columns", None)
PARSER = argparse.ArgumentParser(
description="Create ESRI shp data as layers in a GeoPackage file"
)
PARSER.add_argument(
"--output", type=str, default="geo-model.gpkg", nargs="?", help="output filename"
)
PARSER.add_argument("--search", type=str, default=".", nargs="?", help="search path")
PARSER.add_argument(
"--crs",
type=str,
default="EPSG:32630",
nargs="?",
help="coordinate reference system",
)
ARGS, REST = PARSER.parse_known_intermixed_args()
REST = (REST + [None] * 3)[:3]
OUTPATH, FILEPATH, CRS = [(i or j) for i, j in zip(REST, vars(ARGS).values())]
def list_files(filepath, match):
"""find all filenames containing match in directories under filepath"""
files = ()
for d, _, filenames in walk(filepath):
if "geopandas/datasets" in d:
continue
files = files + tuple(f"{d}/{f}" for f in filenames if match in f)
return files
PATTERNS = [re.compile(i, re.IGNORECASE) for i in ["shapefile", "shape$", "file$"]]
def get_layername(filepath):
"""return layer name from shape-filepath"""
r = Path(filepath)
return r.parent.stem
FILES = [f for f in list_files(FILEPATH, "shp") if f[-4:] == ".shp"]
for f in FILES:
gf = read_dataframe(f)
layername = get_layername(f)
print(f"layer: {layername}")
write_dataframe(gf.to_crs(CRS), OUTPATH, layer=layername)