From 65be28592becbad1f893b15e32b50203797f06c2 Mon Sep 17 00:00:00 2001 From: Bengt Ljungquist Date: Fri, 6 Feb 2026 10:56:09 -0500 Subject: [PATCH] Refactor and fix various issues in microjson2vt module - Update __init__ method in AbstractProjector to accept None as default for bounds - Clean up tile creation logic in MicroJsonVt by removing redundant code - Correct type checks in add_feature and shift_feature_coords functions - Improve getbounds function logic for square bounding box calculation - Remove unused import in utils.py and streamline future result handling --- .gitignore | 1 + src/microjson/microjson2vt/convert.py | 2 +- src/microjson/microjson2vt/microjson2vt.py | 9 ---- src/microjson/microjson2vt/tile.py | 2 +- src/microjson/microjson2vt/wrap.py | 4 +- src/microjson/tilewriter.py | 5 ++- src/microjson/utils.py | 49 ++++++++++------------ 7 files changed, 31 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index 7c05e45..6cf3ddb 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ dist/ *.zip testdata/ *.ipynb_checkpoints +data/ diff --git a/src/microjson/microjson2vt/convert.py b/src/microjson/microjson2vt/convert.py index f6c8634..ed37038 100644 --- a/src/microjson/microjson2vt/convert.py +++ b/src/microjson/microjson2vt/convert.py @@ -33,7 +33,7 @@ class AbstractProjector(ABC): Concrete classes should implement the project_x and project_y methods. """ - def __init__(self, bounds): + def __init__(self, bounds=None): self.bounds = bounds @abstractmethod diff --git a/src/microjson/microjson2vt/microjson2vt.py b/src/microjson/microjson2vt/microjson2vt.py index 3d13e87..0e9f7cd 100644 --- a/src/microjson/microjson2vt/microjson2vt.py +++ b/src/microjson/microjson2vt/microjson2vt.py @@ -253,7 +253,6 @@ def split_tile(self, features, z, x, y, cz=None, cx=None, cy=None): if tile is None: # Use simplified geometries for this zoom level - simplified_features = [ { **feature, @@ -262,14 +261,6 @@ def split_tile(self, features, z, x, y, cz=None, cx=None, cy=None): for feature in features ] - self.tiles[id_] = create_tile(features, z, x, y, options) - tile = self.tiles[id_] - self.tile_coords.append({'z': z, 'x': x, 'y': y}) - - key = f'z{z}' - self.stats[key] = self.stats.get(key, 0) + 1 - self.total += 1 - self.tiles[id_] = create_tile( simplified_features, z, x, y, options) tile = self.tiles[id_] diff --git a/src/microjson/microjson2vt/tile.py b/src/microjson/microjson2vt/tile.py index a0dd6d7..c6c6e91 100644 --- a/src/microjson/microjson2vt/tile.py +++ b/src/microjson/microjson2vt/tile.py @@ -41,7 +41,7 @@ def add_feature(tile, feature, tolerance, options): tile['maxX'] = max(tile['maxX'], feature['maxX']) tile['maxY'] = max(tile['maxY'], feature['maxY']) - if type_ == 'Point' or type == 'MultiPoint': + if type_ == 'Point' or type_ == 'MultiPoint': for i in range(0, len(geom), 3): simplified.append(geom[i]) simplified.append(geom[i + 1]) diff --git a/src/microjson/microjson2vt/wrap.py b/src/microjson/microjson2vt/wrap.py index b2dc27c..89a1761 100644 --- a/src/microjson/microjson2vt/wrap.py +++ b/src/microjson/microjson2vt/wrap.py @@ -40,9 +40,9 @@ def shift_feature_coords(features, offset): # new_geometry = None new_geometry = [] - if type_ == 'Pint' or type_ == 'MultiPint' or type_ == 'LineString': + if type_ == 'Point' or type_ == 'MultiPoint' or type_ == 'LineString': new_geometry = shift_coords(feature.get('geometry'), offset) - elif type_ == 'MultiLineSting' or type_ == 'Polygon': + elif type_ == 'MultiLineString' or type_ == 'Polygon': new_geometry = [] for line in feature.get('geometry'): new_geometry.append(shift_coords(line, offset)) diff --git a/src/microjson/tilewriter.py b/src/microjson/tilewriter.py index 9342d5c..963c3d8 100644 --- a/src/microjson/tilewriter.py +++ b/src/microjson/tilewriter.py @@ -54,8 +54,9 @@ def getbounds(microjson_file: str, square: bool = False) -> List[float]: maxx = max(maxx, coord[0]) maxy = max(maxy, coord[1]) if square: - maxx = max(maxx - minx, maxy - miny) + minx - maxy = max(maxx - minx, maxy - miny) + miny + side = max(maxx - minx, maxy - miny) + maxx = side + minx + maxy = side + miny return [minx, miny, maxx, maxy] diff --git a/src/microjson/utils.py b/src/microjson/utils.py index 84e2656..18f4db0 100644 --- a/src/microjson/utils.py +++ b/src/microjson/utils.py @@ -8,7 +8,6 @@ import re import shutil from concurrent.futures import ThreadPoolExecutor -from concurrent.futures import as_completed from itertools import product import skimage as sk @@ -114,19 +113,18 @@ def _tile_read(self) -> None: x, y, ) - if as_completed(future): # type: ignore - label, coordinates = future.result() - - if len(label) and len(coordinates) > 0: - label = [i + idx for i in range( - 1, len(label) + 1)] - idx = 0 - if len(label) == 1: - idx += label[0] - else: - idx += label[-1] - self.polygons_to_microjson( - i, label, coordinates) + label, coordinates = future.result() + + if len(label) and len(coordinates) > 0: + label = [i + idx for i in range( + 1, len(label) + 1)] + idx = 0 + if len(label) == 1: + idx += label[0] + else: + idx += label[-1] + self.polygons_to_microjson( + i, label, coordinates) else: future = executor.submit( @@ -135,18 +133,17 @@ def _tile_read(self) -> None: x, y, ) - if as_completed(future): # type: ignore - label, coordinates = future.result() - if len(label) and len(coordinates) > 0: - label = [i + idx for i in range( - 1, len(label) + 1)] - idx = 0 - if len(label) == 1: - idx += label[0] - else: - idx += label[-1] - self.polygons_to_microjson( - i, label, coordinates) + label, coordinates = future.result() + if len(label) and len(coordinates) > 0: + label = [i + idx for i in range( + 1, len(label) + 1)] + idx = 0 + if len(label) == 1: + idx += label[0] + else: + idx += label[-1] + self.polygons_to_microjson( + i, label, coordinates) def get_line_number(self, filename, target_string) -> int: line_number = 0