Skip to content

Commit

Permalink
Cli children update (#165)
Browse files Browse the repository at this point in the history
* fix children to be fast and not create all tiles in memory

* fix children and clippy

* fix unused fn

* cargo update
  • Loading branch information
jessekrubin authored Sep 23, 2024
1 parent 96f965e commit 22490bd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 13 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion crates/utiles-core/src/tile_like.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::bbox::WebBBox;
use crate::{flipy, pmtiles, xyz2rmid, BBox, LngLat, Tile};
use crate::{flipy, pmtiles, xyz2rmid, BBox, LngLat, Tile, TileZBox};

/// Trait def for tile-like objects/structs/things/whatevers
pub trait TileLike {
Expand Down Expand Up @@ -203,4 +203,22 @@ pub trait TileLike {
flipy(self.y(), self.z())
)
}

/// return zbox for tile-like
#[must_use]
fn zbox(&self) -> TileZBox {
TileZBox::from_tile(self)
}

/// Return children-zbox for tile-like at optional depth (default 1)
#[must_use]
fn children_zbox(&self, depth: Option<u8>) -> TileZBox {
let d = depth.unwrap_or(1);
let target_zoom = self.z() + d;
let mut zbox = TileZBox::from_tile(self);
while zbox.z() < target_zoom {
zbox = zbox.zoom_in();
}
zbox
}
}
37 changes: 37 additions & 0 deletions crates/utiles-core/src/tile_zbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ impl TileZBox {
self.max.y
}

/// Return the zoom level
#[must_use]
pub fn z(&self) -> u8 {
self.zoom
}

/// Return the zoom level
#[must_use]
pub fn zoom(&self) -> u8 {
Expand Down Expand Up @@ -123,6 +129,37 @@ impl TileZBox {
pub fn mbtiles_sql_where(&self) -> String {
self.mbtiles_sql_where_prefix(None)
}

/// Create zbox from tile
#[must_use]
pub fn from_tile<T: TileLike + ?Sized>(tile: &T) -> Self {
Self {
zoom: tile.z(),
min: Point2d::new(tile.x(), tile.y()),
max: Point2d::new(tile.x(), tile.y()),
}
}

/// Return new zbox one zoom level higher/down z2 -> z3
#[must_use]
pub fn zoom_in(&self) -> Self {
Self {
zoom: self.zoom + 1,
min: Point2d::new(self.min.x * 2, self.min.y * 2),
max: Point2d::new(self.max.x * 2 + 1, self.max.y * 2 + 1),
}
}

/// Return new zbox one zoom level lower/up z3 -> z2
#[must_use]
pub fn zoom_depth(&self, depth: u8) -> Self {
let target_zoom = self.zoom + depth;
let mut zbox = *self;
while zbox.zoom < target_zoom {
zbox = zbox.zoom_in();
}
zbox
}
}

impl TileZBoxIterator {
Expand Down
6 changes: 4 additions & 2 deletions crates/utiles/src/cli/commands/children_parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ pub fn parent_main(args: ParentChildrenArgs) -> UtilesResult<()> {

pub fn children_main(args: ParentChildrenArgs) -> UtilesResult<()> {
let lines = stdinterator_filter::stdin_filtered(args.inargs.input);
let rs = if args.fmtopts.seq { "\x1e\n" } else { "" };
for line in lines {
let lstr = line?.trim_matches(|c| c == '"' || c == '\'').to_string();
let tile = Tile::from_json(&lstr)?;
let children = tile.children(Option::from(tile.z + args.depth));
let tile_zbox = tile.children_zbox(Option::from(args.depth));

let children = tile_zbox.into_iter().map(Tile::from);
for child in children {
let rs = if args.fmtopts.seq { "\x1e\n" } else { "" };
println!("{}{}", rs, child.json_arr());
}
}
Expand Down
17 changes: 17 additions & 0 deletions utiles-pyo3/python/utiles/dev/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import utiles

try:
from orjson import loads as json_loads
except ImportError:
Expand Down Expand Up @@ -69,6 +71,21 @@ def parse_json(self) -> Any:
"""Parse json"""
return json_loads(self.stdout)

@property
def parse_jsonl(self) -> list[Any]:
"""Parse json"""
return [json_loads(line) for line in self.stdout.splitlines()]

@property
def parse_tiles(self) -> list[utiles.Tile]:
"""Parse tile lines"""
return [
utiles.xyz(arr[0], arr[1], arr[2])
for arr in (
json_loads(line) for line in self.stdout.splitlines(keepends=False)
)
]

def fmt(self) -> str:
return "\n".join(
(
Expand Down
14 changes: 10 additions & 4 deletions utiles-pyo3/tests/cli/test_mt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest

import utiles as ut
from utiles.dev.testing import run_cli as _run_cli


Expand Down Expand Up @@ -280,11 +281,16 @@ def test_cli_parent_multidepth(self) -> None:
class TestChildren:
def test_cli_children(self) -> None:
result = _run_cli(["children"], "[243, 166, 9]")

assert result.returncode == 0
assert (
result.stdout
== "[486, 332, 10]\n[487, 332, 10]\n[487, 333, 10]\n[486, 333, 10]\n"
)
json_tiles = result.parse_tiles
expected_tiles = [
(486, 332, 10),
(486, 333, 10),
(487, 332, 10),
(487, 333, 10),
]
assert set(json_tiles) == set((ut.xyz(*el) for el in expected_tiles))


# ===================
Expand Down

0 comments on commit 22490bd

Please sign in to comment.