Skip to content

Commit

Permalink
pass params to processor, meshingMethod grid size now configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
tebben committed Jan 25, 2024
1 parent dd8e25a commit c9efdb9
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 60 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ docker run -p 5000:5000 -v ./ctod_cache:/cache -e CTOD_PORT=5000 -CTOD_LOGGING_L

### V1.0 (In progress)

- Pass processor options
- Pydelatin and/or Martini support
- Refactoring
- Cleanup viewer code
- Wiki Optimizing COG/Performance
- Test big VRT

### Future work (V1.1)

Expand Down Expand Up @@ -98,6 +99,7 @@ Returns a sample Cesium viewer, all values can be changed using the control pane
- **resamplingMethod** : Resampling method for COG: 'nearest', 'bilinear', 'cubic', 'cubic_spline', 'lanczos', 'average', 'mode', 'gauss', 'rms'. Default 'bilinear'
- **cog** (required): Path or URL to COG file.
- **ignoreCache** : Set to true to prevent loading tiles from the cache. Default (False)
- **meshingMethod**: The Meshing method to use: 'grid', 'delatin'

#### Example

Expand Down Expand Up @@ -140,6 +142,12 @@ Get a quantized mesh for tile index z, x, y. Set the minZoom value to retrieve e
- **resamplingMethod** : Resampling method for COG: 'nearest', 'bilinear', 'cubic', 'cubic_spline', 'lanczos', 'average', 'mode', 'gauss', 'rms'. Default 'bilinear'
- **cog** (required): Path or URL to COG file.
- **ignoreCache** : Set to true to prevent loading tiles from the cache. Default (False)
- **meshingMethod**: The Meshing method to use: 'grid', 'delatin'

#### Parameters for meshing method: grid

- **defaultGridSize**: The default grid size (amount of rows/cols) to use if there is no specific zoomGridSizes defined for a requested tile, Default (20)
- **zoomGridSizes**: Per level defined grid size, when requested zoom for tile not specified use defaultGridSize. Default ({"15": 25, "16": 25, "17": 30, "18": 35, "19": 35, "20": 35, "21": 35, "22": 35})

#### Example

Expand Down
4 changes: 3 additions & 1 deletion ctod/core/cog/processor/cog_processor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from abc import ABC, abstractmethod

class CogProcessor(ABC):

class CogProcessor(ABC):
"""Abstract base class for CogProcessors."""

@abstractmethod
def process(self, cog_request):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ctod.core.normals import calculate_normals
from ctod.core.utils import rescale_positions
from pydelatin import Delatin
from tornado import web
from quantized_mesh_encoder.constants import WGS84
from quantized_mesh_encoder.ecef import to_ecef
from quantized_mesh_encoder.ellipsoid import Ellipsoid
Expand All @@ -14,7 +15,8 @@ class CogProcessorQuantizedMeshDelatin(CogProcessor):
- Calculate normals
"""

def __init__(self):
def __init__(self, request: web.RequestHandler):
super().__init__()
self.ellipsoid: Ellipsoid = WGS84

def process(self, cog_request: CogRequest) -> tuple:
Expand Down
64 changes: 52 additions & 12 deletions ctod/core/cog/processor/cog_processor_quantized_mesh_grid.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import json
import logging
import numpy as np

from ctod.core.cog.cog_request import CogRequest
from ctod.core.cog.processor.cog_processor import CogProcessor
from ctod.core.cog.processor.grid import generate_grid
from ctod.core.normals import calculate_normals
from ctod.core.utils import rescale_positions
from tornado import web
from quantized_mesh_encoder.constants import WGS84
from quantized_mesh_encoder.ecef import to_ecef
from quantized_mesh_encoder.ellipsoid import Ellipsoid


class CogProcessorQuantizedMeshGrid(CogProcessor):
"""A CogProcessor for a grid based mesh.
Expand All @@ -19,8 +23,11 @@ class CogProcessorQuantizedMeshGrid(CogProcessor):
ToDo: Grids can be stored in a cache, however generating a grid takes 0.7ms on average
"""

def __init__(self):
def __init__(self, request: web.RequestHandler):
super().__init__()
self.grid_wh = 255
self.ellipsoid: Ellipsoid = WGS84
self._load_settings(request)

def process(self, cog_request: CogRequest) -> tuple:
"""Process a CogRequest and return the vertices, triangles and normals.
Expand All @@ -32,7 +39,8 @@ def process(self, cog_request: CogRequest) -> tuple:
tuple: Generated vertices, triangles and normals
"""

vertices2d, triangles = self.get_grid(25, 25)
grid_size = self._get_grid_size(cog_request.z)
vertices2d, triangles = self._get_grid(grid_size, grid_size)
height_data_indices = np.floor(vertices2d).astype(int)

height_data = cog_request.data.data[0][255 - height_data_indices[:, 1], height_data_indices[:, 0]]
Expand All @@ -46,8 +54,41 @@ def process(self, cog_request: CogRequest) -> tuple:
normals = calculate_normals(cartesian, triangles_new) if cog_request.generate_normals else None

return (vertices_new, triangles_new, normals)

def get_grid(self, num_rows: int, num_cols: int) -> tuple:

def _load_settings(self, request: web.RequestHandler):
"""Parse the config
Args:
config (dict): The config
"""

self.default_grid_size = int(request.get_argument_ignore_case("defaultGridSize", default=20))
self.zoom_grid_sizes = {"15": 25, "16": 25, "17": 30, "18": 35, "19": 35, "20": 35, "21": 35, "22": 35}

zoomGridSizesString = request.get_argument_ignore_case("zoomGridSizes", default=None)
if zoomGridSizesString:
try:
self.zoom_grid_sizes = json.loads(zoomGridSizesString)
except json.JSONDecodeError as e:
logging.warning("Error parsing zoomGridSizes:")

def _get_grid_size(self, zoom: int) -> int:
"""Get the grid size for a specific zoom level
Args:
zoom (int): The zoom level
Returns:
int: The grid size
"""

zoom = str(zoom)
if self.zoom_grid_sizes is not None and zoom in self.zoom_grid_sizes:
return self.zoom_grid_sizes[zoom]
else:
return self.default_grid_size

def _get_grid(self, num_rows: int, num_cols: int) -> tuple:
"""Generate a grid of vertices and triangles
Args:
Expand All @@ -58,14 +99,13 @@ def get_grid(self, num_rows: int, num_cols: int) -> tuple:
tuple: vertices, triangles
"""

width, height = 255, 255

if num_rows > height:
num_rows = height
if num_rows > self.grid_wh:
num_rows = self.grid_wh

if num_cols > width:
num_cols = width
if num_cols > self.grid_wh:
num_cols = self.grid_wh

generated_grid = generate_grid(width, height, num_rows, num_cols)
generated_grid = generate_grid(self.grid_wh, self.grid_wh, num_rows, num_cols)

return generated_grid
return generated_grid

33 changes: 0 additions & 33 deletions ctod/core/settings.py

This file was deleted.

2 changes: 1 addition & 1 deletion ctod/handlers/terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _get_cog_processor(self, meshing_method: str) -> CogProcessor:
"""

