diff --git a/topologytool.py b/topologytool.py index 8192664..6828978 100644 --- a/topologytool.py +++ b/topologytool.py @@ -1,12 +1,15 @@ import os import shutil import sys - +from PIL import Image, ImageOps from country_iso2ify import get_resolver import pycountry from country_bounding_boxes import country_subunits_by_iso_code import requests import ee +import numpy as np +import pandas as pd +import cairocffi as cairo class Topology: def __init__(self, @@ -20,7 +23,8 @@ class Topology: linemap_cols=30, linemap_width=1.0, linemap_roundcap=True, - linemap_transparent=False + linemap_transparent=False, + storage_directory=None, ): self.country = country self.refetch_elevation_data = refetch_elevation_data @@ -35,6 +39,7 @@ class Topology: self.subregion_mode = subregion_mode self.target_region_name = None self.elevation_filename = None + self.storage_directory = storage_directory if self.country is None and self.iso_country is None: self.country = "Switzerland" @@ -106,6 +111,32 @@ class Topology: shutil.copyfileobj(response.raw, out_file) del response + def convert_png_to_data(self): + elevation_data = Image.open(self.elevation_filename).convert('L') + frame_image = Image.new(mode='L', size=(1224,1224), color=0) + frame_image.paste(elevation_data, + ((1224 - elevation_data.size[0]) // 2, + (1224 - elevation_data.size[1]) // 2)) + elevation_data = frame_image + + width, height = elevation_data.size + grid_w, grid_h = self.linemap_rows, self.linemap_cols + + # Calculate cell dimensions + cell_w = width // grid_w + cell_h = height // grid_h + + # Calculate mean values for each cell + # future improve -- only average non-zero values to get coastlines and edges right + values = np.zeros((grid_h, grid_w)) + for r in range(grid_h): + for c in range(grid_w): + # Define box (left, upper, right, lower) + box = (c * cell_w, r * cell_h, (c + 1) * cell_w, (r + 1) * cell_h) + cell = elevation_data.crop(box) + values[r, c] = np.mean(np.array(cell)) + return values + def create_svg_file(self, rotation=0,file_index=None): # if no refetch, check if file exists if os.path.exists(self.elevation_filename): @@ -119,7 +150,7 @@ class Topology: def main(): - topology = Topology(country="France", + topology = Topology(country="Switzerland", subregion_mode=False) topology.list_subunits() print(topology.get_boundingbox())