133 lines
3.6 KiB
Python
Executable File
133 lines
3.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
''' This is a program to get a url from Google ee api based on supplied region'''
|
|
|
|
import logging
|
|
import stat
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import time
|
|
import pycountry
|
|
from country_bounding_boxes import country_subunits_by_iso_code
|
|
from country_iso2ify import get_resolver
|
|
|
|
resolver = get_resolver()
|
|
|
|
|
|
def get_mainland_bbox(country_name):
|
|
# Returns the main continental/primary bounding box
|
|
iso_code = resolver.resolve(country_name)
|
|
countries = country_subunits_by_iso_code(iso_code)
|
|
logging.debug("Country is [{}] iso code is [{}]".format(country_name,iso_code))
|
|
# The library is designed to return the main body of the country
|
|
# in the first result or by filtering for largest area.
|
|
item = None
|
|
if countries:
|
|
for item in countries:
|
|
logging.debug(item.subunit+str(item.bbox))
|
|
item = None
|
|
countries = country_subunits_by_iso_code(iso_code)
|
|
if countries:
|
|
for item in countries:
|
|
if item.subunit == country_name:
|
|
return item.bbox
|
|
return None
|
|
|
|
class Timerlog:
|
|
|
|
def __init__(self, name):
|
|
self.time_start = 0.0
|
|
self.time_end = 0.0
|
|
self.name = name
|
|
|
|
def start(self):
|
|
self.time_start = time.perf_counter()
|
|
return self
|
|
|
|
def end(self):
|
|
self.time_end = time.perf_counter()
|
|
return self
|
|
|
|
def report(self, duration_only=False):
|
|
if duration_only:
|
|
return "Timer [{}] duration [{:.5f}]".format(
|
|
self.name, self.time_end - self.time_start)
|
|
return "Timer [{}] begin [{:.5f}] end [{:.5f}] duration [{:.9f}]".format(
|
|
self.name, self.time_start, self.time_end, self.time_end - self.time_start)
|
|
|
|
program_name = 'get_ee_from_region.py'
|
|
|
|
parser = argparse.ArgumentParser(
|
|
prog=program_name,
|
|
description='get a url from google ee api based on supplied region',
|
|
epilog='Good luck')
|
|
|
|
parser.add_argument(
|
|
"-l",
|
|
"--logging",
|
|
dest='logging',
|
|
metavar='log_file',
|
|
required=False,
|
|
action='store',
|
|
help="log to file")
|
|
|
|
parser.add_argument(
|
|
"-v",
|
|
"--verbose",
|
|
dest='verbose',
|
|
required=False,
|
|
action='store_true',
|
|
help="verbose mode, log output goes to stderr")
|
|
|
|
parser.add_argument(
|
|
"-c",
|
|
"--country",
|
|
dest='country',
|
|
metavar='country',
|
|
default='US',
|
|
action='store',
|
|
help="country to get topo image for")
|
|
|
|
parser.add_argument(
|
|
"-r",
|
|
"--region",
|
|
dest='region',
|
|
metavar='region',
|
|
default='None',
|
|
action='store',
|
|
help="subregion to get, default is to get entire country")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
if args.logging or args.verbose:
|
|
handlers = []
|
|
if args.logging:
|
|
file_handler = logging.FileHandler(filename=args.logging)
|
|
handlers.append(file_handler)
|
|
if args.verbose:
|
|
stderr_handler = logging.StreamHandler(stream=sys.stderr)
|
|
handlers.append(stderr_handler)
|
|
logging.basicConfig(
|
|
encoding='utf-8',
|
|
handlers=handlers,
|
|
format='%(asctime)s.%(msecs)03d %(levelname)s {%(module)s} [%(funcName)s] %(message)s',
|
|
datefmt='%Y-%m-%d,%H:%M:%S',
|
|
level=logging.DEBUG)
|
|
if args.logging:
|
|
logging.debug('logging to log_file requested')
|
|
if args.verbose:
|
|
logging.debug('logging to stderr requested')
|
|
|
|
t = Timerlog("atomic test").start()
|
|
logging.debug(t.end().report())
|
|
|
|
logging.debug("Country is [{}]".format(args.country))
|
|
logging.debug("Region is [{}]".format(args.region))
|
|
|
|
iso_code = resolver.resolve(args.country)
|
|
logging.debug("iso_code for [{}] is [{}]".format(args.country, iso_code))
|
|
|
|
bbox=get_mainland_bbox(args.country)
|
|
logging.debug("BBOX is [{}]".format(bbox))
|