| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
- var _explode = require('@turf/explode');
- var _center = require('@turf/center');
- var _nearestpoint = require('@turf/nearest-point');
- var _booleanpointinpolygon = require('@turf/boolean-point-in-polygon');
- var _helpers = require('@turf/helpers');
- function pointOnFeature(geojson) {
- const fc = normalize(geojson);
- const cent = _center.center.call(void 0, fc);
- let onSurface = false;
- let i = 0;
- while (!onSurface && i < fc.features.length) {
- const geom = fc.features[i].geometry;
- let x, y, x1, y1, x2, y2;
- let onLine = false;
- if (geom.type === "Point") {
- if (cent.geometry.coordinates[0] === geom.coordinates[0] && cent.geometry.coordinates[1] === geom.coordinates[1]) {
- onSurface = true;
- }
- } else if (geom.type === "MultiPoint") {
- let onMultiPoint = false;
- let k = 0;
- while (!onMultiPoint && k < geom.coordinates.length) {
- if (cent.geometry.coordinates[0] === geom.coordinates[k][0] && cent.geometry.coordinates[1] === geom.coordinates[k][1]) {
- onSurface = true;
- onMultiPoint = true;
- }
- k++;
- }
- } else if (geom.type === "LineString") {
- let k = 0;
- while (!onLine && k < geom.coordinates.length - 1) {
- x = cent.geometry.coordinates[0];
- y = cent.geometry.coordinates[1];
- x1 = geom.coordinates[k][0];
- y1 = geom.coordinates[k][1];
- x2 = geom.coordinates[k + 1][0];
- y2 = geom.coordinates[k + 1][1];
- if (pointOnSegment(x, y, x1, y1, x2, y2)) {
- onLine = true;
- onSurface = true;
- }
- k++;
- }
- } else if (geom.type === "MultiLineString") {
- let j = 0;
- while (j < geom.coordinates.length) {
- onLine = false;
- let k = 0;
- const line = geom.coordinates[j];
- while (!onLine && k < line.length - 1) {
- x = cent.geometry.coordinates[0];
- y = cent.geometry.coordinates[1];
- x1 = line[k][0];
- y1 = line[k][1];
- x2 = line[k + 1][0];
- y2 = line[k + 1][1];
- if (pointOnSegment(x, y, x1, y1, x2, y2)) {
- onLine = true;
- onSurface = true;
- }
- k++;
- }
- j++;
- }
- } else if (geom.type === "Polygon" || geom.type === "MultiPolygon") {
- if (_booleanpointinpolygon.booleanPointInPolygon.call(void 0, cent, geom)) {
- onSurface = true;
- }
- }
- i++;
- }
- if (onSurface) {
- return cent;
- } else {
- const vertices = _helpers.featureCollection.call(void 0, []);
- for (let f = 0; f < fc.features.length; f++) {
- vertices.features = vertices.features.concat(
- _explode.explode.call(void 0, fc.features[f]).features
- );
- }
- return _helpers.point.call(void 0, _nearestpoint.nearestPoint.call(void 0, cent, vertices).geometry.coordinates);
- }
- }
- function normalize(geojson) {
- if (geojson.type !== "FeatureCollection") {
- if (geojson.type !== "Feature") {
- return _helpers.featureCollection.call(void 0, [_helpers.feature.call(void 0, geojson)]);
- }
- return _helpers.featureCollection.call(void 0, [geojson]);
- }
- return geojson;
- }
- function pointOnSegment(x, y, x1, y1, x2, y2) {
- const ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
- const ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
- const pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
- return ab === ap + pb;
- }
- var turf_point_on_feature_default = pointOnFeature;
- exports.default = turf_point_on_feature_default; exports.pointOnFeature = pointOnFeature;
- //# sourceMappingURL=index.cjs.map
|