quadtreegrid.py 2.91 KB
Newer Older
1
2
#!/usr/bin/env python3

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Copyright (c) 2021:
#   Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.


20
21
import logging
import sys
22
23
24
import geopandas as gpd
import mercantile
import argparse
25
26
27
28
29

# Add a logger printing error, warning, info and debug messages to the screen
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
logger.info("Launching quadtreegrid")

parser = argparse.ArgumentParser(
    description="""Get all quadkeys from given bounds
                                            or get the bounds of a vector file"""
)
parser.add_argument(
    "-in_bounds", type=float, nargs="+", help="Bounds in order West,South,East,North"
)
parser.add_argument("-in_file", type=str, help="Shapefile with a given geometry")
parser.add_argument(
    "-zoom", type=int, default=18, nargs="?", help="Set zoom level of output tiles, default=18"
)
parser.add_argument(
    "-output",
    type=str,
    nargs="?",
    default="./output_quadkeys.txt",
    help="""Set the output text or CSV filepath,
                    default=./output_quadkeys.txt""",
)
args = parser.parse_args()

Nicolas Garcia Ospina's avatar
black    
Nicolas Garcia Ospina committed
53

54
55
56
57
58
59
60
61
62
63
def main():
    if args.in_bounds:
        sw_lon, sw_lat, ne_lon, ne_lat = args.in_bounds
        print(ne_lat)
    elif args.in_file:
        outline = gpd.read_file(args.in_file).to_crs("epsg:4326")
        geometry = outline.envelope.values[0]
        sw_lon, sw_lat, ne_lon, ne_lat = geometry.bounds
    else:
        raise ValueError("No input geometry found")
64

65
66
67
68
69
70
71
72
    if args.zoom:
        zoom = args.zoom
    else:
        zoom = 18
    if args.output:
        output = args.output
    else:
        output = "./output_quadkeys.txt"
73

74
75
76
77
    logger.info(
        "Zoom level {} quadkeys will be created for the bounding "
        "box {} {} {} {}.".format(zoom, sw_lon, sw_lat, ne_lon, ne_lat)
    )
78

79
80
81
82
83
84
85
86
87
    tiles_generator = mercantile.tiles(sw_lon, sw_lat, ne_lon, ne_lat, zoom)

    logger.info("Writing to {}".format(output))
    outfile = open(output, "w")
    if output[-3:] == "csv":
        outfile.write("quadkey\n")
    for tile in tiles_generator:
        outfile.write(mercantile.quadkey(tile) + "\n")
    outfile.close()
88

89
90
    # Example logging output
    logger.info("quadtreegrid is closing")
91
92
93
    # Leave the program
    sys.exit()

Nicolas Garcia Ospina's avatar
black    
Nicolas Garcia Ospina committed
94

95
96
if __name__ == "__main__":
    main()