turf-point-on-surface
Advanced tools
Comparing version 1.1.1 to 3.0.0-canary.2f5f7167
@@ -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(); |
218
index.js
@@ -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 | ||
24
test.js
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
2
254
0
61
11976
7
1
1
+ Addedturf-bbox@3.0.12(transitive)
+ Addedturf-center@3.0.12(transitive)
+ Addedturf-distance@3.0.12(transitive)
+ Addedturf-explode@3.0.12(transitive)
+ Addedturf-helpers@3.0.12(transitive)
+ Addedturf-inside@3.0.12(transitive)
+ Addedturf-invariant@3.0.12(transitive)
+ Addedturf-meta@3.0.12(transitive)
- Removedturf-featurecollection@^1.0.0
- Removedminimist@1.2.8(transitive)
- Removedturf-center@1.0.3(transitive)
- Removedturf-distance@1.1.0(transitive)
- Removedturf-explode@1.0.1(transitive)
- Removedturf-extent@1.0.4(transitive)
- Removedturf-featurecollection@1.0.1(transitive)
- Removedturf-inside@1.1.4(transitive)
- Removedturf-invariant@1.0.3(transitive)
- Removedturf-meta@1.0.2(transitive)
- Removedturf-point@2.0.1(transitive)