
Security News
libxml2 Maintainer Ends Embargoed Vulnerability Reports, Citing Unsustainable Burden
Libxml2’s solo maintainer drops embargoed security fixes, highlighting the burden on unpaid volunteers who keep critical open source software secure.
Proj4js is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations.
The proj4 npm package is a JavaScript library for performing cartographic projections and coordinate transformations. It allows you to convert geographic coordinates from one coordinate system to another, which is essential for mapping and GIS applications.
Coordinate Transformation
This feature allows you to transform coordinates from one projection system to another. In this example, coordinates in WGS84 (EPSG:4326) are transformed to Web Mercator (EPSG:3857).
const proj4 = require('proj4');
// Define two coordinate systems
const firstProjection = 'EPSG:4326'; // WGS84
const secondProjection = 'EPSG:3857'; // Web Mercator
// Transform coordinates from WGS84 to Web Mercator
const coordinates = [12.4924, 41.8902]; // Longitude, Latitude for Rome, Italy
const transformedCoordinates = proj4(firstProjection, secondProjection, coordinates);
console.log(transformedCoordinates); // Output: [1389919.142, 5146592.928]
Defining Custom Projections
This feature allows you to define custom projections using Proj4 strings. In this example, a UTM projection is defined and used to transform UTM coordinates to WGS84.
const proj4 = require('proj4');
// Define a custom projection
const customProjection = '+proj=utm +zone=33 +ellps=WGS84 +datum=WGS84 +units=m +no_defs';
// Transform coordinates using the custom projection
const coordinates = [500000, 4649776.22482]; // UTM coordinates
const transformedCoordinates = proj4(customProjection, 'EPSG:4326', coordinates);
console.log(transformedCoordinates); // Output: [9.0, 42.0]
Batch Transformations
This feature allows you to perform batch transformations on an array of coordinates. In this example, coordinates for Rome, London, and Tokyo are transformed from WGS84 to Web Mercator.
const proj4 = require('proj4');
// Define two coordinate systems
const firstProjection = 'EPSG:4326'; // WGS84
const secondProjection = 'EPSG:3857'; // Web Mercator
// Batch transform an array of coordinates
const coordinatesArray = [
[12.4924, 41.8902], // Rome
[-0.1276, 51.5074], // London
[139.6917, 35.6895] // Tokyo
];
const transformedArray = coordinatesArray.map(coords => proj4(firstProjection, secondProjection, coords));
console.log(transformedArray); // Output: [[1389919.142, 5146592.928], [-14273.880, 6711545.653], [15550447.354, 4258423.671]]
Proj4js is another JavaScript library for performing cartographic projections and coordinate transformations. It is similar to proj4 but is specifically designed for use in web applications. Proj4js is often used in conjunction with mapping libraries like OpenLayers and Leaflet.
The geodesy package provides tools for geodesic calculations, including coordinate transformations, distance calculations, and more. While it offers similar functionality to proj4, it also includes additional geodesic calculations that may be useful for more advanced GIS applications.
Turf is a JavaScript library for spatial analysis. It includes a wide range of geospatial functions, including coordinate transformations, but also offers additional features like buffering, clustering, and spatial joins. Turf is more comprehensive than proj4 and is suitable for more complex geospatial analysis tasks.
Proj4js is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations. Originally a port of PROJ (then known as PROJ.4) and GCTCP C (Archive) it is a part of the MetaCRS group of projects.
Depending on your preferences
npm install proj4
bower install proj4
component install proj4js/proj4js
or just manually grab the file proj4.js
from the latest release's dist/
folder.
If you do not want to download anything, Proj4js is also hosted on cdnjs for direct use in your browser applications.
The basic signature is:
proj4([fromProjection, ]toProjection[, coordinates])
Projections can be proj or wkt strings, or PROJJSON objects.
Coordinates may be an object of the form {x:x,y:y}
or an array of the form [x,y]
.
When all 3 arguments are given, the result is that the coordinates are transformed from projection1 to projection 2. And returned in the same format that they were given in.
var firstProjection = 'PROJCS["NAD83 / Massachusetts Mainland",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",42.68333333333333],PARAMETER["standard_parallel_2",41.71666666666667],PARAMETER["latitude_of_origin",41],PARAMETER["central_meridian",-71.5],PARAMETER["false_easting",200000],PARAMETER["false_northing",750000],AUTHORITY["EPSG","26986"],AXIS["X",EAST],AXIS["Y",NORTH]]';
var secondProjection = "+proj=gnom +lat_0=90 +lon_0=0 +x_0=6300000 +y_0=6300000 +ellps=WGS84 +datum=WGS84 +units=m +no_defs";
//I'm not going to redefine those two in latter examples.
proj4(firstProjection,secondProjection,[-122.305887, 58.9465872]);
// [-2690575.447893817, 36622916.8071244564]
The library can also parse coordinates provided with an elevation and measure, again as an object of the form {x:x,y:y,z:z,m:m}
or an array of the form [x,y,z,m]
.
proj4(firstProjection,secondProjection,[-122.305887, 58.9465872,10]);
// [-2690575.447893817, 36622916.8071244564, 10]
If only 1 projection is given then it is assumed that it is being projected from WGS84 (fromProjection is WGS84).
proj4(firstProjection,[-71,41]);
// [242075.00535055372, 750123.32090043]
If no coordinates are given an object with two methods is returned, its methods are forward
which projects from the first projection to the second and inverse
which projects from the second to the first.
proj4(firstProjection,secondProjection).forward([-122.305887, 58.9465872]);
// [-2690575.447893817, 36622916.8071244564]
proj4(secondProjection,firstProjection).inverse([-122.305887, 58.9465872]);
// [-2690575.447893817, 36622916.8071244564]
And as above if only one projection is given, it's assumed to be coming from wgs84:
proj4(firstProjection).forward([-71,41]);
// [242075.00535055372, 750123.32090043]
proj4(firstProjection).inverse([242075.00535055372, 750123.32090043]);
// [-71, 40.99999999999986]
Note: The generation of the floating point value 40.99999999999986
in this example represents the fact that some variance in precision is involved in any conversion between one coordinate reference system and another.
If you prefer to define a projection as a string and reference it that way, you may use the proj4.defs method which can be called 2 ways, with a name and projection:
proj4.defs('WGS84', "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees");
or with an array
proj4.defs([
[
'EPSG:4326',
'+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees'],
[
'EPSG:4269',
'+title=NAD83 (long/lat) +proj=longlat +a=6378137.0 +b=6356752.31414036 +ellps=GRS80 +datum=NAD83 +units=degrees'
]
]);
you can then do
proj4('EPSG:4326');
instead of writing out the whole proj definition, by default proj4 has the following projections predefined:
Defined projections can also be accessed through the proj4.defs function (proj4.defs('EPSG:4326')
).
proj4.defs can also be used to define a named alias:
proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
By default, proj4 uses [x,y]
axis order for projected (cartesian) coordinate systems and [x=longitude,y=latitude]
for geographic coordinates. To enforce the axis order of the provided proj or wkt string, use the
proj4(fromProjection, toProjection).forward(coordinate, enforceAxis);
proj4(fromProjection, toProjection).inverse(coordinate, enforceAxis);
signatures with enforceAxis
set to true
:
proj4('+proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees +axis=neu', firstProjection).forward([41, -71], true);
// [242075.00535055372, 750123.32090043]
proj4('+proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees +axis=neu', firstProjection).inverse([242075.00535055372, 750123.32090043], true);
//[40.99999999999986, -71]
//the floating points to answer your question
Proj4js has built-in Helmert transformations for many datums to transform to and from WGS84, which is used as pivot for datum transformations. If a datum is not available or not accurate enough for the desired region, custom Helmert transformations or grid based datum adjustments are supported.
WKT1 definitions can contain a TOWGS84
parameter. For proj strings, towgs84
or nadgrids
can be specified. When using WKT2 or PROJJSON definitions, if a BOUNDCRS
with an ABRIDGEDTRANDFORMATION
to WGS84 is provided, Helmert transformation parameters (like +towgs84=
in proj strings) or the name of the parameter file (like +nadgrids=
in proj strings) will be extracted from it.
To use +nadgrids=
in a proj definition or a WKT2/PROJJSON ABRIDGEDTRANSFORM
with an NTv2
method, first read your NTv2 .gsb
file (e.g. from https://github.com/OSGeo/proj-datumgrid) into an ArrayBuffer, then pass it to proj4.nadgrid
. E.g:
const buffer = fs.readFileSync('ntv2.gsb').buffer
proj4.nadgrid('key', buffer);
then use the given key in your definition, e.g. +nadgrids=@key,null
. See Grid Based Datum Adjustments.
Optionally, if your .gsb
file does not contain latitude and longitude error columns, you can provide an options
object argument to the proj4.nadgrid
call, setting the includeErrorFields
property to false
, e.g:
const buffer = fs.readFileSync('ntv2.gsb').buffer
proj4.nadgrid('key', buffer, {includeErrorFields:false});
If the options argument is omitted, includeErrorFields
is assumed to be true.
To use +nadgrids=
in a proj definition or a WKT2/PROJJSON ABRIDGEDTRANSFORM
with a GeoTIFF
method, first read your .tif
file (e.g. from https://github.com/OSGeo/PROJ-data or https://cdn.proj.org/) into a GeoTIFF instance from the GeoTIFF.js library, then pass it to proj4.nadgrid
. E.g:
import { fromUrl} from "geotiff";
const tiff = await fromUrl('ca_nrc_NA83SCRS.tif');
await proj4.nadgrid('ca_nrc_NA83SCRS.tif', tiff).ready;
Then use the given key in your definition, e.g. proj4.defs("EPSG:32188","+proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +ellps=GRS80 +nadgrids=ca_nrc_NA83SCRS.tif +units=m +no_defs +type=crs");
noting the +nadgrids=ca_nrc_NA83SCRS.tif
parameter.
TypeScript implementation was added to the DefinitelyTyped repository.
$ npm install --save @types/proj4
To set up build tools make sure you have node and grunt-cli installed and then run npm install
.
To do the complete build and browser tests run
node_modules/.bin/grunt
To run node and browser tests run
npm test
To run node tests with coverage run
npm run build
npm run test:coverage
To create a build with only default projections (latlon and Mercator) run
node_modules/.bin/grunt build
To create a build with only custom projections include a comma separated list of projections codes (the file name in 'lib/projections' without the '.js') after a colon, e.g.
node_modules/.bin/grunt build:tmerc
#includes transverse Mercator
node_modules/.bin/grunt build:lcc
#includes lambert conformal conic
node_modules/.bin/grunt build:omerc,moll
#includes oblique Mercator and Mollweide
FAQs
Proj4js is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations.
The npm package proj4 receives a total of 326,484 weekly downloads. As such, proj4 popularity was classified as popular.
We found that proj4 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Libxml2’s solo maintainer drops embargoed security fixes, highlighting the burden on unpaid volunteers who keep critical open source software secure.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.