Overlapping self owned user polygons now union each other
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@ exports.connect = function(done){
|
||||
state.pool = mysql.createPool({
|
||||
host: 'localhost',
|
||||
user: 'root',
|
||||
password: '',
|
||||
password: 'root',
|
||||
port: 3306,
|
||||
database: "territory-walker",
|
||||
//multipleStatements: true
|
||||
|
||||
+40
-1
@@ -53,6 +53,35 @@ BEGIN
|
||||
END //
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER //
|
||||
CREATE PROCEDURE selectUserPolygon
|
||||
(IN gID VARCHAR(6),
|
||||
IN usID VARCHAR(6))
|
||||
BEGIN
|
||||
SELECT * FROM `polygonPoints`
|
||||
JOIN `polygonArea` ON
|
||||
`polygonArea`.polygonID = polygonPoints.polygonID
|
||||
JOIN (SELECT * FROM `userJoinedGames` WHERE gameID = gID) AS userColour ON
|
||||
`polygonArea`.userID = userColour.UserID
|
||||
WHERE
|
||||
polygonArea.gameID = gID AND polygonArea.userID = usID;
|
||||
END //
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER //
|
||||
CREATE PROCEDURE selectNonUserPolygon
|
||||
(IN gID VARCHAR(6),
|
||||
IN usID VARCHAR(6))
|
||||
BEGIN
|
||||
SELECT * FROM `polygonPoints`
|
||||
JOIN `polygonArea` ON
|
||||
`polygonArea`.polygonID = polygonPoints.polygonID
|
||||
JOIN (SELECT * FROM `userJoinedGames` WHERE gameID = gID) AS userColour ON
|
||||
`polygonArea`.userID = userColour.UserID
|
||||
WHERE
|
||||
polygonArea.gameID = gID AND polygonArea.userID != usID;
|
||||
END //
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER //
|
||||
CREATE PROCEDURE insertPolygon
|
||||
@@ -70,6 +99,15 @@ BEGIN
|
||||
END //
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER //
|
||||
CREATE PROCEDURE deletePolygon
|
||||
(IN id VARCHAR(36))
|
||||
BEGIN
|
||||
DELETE FROM `polygonPoints` WHERE polygonID = id;
|
||||
DELETE FROM `polygonArea` WHERE polygonID = id;
|
||||
END //
|
||||
DELIMITER ;
|
||||
|
||||
INSERT INTO `gameInstances` (gameID) VALUES ("samdum");
|
||||
INSERT INTO `users` (userID, nickname) VALUES ("samded", "Lesmaux");
|
||||
INSERT INTO `users` (userID, nickname) VALUES ("fanded", "Umbra Sheep");
|
||||
@@ -80,7 +118,8 @@ INSERT INTO `polygonPoints` (polygonID, lat, lng) VALUES
|
||||
("someuniqueid", -43.5623, 172.5655),
|
||||
("someuniqueid", -43.5623, 172.5650),
|
||||
("someuniqueid", -43.5628, 172.5650),
|
||||
("someuniqueid", -43.5628, 172.5655);
|
||||
("someuniqueid", -43.5628, 172.5655),
|
||||
("someuniqueid", -43.5623, 172.5655);
|
||||
-- TODO CREATE PROCEDURE FOR GENERATING GAME ID
|
||||
-- concat(substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
|
||||
-- substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
|
||||
|
||||
+6
-30
@@ -1,6 +1,7 @@
|
||||
let db = require("../config/db.js");
|
||||
let mathFunc = require("./mathFunc.js");
|
||||
let util = require("util");
|
||||
let areaFunctions = require("./areaFunctions.js");
|
||||
|
||||
module.exports = {
|
||||
getPolygons : function (req, res) {
|
||||
@@ -49,36 +50,11 @@ module.exports = {
|
||||
let distance = mathFunc.gpsToMeters(firstPoint.lat, firstPoint.lng, lastPoint.lat, lastPoint.lng);
|
||||
console.log(distance);
|
||||
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) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return res.send(400);
|
||||
}
|
||||
|
||||
let polygonID = rows[0][0].polygonID;
|
||||
query = "INSERT INTO polygonPoints (polygonID, lat, lng) VALUES ";
|
||||
let delimiterNecessary = false;
|
||||
|
||||
points.map((point) => {
|
||||
if (delimiterNecessary) {
|
||||
query += ",";
|
||||
}
|
||||
query += util.format("(\"%s\",%s,%s)", polygonID, point.lat, point.lng);
|
||||
delimiterNecessary = true;
|
||||
});
|
||||
query += ";";
|
||||
|
||||
db.get().query(query, function(err, pointRows) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return res.send(400);
|
||||
}
|
||||
res.send(rows[0][0]);
|
||||
return;
|
||||
});
|
||||
});
|
||||
if (lastPoint.lat != firstPoint.lat && lastPoint.lng != firstPoint.lng) {
|
||||
// points.pop();
|
||||
points.push({lat: firstPoint.lat, lng: lastPoint.lng});
|
||||
}
|
||||
areaFunctions.collatePolygons(res, gameID, userID, points);
|
||||
} else {
|
||||
res.send([]);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
let db = require("../config/db.js");
|
||||
let util = require("util");
|
||||
let turf = require("turf");
|
||||
let mathFunc = require("./mathFunc.js");
|
||||
|
||||
module.exports = {
|
||||
organisePolygonPoints(polygonPoints) {
|
||||
console.log(polygonPoints);
|
||||
let polygons = []
|
||||
let polygonDict = {};
|
||||
polygonPoints.map((point) => {
|
||||
if (!(point.polygonID in polygonDict)) {
|
||||
polygonDict[point.polygonID] = {};
|
||||
polygonDict[point.polygonID].coords = [];
|
||||
polygonDict[point.polygonID].gameID = point.gameID;
|
||||
polygonDict[point.polygonID].userID = point.userID;
|
||||
polygonDict[point.polygonID].colour = point.colour;
|
||||
polygonDict[point.polygonID].polygonID = point.polygonID;
|
||||
}
|
||||
polygonDict[point.polygonID].coords.push({lat: point.lat, lng: point.lng});
|
||||
});
|
||||
for (let [polygon, polygonData] of Object.entries(polygonDict)){
|
||||
polygons.push(polygonDict[polygon]);
|
||||
}
|
||||
return polygons
|
||||
},
|
||||
submitFinalCollation(res, gameID, userID, points, mergedPolygonIDs) {
|
||||
console.log(module.exports.convertPointsObjToArray(points));
|
||||
let area = mathFunc.calculateArea(points);
|
||||
query = util.format("CALL insertPolygon(\"%s\", \"%s\", %d)", gameID, userID, area);
|
||||
db.get().query(query, function(err, rows) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return res.send(400);
|
||||
}
|
||||
|
||||
let polygonID = rows[0][0].polygonID;
|
||||
query = "INSERT INTO polygonPoints (polygonID, lat, lng) VALUES ";
|
||||
let delimiterNecessary = false;
|
||||
|
||||
points.map((point) => {
|
||||
if (delimiterNecessary) {
|
||||
query += ",";
|
||||
}
|
||||
query += util.format("(\"%s\",%s,%s)", polygonID, point.lat, point.lng);
|
||||
delimiterNecessary = true;
|
||||
});
|
||||
query += ";";
|
||||
|
||||
db.get().query(query, function(err, pointRows) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return res.send(400);
|
||||
}
|
||||
module.exports.deleteMergedPolygons(mergedPolygonIDs, () => res.send(rows[0][0]));
|
||||
// res.send(rows[0][0]);
|
||||
return;
|
||||
});
|
||||
});
|
||||
},
|
||||
deleteMergedPolygons(mergedPolygonIDs, next) {
|
||||
if (mergedPolygonIDs.length > 0) {
|
||||
let id = mergedPolygonIDs.pop();
|
||||
console.log(id);
|
||||
let deleteQuery = util.format("CALL deletePolygon(\"%s\");", id);
|
||||
db.get().query(deleteQuery, function(err, pointRows) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return res.send(400);
|
||||
}
|
||||
module.exports.deleteMergedPolygons(mergedPolygonIDs, next);
|
||||
return;
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
},
|
||||
collatePolygons(res, gameID, userID, points) {
|
||||
let query = util.format("CALL selectUserPolygon(\"%s\", \"%s\");", gameID, userID);
|
||||
db.get().query(query, function (err, rows) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
res.send(500);
|
||||
}
|
||||
let polygonPoints = points;
|
||||
let polygons = module.exports.organisePolygonPoints(rows[0]);
|
||||
let polygonResult = [];
|
||||
let polygonsMerged = [];
|
||||
polygons.map((polygon) => {
|
||||
let intersect = module.exports.checkIntersects(polygonPoints, polygon.coords);
|
||||
if (intersect != null) {
|
||||
polygonPoints = module.exports.unionPolygon(polygonPoints, polygon.coords);
|
||||
polygonsMerged.push(polygon.polygonID);
|
||||
} else {
|
||||
polygonResult.push(polygon);
|
||||
}
|
||||
});
|
||||
module.exports.submitFinalCollation(res, gameID, userID, polygonPoints, polygonsMerged);
|
||||
});
|
||||
},
|
||||
unionPolygon(points1, points2){
|
||||
let arrayPoints1 = [module.exports.convertPointsObjToArray(points1)];
|
||||
let arrayPoints2 = [module.exports.convertPointsObjToArray(points2)];
|
||||
let poly1 = turf.polygon(arrayPoints1);
|
||||
let poly2 = turf.polygon(arrayPoints2);
|
||||
let unionPoly = turf.union(poly1, poly2);
|
||||
return module.exports.convertPointsArrayToObj(unionPoly.geometry.coordinates[0]);
|
||||
},
|
||||
checkIntersects(points1, points2) {
|
||||
let arrayPoints1 = [module.exports.convertPointsObjToArray(points1)];
|
||||
let arrayPoints2 = [module.exports.convertPointsObjToArray(points2)];
|
||||
let poly1 = turf.polygon(arrayPoints1);
|
||||
let poly2 = turf.polygon(arrayPoints2);
|
||||
return turf.intersect(poly1, poly2);
|
||||
},
|
||||
convertPointsObjToArray(points){
|
||||
let array = points.map((point) => {
|
||||
return [point.lng, point.lat];
|
||||
});
|
||||
return array;
|
||||
},
|
||||
convertPointsArrayToObj(points){
|
||||
let array = points.map((point) => {
|
||||
return {lat: point[1], lng: point[0]};
|
||||
});
|
||||
return array;
|
||||
}
|
||||
}
|
||||
+39
-36
@@ -14,46 +14,49 @@ module.exports = {
|
||||
return d * 1000; // meters
|
||||
},
|
||||
calculateArea: function (points) {
|
||||
var polygon = turf.polygon([[[125, -15], [113, -22], [154, -27], [144, -15], [125, -15]]]);
|
||||
var polygon = turf.polygon([points.map((point) => {
|
||||
return [point.lng, point.lat];
|
||||
})]);
|
||||
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"});
|
||||
return 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 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 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
|
||||
// var intersection = turf.intersect(poly1, poly2);
|
||||
// console.log(intersection);//null if no intersect
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user