|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +"""Checks if tile (z, x, y) contains inside regions (lua data file). |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +from collections import defaultdict |
| 9 | + |
| 10 | +from pyosm.point import LatLong, zxy_to_latlong |
| 11 | +from pyosm.polygon import Polygon |
| 12 | + |
| 13 | + |
| 14 | +def parse_args(): |
| 15 | + p = argparse.ArgumentParser(description=__doc__) |
| 16 | + p.add_argument("-i", "--input", type=str, required=True, help="input lua data file") |
| 17 | + p.add_argument("-z", type=int, required=True, help="z coord") |
| 18 | + p.add_argument("-x", type=int, required=True, help="x coord") |
| 19 | + p.add_argument("-y", type=int, required=True, help="y coord") |
| 20 | + return p.parse_args() |
| 21 | + |
| 22 | + |
| 23 | +def parse_lua_data_file(filepath): |
| 24 | + polygons = defaultdict(list) |
| 25 | + section = 0 |
| 26 | + with open(filepath, "rb") as f: |
| 27 | + for line in f.readlines(): |
| 28 | + if line.strip() in ["local region = {", "return region"]: |
| 29 | + continue |
| 30 | + |
| 31 | + if line.strip() == "{": |
| 32 | + section += 1 |
| 33 | + |
| 34 | + if line.strip().startswith("{lon="): |
| 35 | + lon = line.split(",")[0].split("=")[1] |
| 36 | + lat = line.split(",")[1].split("=")[1][:-1] |
| 37 | + polygons[section].append(LatLong(lat=float(lat), long=float(lon))) |
| 38 | + return polygons |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + args = parse_args() |
| 43 | + |
| 44 | + ll = zxy_to_latlong(args.z, args.x, args.y) |
| 45 | + polygons = parse_lua_data_file(args.input) |
| 46 | + for k, points in polygons.items(): |
| 47 | + polygon = Polygon(points) |
| 48 | + if ll in polygon: |
| 49 | + print("{key}: {point} in {polygon}".format(key=k, point=ll, polygon=polygon)) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments