got turf area and intersection and union working

main
Umbra Sheep 7 years ago
parent ad2c57c78d
commit 9d8ed9cf0d

893
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -13,6 +13,7 @@
"mysql": "^2.16.0",
"request": "^2.88.0",
"restify": "^7.2.1",
"restify-simple-versioning": "^1.0.0"
"restify-simple-versioning": "^1.0.0",
"turf": "^3.0.14"
}
}

@ -4,6 +4,7 @@ let util = require("util");
module.exports = {
getPolygons : function (req, res) {
mathFunc.calculateArea([]);
if (req.query.gameID != null) {
let query = "CALL getPolygonPoints(\"" + req.query.gameID + "\");";
db.get().query(query, function(err, rows){
@ -47,7 +48,7 @@ module.exports = {
let lastPoint = points[points.length - 1];
let distance = mathFunc.gpsToMeters(firstPoint.lat, firstPoint.lng, lastPoint.lat, lastPoint.lng);
console.log(distance);
if (distance < 3) {
if (distance < 10) {
//area is just 1 for now we fix later.
query = util.format("CALL insertPolygon(\"%s\", \"%s\", 1)", gameID, userID);
db.get().query(query, function(err, rows) {

@ -1,3 +1,6 @@
// import Turf from "turf";
let turf = require("turf");
module.exports = {
gpsToMeters: function (lat1, lon1, lat2, lon2){ // generally used geo measurement function
var R = 6378.137; // Radius of earth in KM
@ -9,5 +12,48 @@ module.exports = {
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d * 1000; // meters
},
calculateArea: function (points) {
var polygon = turf.polygon([[[125, -15], [113, -22], [154, -27], [144, -15], [125, -15]]]);
var area = turf.area(polygon);
console.log(area);
var poly1 = turf.polygon([[
[-82.574787, 35.594087],
[-82.574787, 35.615581],
[-82.545261, 35.615581],
[-82.545261, 35.594087],
[-82.574787, 35.594087]
]], {"fill": "#0f0"});
var poly2 = turf.polygon([[
[-82.560024, 35.585153],
[-82.560024, 35.602602],
[-82.52964, 35.602602],
[-82.52964, 35.585153],
[-82.560024, 35.585153]
]], {"fill": "#00f"});
var union = turf.union(poly1, poly2);
console.log(union.geometry.coordinates);
var poly1 = turf.polygon([[
[-122.801742, 45.48565],
[-122.801742, 45.60491],
[-122.584762, 45.60491],
[-122.584762, 45.48565],
[-122.801742, 45.48565]
]]);
var poly2 = turf.polygon([[
[-122.520217, 45.535693],
[-122.64038, 45.553967],
[-122.720031, 45.526554],
[-122.669906, 45.507309],
[-122.723464, 45.446643],
[-122.532577, 45.408574],
[-122.487258, 45.477466],
[-122.520217, 45.535693]
]]);
var intersection = turf.intersect(poly1, poly2);
console.log(intersection);//null if no intersect
}
}
Loading…
Cancel
Save