cog_processor = self.cog_processors.get(meshing_method, self.cog_processors["default"])
return cog_processor()
return cog_processor(self)

def _return_empty_terrain(self, tms: TileMatrixSet, cog: str, meshing_method: str, resampling_method, z: int, x: int, y: int):
"""Return an empty terrain tile
Expand Down
1 change: 1 addition & 0 deletions ctod/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Pane } from "https://cdn.jsdelivr.net/npm/[email protected]/dist/tweakpane.min.js";
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cesium/1.106.0/Cesium.js"></script>
<script src="./static/utils.js"></script>
<script src="./static/index.js"></script>
<script src="./static/controls.js"></script>
<script src="./static/slopeshade.js"></script>
Expand Down
9 changes: 4 additions & 5 deletions ctod/templates/static/controls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var module, pane, terrainFolder, layerFolder, materialFolder, urlParams;
var module, pane, terrainFolder, layerFolder, materialFolder;

var minZoomValue = 1;
var maxZoomValue = 21;
Expand All @@ -13,7 +13,6 @@ document.addEventListener("DOMContentLoaded", async () => {
module = await import(
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tweakpane.min.js"
);
urlParams = new URLSearchParams(window.location.search);
setupTweakpane();
loadCesium();
} catch (error) {
Expand Down Expand Up @@ -223,13 +222,13 @@ function updateTerrainProvider() {
}

function getStringParameterValue(param, defaultValue) {
return urlParams.get(param) || defaultValue;
return getUrlParamIgnoreCase(param) || defaultValue;
}

function getIntParameterValue(param, defaultValue) {
return urlParams.get(param) ? parseInt(urlParams.get(param)) : defaultValue;
return getUrlParamIgnoreCase(param) ? parseInt(getUrlParamIgnoreCase(param)) : defaultValue;
}

function getBoolParameterValue(param, defaultValue) {
return urlParams.get(param) ? urlParams.get(param).toLowerCase() === "true" ? true : false : defaultValue;
return getUrlParamIgnoreCase(param) ? getUrlParamIgnoreCase(param).toLowerCase() === "true" ? true : false : defaultValue;
}
11 changes: 5 additions & 6 deletions ctod/templates/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ function initializeLayers() {
gridLayer.show = false;
coordinateLayer.show = false;

const urlParams = new URLSearchParams(window.location.search);
const minZoom = urlParams.get("minZoom") || 1;
const maxZoom = urlParams.get("maxZoom") || 21;
const minZoom = getUrlParamIgnoreCase("minZoom") || 1;
const maxZoom = getUrlParamIgnoreCase("maxZoom") || 21;
const cog =
urlParams.get("cog") ||
getUrlParamIgnoreCase("cog") ||
"./ctod/files/test_cog.tif";
const skipCache = urlParams.get("skipCache") || false;
const meshingMethod = urlParams.get("meshingMethod") || "grid";
const skipCache = getUrlParamIgnoreCase("skipCache") || false;
const meshingMethod = getUrlParamIgnoreCase("meshingMethod") || "grid";
setTerrainProvider(minZoom, maxZoom, cog, "bilinear", skipCache, meshingMethod);

streetsLayer.show = true;
Expand Down
10 changes: 10 additions & 0 deletions ctod/templates/static/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function getUrlParamIgnoreCase(name) {
const urlParams = new URLSearchParams(window.location.search);
for (const [key, value] of urlParams.entries()) {
if (key.toLowerCase() === name.toLowerCase()) {
return value;
}
}
return null;
}

0 comments on commit c9efdb9

Please sign in to comment.