Crates.io | proj4rs |
lib.rs | proj4rs |
version | 0.1.3 |
source | src |
created_at | 2023-06-08 14:30:47.415027 |
updated_at | 2024-05-18 15:21:40.404715 |
description | Rust adaptation of Proj4 |
homepage | https://github.com/3liz/proj4rs/ |
repository | https://github.com/3liz/proj4rs/ |
max_upload_size | |
id | 885525 |
size | 279,150 |
Rust library for transforming geographic point coordinates from one coordinate system to another. This is a pure Rust implementation of the PROJ.4 project.
The documentation is available on docs.rs and the demo on docs.3liz.org.
wasm32-unknown-unknown
target.libproj
or sqlite3
is needed.Define the coordinate system with proj strings and use the transform
function.
You can easily get the projection string of any coordinate system
from EPSG.io.
Note: Proj4rs use radians as natural angular unit (as does the original proj library)
Example:
use proj4rs;
use proj4rs::proj::Proj;
// EPSG:5174 - Example
let from = Proj::from_proj_string(concat!(
"+proj=tmerc +lat_0=38 +lon_0=127.002890277778",
" +k=1 +x_0=200000 +y_0=500000 +ellps=bessel",
" +towgs84=-145.907,505.034,685.756,-1.162,2.347,1.592,6.342",
" +units=m +no_defs +type=crs"
))
.unwrap();
// EPSG:4326 - WGS84, known to us as basic longitude and latitude.
let to = Proj::from_proj_string(concat!(
"+proj=longlat +ellps=WGS84",
" +datum=WGS84 +no_defs"
))
.unwrap();
let mut point_3d = (198236.3200000003, 453407.8560000006, 0.0);
proj4rs::transform::transform(&from, &to, &mut point_3d).unwrap();
// Note that WGS84 output from this library is in radians, not degrees.
point_3d.0 = point_3d.0.to_degrees();
point_3d.1 = point_3d.1.to_degrees();
// Output in longitude, latitude, and height.
println!("{} {}",point_3d.0, point_3d.1); // 126.98069676435814, 37.58308534678718
If you need full support for WKT, please rely on proj
which provides
a great implementation of the standards.
If you want WKT support in WASM, please have a look at:
Nadgrid support is still experimental. Currently, only Ntv2 multi grids are supported for native build and WASM.
When compiled for WASM, the library exposes JavaScript API that is very similar to that of proj4js. A thin JavaScript layer provides full compatibility with proj4js and thus can be used as a proj4js replacement.
Example:
let from = new Proj.Projection("+proj=latlong +ellps=GRS80");
let to = new Proj.Projection("+proj=etmerc +ellps=GRS80");
let point = new Proj.Point(2.0, 1.0, 0.0);
// Point is transformed in place
Proj.transform(from, to, point);
Install wasm-pack
wasm-pack build --target web --no-default-features
Or if you have installed cargo-make, use the following command:
cargo make wasm
There is a [index.html
] file for testing the WASM module in a navigator.
For security reasons, you need to run it from a server. You can start a Python server with the following command:
python3 -m http.server
The server will automatically serve the index.html
file in the current directory.
cargo make wasm_bundle
This will create a npm bundler package in pkg-bundler