Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

@turf/area

Package Overview
Dependencies
Maintainers
4
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/area - npm Package Compare versions

Comparing version
6.0.0
to
6.0.1
+17
index.d.ts
import { Feature, FeatureCollection, Geometry } from "@turf/helpers";
/**
* Takes one or more features and returns their area in square meters.
*
* @name area
* @param {GeoJSON} geojson input GeoJSON feature(s)
* @returns {number} area in square meters
* @example
* var polygon = turf.polygon([[[125, -15], [113, -22], [154, -27], [144, -15], [125, -15]]]);
*
* var area = turf.area(polygon);
*
* //addToMap
* var addToMap = [polygon]
* polygon.properties.area = area
*/
export default function area(geojson: Feature<any> | FeatureCollection<any> | Geometry): number;
+21
-19

@@ -26,2 +26,3 @@ "use strict";

}
exports.default = area;
/**

@@ -35,28 +36,29 @@ * Calculate Area

function calculateArea(geom) {
var area = 0;
var total = 0;
var i;
switch (geom.type) {
case 'Polygon':
case "Polygon":
return polygonArea(geom.coordinates);
case 'MultiPolygon':
case "MultiPolygon":
for (i = 0; i < geom.coordinates.length; i++) {
area += polygonArea(geom.coordinates[i]);
total += polygonArea(geom.coordinates[i]);
}
return area;
case 'Point':
case 'MultiPoint':
case 'LineString':
case 'MultiLineString':
return total;
case "Point":
case "MultiPoint":
case "LineString":
case "MultiLineString":
return 0;
}
return 0;
}
function polygonArea(coords) {
var area = 0;
var total = 0;
if (coords && coords.length > 0) {
area += Math.abs(ringArea(coords[0]));
total += Math.abs(ringArea(coords[0]));
for (var i = 1; i < coords.length; i++) {
area -= Math.abs(ringArea(coords[i]));
total -= Math.abs(ringArea(coords[i]));
}
}
return area;
return total;
}

@@ -69,3 +71,4 @@ /**

* Reference:
* Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion
* Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for Polygons on a Sphere",
* JPL Publication 07-03, Jet Propulsion
* Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409

@@ -84,3 +87,3 @@ *

var i;
var area = 0;
var total = 0;
var coordsLength = coords.length;

@@ -107,7 +110,7 @@ if (coordsLength > 2) {

p3 = coords[upperIndex];
area += (rad(p3[0]) - rad(p1[0])) * Math.sin(rad(p2[1]));
total += (rad(p3[0]) - rad(p1[0])) * Math.sin(rad(p2[1]));
}
area = area * RADIUS * RADIUS / 2;
total = total * RADIUS * RADIUS / 2;
}
return area;
return total;
}

@@ -117,2 +120,1 @@ function rad(num) {

}
exports.default = area;
{
"name": "@turf/area",
"version": "6.0.0",
"version": "6.0.1",
"description": "turf area module",
"main": "index",
"types": "index.d.ts",
"files": [
"index.js",
"index.ts"
"index.d.ts"
],

@@ -38,3 +39,5 @@ "scripts": {

"tape": "*",
"write-json-file": "*"
"write-json-file": "*",
"tslint": "*",
"@types/tape": "*"
},

@@ -41,0 +44,0 @@ "dependencies": {

@@ -11,3 +11,3 @@ # @turf/area

- `geojson` **[GeoJSON](https://tools.ietf.org/html/rfc7946#section-3)** input GeoJSON feature(s)
- `geojson` **[GeoJSON][1]** input GeoJSON feature(s)

@@ -26,4 +26,8 @@ **Examples**

Returns **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** area in square meters
Returns **[number][2]** area in square meters
[1]: https://tools.ietf.org/html/rfc7946#section-3
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
<!-- This file is automatically generated. Please don't edit it directly:

@@ -30,0 +34,0 @@ if you find an error, edit the source file (likely index.js), and re-run

import { Feature, FeatureCollection, Geometry } from '@turf/helpers';
import { geomReduce } from '@turf/meta';
// Note: change RADIUS => earthRadius
const RADIUS = 6378137;
/**
* Takes one or more features and returns their area in square meters.
*
* @name area
* @param {GeoJSON} geojson input GeoJSON feature(s)
* @returns {number} area in square meters
* @example
* var polygon = turf.polygon([[[125, -15], [113, -22], [154, -27], [144, -15], [125, -15]]]);
*
* var area = turf.area(polygon);
*
* //addToMap
* var addToMap = [polygon]
* polygon.properties.area = area
*/
function area(geojson: Feature<any> | FeatureCollection<any> | Geometry) {
return geomReduce(geojson, (value, geom) => {
return value + calculateArea(geom);
}, 0);
}
/**
* Calculate Area
*
* @private
* @param {Geometry} geom GeoJSON Geometries
* @returns {number} area
*/
function calculateArea(geom: Geometry) {
let area = 0
let i;
switch (geom.type) {
case 'Polygon':
return polygonArea(geom.coordinates);
case 'MultiPolygon':
for (i = 0; i < geom.coordinates.length; i++) {
area += polygonArea(geom.coordinates[i]);
}
return area;
case 'Point':
case 'MultiPoint':
case 'LineString':
case 'MultiLineString':
return 0;
}
}
function polygonArea(coords: any) {
var area = 0;
if (coords && coords.length > 0) {
area += Math.abs(ringArea(coords[0]));
for (var i = 1; i < coords.length; i++) {
area -= Math.abs(ringArea(coords[i]));
}
}
return area;
}
/**
* @private
* Calculate the approximate area of the polygon were it projected onto the earth.
* Note that this area will be positive if ring is oriented clockwise, otherwise it will be negative.
*
* Reference:
* Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion
* Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409
*
* @param {Array<Array<number>>} coords Ring Coordinates
* @returns {number} The approximate signed geodesic area of the polygon in square meters.
*/
function ringArea(coords: number[][]) {
var p1;
var p2;
var p3;
var lowerIndex;
var middleIndex;
var upperIndex;
var i;
var area = 0;
var coordsLength = coords.length;
if (coordsLength > 2) {
for (i = 0; i < coordsLength; i++) {
if (i === coordsLength - 2) { // i = N-2
lowerIndex = coordsLength - 2;
middleIndex = coordsLength - 1;
upperIndex = 0;
} else if (i === coordsLength - 1) { // i = N-1
lowerIndex = coordsLength - 1;
middleIndex = 0;
upperIndex = 1;
} else { // i = 0 to N-3
lowerIndex = i;
middleIndex = i + 1;
upperIndex = i + 2;
}
p1 = coords[lowerIndex];
p2 = coords[middleIndex];
p3 = coords[upperIndex];
area += (rad(p3[0]) - rad(p1[0])) * Math.sin(rad(p2[1]));
}
area = area * RADIUS * RADIUS / 2;
}
return area;
}
function rad(num: number) {
return num * Math.PI / 180;
}
export default area;