add mean values to create svg

This commit is contained in:
admin 2026-02-01 14:33:06 -08:00
parent 8dbba6368b
commit 3725b6960e

View File

@ -1,12 +1,15 @@
import os import os
import shutil import shutil
import sys import sys
from PIL import Image, ImageOps
from country_iso2ify import get_resolver from country_iso2ify import get_resolver
import pycountry import pycountry
from country_bounding_boxes import country_subunits_by_iso_code from country_bounding_boxes import country_subunits_by_iso_code
import requests import requests
import ee import ee
import numpy as np
import pandas as pd
import cairocffi as cairo
class Topology: class Topology:
def __init__(self, def __init__(self,
@ -20,7 +23,8 @@ class Topology:
linemap_cols=30, linemap_cols=30,
linemap_width=1.0, linemap_width=1.0,
linemap_roundcap=True, linemap_roundcap=True,
linemap_transparent=False linemap_transparent=False,
storage_directory=None,
): ):
self.country = country self.country = country
self.refetch_elevation_data = refetch_elevation_data self.refetch_elevation_data = refetch_elevation_data
@ -35,6 +39,7 @@ class Topology:
self.subregion_mode = subregion_mode self.subregion_mode = subregion_mode
self.target_region_name = None self.target_region_name = None
self.elevation_filename = None self.elevation_filename = None
self.storage_directory = storage_directory
if self.country is None and self.iso_country is None: if self.country is None and self.iso_country is None:
self.country = "Switzerland" self.country = "Switzerland"
@ -106,6 +111,32 @@ class Topology:
shutil.copyfileobj(response.raw, out_file) shutil.copyfileobj(response.raw, out_file)
del response 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): def create_svg_file(self, rotation=0,file_index=None):
# if no refetch, check if file exists # if no refetch, check if file exists
if os.path.exists(self.elevation_filename): if os.path.exists(self.elevation_filename):
@ -119,7 +150,7 @@ class Topology:
def main(): def main():
topology = Topology(country="France", topology = Topology(country="Switzerland",
subregion_mode=False) subregion_mode=False)
topology.list_subunits() topology.list_subunits()
print(topology.get_boundingbox()) print(topology.get_boundingbox())