/
index.py
92 lines (83 loc) · 3.23 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Create index for process output."""
import logging
from shapely import wkt
import tqdm
import yaml
import mapchete
from mapchete.config import _map_to_new_config
from mapchete.index import zoom_index_gen
from mapchete.tile import BufferedTilePyramid
# lower stream output log level
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
stream_handler.setLevel(logging.ERROR)
logging.getLogger().addHandler(stream_handler)
logger = logging.getLogger(__name__)
def index(args):
if args.debug:
logging.getLogger("mapchete").setLevel(logging.DEBUG)
stream_handler.setLevel(logging.DEBUG)
if not any([args.geojson, args.gpkg, args.txt]):
raise ValueError(
"one of 'geojson', 'gpkg', or 'txt' must be provided")
# process single tile
if args.tile:
conf = _map_to_new_config(
yaml.load(open(args.mapchete_file, "r").read()))
tile = BufferedTilePyramid(
conf["pyramid"]["grid"],
metatiling=conf["pyramid"].get("metatiling", 1),
pixelbuffer=conf["pyramid"].get("pixelbuffer", 0)
).tile(*args.tile)
with mapchete.open(
args.mapchete_file, mode="readonly", bounds=tile.bounds,
zoom=tile.zoom
) as mp:
out_dir = args.out_dir if args.out_dir else mp.config.output.path
for tile in tqdm.tqdm(
zoom_index_gen(
mp=mp,
zoom=tile.zoom,
out_dir=out_dir,
geojson=args.geojson,
gpkg=args.gpkg,
txt=args.txt,
fieldname=args.fieldname,
basepath=args.basepath,
for_gdal=args.for_gdal),
total=mp.count_tiles(tile.zoom, tile.zoom),
unit="tile",
disable=args.debug
):
logger.debug(tile)
else:
if args.wkt_geometry:
bounds = wkt.loads(args.wkt_geometry).bounds
else:
bounds = args.bounds
with mapchete.open(
args.mapchete_file, mode="readonly", zoom=args.zoom, bounds=bounds
) as mp:
out_dir = args.out_dir if args.out_dir else mp.config.output.path
logger.debug("process bounds: %s", mp.config.init_bounds)
logger.debug("process zooms: %s", mp.config.init_zoom_levels)
logger.debug("fieldname: %s", args.fieldname)
for z in mp.config.init_zoom_levels:
logger.debug("zoom %s", z)
for tile in tqdm.tqdm(
zoom_index_gen(
mp=mp,
zoom=z,
out_dir=out_dir,
geojson=args.geojson,
gpkg=args.gpkg,
txt=args.txt,
fieldname=args.fieldname,
basepath=args.basepath,
for_gdal=args.for_gdal),
total=mp.count_tiles(z, z),
unit="tile",
disable=args.debug
):
logger.debug(tile)