From 1b2958426fd58c861fc968102ffc9ab113d45f54 Mon Sep 17 00:00:00 2001 From: DanielJDufour Date: Fri, 22 Dec 2023 16:45:37 -0500 Subject: [PATCH] added insert_null_strategy --- README.md | 6 ++++++ geowarp.d.ts | 1 + geowarp.js | 49 +++++++++++++++++++++++++++++-------------------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 95de95f..5a1520d 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,12 @@ const result = geowarp({ return pixel.map(value => Math.min(value, 100)); }, + // optional + // whether to insert or skip null values + // "skip" - default, don't insert null values + // "insert" - try to insert null values into output data + insert_null_strategy: "skip", + // optional // array of band indexes to read from // use this if your expr function only uses select bands diff --git a/geowarp.d.ts b/geowarp.d.ts index 6430db0..71148b4 100644 --- a/geowarp.d.ts +++ b/geowarp.d.ts @@ -52,6 +52,7 @@ export default function geowarp(options: { cutline_srs?: number | string | undefined; cutline_forward?: reproject | undefined; cutline_strategy?: "inside" | "outside" | string | undefined; + insert_null_strategy?: "skip" | "insert" | undefined; skip_no_data_strategy?: "any" | "all" | undefined; cache_process?: true | false | boolean | undefined; turbo?: boolean | undefined; diff --git a/geowarp.js b/geowarp.js index 098c775..10a14ca 100644 --- a/geowarp.js +++ b/geowarp.js @@ -155,6 +155,7 @@ const geowarp = function geowarp({ turbo = false, // enable experimental turbocharging via proj-turbo insert_pixel, // over-ride function that inserts data into output multi-dimensional array insert_sample, // over-ride function that inserts each sample into the output multi-dimensional array (calls insert) + insert_null_strategy = "skip", // whether to insert or skip null values skip_no_data_strategy, // skip processing pixels if "any" or "all" values are "no data" cache_process = false // whether to try to cache the processing step // cache_functions // this really helps if functions are asynchronous and require posting to a web worker @@ -669,18 +670,20 @@ const geowarp = function geowarp({ // apply band math expression, no-data mapping, and rounding when applicable const pixel = process({ pixel: raw_values }); - if (cutline) { - intersect_options.per_pixel = ({ row, column }) => { - if (segments_by_row[row].some(([start, end]) => column >= start && column <= end)) { + if (pixel !== null || insert_null_strategy === "insert") { + if (cutline) { + intersect_options.per_pixel = ({ row, column }) => { + if (segments_by_row[row].some(([start, end]) => column >= start && column <= end)) { + insert_sample({ raw: raw_values, pixel, row, column }); + } + }; + } else { + intersect_options.per_pixel = ({ row, column }) => { insert_sample({ raw: raw_values, pixel, row, column }); - } - }; - } else { - intersect_options.per_pixel = ({ row, column }) => { - insert_sample({ raw: raw_values, pixel, row, column }); - }; + }; + } + dufour_peyton_intersection.calculate(intersect_options); } - dufour_peyton_intersection.calculate(intersect_options); } } } @@ -710,14 +713,16 @@ const geowarp = function geowarp({ if (should_skip(raw_values)) continue; const pixel = process({ pixel: raw_values }); - insert_sample({ - row: r, - column: c, - pixel, - raw: raw_values, - x_in_raster_pixels, - y_in_raster_pixels - }); + if (pixel !== null || insert_null_strategy === "insert") { + insert_sample({ + row: r, + column: c, + pixel, + raw: raw_values, + x_in_raster_pixels, + y_in_raster_pixels + }); + } } } } @@ -815,7 +820,9 @@ const geowarp = function geowarp({ } if (should_skip(raw_values)) continue; const pixel = process({ pixel: raw_values }); - insert_sample({ row: r, column: c, pixel, raw: raw_values }); + if (pixel !== null || insert_null_strategy === "insert") { + insert_sample({ row: r, column: c, pixel, raw: raw_values }); + } } } } @@ -923,7 +930,9 @@ const geowarp = function geowarp({ if (should_skip(raw_values)) continue; const pixel = process({ pixel: raw_values }); - insert_sample({ row: r, column: c, pixel, raw: raw_values }); + if (pixel !== null || insert_null_strategy === "insert") { + insert_sample({ row: r, column: c, pixel, raw: raw_values }); + } } } }