@terraformer/spatial
Advanced tools
Comparing version 2.1.1 to 2.1.2
/* @preserve | ||
* @terraformer/spatial - v2.1.0 - MIT | ||
* @terraformer/spatial - v2.1.1 - MIT | ||
* Copyright (c) 2012-2022 Environmental Systems Research Institute, Inc. | ||
* Tue May 31 2022 16:59:21 GMT-0700 (Pacific Daylight Time) | ||
* Tue Aug 02 2022 14:23:50 GMT-0700 (Pacific Daylight Time) | ||
*/ | ||
/* | ||
Internal: Calculate an bounding box from an nested array of positions | ||
[ | ||
[ | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
] | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
] | ||
*/ | ||
var calculateBoundsFromNestedArrays = function calculateBoundsFromNestedArrays(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var inner = array[i]; | ||
for (var j = 0; j < inner.length; j++) { | ||
var lonlat = inner[j]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate a bounding box from an array of arrays of arrays | ||
[ | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
] | ||
*/ | ||
var calculateBoundsFromNestedArrayOfArrays = function calculateBoundsFromNestedArrayOfArrays(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var inner = array[i]; // return calculateBoundsFromNestedArrays(inner); // more DRY? | ||
for (var j = 0; j < inner.length; j++) { | ||
var innerinner = inner[j]; | ||
for (var k = 0; k < innerinner.length; k++) { | ||
var lonlat = innerinner[k]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate a bounding box from an array of positions | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
*/ | ||
var calculateBoundsFromArray = function calculateBoundsFromArray(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var lonlat = array[i]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate an bounding box for a feature collection | ||
*/ | ||
var calculateBoundsForFeatureCollection = function calculateBoundsForFeatureCollection(featureCollection) { | ||
var extents = []; | ||
for (var i = featureCollection.features.length - 1; i >= 0; i--) { | ||
var extent = calculateBounds(featureCollection.features[i].geometry); | ||
extents.push([extent[0], extent[1]]); | ||
extents.push([extent[2], extent[3]]); | ||
} | ||
return calculateBoundsFromArray(extents); | ||
}; | ||
/* | ||
Internal: Calculate an bounding box for a geometry collection | ||
*/ | ||
var calculateBoundsForGeometryCollection = function calculateBoundsForGeometryCollection(geometryCollection) { | ||
var extents = []; | ||
for (var i = geometryCollection.geometries.length - 1; i >= 0; i--) { | ||
var extent = calculateBounds(geometryCollection.geometries[i]); | ||
extents.push([extent[0], extent[1]]); | ||
extents.push([extent[2], extent[3]]); | ||
} | ||
return calculateBoundsFromArray(extents); | ||
}; | ||
var calculateBounds = function calculateBounds(geojson) { | ||
if (geojson.type) { | ||
switch (geojson.type) { | ||
case 'Point': | ||
return [geojson.coordinates[0], geojson.coordinates[1], geojson.coordinates[0], geojson.coordinates[1]]; | ||
case 'MultiPoint': | ||
return calculateBoundsFromArray(geojson.coordinates); | ||
case 'LineString': | ||
return calculateBoundsFromArray(geojson.coordinates); | ||
case 'MultiLineString': | ||
return calculateBoundsFromNestedArrays(geojson.coordinates); | ||
case 'Polygon': | ||
return calculateBoundsFromNestedArrays(geojson.coordinates); | ||
case 'MultiPolygon': | ||
return calculateBoundsFromNestedArrayOfArrays(geojson.coordinates); | ||
case 'Feature': | ||
return geojson.geometry ? calculateBounds(geojson.geometry) : null; | ||
case 'FeatureCollection': | ||
return calculateBoundsForFeatureCollection(geojson); | ||
case 'GeometryCollection': | ||
return calculateBoundsForGeometryCollection(geojson); | ||
default: | ||
throw new Error('Unknown type: ' + geojson.type); | ||
} | ||
} | ||
return null; | ||
}; | ||
var EARTH_RADIUS = 6378137; | ||
var DEGREES_PER_RADIAN = 57.295779513082320; | ||
var RADIANS_PER_DEGREE = 0.017453292519943; | ||
var MercatorCRS = { | ||
type: 'link', | ||
properties: { | ||
href: 'http://spatialreference.org/ref/sr-org/6928/ogcwkt/', | ||
type: 'ogcwkt' | ||
} | ||
}; | ||
var GeographicCRS = { | ||
type: 'link', | ||
properties: { | ||
href: 'http://spatialreference.org/ref/epsg/4326/ogcwkt/', | ||
type: 'ogcwkt' | ||
} | ||
}; | ||
/* | ||
Internal: Convert radians to degrees. Used by spatial reference converters. | ||
*/ | ||
var radToDeg = function radToDeg(rad) { | ||
return rad * DEGREES_PER_RADIAN; | ||
}; | ||
/* | ||
Internal: Convert degrees to radians. Used by spatial reference converters. | ||
*/ | ||
var degToRad = function degToRad(deg) { | ||
return deg * RADIANS_PER_DEGREE; | ||
}; | ||
var positionToGeographic = function positionToGeographic(position) { | ||
var x = position[0]; | ||
var y = position[1]; | ||
return [radToDeg(x / EARTH_RADIUS) - Math.floor((radToDeg(x / EARTH_RADIUS) + 180) / 360) * 360, radToDeg(Math.PI / 2 - 2 * Math.atan(Math.exp(-1.0 * y / EARTH_RADIUS)))]; | ||
}; | ||
var positionToMercator = function positionToMercator(position) { | ||
var lng = position[0]; | ||
var lat = Math.max(Math.min(position[1], 89.99999), -89.99999); | ||
return [degToRad(lng) * EARTH_RADIUS, EARTH_RADIUS / 2.0 * Math.log((1.0 + Math.sin(degToRad(lat))) / (1.0 - Math.sin(degToRad(lat))))]; | ||
}; | ||
function _typeof(obj) { | ||
@@ -93,46 +365,3 @@ "@babel/helpers - typeof"; | ||
var EARTH_RADIUS = 6378137; | ||
var DEGREES_PER_RADIAN = 57.295779513082320; | ||
var RADIANS_PER_DEGREE = 0.017453292519943; | ||
var MercatorCRS = { | ||
type: 'link', | ||
properties: { | ||
href: 'http://spatialreference.org/ref/sr-org/6928/ogcwkt/', | ||
type: 'ogcwkt' | ||
} | ||
}; | ||
var GeographicCRS = { | ||
type: 'link', | ||
properties: { | ||
href: 'http://spatialreference.org/ref/epsg/4326/ogcwkt/', | ||
type: 'ogcwkt' | ||
} | ||
}; | ||
/* | ||
Internal: Convert radians to degrees. Used by spatial reference converters. | ||
*/ | ||
var radToDeg = function radToDeg(rad) { | ||
return rad * DEGREES_PER_RADIAN; | ||
}; | ||
/* | ||
Internal: Convert degrees to radians. Used by spatial reference converters. | ||
*/ | ||
var degToRad = function degToRad(deg) { | ||
return deg * RADIANS_PER_DEGREE; | ||
}; | ||
var positionToGeographic = function positionToGeographic(position) { | ||
var x = position[0]; | ||
var y = position[1]; | ||
return [radToDeg(x / EARTH_RADIUS) - Math.floor((radToDeg(x / EARTH_RADIUS) + 180) / 360) * 360, radToDeg(Math.PI / 2 - 2 * Math.atan(Math.exp(-1.0 * y / EARTH_RADIUS)))]; | ||
}; | ||
var positionToMercator = function positionToMercator(position) { | ||
var lng = position[0]; | ||
var lat = Math.max(Math.min(position[1], 89.99999), -89.99999); | ||
return [degToRad(lng) * EARTH_RADIUS, EARTH_RADIUS / 2.0 * Math.log((1.0 + Math.sin(degToRad(lat))) / (1.0 - Math.sin(degToRad(lat))))]; | ||
}; | ||
/* | ||
Internal: used for sorting | ||
@@ -331,231 +560,2 @@ */ | ||
/* | ||
Internal: Calculate an bounding box from an nested array of positions | ||
[ | ||
[ | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
] | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
] | ||
*/ | ||
var calculateBoundsFromNestedArrays = function calculateBoundsFromNestedArrays(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var inner = array[i]; | ||
for (var j = 0; j < inner.length; j++) { | ||
var lonlat = inner[j]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate a bounding box from an array of arrays of arrays | ||
[ | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
] | ||
*/ | ||
var calculateBoundsFromNestedArrayOfArrays = function calculateBoundsFromNestedArrayOfArrays(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var inner = array[i]; // return calculateBoundsFromNestedArrays(inner); // more DRY? | ||
for (var j = 0; j < inner.length; j++) { | ||
var innerinner = inner[j]; | ||
for (var k = 0; k < innerinner.length; k++) { | ||
var lonlat = innerinner[k]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate a bounding box from an array of positions | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
*/ | ||
var calculateBoundsFromArray = function calculateBoundsFromArray(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var lonlat = array[i]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate an bounding box for a feature collection | ||
*/ | ||
var calculateBoundsForFeatureCollection = function calculateBoundsForFeatureCollection(featureCollection) { | ||
var extents = []; | ||
for (var i = featureCollection.features.length - 1; i >= 0; i--) { | ||
var extent = calculateBounds(featureCollection.features[i].geometry); | ||
extents.push([extent[0], extent[1]]); | ||
extents.push([extent[2], extent[3]]); | ||
} | ||
return calculateBoundsFromArray(extents); | ||
}; | ||
/* | ||
Internal: Calculate an bounding box for a geometry collection | ||
*/ | ||
var calculateBoundsForGeometryCollection = function calculateBoundsForGeometryCollection(geometryCollection) { | ||
var extents = []; | ||
for (var i = geometryCollection.geometries.length - 1; i >= 0; i--) { | ||
var extent = calculateBounds(geometryCollection.geometries[i]); | ||
extents.push([extent[0], extent[1]]); | ||
extents.push([extent[2], extent[3]]); | ||
} | ||
return calculateBoundsFromArray(extents); | ||
}; | ||
var calculateBounds = function calculateBounds(geojson) { | ||
if (geojson.type) { | ||
switch (geojson.type) { | ||
case 'Point': | ||
return [geojson.coordinates[0], geojson.coordinates[1], geojson.coordinates[0], geojson.coordinates[1]]; | ||
case 'MultiPoint': | ||
return calculateBoundsFromArray(geojson.coordinates); | ||
case 'LineString': | ||
return calculateBoundsFromArray(geojson.coordinates); | ||
case 'MultiLineString': | ||
return calculateBoundsFromNestedArrays(geojson.coordinates); | ||
case 'Polygon': | ||
return calculateBoundsFromNestedArrays(geojson.coordinates); | ||
case 'MultiPolygon': | ||
return calculateBoundsFromNestedArrayOfArrays(geojson.coordinates); | ||
case 'Feature': | ||
return geojson.geometry ? calculateBounds(geojson.geometry) : null; | ||
case 'FeatureCollection': | ||
return calculateBoundsForFeatureCollection(geojson); | ||
case 'GeometryCollection': | ||
return calculateBoundsForGeometryCollection(geojson); | ||
default: | ||
throw new Error('Unknown type: ' + geojson.type); | ||
} | ||
} | ||
return null; | ||
}; | ||
var convexHull = function convexHull(geojson) { | ||
@@ -869,2 +869,6 @@ var coordinates = []; | ||
if (geoJSON.type === 'MultiPolygon' && multipolygonIntersection(geoJSON, comparisonGeoJSON)) { | ||
return true; | ||
} | ||
if (geoJSON.type !== 'Point' && geoJSON.type !== 'MultiPoint' && comparisonGeoJSON.type !== 'Point' && comparisonGeoJSON.type !== 'MultiPoint') { | ||
@@ -882,2 +886,12 @@ return arraysIntersectArrays(geoJSON.coordinates, comparisonGeoJSON.coordinates); | ||
function multipolygonIntersection(geoJSON, comparisonGeoJSON) { | ||
return geoJSON.coordinates.some(function (coordinates) { | ||
var componentPolygon = { | ||
type: 'Polygon', | ||
coordinates: coordinates | ||
}; | ||
return within(componentPolygon, comparisonGeoJSON) || within(comparisonGeoJSON, componentPolygon); | ||
}); | ||
} | ||
var VINCENTY = { | ||
@@ -1027,2 +1041,2 @@ a: 6378137, | ||
export { GeographicCRS, MercatorCRS, calculateBounds, calculateEnvelope, contains, convexHull, intersects, isConvex, polygonContainsPoint, positionToGeographic, positionToMercator, toCircle, toGeographic, toMercator, within }; | ||
export { GeographicCRS, MercatorCRS, applyConverter, calculateBounds, calculateEnvelope, contains, convexHull, intersects, isConvex, polygonContainsPoint, positionToGeographic, positionToMercator, toCircle, toGeographic, toMercator, within }; |
/* @preserve | ||
* @terraformer/spatial - v2.1.0 - MIT | ||
* @terraformer/spatial - v2.1.1 - MIT | ||
* Copyright (c) 2012-2022 Environmental Systems Research Institute, Inc. | ||
* Tue May 31 2022 16:59:18 GMT-0700 (Pacific Daylight Time) | ||
* Tue Aug 02 2022 14:23:46 GMT-0700 (Pacific Daylight Time) | ||
*/ | ||
@@ -9,5 +9,277 @@ (function (global, factory) { | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.Terraformer = global.Terraformer || {})); | ||
}(this, function (exports) { 'use strict'; | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Terraformer = global.Terraformer || {})); | ||
})(this, (function (exports) { 'use strict'; | ||
/* | ||
Internal: Calculate an bounding box from an nested array of positions | ||
[ | ||
[ | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
] | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
] | ||
*/ | ||
var calculateBoundsFromNestedArrays = function calculateBoundsFromNestedArrays(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var inner = array[i]; | ||
for (var j = 0; j < inner.length; j++) { | ||
var lonlat = inner[j]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate a bounding box from an array of arrays of arrays | ||
[ | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
] | ||
*/ | ||
var calculateBoundsFromNestedArrayOfArrays = function calculateBoundsFromNestedArrayOfArrays(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var inner = array[i]; // return calculateBoundsFromNestedArrays(inner); // more DRY? | ||
for (var j = 0; j < inner.length; j++) { | ||
var innerinner = inner[j]; | ||
for (var k = 0; k < innerinner.length; k++) { | ||
var lonlat = innerinner[k]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate a bounding box from an array of positions | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
*/ | ||
var calculateBoundsFromArray = function calculateBoundsFromArray(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var lonlat = array[i]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate an bounding box for a feature collection | ||
*/ | ||
var calculateBoundsForFeatureCollection = function calculateBoundsForFeatureCollection(featureCollection) { | ||
var extents = []; | ||
for (var i = featureCollection.features.length - 1; i >= 0; i--) { | ||
var extent = calculateBounds(featureCollection.features[i].geometry); | ||
extents.push([extent[0], extent[1]]); | ||
extents.push([extent[2], extent[3]]); | ||
} | ||
return calculateBoundsFromArray(extents); | ||
}; | ||
/* | ||
Internal: Calculate an bounding box for a geometry collection | ||
*/ | ||
var calculateBoundsForGeometryCollection = function calculateBoundsForGeometryCollection(geometryCollection) { | ||
var extents = []; | ||
for (var i = geometryCollection.geometries.length - 1; i >= 0; i--) { | ||
var extent = calculateBounds(geometryCollection.geometries[i]); | ||
extents.push([extent[0], extent[1]]); | ||
extents.push([extent[2], extent[3]]); | ||
} | ||
return calculateBoundsFromArray(extents); | ||
}; | ||
var calculateBounds = function calculateBounds(geojson) { | ||
if (geojson.type) { | ||
switch (geojson.type) { | ||
case 'Point': | ||
return [geojson.coordinates[0], geojson.coordinates[1], geojson.coordinates[0], geojson.coordinates[1]]; | ||
case 'MultiPoint': | ||
return calculateBoundsFromArray(geojson.coordinates); | ||
case 'LineString': | ||
return calculateBoundsFromArray(geojson.coordinates); | ||
case 'MultiLineString': | ||
return calculateBoundsFromNestedArrays(geojson.coordinates); | ||
case 'Polygon': | ||
return calculateBoundsFromNestedArrays(geojson.coordinates); | ||
case 'MultiPolygon': | ||
return calculateBoundsFromNestedArrayOfArrays(geojson.coordinates); | ||
case 'Feature': | ||
return geojson.geometry ? calculateBounds(geojson.geometry) : null; | ||
case 'FeatureCollection': | ||
return calculateBoundsForFeatureCollection(geojson); | ||
case 'GeometryCollection': | ||
return calculateBoundsForGeometryCollection(geojson); | ||
default: | ||
throw new Error('Unknown type: ' + geojson.type); | ||
} | ||
} | ||
return null; | ||
}; | ||
var EARTH_RADIUS = 6378137; | ||
var DEGREES_PER_RADIAN = 57.295779513082320; | ||
var RADIANS_PER_DEGREE = 0.017453292519943; | ||
var MercatorCRS = { | ||
type: 'link', | ||
properties: { | ||
href: 'http://spatialreference.org/ref/sr-org/6928/ogcwkt/', | ||
type: 'ogcwkt' | ||
} | ||
}; | ||
var GeographicCRS = { | ||
type: 'link', | ||
properties: { | ||
href: 'http://spatialreference.org/ref/epsg/4326/ogcwkt/', | ||
type: 'ogcwkt' | ||
} | ||
}; | ||
/* | ||
Internal: Convert radians to degrees. Used by spatial reference converters. | ||
*/ | ||
var radToDeg = function radToDeg(rad) { | ||
return rad * DEGREES_PER_RADIAN; | ||
}; | ||
/* | ||
Internal: Convert degrees to radians. Used by spatial reference converters. | ||
*/ | ||
var degToRad = function degToRad(deg) { | ||
return deg * RADIANS_PER_DEGREE; | ||
}; | ||
var positionToGeographic = function positionToGeographic(position) { | ||
var x = position[0]; | ||
var y = position[1]; | ||
return [radToDeg(x / EARTH_RADIUS) - Math.floor((radToDeg(x / EARTH_RADIUS) + 180) / 360) * 360, radToDeg(Math.PI / 2 - 2 * Math.atan(Math.exp(-1.0 * y / EARTH_RADIUS)))]; | ||
}; | ||
var positionToMercator = function positionToMercator(position) { | ||
var lng = position[0]; | ||
var lat = Math.max(Math.min(position[1], 89.99999), -89.99999); | ||
return [degToRad(lng) * EARTH_RADIUS, EARTH_RADIUS / 2.0 * Math.log((1.0 + Math.sin(degToRad(lat))) / (1.0 - Math.sin(degToRad(lat))))]; | ||
}; | ||
function _typeof(obj) { | ||
@@ -100,46 +372,3 @@ "@babel/helpers - typeof"; | ||
var EARTH_RADIUS = 6378137; | ||
var DEGREES_PER_RADIAN = 57.295779513082320; | ||
var RADIANS_PER_DEGREE = 0.017453292519943; | ||
var MercatorCRS = { | ||
type: 'link', | ||
properties: { | ||
href: 'http://spatialreference.org/ref/sr-org/6928/ogcwkt/', | ||
type: 'ogcwkt' | ||
} | ||
}; | ||
var GeographicCRS = { | ||
type: 'link', | ||
properties: { | ||
href: 'http://spatialreference.org/ref/epsg/4326/ogcwkt/', | ||
type: 'ogcwkt' | ||
} | ||
}; | ||
/* | ||
Internal: Convert radians to degrees. Used by spatial reference converters. | ||
*/ | ||
var radToDeg = function radToDeg(rad) { | ||
return rad * DEGREES_PER_RADIAN; | ||
}; | ||
/* | ||
Internal: Convert degrees to radians. Used by spatial reference converters. | ||
*/ | ||
var degToRad = function degToRad(deg) { | ||
return deg * RADIANS_PER_DEGREE; | ||
}; | ||
var positionToGeographic = function positionToGeographic(position) { | ||
var x = position[0]; | ||
var y = position[1]; | ||
return [radToDeg(x / EARTH_RADIUS) - Math.floor((radToDeg(x / EARTH_RADIUS) + 180) / 360) * 360, radToDeg(Math.PI / 2 - 2 * Math.atan(Math.exp(-1.0 * y / EARTH_RADIUS)))]; | ||
}; | ||
var positionToMercator = function positionToMercator(position) { | ||
var lng = position[0]; | ||
var lat = Math.max(Math.min(position[1], 89.99999), -89.99999); | ||
return [degToRad(lng) * EARTH_RADIUS, EARTH_RADIUS / 2.0 * Math.log((1.0 + Math.sin(degToRad(lat))) / (1.0 - Math.sin(degToRad(lat))))]; | ||
}; | ||
/* | ||
Internal: used for sorting | ||
@@ -338,231 +567,2 @@ */ | ||
/* | ||
Internal: Calculate an bounding box from an nested array of positions | ||
[ | ||
[ | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
] | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
] | ||
*/ | ||
var calculateBoundsFromNestedArrays = function calculateBoundsFromNestedArrays(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var inner = array[i]; | ||
for (var j = 0; j < inner.length; j++) { | ||
var lonlat = inner[j]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate a bounding box from an array of arrays of arrays | ||
[ | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
[ [lng, lat],[lng, lat],[lng, lat] ] | ||
] | ||
*/ | ||
var calculateBoundsFromNestedArrayOfArrays = function calculateBoundsFromNestedArrayOfArrays(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var inner = array[i]; // return calculateBoundsFromNestedArrays(inner); // more DRY? | ||
for (var j = 0; j < inner.length; j++) { | ||
var innerinner = inner[j]; | ||
for (var k = 0; k < innerinner.length; k++) { | ||
var lonlat = innerinner[k]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate a bounding box from an array of positions | ||
[ | ||
[lng, lat],[lng, lat],[lng, lat] | ||
] | ||
*/ | ||
var calculateBoundsFromArray = function calculateBoundsFromArray(array) { | ||
var x1 = null; | ||
var x2 = null; | ||
var y1 = null; | ||
var y2 = null; | ||
for (var i = 0; i < array.length; i++) { | ||
var lonlat = array[i]; | ||
var lon = lonlat[0]; | ||
var lat = lonlat[1]; | ||
if (x1 === null) { | ||
x1 = lon; | ||
} else if (lon < x1) { | ||
x1 = lon; | ||
} | ||
if (x2 === null) { | ||
x2 = lon; | ||
} else if (lon > x2) { | ||
x2 = lon; | ||
} | ||
if (y1 === null) { | ||
y1 = lat; | ||
} else if (lat < y1) { | ||
y1 = lat; | ||
} | ||
if (y2 === null) { | ||
y2 = lat; | ||
} else if (lat > y2) { | ||
y2 = lat; | ||
} | ||
} | ||
return [x1, y1, x2, y2]; | ||
}; | ||
/* | ||
Internal: Calculate an bounding box for a feature collection | ||
*/ | ||
var calculateBoundsForFeatureCollection = function calculateBoundsForFeatureCollection(featureCollection) { | ||
var extents = []; | ||
for (var i = featureCollection.features.length - 1; i >= 0; i--) { | ||
var extent = calculateBounds(featureCollection.features[i].geometry); | ||
extents.push([extent[0], extent[1]]); | ||
extents.push([extent[2], extent[3]]); | ||
} | ||
return calculateBoundsFromArray(extents); | ||
}; | ||
/* | ||
Internal: Calculate an bounding box for a geometry collection | ||
*/ | ||
var calculateBoundsForGeometryCollection = function calculateBoundsForGeometryCollection(geometryCollection) { | ||
var extents = []; | ||
for (var i = geometryCollection.geometries.length - 1; i >= 0; i--) { | ||
var extent = calculateBounds(geometryCollection.geometries[i]); | ||
extents.push([extent[0], extent[1]]); | ||
extents.push([extent[2], extent[3]]); | ||
} | ||
return calculateBoundsFromArray(extents); | ||
}; | ||
var calculateBounds = function calculateBounds(geojson) { | ||
if (geojson.type) { | ||
switch (geojson.type) { | ||
case 'Point': | ||
return [geojson.coordinates[0], geojson.coordinates[1], geojson.coordinates[0], geojson.coordinates[1]]; | ||
case 'MultiPoint': | ||
return calculateBoundsFromArray(geojson.coordinates); | ||
case 'LineString': | ||
return calculateBoundsFromArray(geojson.coordinates); | ||
case 'MultiLineString': | ||
return calculateBoundsFromNestedArrays(geojson.coordinates); | ||
case 'Polygon': | ||
return calculateBoundsFromNestedArrays(geojson.coordinates); | ||
case 'MultiPolygon': | ||
return calculateBoundsFromNestedArrayOfArrays(geojson.coordinates); | ||
case 'Feature': | ||
return geojson.geometry ? calculateBounds(geojson.geometry) : null; | ||
case 'FeatureCollection': | ||
return calculateBoundsForFeatureCollection(geojson); | ||
case 'GeometryCollection': | ||
return calculateBoundsForGeometryCollection(geojson); | ||
default: | ||
throw new Error('Unknown type: ' + geojson.type); | ||
} | ||
} | ||
return null; | ||
}; | ||
var convexHull = function convexHull(geojson) { | ||
@@ -876,2 +876,6 @@ var coordinates = []; | ||
if (geoJSON.type === 'MultiPolygon' && multipolygonIntersection(geoJSON, comparisonGeoJSON)) { | ||
return true; | ||
} | ||
if (geoJSON.type !== 'Point' && geoJSON.type !== 'MultiPoint' && comparisonGeoJSON.type !== 'Point' && comparisonGeoJSON.type !== 'MultiPoint') { | ||
@@ -889,2 +893,12 @@ return arraysIntersectArrays(geoJSON.coordinates, comparisonGeoJSON.coordinates); | ||
function multipolygonIntersection(geoJSON, comparisonGeoJSON) { | ||
return geoJSON.coordinates.some(function (coordinates) { | ||
var componentPolygon = { | ||
type: 'Polygon', | ||
coordinates: coordinates | ||
}; | ||
return within(componentPolygon, comparisonGeoJSON) || within(comparisonGeoJSON, componentPolygon); | ||
}); | ||
} | ||
var VINCENTY = { | ||
@@ -1036,2 +1050,3 @@ a: 6378137, | ||
exports.MercatorCRS = MercatorCRS; | ||
exports.applyConverter = applyConverter; | ||
exports.calculateBounds = calculateBounds; | ||
@@ -1038,0 +1053,0 @@ exports.calculateEnvelope = calculateEnvelope; |
{ | ||
"name": "@terraformer/spatial", | ||
"description": "Spatial predicates for GeoJSON.", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"author": "Patrick Arlt <patrick.arlt@gmail.com>", | ||
@@ -15,3 +15,3 @@ "bugs": { | ||
"dependencies": { | ||
"@terraformer/common": "^2.0.7" | ||
"@terraformer/common": "^2.1.2" | ||
}, | ||
@@ -45,3 +45,3 @@ "files": [ | ||
}, | ||
"gitHead": "69dd81e25bdfda536b697660e4df054b1e8ba663" | ||
"gitHead": "762903ff979102e9540732641425d0ee290efccd" | ||
} |
@@ -9,4 +9,4 @@ # @terraformer/spatial | ||
[npm-url]: https://www.npmjs.com/package/@terraformer/spatial | ||
[travis-image]: https://img.shields.io/travis/terraformer-js/terraformer/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/terraformer-js/terraformer | ||
[travis-image]: https://app.travis-ci.com/terraformer-js/terraformer.svg?branch=main | ||
[travis-url]: https://app.travis-ci.com/terraformer-js/terraformer | ||
[standard-image]: https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square | ||
@@ -32,2 +32,3 @@ [standard-url]: http://npm.im/semistandard | ||
* [.GeographicCRS](#module_Terraformer.GeographicCRS) | ||
* [.applyConverter(GeoJSON, function)](#module_Terraformer.applyConverter) ⇒ <code>object</code> | ||
* [.calculateBounds(GeoJSON)](#module_Terraformer.calculateBounds) ⇒ <code>Array.<Number></code> | ||
@@ -58,2 +59,25 @@ * [.calculateEnvelope(GeoJSON)](#module_Terraformer.calculateEnvelope) ⇒ <code>Object</code> | ||
**Kind**: static constant of [<code>Terraformer</code>](#module_Terraformer) | ||
<a name="module_Terraformer.applyConverter"></a> | ||
### Terraformer.applyConverter(GeoJSON, function) ⇒ <code>object</code> | ||
Runs the passed function against every Coordinate in the geojson object. | ||
**Kind**: static method of [<code>Terraformer</code>](#module_Terraformer) | ||
**Returns**: <code>object</code> - GeoJSON - [GeoJSON](https://tools.ietf.org/html/rfc7946) with altered coordinates. | ||
```js | ||
import { applyConverter } from "@terraformer/spatial" | ||
applyConverter({ | ||
type: "Point", | ||
coordinates: [ 45, 60 ] | ||
}, (coord) => [coord[0] + 1, coord[1] - 1]) | ||
>> { type: "Point", coordinates: [ 46, 59 ] } | ||
``` | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| GeoJSON | <code>object</code> | The input [GeoJSON](https://tools.ietf.org/html/rfc7946) Geometry, Feature, GeometryCollection or FeatureCollection. | | ||
| function | <code>function</code> | Your function will be passed a Coordinate and will be expected to return a Coordinate. | | ||
<a name="module_Terraformer.calculateBounds"></a> | ||
@@ -60,0 +84,0 @@ |
Sorry, the diff of this file is not supported yet
78651
1762
418
Updated@terraformer/common@^2.1.2