What is proj4?
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.
What are proj4's main functionalities?
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]]
Other packages similar to proj4
proj4js
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.
geodesy
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
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
Proj4js is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations.
Originally a port of PROJ.4 and GCTCP C it is
a part of the MetaCRS group of projects.
##Installing
Depending on your preferences
npm install proj4
bower install proj4
jam install proj4
or just manually grab the file dist/proj4.js
##Using
the basic signature is:
proj4(fromProjection[, toProjection2, coordinates])
Projections can be proj or wkt strings, or a proj4.Proj object.
Coordinates may be proj4.Point objects, 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";
proj4(firstProjection,secondProjection,[2,5]);
If only 1 projection is given then it is assumed that it is being projected from WGS84 (fromProjection is WGS84).
proj4(firstProjection,[-71,41]);
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([2,5]);
proj4(secondProjection,firstProjection).inverse([2,5]);
and as above if only one projection is given, it's assumed to be coming from wgs84
proj4(firstProjection).forward([-71,41]);
proj4(firstProjection).inverse([242075.00535055372, 750123.32090043]);
##Developing
to set up build tools make sure you have node installed and run npm install
to build run grunt
if that doesn't work try:
npm install -g grunt-cli