Add new function proj4ToProj4 to turf/projection#3046
Conversation
|
Thanks for putting this together @BenPortner. Overall would be supportive of this. Though the spec says geojson is WSG84 by default, seems reasonable if we offer projection services to one alternative (mercator) we should offer it to others. Couple of questions ...
|
|
Hello @smallsaucepan,
proj4 really just expects strings. The string can be a pre-defined name or a projection definition. proj4 exports a type Pre-defined names (from the docs): Projection definition type: export type ProjectionDefinition = {
title: string;
projName?: string;
ellps?: string;
datum?: import("./Proj.js").DatumDefinition;
datumName?: string;
rf?: number;
lat0?: number;
lat1?: number;
lat2?: number;
lat_ts?: number;
long0?: number;
long1?: number;
long2?: number;
alpha?: number;
longc?: number;
x0?: number;
y0?: number;
k0?: number;
a?: number;
b?: number;
R_A?: true;
zone?: number;
utmSouth?: true;
datum_params?: string | Array<number>;
to_meter?: number;
units?: string;
from_greenwich?: number;
datumCode?: string;
nadgrids?: string;
axis?: string;
sphere?: boolean;
rectified_grid_angle?: number;
approx?: boolean;
over?: boolean;
projStr?: string;
inverse: <T extends import("./core").TemplateCoordinates>(coordinates: T, enforceAxis?: boolean) => T;
forward: <T extends import("./core").TemplateCoordinates>(coordinates: T, enforceAxis?: boolean) => T;
};
That would be a plus of 23% on turf.min.js:
I setup a local ESM project with proj4 and it works just fine: package.json: {
"name": "deleteme",
"version": "1.0.0",
"private": true,
"type": "module",
"description": "Minimal Node.js ESM project using proj4",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js"
},
"dependencies": {
"proj4": "^2.20.4"
}
}index.js: import proj4 from "proj4";
// Convert from WGS84 (lon/lat) to Web Mercator
const wgs84 = "EPSG:4326";
const webMercator = "EPSG:3857";
const coordinate = [-74.006, 40.7128];
const projected = proj4(wgs84, webMercator, coordinate);
console.log("Input (EPSG:4326):", coordinate);
console.log("Output (EPSG:3857):", projected);Console: C:\Users\BNPR\code\deleteme> npm run start
> deleteme@1.0.0 start
> node src/index.js
Input (EPSG:4326): [ -74.006, 40.7128 ]
Output (EPSG:3857): [ -8238310.235647004, 4970071.579142423 ] |
|
👋 Thanks for putting this together. I'm kind of curious what your use-case is for having Turf as an intermediary for proj4 instead of using it directly, perhaps leveraging |
|
Hi @mfedderly, Legitimate question! My use case is here: https://github.com/BenPortner/geojson-atlas Of course I can use Turfjs and Proj4 separately, but it would be more convenient to have them integrated, especially since Turfjs already has a projection module. The current module seems somewhat underpowered. Since I did the integration for my project, I thought I'd share it with the world. Of course I leave it up to you to decide if the increase in package size is acceptable. |
This PR adds a new function
proj4ToProj4to theturf-projectionpackage. The function allows conversions from an arbitrary input projection to an arbitrary output projection, as long as it is implemented in Proj4js. Before, only conversions from WGS84 to Mercator and vice-versa were possible.Example use:
This is not a breaking change. Both of the original
toMercatorandtoWgs84functions are left untouched (however, it might be worthwhile removing them, as they are easily replaced byproj4ToProj4). Proj4js has been added as a project dependency (it had already been a dev dependency before).I've read the steps for preparing a pull request. I've added appropriate tests for the new function (currently testing only WGS84->Mercator and Mercator->WGS84, based on the already existing test files). All tests are ok. The docs have not yet been updated to include the new function because I first want to see if this sparks your interest at all.