mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-14 17:52:55 +00:00
Fix merge (util.geo -> geo)
This commit is contained in:
35
js/id/geo.js
35
js/id/geo.js
@@ -73,3 +73,38 @@ iD.geo.polygonIntersectsPolygon = function(outer, inner) {
|
||||
return iD.geo.pointInPolygon(point, outer);
|
||||
});
|
||||
};
|
||||
|
||||
// May have issues with self interesecting polygons
|
||||
iD.geo.polygonCentroid = function(polygon) {
|
||||
var x = 0,
|
||||
y = 0,
|
||||
area = iD.geo.area(polygon);
|
||||
for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
||||
var xi = polygon[i][0], yi = polygon[i][1];
|
||||
var xj = polygon[j][0], yj = polygon[j][1];
|
||||
x += (xi + xj) * (xj * yi - xi * yj);
|
||||
y += (yi + yj) * (xj * yi - xi * yj);
|
||||
}
|
||||
return [x / 6 / area, y / 6 / area];
|
||||
};
|
||||
|
||||
iD.geo.area = function(polygon) {
|
||||
var area = 0;
|
||||
for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
||||
var xi = polygon[i][0], yi = polygon[i][1];
|
||||
var xj = polygon[j][0], yj = polygon[j][1];
|
||||
area += xj * yi - xi * yj;
|
||||
}
|
||||
return area/2;
|
||||
};
|
||||
|
||||
iD.geo.pathLength = function(path) {
|
||||
var length = 0,
|
||||
dx, dy;
|
||||
for (var i = 0; i < path.length - 1; i++) {
|
||||
dx = path[i][0] - path[i + 1][0];
|
||||
dy = path[i][1] - path[i + 1][1];
|
||||
length += Math.sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
return length;
|
||||
};
|
||||
|
||||
@@ -255,7 +255,7 @@ iD.svg.Labels = function(projection) {
|
||||
|
||||
function getLineLabel(entity, width, height) {
|
||||
var nodes = _.pluck(entity.nodes, 'loc').map(projection),
|
||||
length = iD.util.geo.pathLength(nodes);
|
||||
length = iD.geo.pathLength(nodes);
|
||||
if (length < width + 20) return;
|
||||
|
||||
// 50, 40, 60, 30, 70
|
||||
@@ -284,7 +284,7 @@ iD.svg.Labels = function(projection) {
|
||||
function getAreaLabel(entity, width, height) {
|
||||
var nodes = _.pluck(entity.nodes, 'loc')
|
||||
.map(iD.svg.RoundProjection(projection)),
|
||||
centroid = iD.util.geo.polygonCentroid(nodes),
|
||||
centroid = iD.geo.polygonCentroid(nodes),
|
||||
extent = entity.extent(graph),
|
||||
entitywidth = projection(extent[1])[0] - projection(extent[0])[0];
|
||||
|
||||
|
||||
111
js/id/util.js
111
js/id/util.js
@@ -82,114 +82,3 @@ iD.util.getStyle = function(selector) {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
iD.util.geo = {};
|
||||
|
||||
iD.util.geo.roundCoords = function(c) {
|
||||
return [Math.floor(c[0]), Math.floor(c[1])];
|
||||
};
|
||||
|
||||
iD.util.geo.interp = function(p1, p2, t) {
|
||||
return [p1[0] + (p2[0] - p1[0]) * t,
|
||||
p1[1] + (p2[1] - p1[1]) * t];
|
||||
};
|
||||
|
||||
iD.util.geo.dist = function(a, b) {
|
||||
return Math.sqrt(Math.pow(a[0] - b[0], 2) +
|
||||
Math.pow(a[1] - b[1], 2));
|
||||
};
|
||||
|
||||
iD.util.geo.chooseIndex = function(way, point, map) {
|
||||
var dist = iD.util.geo.dist,
|
||||
projNodes = way.nodes.map(function(n) {
|
||||
return map.projection(n.loc);
|
||||
});
|
||||
|
||||
for (var i = 0, changes = []; i < projNodes.length - 1; i++) {
|
||||
changes[i] =
|
||||
(dist(projNodes[i], point) + dist(point, projNodes[i + 1])) /
|
||||
dist(projNodes[i], projNodes[i + 1]);
|
||||
}
|
||||
|
||||
var idx = _.indexOf(changes, _.min(changes)),
|
||||
ratio = dist(projNodes[idx], point) / dist(projNodes[idx], projNodes[idx + 1]),
|
||||
loc = iD.util.geo.interp(way.nodes[idx].loc, way.nodes[idx + 1].loc, ratio);
|
||||
|
||||
return {
|
||||
index: idx + 1,
|
||||
loc: loc
|
||||
};
|
||||
};
|
||||
|
||||
// Return whether point is contained in polygon.
|
||||
//
|
||||
// `point` should be a 2-item array of coordinates.
|
||||
// `polygon` should be an array of 2-item arrays of coordinates.
|
||||
//
|
||||
// From https://github.com/substack/point-in-polygon.
|
||||
// ray-casting algorithm based on
|
||||
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
|
||||
//
|
||||
iD.util.geo.pointInPolygon = function(point, polygon) {
|
||||
var x = point[0],
|
||||
y = point[1],
|
||||
inside = false;
|
||||
|
||||
for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
||||
var xi = polygon[i][0], yi = polygon[i][1];
|
||||
var xj = polygon[j][0], yj = polygon[j][1];
|
||||
|
||||
var intersect = ((yi > y) != (yj > y)) &&
|
||||
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
|
||||
if (intersect) inside = !inside;
|
||||
}
|
||||
|
||||
return inside;
|
||||
};
|
||||
|
||||
iD.util.geo.polygonContainsPolygon = function(outer, inner) {
|
||||
return _.every(inner, function (point) {
|
||||
return iD.util.geo.pointInPolygon(point, outer);
|
||||
});
|
||||
};
|
||||
|
||||
iD.util.geo.polygonIntersectsPolygon = function(outer, inner) {
|
||||
return _.some(inner, function (point) {
|
||||
return iD.util.geo.pointInPolygon(point, outer);
|
||||
});
|
||||
};
|
||||
|
||||
// May have issues with self interesecting polygons
|
||||
iD.util.geo.polygonCentroid = function(polygon) {
|
||||
var x = 0,
|
||||
y = 0,
|
||||
area = iD.util.geo.area(polygon);
|
||||
for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
||||
var xi = polygon[i][0], yi = polygon[i][1];
|
||||
var xj = polygon[j][0], yj = polygon[j][1];
|
||||
x += (xi + xj) * (xj * yi - xi * yj);
|
||||
y += (yi + yj) * (xj * yi - xi * yj);
|
||||
}
|
||||
return [x / 6 / area, y / 6 / area];
|
||||
};
|
||||
|
||||
iD.util.geo.area = function(polygon) {
|
||||
var area = 0;
|
||||
for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
||||
var xi = polygon[i][0], yi = polygon[i][1];
|
||||
var xj = polygon[j][0], yj = polygon[j][1];
|
||||
area += xj * yi - xi * yj;
|
||||
}
|
||||
return area/2;
|
||||
};
|
||||
|
||||
iD.util.geo.pathLength = function(path) {
|
||||
var length = 0,
|
||||
dx, dy;
|
||||
for (var i = 0; i < path.length - 1; i++) {
|
||||
dx = path[i][0] - path[i + 1][0];
|
||||
dy = path[i][1] - path[i + 1][1];
|
||||
length += Math.sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
return length;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user