agentscript
Version:
AgentScript Model in Model/View architecture
1,166 lines (1,159 loc) • 35.4 kB
JavaScript
var earthRadius = 6371008.8;
var factors = {
centimeters: earthRadius * 100,
centimetres: earthRadius * 100,
degrees: earthRadius / 111325,
feet: earthRadius * 3.28084,
inches: earthRadius * 39.37,
kilometers: earthRadius / 1000,
kilometres: earthRadius / 1000,
meters: earthRadius,
metres: earthRadius,
miles: earthRadius / 1609.344,
millimeters: earthRadius * 1000,
millimetres: earthRadius * 1000,
nauticalmiles: earthRadius / 1852,
radians: 1,
yards: earthRadius * 1.0936,
};
var areaFactors = {
acres: 0.000247105,
centimeters: 10000,
centimetres: 10000,
feet: 10.763910417,
hectares: 0.0001,
inches: 1550.003100006,
kilometers: 0.000001,
kilometres: 0.000001,
meters: 1,
metres: 1,
miles: 3.86e-7,
millimeters: 1000000,
millimetres: 1000000,
yards: 1.195990046,
};
function feature(geom, properties, options) {
if (options === void 0) { options = {}; }
var feat = { type: "Feature" };
if (options.id === 0 || options.id) {
feat.id = options.id;
}
if (options.bbox) {
feat.bbox = options.bbox;
}
feat.properties = properties || {};
feat.geometry = geom;
return feat;
}
function geometry(type, coordinates, _options) {
switch (type) {
case "Point":
return point(coordinates).geometry;
case "LineString":
return lineString(coordinates).geometry;
case "Polygon":
return polygon(coordinates).geometry;
case "MultiPoint":
return multiPoint(coordinates).geometry;
case "MultiLineString":
return multiLineString(coordinates).geometry;
case "MultiPolygon":
return multiPolygon(coordinates).geometry;
default:
throw new Error(type + " is invalid");
}
}
function point(coordinates, properties, options) {
if (options === void 0) { options = {}; }
if (!coordinates) {
throw new Error("coordinates is required");
}
if (!Array.isArray(coordinates)) {
throw new Error("coordinates must be an Array");
}
if (coordinates.length < 2) {
throw new Error("coordinates must be at least 2 numbers long");
}
if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) {
throw new Error("coordinates must contain numbers");
}
var geom = {
type: "Point",
coordinates: coordinates,
};
return feature(geom, properties, options);
}
function points(coordinates, properties, options) {
if (options === void 0) { options = {}; }
return featureCollection(coordinates.map(function (coords) {
return point(coords, properties);
}), options);
}
function polygon(coordinates, properties, options) {
if (options === void 0) { options = {}; }
for (var _i = 0, coordinates_1 = coordinates; _i < coordinates_1.length; _i++) {
var ring = coordinates_1[_i];
if (ring.length < 4) {
throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");
}
for (var j = 0; j < ring[ring.length - 1].length; j++) {
if (ring[ring.length - 1][j] !== ring[0][j]) {
throw new Error("First and last Position are not equivalent.");
}
}
}
var geom = {
type: "Polygon",
coordinates: coordinates,
};
return feature(geom, properties, options);
}
function polygons(coordinates, properties, options) {
if (options === void 0) { options = {}; }
return featureCollection(coordinates.map(function (coords) {
return polygon(coords, properties);
}), options);
}
function lineString(coordinates, properties, options) {
if (options === void 0) { options = {}; }
if (coordinates.length < 2) {
throw new Error("coordinates must be an array of two or more positions");
}
var geom = {
type: "LineString",
coordinates: coordinates,
};
return feature(geom, properties, options);
}
function lineStrings(coordinates, properties, options) {
if (options === void 0) { options = {}; }
return featureCollection(coordinates.map(function (coords) {
return lineString(coords, properties);
}), options);
}
function featureCollection(features, options) {
if (options === void 0) { options = {}; }
var fc = { type: "FeatureCollection" };
if (options.id) {
fc.id = options.id;
}
if (options.bbox) {
fc.bbox = options.bbox;
}
fc.features = features;
return fc;
}
function multiLineString(coordinates, properties, options) {
if (options === void 0) { options = {}; }
var geom = {
type: "MultiLineString",
coordinates: coordinates,
};
return feature(geom, properties, options);
}
function multiPoint(coordinates, properties, options) {
if (options === void 0) { options = {}; }
var geom = {
type: "MultiPoint",
coordinates: coordinates,
};
return feature(geom, properties, options);
}
function multiPolygon(coordinates, properties, options) {
if (options === void 0) { options = {}; }
var geom = {
type: "MultiPolygon",
coordinates: coordinates,
};
return feature(geom, properties, options);
}
function geometryCollection(geometries, properties, options) {
if (options === void 0) { options = {}; }
var geom = {
type: "GeometryCollection",
geometries: geometries,
};
return feature(geom, properties, options);
}
function radiansToLength(radians, units) {
if (units === void 0) { units = "kilometers"; }
var factor = factors[units];
if (!factor) {
throw new Error(units + " units is invalid");
}
return radians * factor;
}
function lengthToRadians(distance, units) {
if (units === void 0) { units = "kilometers"; }
var factor = factors[units];
if (!factor) {
throw new Error(units + " units is invalid");
}
return distance / factor;
}
function lengthToDegrees(distance, units) {
return radiansToDegrees(lengthToRadians(distance, units));
}
function bearingToAzimuth(bearing) {
var angle = bearing % 360;
if (angle < 0) {
angle += 360;
}
return angle;
}
function radiansToDegrees(radians) {
var degrees = radians % (2 * Math.PI);
return (degrees * 180) / Math.PI;
}
function convertLength(length, originalUnit, finalUnit) {
if (originalUnit === void 0) { originalUnit = "kilometers"; }
if (finalUnit === void 0) { finalUnit = "kilometers"; }
if (!(length >= 0)) {
throw new Error("length must be a positive number");
}
return radiansToLength(lengthToRadians(length, originalUnit), finalUnit);
}
function convertArea(area, originalUnit, finalUnit) {
if (originalUnit === void 0) { originalUnit = "meters"; }
if (finalUnit === void 0) { finalUnit = "kilometers"; }
if (!(area >= 0)) {
throw new Error("area must be a positive number");
}
var startFactor = areaFactors[originalUnit];
if (!startFactor) {
throw new Error("invalid original units");
}
var finalFactor = areaFactors[finalUnit];
if (!finalFactor) {
throw new Error("invalid final units");
}
return (area / startFactor) * finalFactor;
}
function isNumber(num) {
return !isNaN(num) && num !== null && !Array.isArray(num);
}
function isObject(input) {
return !!input && input.constructor === Object;
}
function coordEach(geojson, callback, excludeWrapCoord) {
if (geojson === null) return;
var j,
k,
l,
geometry,
stopG,
coords,
geometryMaybeCollection,
wrapShrink = 0,
coordIndex = 0,
isGeometryCollection,
type = geojson.type,
isFeatureCollection = type === "FeatureCollection",
isFeature = type === "Feature",
stop = isFeatureCollection ? geojson.features.length : 1;
for (var featureIndex = 0; featureIndex < stop; featureIndex++) {
geometryMaybeCollection = isFeatureCollection
? geojson.features[featureIndex].geometry
: isFeature
? geojson.geometry
: geojson;
isGeometryCollection = geometryMaybeCollection
? geometryMaybeCollection.type === "GeometryCollection"
: false;
stopG = isGeometryCollection
? geometryMaybeCollection.geometries.length
: 1;
for (var geomIndex = 0; geomIndex < stopG; geomIndex++) {
var multiFeatureIndex = 0;
var geometryIndex = 0;
geometry = isGeometryCollection
? geometryMaybeCollection.geometries[geomIndex]
: geometryMaybeCollection;
if (geometry === null) continue;
coords = geometry.coordinates;
var geomType = geometry.type;
wrapShrink =
excludeWrapCoord &&
(geomType === "Polygon" || geomType === "MultiPolygon")
? 1
: 0;
switch (geomType) {
case null:
break;
case "Point":
if (
callback(
coords,
coordIndex,
featureIndex,
multiFeatureIndex,
geometryIndex
) === false
)
return false;
coordIndex++;
multiFeatureIndex++;
break;
case "LineString":
case "MultiPoint":
for (j = 0; j < coords.length; j++) {
if (
callback(
coords[j],
coordIndex,
featureIndex,
multiFeatureIndex,
geometryIndex
) === false
)
return false;
coordIndex++;
if (geomType === "MultiPoint") multiFeatureIndex++;
}
if (geomType === "LineString") multiFeatureIndex++;
break;
case "Polygon":
case "MultiLineString":
for (j = 0; j < coords.length; j++) {
for (k = 0; k < coords[j].length - wrapShrink; k++) {
if (
callback(
coords[j][k],
coordIndex,
featureIndex,
multiFeatureIndex,
geometryIndex
) === false
)
return false;
coordIndex++;
}
if (geomType === "MultiLineString") multiFeatureIndex++;
if (geomType === "Polygon") geometryIndex++;
}
if (geomType === "Polygon") multiFeatureIndex++;
break;
case "MultiPolygon":
for (j = 0; j < coords.length; j++) {
geometryIndex = 0;
for (k = 0; k < coords[j].length; k++) {
for (l = 0; l < coords[j][k].length - wrapShrink; l++) {
if (
callback(
coords[j][k][l],
coordIndex,
featureIndex,
multiFeatureIndex,
geometryIndex
) === false
)
return false;
coordIndex++;
}
geometryIndex++;
}
multiFeatureIndex++;
}
break;
case "GeometryCollection":
for (j = 0; j < geometry.geometries.length; j++)
if (
coordEach(geometry.geometries[j], callback, excludeWrapCoord) ===
false
)
return false;
break;
default:
throw new Error("Unknown Geometry Type");
}
}
}
}
function coordReduce(geojson, callback, initialValue, excludeWrapCoord) {
var previousValue = initialValue;
coordEach(
geojson,
function (
currentCoord,
coordIndex,
featureIndex,
multiFeatureIndex,
geometryIndex
) {
if (coordIndex === 0 && initialValue === undefined)
previousValue = currentCoord;
else
previousValue = callback(
previousValue,
currentCoord,
coordIndex,
featureIndex,
multiFeatureIndex,
geometryIndex
);
},
excludeWrapCoord
);
return previousValue;
}
function propEach(geojson, callback) {
var i;
switch (geojson.type) {
case "FeatureCollection":
for (i = 0; i < geojson.features.length; i++) {
if (callback(geojson.features[i].properties, i) === false) break;
}
break;
case "Feature":
callback(geojson.properties, 0);
break;
}
}
function propReduce(geojson, callback, initialValue) {
var previousValue = initialValue;
propEach(geojson, function (currentProperties, featureIndex) {
if (featureIndex === 0 && initialValue === undefined)
previousValue = currentProperties;
else
previousValue = callback(previousValue, currentProperties, featureIndex);
});
return previousValue;
}
function featureEach(geojson, callback) {
if (geojson.type === "Feature") {
callback(geojson, 0);
} else if (geojson.type === "FeatureCollection") {
for (var i = 0; i < geojson.features.length; i++) {
if (callback(geojson.features[i], i) === false) break;
}
}
}
function featureReduce(geojson, callback, initialValue) {
var previousValue = initialValue;
featureEach(geojson, function (currentFeature, featureIndex) {
if (featureIndex === 0 && initialValue === undefined)
previousValue = currentFeature;
else previousValue = callback(previousValue, currentFeature, featureIndex);
});
return previousValue;
}
function coordAll(geojson) {
var coords = [];
coordEach(geojson, function (coord) {
coords.push(coord);
});
return coords;
}
function geomEach(geojson, callback) {
var i,
j,
g,
geometry,
stopG,
geometryMaybeCollection,
isGeometryCollection,
featureProperties,
featureBBox,
featureId,
featureIndex = 0,
isFeatureCollection = geojson.type === "FeatureCollection",
isFeature = geojson.type === "Feature",
stop = isFeatureCollection ? geojson.features.length : 1;
for (i = 0; i < stop; i++) {
geometryMaybeCollection = isFeatureCollection
? geojson.features[i].geometry
: isFeature
? geojson.geometry
: geojson;
featureProperties = isFeatureCollection
? geojson.features[i].properties
: isFeature
? geojson.properties
: {};
featureBBox = isFeatureCollection
? geojson.features[i].bbox
: isFeature
? geojson.bbox
: undefined;
featureId = isFeatureCollection
? geojson.features[i].id
: isFeature
? geojson.id
: undefined;
isGeometryCollection = geometryMaybeCollection
? geometryMaybeCollection.type === "GeometryCollection"
: false;
stopG = isGeometryCollection
? geometryMaybeCollection.geometries.length
: 1;
for (g = 0; g < stopG; g++) {
geometry = isGeometryCollection
? geometryMaybeCollection.geometries[g]
: geometryMaybeCollection;
if (geometry === null) {
if (
callback(
null,
featureIndex,
featureProperties,
featureBBox,
featureId
) === false
)
return false;
continue;
}
switch (geometry.type) {
case "Point":
case "LineString":
case "MultiPoint":
case "Polygon":
case "MultiLineString":
case "MultiPolygon": {
if (
callback(
geometry,
featureIndex,
featureProperties,
featureBBox,
featureId
) === false
)
return false;
break;
}
case "GeometryCollection": {
for (j = 0; j < geometry.geometries.length; j++) {
if (
callback(
geometry.geometries[j],
featureIndex,
featureProperties,
featureBBox,
featureId
) === false
)
return false;
}
break;
}
default:
throw new Error("Unknown Geometry Type");
}
}
featureIndex++;
}
}
function geomReduce(geojson, callback, initialValue) {
var previousValue = initialValue;
geomEach(
geojson,
function (
currentGeometry,
featureIndex,
featureProperties,
featureBBox,
featureId
) {
if (featureIndex === 0 && initialValue === undefined)
previousValue = currentGeometry;
else
previousValue = callback(
previousValue,
currentGeometry,
featureIndex,
featureProperties,
featureBBox,
featureId
);
}
);
return previousValue;
}
function flattenEach(geojson, callback) {
geomEach(geojson, function (geometry, featureIndex, properties, bbox, id) {
var type = geometry === null ? null : geometry.type;
switch (type) {
case null:
case "Point":
case "LineString":
case "Polygon":
if (
callback(
feature(geometry, properties, { bbox: bbox, id: id }),
featureIndex,
0
) === false
)
return false;
return;
}
var geomType;
switch (type) {
case "MultiPoint":
geomType = "Point";
break;
case "MultiLineString":
geomType = "LineString";
break;
case "MultiPolygon":
geomType = "Polygon";
break;
}
for (
var multiFeatureIndex = 0;
multiFeatureIndex < geometry.coordinates.length;
multiFeatureIndex++
) {
var coordinate = geometry.coordinates[multiFeatureIndex];
var geom = {
type: geomType,
coordinates: coordinate,
};
if (
callback(feature(geom, properties), featureIndex, multiFeatureIndex) ===
false
)
return false;
}
});
}
function flattenReduce(geojson, callback, initialValue) {
var previousValue = initialValue;
flattenEach(
geojson,
function (currentFeature, featureIndex, multiFeatureIndex) {
if (
featureIndex === 0 &&
multiFeatureIndex === 0 &&
initialValue === undefined
)
previousValue = currentFeature;
else
previousValue = callback(
previousValue,
currentFeature,
featureIndex,
multiFeatureIndex
);
}
);
return previousValue;
}
function segmentEach(geojson, callback) {
flattenEach(geojson, function (feature, featureIndex, multiFeatureIndex) {
var segmentIndex = 0;
if (!feature.geometry) return;
var type = feature.geometry.type;
if (type === "Point" || type === "MultiPoint") return;
var previousCoords;
var previousFeatureIndex = 0;
var previousMultiIndex = 0;
var prevGeomIndex = 0;
if (
coordEach(
feature,
function (
currentCoord,
coordIndex,
featureIndexCoord,
multiPartIndexCoord,
geometryIndex
) {
if (
previousCoords === undefined ||
featureIndex > previousFeatureIndex ||
multiPartIndexCoord > previousMultiIndex ||
geometryIndex > prevGeomIndex
) {
previousCoords = currentCoord;
previousFeatureIndex = featureIndex;
previousMultiIndex = multiPartIndexCoord;
prevGeomIndex = geometryIndex;
segmentIndex = 0;
return;
}
var currentSegment = lineString(
[previousCoords, currentCoord],
feature.properties
);
if (
callback(
currentSegment,
featureIndex,
multiFeatureIndex,
geometryIndex,
segmentIndex
) === false
)
return false;
segmentIndex++;
previousCoords = currentCoord;
}
) === false
)
return false;
});
}
function segmentReduce(geojson, callback, initialValue) {
var previousValue = initialValue;
var started = false;
segmentEach(
geojson,
function (
currentSegment,
featureIndex,
multiFeatureIndex,
geometryIndex,
segmentIndex
) {
if (started === false && initialValue === undefined)
previousValue = currentSegment;
else
previousValue = callback(
previousValue,
currentSegment,
featureIndex,
multiFeatureIndex,
geometryIndex,
segmentIndex
);
started = true;
}
);
return previousValue;
}
function lineEach(geojson, callback) {
if (!geojson) throw new Error("geojson is required");
flattenEach(geojson, function (feature, featureIndex, multiFeatureIndex) {
if (feature.geometry === null) return;
var type = feature.geometry.type;
var coords = feature.geometry.coordinates;
switch (type) {
case "LineString":
if (callback(feature, featureIndex, multiFeatureIndex, 0, 0) === false)
return false;
break;
case "Polygon":
for (
var geometryIndex = 0;
geometryIndex < coords.length;
geometryIndex++
) {
if (
callback(
lineString(coords[geometryIndex], feature.properties),
featureIndex,
multiFeatureIndex,
geometryIndex
) === false
)
return false;
}
break;
}
});
}
function lineReduce(geojson, callback, initialValue) {
var previousValue = initialValue;
lineEach(
geojson,
function (currentLine, featureIndex, multiFeatureIndex, geometryIndex) {
if (featureIndex === 0 && initialValue === undefined)
previousValue = currentLine;
else
previousValue = callback(
previousValue,
currentLine,
featureIndex,
multiFeatureIndex,
geometryIndex
);
}
);
return previousValue;
}
function findSegment(geojson, options) {
options = options || {};
if (!isObject(options)) throw new Error("options is invalid");
var featureIndex = options.featureIndex || 0;
var multiFeatureIndex = options.multiFeatureIndex || 0;
var geometryIndex = options.geometryIndex || 0;
var segmentIndex = options.segmentIndex || 0;
var properties = options.properties;
var geometry;
switch (geojson.type) {
case "FeatureCollection":
if (featureIndex < 0)
featureIndex = geojson.features.length + featureIndex;
properties = properties || geojson.features[featureIndex].properties;
geometry = geojson.features[featureIndex].geometry;
break;
case "Feature":
properties = properties || geojson.properties;
geometry = geojson.geometry;
break;
case "Point":
case "MultiPoint":
return null;
case "LineString":
case "Polygon":
case "MultiLineString":
case "MultiPolygon":
geometry = geojson;
break;
default:
throw new Error("geojson is invalid");
}
if (geometry === null) return null;
var coords = geometry.coordinates;
switch (geometry.type) {
case "Point":
case "MultiPoint":
return null;
case "LineString":
if (segmentIndex < 0) segmentIndex = coords.length + segmentIndex - 1;
return lineString(
[coords[segmentIndex], coords[segmentIndex + 1]],
properties,
options
);
case "Polygon":
if (geometryIndex < 0) geometryIndex = coords.length + geometryIndex;
if (segmentIndex < 0)
segmentIndex = coords[geometryIndex].length + segmentIndex - 1;
return lineString(
[
coords[geometryIndex][segmentIndex],
coords[geometryIndex][segmentIndex + 1],
],
properties,
options
);
case "MultiLineString":
if (multiFeatureIndex < 0)
multiFeatureIndex = coords.length + multiFeatureIndex;
if (segmentIndex < 0)
segmentIndex = coords[multiFeatureIndex].length + segmentIndex - 1;
return lineString(
[
coords[multiFeatureIndex][segmentIndex],
coords[multiFeatureIndex][segmentIndex + 1],
],
properties,
options
);
case "MultiPolygon":
if (multiFeatureIndex < 0)
multiFeatureIndex = coords.length + multiFeatureIndex;
if (geometryIndex < 0)
geometryIndex = coords[multiFeatureIndex].length + geometryIndex;
if (segmentIndex < 0)
segmentIndex =
coords[multiFeatureIndex][geometryIndex].length - segmentIndex - 1;
return lineString(
[
coords[multiFeatureIndex][geometryIndex][segmentIndex],
coords[multiFeatureIndex][geometryIndex][segmentIndex + 1],
],
properties,
options
);
}
throw new Error("geojson is invalid");
}
function findPoint(geojson, options) {
options = options || {};
if (!isObject(options)) throw new Error("options is invalid");
var featureIndex = options.featureIndex || 0;
var multiFeatureIndex = options.multiFeatureIndex || 0;
var geometryIndex = options.geometryIndex || 0;
var coordIndex = options.coordIndex || 0;
var properties = options.properties;
var geometry;
switch (geojson.type) {
case "FeatureCollection":
if (featureIndex < 0)
featureIndex = geojson.features.length + featureIndex;
properties = properties || geojson.features[featureIndex].properties;
geometry = geojson.features[featureIndex].geometry;
break;
case "Feature":
properties = properties || geojson.properties;
geometry = geojson.geometry;
break;
case "Point":
case "MultiPoint":
return null;
case "LineString":
case "Polygon":
case "MultiLineString":
case "MultiPolygon":
geometry = geojson;
break;
default:
throw new Error("geojson is invalid");
}
if (geometry === null) return null;
var coords = geometry.coordinates;
switch (geometry.type) {
case "Point":
return point(coords, properties, options);
case "MultiPoint":
if (multiFeatureIndex < 0)
multiFeatureIndex = coords.length + multiFeatureIndex;
return point(coords[multiFeatureIndex], properties, options);
case "LineString":
if (coordIndex < 0) coordIndex = coords.length + coordIndex;
return point(coords[coordIndex], properties, options);
case "Polygon":
if (geometryIndex < 0) geometryIndex = coords.length + geometryIndex;
if (coordIndex < 0)
coordIndex = coords[geometryIndex].length + coordIndex;
return point(coords[geometryIndex][coordIndex], properties, options);
case "MultiLineString":
if (multiFeatureIndex < 0)
multiFeatureIndex = coords.length + multiFeatureIndex;
if (coordIndex < 0)
coordIndex = coords[multiFeatureIndex].length + coordIndex;
return point(coords[multiFeatureIndex][coordIndex], properties, options);
case "MultiPolygon":
if (multiFeatureIndex < 0)
multiFeatureIndex = coords.length + multiFeatureIndex;
if (geometryIndex < 0)
geometryIndex = coords[multiFeatureIndex].length + geometryIndex;
if (coordIndex < 0)
coordIndex =
coords[multiFeatureIndex][geometryIndex].length - coordIndex;
return point(
coords[multiFeatureIndex][geometryIndex][coordIndex],
properties,
options
);
}
throw new Error("geojson is invalid");
}
function bbox(geojson) {
var result = [Infinity, Infinity, -Infinity, -Infinity];
coordEach(geojson, function (coord) {
if (result[0] > coord[0]) {
result[0] = coord[0];
}
if (result[1] > coord[1]) {
result[1] = coord[1];
}
if (result[2] < coord[0]) {
result[2] = coord[0];
}
if (result[3] < coord[1]) {
result[3] = coord[1];
}
});
return result;
}
bbox["default"] = bbox;
function bboxPolygon(bbox, options) {
if (options === void 0) { options = {}; }
var west = Number(bbox[0]);
var south = Number(bbox[1]);
var east = Number(bbox[2]);
var north = Number(bbox[3]);
if (bbox.length === 6) {
throw new Error("@turf/bbox-polygon does not support BBox with 6 positions");
}
var lowLeft = [west, south];
var topLeft = [west, north];
var topRight = [east, north];
var lowRight = [east, south];
return polygon([[lowLeft, lowRight, topRight, topLeft, lowLeft]], options.properties, { bbox: bbox, id: options.id });
}
function getCoord(coord) {
if (!coord) {
throw new Error("coord is required");
}
if (!Array.isArray(coord)) {
if (coord.type === "Feature" &&
coord.geometry !== null &&
coord.geometry.type === "Point") {
return coord.geometry.coordinates;
}
if (coord.type === "Point") {
return coord.coordinates;
}
}
if (Array.isArray(coord) &&
coord.length >= 2 &&
!Array.isArray(coord[0]) &&
!Array.isArray(coord[1])) {
return coord;
}
throw new Error("coord must be GeoJSON Point or an Array of numbers");
}
function getCoords(coords) {
if (Array.isArray(coords)) {
return coords;
}
if (coords.type === "Feature") {
if (coords.geometry !== null) {
return coords.geometry.coordinates;
}
}
else {
if (coords.coordinates) {
return coords.coordinates;
}
}
throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array");
}
function containsNumber(coordinates) {
if (coordinates.length > 1 &&
isNumber(coordinates[0]) &&
isNumber(coordinates[1])) {
return true;
}
if (Array.isArray(coordinates[0]) && coordinates[0].length) {
return containsNumber(coordinates[0]);
}
throw new Error("coordinates must only contain numbers");
}
function geojsonType(value, type, name) {
if (!type || !name) {
throw new Error("type and name required");
}
if (!value || value.type !== type) {
throw new Error("Invalid input to " +
name +
": must be a " +
type +
", given " +
value.type);
}
}
function featureOf(feature, type, name) {
if (!feature) {
throw new Error("No feature passed");
}
if (!name) {
throw new Error(".featureOf() requires a name");
}
if (!feature || feature.type !== "Feature" || !feature.geometry) {
throw new Error("Invalid input to " + name + ", Feature with geometry required");
}
if (!feature.geometry || feature.geometry.type !== type) {
throw new Error("Invalid input to " +
name +
": must be a " +
type +
", given " +
feature.geometry.type);
}
}
function collectionOf(featureCollection, type, name) {
if (!featureCollection) {
throw new Error("No featureCollection passed");
}
if (!name) {
throw new Error(".collectionOf() requires a name");
}
if (!featureCollection || featureCollection.type !== "FeatureCollection") {
throw new Error("Invalid input to " + name + ", FeatureCollection required");
}
for (var _i = 0, _a = featureCollection.features; _i < _a.length; _i++) {
var feature = _a[_i];
if (!feature || feature.type !== "Feature" || !feature.geometry) {
throw new Error("Invalid input to " + name + ", Feature with geometry required");
}
if (!feature.geometry || feature.geometry.type !== type) {
throw new Error("Invalid input to " +
name +
": must be a " +
type +
", given " +
feature.geometry.type);
}
}
}
function getGeom(geojson) {
if (geojson.type === "Feature") {
return geojson.geometry;
}
return geojson;
}
function getType(geojson, _name) {
if (geojson.type === "FeatureCollection") {
return "FeatureCollection";
}
if (geojson.type === "GeometryCollection") {
return "GeometryCollection";
}
if (geojson.type === "Feature" && geojson.geometry !== null) {
return geojson.geometry.type;
}
return geojson.type;
}
function booleanPointInPolygon(point, polygon, options) {
if (options === void 0) { options = {}; }
if (!point) {
throw new Error("point is required");
}
if (!polygon) {
throw new Error("polygon is required");
}
var pt = getCoord(point);
var geom = getGeom(polygon);
var type = geom.type;
var bbox = polygon.bbox;
var polys = geom.coordinates;
if (bbox && inBBox(pt, bbox) === false) {
return false;
}
if (type === "Polygon") {
polys = [polys];
}
var insidePoly = false;
for (var i = 0; i < polys.length && !insidePoly; i++) {
if (inRing(pt, polys[i][0], options.ignoreBoundary)) {
var inHole = false;
var k = 1;
while (k < polys[i].length && !inHole) {
if (inRing(pt, polys[i][k], !options.ignoreBoundary)) {
inHole = true;
}
k++;
}
if (!inHole) {
insidePoly = true;
}
}
}
return insidePoly;
}
function inRing(pt, ring, ignoreBoundary) {
var isInside = false;
if (ring[0][0] === ring[ring.length - 1][0] &&
ring[0][1] === ring[ring.length - 1][1]) {
ring = ring.slice(0, ring.length - 1);
}
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) {
var xi = ring[i][0];
var yi = ring[i][1];
var xj = ring[j][0];
var yj = ring[j][1];
var onBoundary = pt[1] * (xi - xj) + yi * (xj - pt[0]) + yj * (pt[0] - xi) === 0 &&
(xi - pt[0]) * (xj - pt[0]) <= 0 &&
(yi - pt[1]) * (yj - pt[1]) <= 0;
if (onBoundary) {
return !ignoreBoundary;
}
var intersect = yi > pt[1] !== yj > pt[1] &&
pt[0] < ((xj - xi) * (pt[1] - yi)) / (yj - yi) + xi;
if (intersect) {
isInside = !isInside;
}
}
return isInside;
}
function inBBox(pt, bbox) {
return (bbox[0] <= pt[0] && bbox[1] <= pt[1] && bbox[2] >= pt[0] && bbox[3] >= pt[1]);
}
export { bbox, bboxPolygon, bearingToAzimuth, booleanPointInPolygon, collectionOf, containsNumber, convertArea, convertLength, coordAll, coordEach, coordReduce, feature, featureCollection, featureEach, featureOf, featureReduce, findPoint, findSegment, flattenEach, flattenReduce, geojsonType, geomEach, geomReduce, geometry, geometryCollection, getCoord, getCoords, getGeom, getType, lengthToDegrees, lengthToRadians, lineEach, lineReduce, lineString, lineStrings, multiLineString, multiPoint, multiPolygon, point, points, polygon, polygons, propEach, propReduce, radiansToLength, segmentEach, segmentReduce };