diff --git a/Cargo.toml b/Cargo.toml index c1e3b91..5cb2bdb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ members = [ "proj4rs", "proj4rs-clib", - "proj4rs-php", + "proj4rs-php", ] resolver = "2" @@ -23,6 +23,6 @@ strip = "debuginfo" [patch.crates-io] # Use local crates -proj4rs = { path = "proj4rs" } +proj4rs = { path = "./proj4rs" } diff --git a/js/.docker/Dockerfile b/js/.docker/Dockerfile index 3de7364..c880d40 100644 --- a/js/.docker/Dockerfile +++ b/js/.docker/Dockerfile @@ -1,3 +1,3 @@ # syntax=docker/dockerfile:1 -FROM node:19-bullseye-slim +FROM node:22-bookworm-slim diff --git a/js/tests/nodejs/proj4rs.js b/js/tests/nodejs/proj4rs.js new file mode 100644 index 0000000..4e8574a --- /dev/null +++ b/js/tests/nodejs/proj4rs.js @@ -0,0 +1,3 @@ +// Statements to load wasm module in nodejs REPL +let Proj +import("../../pkg-node/proj4rs.js").then(module => { Proj = module }); diff --git a/js/tests/nodejs/run_tests.js b/js/tests/nodejs/run_tests.js new file mode 100644 index 0000000..e69de29 diff --git a/js/tests/nodejs/test_core.js b/js/tests/nodejs/test_core.js new file mode 100644 index 0000000..eaa52cd --- /dev/null +++ b/js/tests/nodejs/test_core.js @@ -0,0 +1,23 @@ +let Proj = await import("../../pkg-node/proj4rs.js"); + +function test_core() { + //EPSG:3006 + var sweref99tm = '+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs'; + // EPSG:3021 + var rt90 = '+lon_0=15.808277777799999 +lat_0=0.0 +k=1.0 +x_0=1500000.0 +y_0=0.0 +proj=tmerc +ellps=bessel +units=m +towgs84=414.1,41.3,603.1,-0.855,2.141,-7.023,0 +no_defs'; + let from = new Proj.Projection(sweref99tm); + let to = new Proj.Projection(rt90); + let point = new Proj.Point(319180, 6399862, 0.0); + Proj.transform(from, to, point); + console.log(`=> ${point.x} ${point.y}`); + + var epsg2154 = '+proj=lcc +lat_0=46.5 +lon_0=3 +lat_1=49 +lat_2=44 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs'; + var epsg3857 = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs'; + from = new Proj.Projection(epsg2154); + to = new Proj.Projection(epsg3857); + point = new Proj.Point(489353.59, 6587552.2, 0.0); + Proj.transform(from, to, point); + console.log(`=> ${point.x} ${point.y}`); +} + +test_core(); diff --git a/proj4rs-clib/Cargo.toml b/proj4rs-clib/Cargo.toml index 3a55619..b34ed00 100644 --- a/proj4rs-clib/Cargo.toml +++ b/proj4rs-clib/Cargo.toml @@ -17,7 +17,7 @@ name = "proj4rs_c" crate-type = ["cdylib"] [dependencies] -proj4rs = "*" +proj4rs = "~0.1" libc = { version = "0.2" } [features] diff --git a/proj4rs-php/Cargo.toml b/proj4rs-php/Cargo.toml index 8a54ddf..f493a4f 100644 --- a/proj4rs-php/Cargo.toml +++ b/proj4rs-php/Cargo.toml @@ -14,8 +14,8 @@ name = "proj4rs_php" crate-type = ["cdylib"] [dependencies] -proj4rs = "*" -ext-php-rs = "*" +proj4rs = "~0.1" +ext-php-rs = "0.12" log = { version = "0.4", optional = true } [features] diff --git a/proj4rs/Cargo.toml b/proj4rs/Cargo.toml index 3d1b467..40e160c 100644 --- a/proj4rs/Cargo.toml +++ b/proj4rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "proj4rs" -version = "0.1.5" +version = "0.1.6" edition = "2021" description = "Rust adaptation of Proj4" readme = "../README.md" @@ -31,7 +31,6 @@ log = { version = "0.4", optional = true } [dev-dependencies] approx = "0.5" clap = { version = "4", features = ["derive"] } -criterion = { version = "0.5" } env_logger = "0.11" log = "0.4" @@ -60,10 +59,6 @@ js-sys = "0.3" web-sys = { version = "0.3", features = ["console"] } console_log = "1.0" -[[bench]] -name = "bench_proj" -harness = false - [[example]] name = "rsproj" diff --git a/proj4rs/Makefile.toml b/proj4rs/Makefile.toml index 66f81d3..70e0502 100644 --- a/proj4rs/Makefile.toml +++ b/proj4rs/Makefile.toml @@ -78,3 +78,17 @@ args = [ "--features=proj4js-compat", ] +[tasks.wasm_node] +description = "Build wasm package as nodejs bundle" +command = "wasm-pack" +args = [ + "build", + "--out-dir=../js/pkg-node", + "--release", + "--target=nodejs", + "--no-default-features", + "--features=logging", + "--features=proj4js-compat", + "--features=with-wasm-entrypoint", +] + diff --git a/proj4rs/benches/bench_proj.rs b/proj4rs/benches/bench_proj.rs deleted file mode 100644 index 097d3bd..0000000 --- a/proj4rs/benches/bench_proj.rs +++ /dev/null @@ -1,38 +0,0 @@ -//! -//! Compare loop vs try_fold -//! -//! -use proj4rs::proj::Proj; -use proj4rs::transform::transform; - -use criterion::{black_box, criterion_group, criterion_main, Criterion}; - -fn etmerc_transform(itermax: usize) { - let d = 1.0 / (itermax as f64); - - let mut data: Vec<(f64, f64, f64)> = (1..=itermax) - .map(|i| { - ( - (-2.0f64 + (i as f64) * 4.0 * d).to_radians(), - (-1.0f64 + (i as f64) * 2.0 * d).to_radians(), - //2.0f64.to_radians(), - //1.0f64.to_radians(), - 0., - ) - }) - .collect(); - - let from = Proj::from_proj_string("+proj=latlong +ellps=GRS80").unwrap(); - let to = Proj::from_proj_string("+proj=etmerc +ellps=GRS80").unwrap(); - - transform(&from, &to, data.as_mut_slice()).unwrap(); -} - -fn criterion_benchmark_proj(c: &mut Criterion) { - c.bench_function("tmerc forward", |b| { - b.iter(|| etmerc_transform(black_box(10_000usize))) - }); -} - -criterion_group!(benches, criterion_benchmark_proj); -criterion_main!(benches); diff --git a/proj4rs/src/wasm/mod.rs b/proj4rs/src/wasm/mod.rs index 79f1484..6ff0bf2 100644 --- a/proj4rs/src/wasm/mod.rs +++ b/proj4rs/src/wasm/mod.rs @@ -3,7 +3,7 @@ //! mod nadgrids; -use crate::{errors, proj, transform}; +use crate::{errors, proj, transform, transform::TransformClosure}; use wasm_bindgen::prelude::*; use crate::log; @@ -14,6 +14,8 @@ use crate::log; pub fn main() { #[cfg(feature = "logging")] console_log::init_with_level(log::Level::Trace).unwrap(); + + log::info!("Initialized proj4rs wasm module.") } // ---------------------------- @@ -74,9 +76,9 @@ impl Projection { // --------------------------- #[wasm_bindgen] pub struct Point { - x: f64, - y: f64, - z: f64, + pub x: f64, + pub y: f64, + pub z: f64, } #[wasm_bindgen] @@ -85,31 +87,6 @@ impl Point { pub fn new(x: f64, y: f64, z: f64) -> Point { Self { x, y, z } } - - #[wasm_bindgen(getter)] - pub fn x(&self) -> f64 { - self.x - } - #[wasm_bindgen(setter)] - pub fn set_x(&mut self, x: f64) { - self.x = x; - } - #[wasm_bindgen(getter)] - pub fn y(&self) -> f64 { - self.y - } - #[wasm_bindgen(setter)] - pub fn set_y(&mut self, y: f64) { - self.y = y; - } - #[wasm_bindgen(getter)] - pub fn z(&self) -> f64 { - self.z - } - #[wasm_bindgen(setter)] - pub fn set_z(&mut self, z: f64) { - self.z = z; - } } impl transform::Transform for Point { @@ -117,9 +94,7 @@ impl transform::Transform for Point { /// as soon as with have invalid coordinates or /// that the reprojection failed #[cfg(feature = "wasm-strict")] - fn transform_coordinates(&mut self, f: &mut F) -> errors::Result<()> - where - F: FnMut(f64, f64, f64) -> errors::Result<(f64, f64, f64)>, + fn transform_coordinates(&mut self, f: &mut F) -> errors::Result<()> { f(self.x, self.y, self.z).map(|(x, y, z)| { self.x = x; @@ -131,9 +106,7 @@ impl transform::Transform for Point { /// of projection failure /// Note: this is what is expected mostly from js app (at least OpenLayer) #[cfg(not(feature = "wasm-strict"))] - fn transform_coordinates(&mut self, f: &mut F) -> errors::Result<()> - where - F: FnMut(f64, f64, f64) -> errors::Result<(f64, f64, f64)>, + fn transform_coordinates(&mut self, f: &mut F) -> errors::Result<()> { f(self.x, self.y, self.z) .map(|(x, y, z)| {