Socket
Socket
Sign inDemoInstall

turf-point-on-surface

Package Overview
Dependencies
Maintainers
9
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

turf-point-on-surface - npm Package Compare versions

Comparing version 1.1.1 to 3.0.0-canary.2f5f7167

4

bench.js

@@ -5,3 +5,3 @@ var centroid = require('./');

var fc = JSON.parse(fs.readFileSync(__dirname + '/fixtures/polygons.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname + '/test/polygons.geojson'));

@@ -19,2 +19,2 @@ var suite = new Benchmark.Suite('turf-point-on-surface');

})
.run();
.run();

@@ -1,2 +0,2 @@

var featureCollection = require('turf-featurecollection');
var featureCollection = require('turf-helpers').featureCollection;
var centroid = require('turf-center');

@@ -8,4 +8,3 @@ var distance = require('turf-distance');

/**
* Finds a {@link Point} guaranteed to be on the surface of
* {@link GeoJSON} object.
* Takes a feature and returns a {@link Point} guaranteed to be on the surface of the feature.
*

@@ -16,5 +15,4 @@ * * Given a {@link Polygon}, the point will be in the area of the polygon

*
* @module turf/point-on-surface
* @category measurement
* @param {GeoJSON} input any GeoJSON object
* @param {(Feature|FeatureCollection)} fc any feature or set of features
* @returns {Feature} a point on the surface of `input`

@@ -37,116 +35,118 @@ * @example

*/
module.exports = function(fc) {
// normalize
if(fc.type != 'FeatureCollection') {
if(fc.type != 'Feature') {
fc = {
type: 'Feature',
geometry: fc,
properties: {}
};
function pointOnSurface(fc) {
// normalize
if (fc.type !== 'FeatureCollection') {
if (fc.type !== 'Feature') {
fc = {
type: 'Feature',
geometry: fc,
properties: {}
};
}
fc = featureCollection([fc]);
}
fc = featureCollection([fc]);
}
//get centroid
var cent = centroid(fc);
//get centroid
var cent = centroid(fc);
// check to see if centroid is on surface
var onSurface = false;
var i = 0;
while(!onSurface && i < fc.features.length) {
var geom = fc.features[i].geometry;
if (geom.type === 'Point') {
if (cent.geometry.coordinates[0] === geom.coordinates[0] &&
// check to see if centroid is on surface
var onSurface = false;
var i = 0;
while (!onSurface && i < fc.features.length) {
var geom = fc.features[i].geometry;
var x, y, x1, y1, x2, y2, k;
var 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') {
var onMultiPoint = false;
var k = 0;
while(!onMultiPoint && k < geom.coordinates.length) {
if (cent.geometry.coordinates[0] === geom.coordinates[k][0] &&
onSurface = true;
}
} else if (geom.type === 'MultiPoint') {
var onMultiPoint = false;
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;
onSurface = true;
onMultiPoint = true;
}
k++;
}
} else if (geom.type === 'LineString') {
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') {
var j = 0;
while (j < geom.coordinates.length) {
onLine = false;
k = 0;
var 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') {
var f = {
type: 'Feature',
geometry: geom,
properties: {}
};
if (inside(cent, f)) {
onSurface = true;
}
}
k++;
}
} else if(geom.type === 'LineString') {
var onLine = false;
var k = 0;
while(!onLine && k < geom.coordinates.length - 1) {
var x = cent.geometry.coordinates[0];
var y = cent.geometry.coordinates[1];
var x1 = geom.coordinates[k][0];
var y1 = geom.coordinates[k][1];
var x2 = geom.coordinates[k+1][0];
var y2 = geom.coordinates[k+1][1];
if(pointOnSegment(x, y, x1, y1, x2, y2)) {
onLine = true;
onSurface = true;
i++;
}
if (onSurface) {
return cent;
} else {
var vertices = featureCollection([]);
for (i = 0; i < fc.features.length; i++) {
vertices.features = vertices.features.concat(explode(fc.features[i]).features);
}
k++;
}
} else if(geom.type === 'MultiLineString') {
var onMultiLine = false;
var j = 0;
while(!onMultiLine && j < geom.coordinates.length) {
var onLine = false;
var k = 0;
var line = geom.coordinates[j];
while(!onLine && k < line.length - 1) {
var x = cent.geometry.coordinates[0];
var y = cent.geometry.coordinates[1];
var x1 = line[k][0];
var y1 = line[k][1];
var x2 = line[k+1][0];
var y2 = line[k+1][1];
if(pointOnSegment(x, y, x1, y1, x2, y2)) {
onLine = true;
onSurface = true;
}
k++;
var closestVertex;
var closestDistance = Infinity;
for (i = 0; i < vertices.features.length; i++) {
var dist = distance(cent, vertices.features[i], 'miles');
if (dist < closestDistance) {
closestDistance = dist;
closestVertex = vertices.features[i];
}
}
j++;
}
} else if(geom.type === 'Polygon' || geom.type === 'MultiPolygon') {
var f = {
type: 'Feature',
geometry: geom,
properties: {}
};
if(inside(cent, f)) {
onSurface = true;
}
return closestVertex;
}
i++;
}
if(onSurface) {
return cent;
} else {
var vertices = featureCollection([]);
for(var i = 0; i < fc.features.length; i++) {
vertices.features = vertices.features.concat(explode(fc.features[i]).features);
}
function pointOnSegment(x, y, x1, y1, x2, y2) {
var ab = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
var ap = Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
var pb = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
if (ab === ap + pb) {
return true;
}
var closestVertex;
var closestDistance = Infinity;
for(var i = 0; i < vertices.features.length; i++) {
var dist = distance(cent, vertices.features[i], 'miles');
if(dist < closestDistance) {
closestDistance = dist;
closestVertex = vertices.features[i];
}
}
return closestVertex;
}
};
}
function pointOnSegment (x, y, x1, y1, x2, y2) {
var ab = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
var ap = Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
var pb = Math.sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y));
if(ab === ap + pb) {
return true;
}
}
module.exports = pointOnSurface;
{
"name": "turf-point-on-surface",
"version": "1.1.1",
"version": "3.0.0-canary.2f5f7167",
"description": "turf point-on-surface module",
"main": "index.js",
"scripts": {
"test": "node test.js",
"doc": "dox -r < index.js | doxme --readme > README.md"
"test": "node test.js"
},

@@ -30,13 +29,11 @@ "repository": {

"tape": "^3.5.0",
"benchmark": "^1.0.0",
"dox": "^0.6.1",
"doxme": "^1.4.3"
"benchmark": "^1.0.0"
},
"dependencies": {
"turf-center": "^1.0.1",
"turf-distance": "^1.0.0",
"turf-explode": "^1.0.0",
"turf-featurecollection": "^1.0.0",
"turf-inside": "^1.1.3"
"turf-center": "^3.0.0-canary.2f5f7167",
"turf-distance": "^3.0.0-canary.2f5f7167",
"turf-explode": "^3.0.0-canary.2f5f7167",
"turf-inside": "^3.0.0-canary.2f5f7167",
"turf-helpers": "^3.0.0-canary.2f5f7167"
}
}

@@ -8,6 +8,5 @@ # turf-point-on-surface

### `turf.pointOnSurface(input)`
### `turf.point-on-surface(input)`
Finds a Point guaranteed to be on the surface of
GeoJSON object.
Takes a feature and returns a Point guaranteed to be on the surface of the feature.

@@ -21,5 +20,5 @@ * Given a Polygon, the point will be in the area of the polygon

| parameter | type | description |
| --------- | ------- | ------------------ |
| `input` | GeoJSON | any GeoJSON object |
| parameter | type | description |
| --------- | -------------------------- | ------------------------------ |
| `input` | Feature\,FeatureCollection | any feature or set of features |

@@ -33,12 +32,17 @@

// place a point on it
//=polygon
var pointOnPolygon = turf.pointOnSurface(polygon);
// show both of them
var fc = turf.featurecollection([polygon, pointOnPolygon]);
//=fc
var resultFeatures = polygon.features.concat(pointOnPolygon);
var result = {
"type": "FeatureCollection",
"features": resultFeatures
};
//=result
```
**Returns** `Feature`, a point on the surface
**Returns** `Feature`, a point on the surface of `input`

@@ -59,1 +63,2 @@ ## Installation

@@ -7,3 +7,3 @@ var test = require('tape');

test('point-on-surface -- closest vertex on polygons', function(t) {
var fc = JSON.parse(fs.readFileSync(__dirname + '/fixtures/polygons.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname + '/test/polygons.geojson'));
var cent = centroid(fc);

@@ -21,3 +21,3 @@

test('point-on-surface -- centroid on polygon surface', function(t) {
var fc = JSON.parse(fs.readFileSync(__dirname + '/fixtures/polygon-in-center.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname + '/test/polygon-in-center.geojson'));
var cent = centroid(fc);

@@ -31,7 +31,7 @@

t.true(inside(cent, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
type: "Feature",
properties: {},
geometry: {
type: "Polygon",
coordinates: [
[

@@ -67,3 +67,3 @@ [

test('point-on-surface -- closest vertex on lines', function(t) {
var fc = JSON.parse(fs.readFileSync(__dirname + '/fixtures/lines.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname + '/test/lines.geojson'));
var cent = centroid(fc);

@@ -81,3 +81,3 @@

test('point-on-surface -- closest vertex on multilinestring', function(t) {
var fc = JSON.parse(fs.readFileSync(__dirname + '/fixtures/multiline.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname + '/test/multiline.geojson'));
var cent = centroid(fc);

@@ -95,3 +95,3 @@

test('point-on-surface -- multipolygon', function(t) {
var fc = JSON.parse(fs.readFileSync(__dirname + '/fixtures/multipolygon.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname + '/test/multipolygon.geojson'));
var cent = centroid(fc);

@@ -109,3 +109,3 @@

test('point-on-surface -- multipoint', function(t) {
var fc = JSON.parse(fs.readFileSync(__dirname + '/fixtures/multipoint.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname + '/test/multipoint.geojson'));
var cent = centroid(fc);

@@ -120,2 +120,2 @@

t.end();
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc