Skip to content

Commit

Permalink
speed up pixel inversion, alias nearest<->near, update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Jul 22, 2023
1 parent 1789806 commit 5c46e9b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
60 changes: 38 additions & 22 deletions geowarp.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,28 @@ const geowarp = function geowarp({

if (debug_level >= 1) console.log("[geowarp] method:", method);

// see if can create direct pixel affine transformation
// skipping over spatial reference system
let inverse_pixel = ([c, r]) => {
const x = out_xmin + c * out_sample_width + half_out_sample_width;
const y = out_ymax - r * out_sample_height - half_out_sample_height;
const pt_out_srs = [x, y];
const pt_in_srs = same_srs ? pt_out_srs : inverse(pt_out_srs);
const pt_in_img = in_srs_pt_to_in_img_pt(pt_in_srs).map(n => Math.floor(n));
return pt_in_img;
};

if (turbo) {
const { reproject } = turbocharge({
bbox: [0, 0, out_width, out_height],
debug_level: 0,
quiet: true,
reproject: inverse_pixel,
threshold: [half_out_sample_width, half_out_sample_height]
});
if (reproject) inverse_pixel = pt => reproject(pt).map(n => Math.round(n));
}

let forward_turbocharged, inverse_turbocharged;
if (turbo) {
if (forward) {
Expand Down Expand Up @@ -544,7 +566,7 @@ const geowarp = function geowarp({
// const [invCached, clearInvCache] = cacheFunction(inv);

let out_sample_height_in_srs, out_sample_width_in_srs, pixel_height_ratio, pixel_width_ratio;
if (method === "near-vectorize") {
if (method === "near-vectorize" || method === "nearest-vectorize") {
if (debug_level >= 2) console.log('[geowarp] choosing between "near" and "vectorize" for best speed');

out_bbox_in_srs ??= reprojectBoundingBox(out_bbox, inverse, { density: 100 });
Expand Down Expand Up @@ -659,20 +681,15 @@ const geowarp = function geowarp({
}
}
}
} else if (method === "near") {
} else if (method === "near" || method === "nearest") {
const rmax = Math.min(row_end, out_height_in_samples);
let y = out_ymax + half_out_sample_height - row_start * out_sample_height;
for (let r = row_start; r < rmax; r++) {
// if (clear_process_cache) clear_process_cache();
y -= out_sample_height;
const segments = segments_by_row[r];
for (let iseg = 0; iseg < segments.length; iseg++) {
const [cstart, cend] = segments[iseg];
for (let c = cstart; c <= cend; c++) {
const x = out_xmin + c * out_sample_width + half_out_sample_width;
const pt_out_srs = [x, y];
const pt_in_srs = same_srs ? pt_out_srs : inv(pt_out_srs);
const [x_in_raster_pixels, y_in_raster_pixels] = in_srs_pt_to_in_img_pt(pt_in_srs).map(n => Math.floor(n));
const [x_in_raster_pixels, y_in_raster_pixels] = inverse_pixel([c, r]);

let raw_values = [];

Expand All @@ -693,8 +710,6 @@ const geowarp = function geowarp({
column: c,
pixel,
raw: raw_values,
pt_in_srs,
pt_out_srs,
x_in_raster_pixels,
y_in_raster_pixels
});
Expand Down Expand Up @@ -909,18 +924,19 @@ const geowarp = function geowarp({
}
}

if (debug_level >= 1) console.log("[geowarp] took " + (performance.now() - start_time).toFixed(0) + "ms");

const generate_result = () => ({
data: out_data,
out_bands,
out_layout,
out_pixel_height,
out_pixel_width,
out_sample_height,
out_sample_width,
read_bands
});
const generate_result = () => {
if (debug_level >= 1) console.log("[geowarp] took " + (performance.now() - start_time).toFixed(0) + "ms");
return {
data: out_data,
out_bands,
out_layout,
out_pixel_height,
out_pixel_width,
out_sample_height,
out_sample_width,
read_bands
};
};

if (pending > 0) {
// async return
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,32 @@
"homepage": "https://github.com/DanielJDufour/geowarp#readme",
"devDependencies": {
"@mapbox/tilebelt": "^1.0.2",
"@types/node": "^18.16.3",
"@types/node": "^20.4.4",
"fast-counter": "^0.1.0",
"find-and-read": "^1.2.0",
"flug": "^2.6.0",
"geotiff": "1.0.9",
"geotiff-palette": "^0.1.0",
"geotiff-precise-bbox": "^0.2.0",
"geotiff-read-bbox": "^1.3.0",
"geotiff-read-bbox": "^2.0.2",
"pngjs": "^7.0.0",
"proj4-fully-loaded": "^0.2.0",
"quick-resolve": "^0.0.1",
"typescript": "^5.0.4",
"typescript": "^5.1.6",
"write-image": "^0.2.0"
},
"dependencies": {
"bbox-fns": "^0.8.0",
"bbox-fns": "^0.13.0",
"calc-image-stats": "^0.7.0",
"calc-stats": "^2.1.0",
"dufour-peyton-intersection": "^0.1.2",
"dufour-peyton-intersection": "^0.1.3",
"fast-max": "^0.4.0",
"fast-min": "^0.3.0",
"faster-median": "^1.0.0",
"geoaffine": "^0.2.0",
"get-depth": "^0.0.3",
"proj-turbo": "^0.0.1",
"reproject-bbox": "^0.10.0",
"quick-resolve": "^0.0.1",
"reproject-bbox": "^0.12.0",
"reproject-geojson": "^0.3.0",
"segflip": "^0.0.2",
"typed-array-ranges": "^0.0.0",
Expand Down

0 comments on commit 5c46e9b

Please sign in to comment.