From 8dbba6368bce8dcd85989a1f722b1f92ded6c070 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 31 Jan 2026 19:51:48 -0800 Subject: [PATCH] more function to class Topology --- .idea/dictionaries/project.xml | 1 + get_ee_from_region.py | 1 + topologytool.py | 92 ++++++++++++++++++++++++++++------ 3 files changed, 80 insertions(+), 14 deletions(-) diff --git a/.idea/dictionaries/project.xml b/.idea/dictionaries/project.xml index 73eb773..9543c35 100644 --- a/.idea/dictionaries/project.xml +++ b/.idea/dictionaries/project.xml @@ -1,6 +1,7 @@ + boundingbox linemap diff --git a/get_ee_from_region.py b/get_ee_from_region.py index d9f1384..fa2c75a 100755 --- a/get_ee_from_region.py +++ b/get_ee_from_region.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 ''' This is a program to get a url from Google ee api based on supplied region''' from timerlog import Timerlog +from topologytool import Topology import logging import stat diff --git a/topologytool.py b/topologytool.py index 05772ef..8192664 100644 --- a/topologytool.py +++ b/topologytool.py @@ -1,14 +1,19 @@ +import os +import shutil +import sys from country_iso2ify import get_resolver import pycountry from country_bounding_boxes import country_subunits_by_iso_code - - +import requests +import ee class Topology: def __init__(self, country=None, iso_country=None, + subregion_mode=False, + refetch_elevation_data=False, subregion=None, linemap_size=(800,800), linemap_rows=30, @@ -18,6 +23,7 @@ class Topology: linemap_transparent=False ): self.country = country + self.refetch_elevation_data = refetch_elevation_data self.iso_country = iso_country self.subregion = subregion self.linemap_size = linemap_size @@ -26,17 +32,25 @@ class Topology: self.linemap_width = linemap_width self.linemap_roundcap = linemap_roundcap self.linemap_transparent = linemap_transparent - if self.subregion is None: - self.subregion = self.country + self.subregion_mode = subregion_mode + self.target_region_name = None + self.elevation_filename = None + if self.country is None and self.iso_country is None: self.country = "Switzerland" if self.country is None: self.country = pycountry.countries.get(alpha_2=self.iso_country).name self.subregion = self.country - return if self.iso_country is None: self.iso_country = get_resolver().resolve(self.country) - return + if self.subregion_mode is False: + self.target_region_name=self.country + self.subregion = self.country + else: + self.target_region_name=self.subregion + self.elevation_filename = self.target_region_name+".png" + print("Elevation file name is [{}]".format(self.elevation_filename)) + def list_subunits(self): countries = country_subunits_by_iso_code(self.iso_country) @@ -46,9 +60,9 @@ class Topology: for item in countries: print("Subunit of [{}]: [{}]".format(self.country, item.subunit)) - def get_mainland_bbox(self): + def get_boundingbox(self): countries = country_subunits_by_iso_code(self.iso_country) - print("bbox--Country is [{}] iso code is [{}]".format(self.country, self.iso_country)) + print("bbox--Country is [{}] iso code is [{}] subregion is [{}]".format(self.country, self.iso_country, self.subregion)) # The library is designed to return the main body of the country # in the first result or by filtering for largest area. item = None @@ -65,13 +79,63 @@ class Topology: return item.bbox return None -if __name__ == "__main__": - topology = Topology(country="France") + def create_elevation_file(self): + ee.Initialize(project="arizona-topo") + elv = ee.Image('USGS/SRTMGL1_003') + boundingbox = self.get_boundingbox() + region_of_interest = ee.Geometry.BBox(boundingbox[0], + boundingbox[1], + boundingbox[2], + boundingbox[3]) + countries = ee.FeatureCollection('FAO/GAUL/2015/level1').select('ADM0_NAME') + # logging.debug("Countries are [{}]".format(countries)) + states = ee.FeatureCollection('FAO/GAUL/2015/level1').select('ADM1_NAME') + + target_boundary = countries.filter(ee.Filter.eq('ADM0_NAME', self.target_region_name)) + elevation_image = elv.updateMask(elv.gt(0)) + elevation_clip = elevation_image.clip(target_boundary) + url = elevation_clip.getThumbUrl({ + 'min': -300, 'max': 8500, 'region': region_of_interest, 'dimensions': 1024, + 'crs': 'EPSG:4326', + 'fileFormat': 'GeoTIFF', + }) + print("url is [{}]".format(url)) + print("Downloading image from [{}]".format(url)) + response = requests.get(url, stream=True) + with open(self.elevation_filename, 'wb') as out_file: + shutil.copyfileobj(response.raw, out_file) + del response + + def create_svg_file(self, rotation=0,file_index=None): + # if no refetch, check if file exists + if os.path.exists(self.elevation_filename): + print("Elevation file exists") + if self.refetch_elevation_data: + print("Refetching elevation data requested") + self.create_elevation_file() + else: + print("Elevation file does not exist, fetching") + self.create_elevation_file() + + +def main(): + topology = Topology(country="France", + subregion_mode=False) topology.list_subunits() - print(topology.get_mainland_bbox()) - t2=Topology(country="United Kingdom",subregion="Wales") + print(topology.get_boundingbox()) + topology.create_svg_file(rotation=0) + + sys.exit(0) + + t2=Topology(country="United Kingdom", + subregion_mode=True, + subregion="Wales") t2.list_subunits() - print(t2.get_mainland_bbox()) + print(t2.get_boundingbox()) + t3=Topology(iso_country="US") t3.list_subunits() - print(t3.get_mainland_bbox()) \ No newline at end of file + print(t3.get_boundingbox()) + +if __name__ == "__main__": + main() \ No newline at end of file