1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.geom.Line2D;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Comparator;
|
---|
7 | import java.util.HashSet;
|
---|
8 | import java.util.LinkedHashSet;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.command.AddCommand;
|
---|
14 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
15 | import org.openstreetmap.josm.command.Command;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.NodePositionComparator;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Some tools for geometry related tasks.
|
---|
24 | *
|
---|
25 | * @author viesturs
|
---|
26 | */
|
---|
27 | public class Geometry {
|
---|
28 | public enum PolygonIntersection {FIRST_INSIDE_SECOND, SECOND_INSIDE_FIRST, OUTSIDE, CROSSING}
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Will find all intersection and add nodes there for list of given ways. Handles self-intersections too.
|
---|
32 | * And make commands to add the intersection points to ways.
|
---|
33 | * @param List<Way> - a list of ways to test
|
---|
34 | * @return ArrayList<Node> List of new nodes
|
---|
35 | * Prerequisite: no two nodes have the same coordinates.
|
---|
36 | */
|
---|
37 | public static Set<Node> addIntersections(List<Way> ways, boolean test, List<Command> cmds) {
|
---|
38 |
|
---|
39 | //stupid java, cannot instantiate array of generic classes..
|
---|
40 | @SuppressWarnings("unchecked")
|
---|
41 | ArrayList<Node>[] newNodes = new ArrayList[ways.size()];
|
---|
42 | BBox[] wayBounds = new BBox[ways.size()];
|
---|
43 | boolean[] changedWays = new boolean[ways.size()];
|
---|
44 |
|
---|
45 | Set<Node> intersectionNodes = new LinkedHashSet<Node>();
|
---|
46 |
|
---|
47 | //copy node arrays for local usage.
|
---|
48 | for (int pos = 0; pos < ways.size(); pos ++) {
|
---|
49 | newNodes[pos] = new ArrayList<Node>(ways.get(pos).getNodes());
|
---|
50 | wayBounds[pos] = getNodesBounds(newNodes[pos]);
|
---|
51 | changedWays[pos] = false;
|
---|
52 | }
|
---|
53 |
|
---|
54 | //iterate over all way pairs and introduce the intersections
|
---|
55 | Comparator<Node> coordsComparator = new NodePositionComparator();
|
---|
56 |
|
---|
57 | WayLoop: for (int seg1Way = 0; seg1Way < ways.size(); seg1Way ++) {
|
---|
58 | for (int seg2Way = seg1Way; seg2Way < ways.size(); seg2Way ++) {
|
---|
59 |
|
---|
60 | //do not waste time on bounds that do not intersect
|
---|
61 | if (!wayBounds[seg1Way].intersects(wayBounds[seg2Way])) {
|
---|
62 | continue;
|
---|
63 | }
|
---|
64 |
|
---|
65 | ArrayList<Node> way1Nodes = newNodes[seg1Way];
|
---|
66 | ArrayList<Node> way2Nodes = newNodes[seg2Way];
|
---|
67 |
|
---|
68 | //iterate over primary segmemt
|
---|
69 | for (int seg1Pos = 0; seg1Pos + 1 < way1Nodes.size(); seg1Pos ++) {
|
---|
70 |
|
---|
71 | //iterate over secondary segment
|
---|
72 | int seg2Start = seg1Way != seg2Way ? 0: seg1Pos + 2;//skip the adjacent segment
|
---|
73 |
|
---|
74 | for (int seg2Pos = seg2Start; seg2Pos + 1< way2Nodes.size(); seg2Pos ++) {
|
---|
75 |
|
---|
76 | //need to get them again every time, because other segments may be changed
|
---|
77 | Node seg1Node1 = way1Nodes.get(seg1Pos);
|
---|
78 | Node seg1Node2 = way1Nodes.get(seg1Pos + 1);
|
---|
79 | Node seg2Node1 = way2Nodes.get(seg2Pos);
|
---|
80 | Node seg2Node2 = way2Nodes.get(seg2Pos + 1);
|
---|
81 |
|
---|
82 | int commonCount = 0;
|
---|
83 | //test if we have common nodes to add.
|
---|
84 | if (seg1Node1 == seg2Node1 || seg1Node1 == seg2Node2) {
|
---|
85 | commonCount ++;
|
---|
86 |
|
---|
87 | if (seg1Way == seg2Way &&
|
---|
88 | seg1Pos == 0 &&
|
---|
89 | seg2Pos == way2Nodes.size() -2) {
|
---|
90 | //do not add - this is first and last segment of the same way.
|
---|
91 | } else {
|
---|
92 | intersectionNodes.add(seg1Node1);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (seg1Node2 == seg2Node1 || seg1Node2 == seg2Node2) {
|
---|
97 | commonCount ++;
|
---|
98 |
|
---|
99 | intersectionNodes.add(seg1Node2);
|
---|
100 | }
|
---|
101 |
|
---|
102 | //no common nodes - find intersection
|
---|
103 | if (commonCount == 0) {
|
---|
104 | EastNorth intersection = getSegmentSegmentIntersection(
|
---|
105 | seg1Node1.getEastNorth(), seg1Node2.getEastNorth(),
|
---|
106 | seg2Node1.getEastNorth(), seg2Node2.getEastNorth());
|
---|
107 |
|
---|
108 | if (intersection != null) {
|
---|
109 | if (test) {
|
---|
110 | intersectionNodes.add(seg2Node1);
|
---|
111 | return intersectionNodes;
|
---|
112 | }
|
---|
113 |
|
---|
114 | Node newNode = new Node(Main.getProjection().eastNorth2latlon(intersection));
|
---|
115 | Node intNode = newNode;
|
---|
116 | boolean insertInSeg1 = false;
|
---|
117 | boolean insertInSeg2 = false;
|
---|
118 |
|
---|
119 | //find if the intersection point is at end point of one of the segments, if so use that point
|
---|
120 |
|
---|
121 | //segment 1
|
---|
122 | if (coordsComparator.compare(newNode, seg1Node1) == 0) {
|
---|
123 | intNode = seg1Node1;
|
---|
124 | } else if (coordsComparator.compare(newNode, seg1Node2) == 0) {
|
---|
125 | intNode = seg1Node2;
|
---|
126 | } else {
|
---|
127 | insertInSeg1 = true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | //segment 2
|
---|
131 | if (coordsComparator.compare(newNode, seg2Node1) == 0) {
|
---|
132 | intNode = seg2Node1;
|
---|
133 | } else if (coordsComparator.compare(newNode, seg2Node2) == 0) {
|
---|
134 | intNode = seg2Node2;
|
---|
135 | } else {
|
---|
136 | insertInSeg2 = true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (insertInSeg1) {
|
---|
140 | way1Nodes.add(seg1Pos +1, intNode);
|
---|
141 | changedWays[seg1Way] = true;
|
---|
142 |
|
---|
143 | //fix seg2 position, as indexes have changed, seg2Pos is always bigger than seg1Pos on the same segment.
|
---|
144 | if (seg2Way == seg1Way) {
|
---|
145 | seg2Pos ++;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (insertInSeg2) {
|
---|
150 | way2Nodes.add(seg2Pos +1, intNode);
|
---|
151 | changedWays[seg2Way] = true;
|
---|
152 |
|
---|
153 | //Do not need to compare again to already split segment
|
---|
154 | seg2Pos ++;
|
---|
155 | }
|
---|
156 |
|
---|
157 | intersectionNodes.add(intNode);
|
---|
158 |
|
---|
159 | if (intNode == newNode) {
|
---|
160 | cmds.add(new AddCommand(intNode));
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 | else if (test && intersectionNodes.size() > 0)
|
---|
165 | return intersectionNodes;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | for (int pos = 0; pos < ways.size(); pos ++) {
|
---|
173 | if (changedWays[pos] == false) {
|
---|
174 | continue;
|
---|
175 | }
|
---|
176 |
|
---|
177 | Way way = ways.get(pos);
|
---|
178 | Way newWay = new Way(way);
|
---|
179 | newWay.setNodes(newNodes[pos]);
|
---|
180 |
|
---|
181 | cmds.add(new ChangeCommand(way, newWay));
|
---|
182 | }
|
---|
183 |
|
---|
184 | return intersectionNodes;
|
---|
185 | }
|
---|
186 |
|
---|
187 | private static BBox getNodesBounds(ArrayList<Node> nodes) {
|
---|
188 |
|
---|
189 | BBox bounds = new BBox(nodes.get(0));
|
---|
190 | for(Node n: nodes) {
|
---|
191 | bounds.add(n.getCoor());
|
---|
192 | }
|
---|
193 | return bounds;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Tests if given point is to the right side of path consisting of 3 points.
|
---|
198 | * @param lineP1 first point in path
|
---|
199 | * @param lineP2 second point in path
|
---|
200 | * @param lineP3 third point in path
|
---|
201 | * @param testPoint
|
---|
202 | * @return true if to the right side, false otherwise
|
---|
203 | */
|
---|
204 | public static boolean isToTheRightSideOfLine(Node lineP1, Node lineP2, Node lineP3, Node testPoint) {
|
---|
205 | boolean pathBendToRight = angleIsClockwise(lineP1, lineP2, lineP3);
|
---|
206 | boolean rightOfSeg1 = angleIsClockwise(lineP1, lineP2, testPoint);
|
---|
207 | boolean rightOfSeg2 = angleIsClockwise(lineP2, lineP3, testPoint);
|
---|
208 |
|
---|
209 | if (pathBendToRight)
|
---|
210 | return rightOfSeg1 && rightOfSeg2;
|
---|
211 | else
|
---|
212 | return !(!rightOfSeg1 && !rightOfSeg2);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * This method tests if secondNode is clockwise to first node.
|
---|
217 | * @param commonNode starting point for both vectors
|
---|
218 | * @param firstNode first vector end node
|
---|
219 | * @param secondNode second vector end node
|
---|
220 | * @return true if first vector is clockwise before second vector.
|
---|
221 | */
|
---|
222 | public static boolean angleIsClockwise(Node commonNode, Node firstNode, Node secondNode) {
|
---|
223 | double dy1 = (firstNode.getEastNorth().getY() - commonNode.getEastNorth().getY());
|
---|
224 | double dy2 = (secondNode.getEastNorth().getY() - commonNode.getEastNorth().getY());
|
---|
225 | double dx1 = (firstNode.getEastNorth().getX() - commonNode.getEastNorth().getX());
|
---|
226 | double dx2 = (secondNode.getEastNorth().getX() - commonNode.getEastNorth().getX());
|
---|
227 |
|
---|
228 | return dy1 * dx2 - dx1 * dy2 > 0;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Finds the intersection of two line segments
|
---|
233 | * @return EastNorth null if no intersection was found, the EastNorth coordinates of the intersection otherwise
|
---|
234 | */
|
---|
235 | public static EastNorth getSegmentSegmentIntersection(
|
---|
236 | EastNorth p1, EastNorth p2,
|
---|
237 | EastNorth p3, EastNorth p4) {
|
---|
238 | double x1 = p1.getX();
|
---|
239 | double y1 = p1.getY();
|
---|
240 | double x2 = p2.getX();
|
---|
241 | double y2 = p2.getY();
|
---|
242 | double x3 = p3.getX();
|
---|
243 | double y3 = p3.getY();
|
---|
244 | double x4 = p4.getX();
|
---|
245 | double y4 = p4.getY();
|
---|
246 |
|
---|
247 | //TODO: do this locally.
|
---|
248 | if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null;
|
---|
249 |
|
---|
250 | // Convert line from (point, point) form to ax+by=c
|
---|
251 | double a1 = y2 - y1;
|
---|
252 | double b1 = x1 - x2;
|
---|
253 | double c1 = x2*y1 - x1*y2;
|
---|
254 |
|
---|
255 | double a2 = y4 - y3;
|
---|
256 | double b2 = x3 - x4;
|
---|
257 | double c2 = x4*y3 - x3*y4;
|
---|
258 |
|
---|
259 | // Solve the equations
|
---|
260 | double det = a1*b2 - a2*b1;
|
---|
261 | if (det == 0) return null; // Lines are parallel
|
---|
262 |
|
---|
263 | double x = (b1*c2 - b2*c1)/det;
|
---|
264 | double y = (a2*c1 -a1*c2)/det;
|
---|
265 |
|
---|
266 | return new EastNorth(x, y);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Finds the intersection of two lines of infinite length.
|
---|
271 | * @return EastNorth null if no intersection was found, the coordinates of the intersection otherwise
|
---|
272 | */
|
---|
273 | public static EastNorth getLineLineIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
|
---|
274 |
|
---|
275 | // Convert line from (point, point) form to ax+by=c
|
---|
276 | double a1 = p2.getY() - p1.getY();
|
---|
277 | double b1 = p1.getX() - p2.getX();
|
---|
278 | double c1 = p2.getX() * p1.getY() - p1.getX() * p2.getY();
|
---|
279 |
|
---|
280 | double a2 = p4.getY() - p3.getY();
|
---|
281 | double b2 = p3.getX() - p4.getX();
|
---|
282 | double c2 = p4.getX() * p3.getY() - p3.getX() * p4.getY();
|
---|
283 |
|
---|
284 | // Solve the equations
|
---|
285 | double det = a1 * b2 - a2 * b1;
|
---|
286 | if (det == 0)
|
---|
287 | return null; // Lines are parallel
|
---|
288 |
|
---|
289 | return new EastNorth((b1 * c2 - b2 * c1) / det, (a2 * c1 - a1 * c2) / det);
|
---|
290 | }
|
---|
291 |
|
---|
292 | public static boolean segmentsParallel(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
|
---|
293 | // Convert line from (point, point) form to ax+by=c
|
---|
294 | double a1 = p2.getY() - p1.getY();
|
---|
295 | double b1 = p1.getX() - p2.getX();
|
---|
296 |
|
---|
297 | double a2 = p4.getY() - p3.getY();
|
---|
298 | double b2 = p3.getX() - p4.getX();
|
---|
299 |
|
---|
300 | // Solve the equations
|
---|
301 | double det = a1 * b2 - a2 * b1;
|
---|
302 | // remove influence of of scaling factor
|
---|
303 | det /= Math.sqrt(a1*a1 + b1*b1) * Math.sqrt(a2*a2 + b2*b2);
|
---|
304 | return Math.abs(det) < 1e-3;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Calculates closest point to a line segment.
|
---|
309 | * @param segmentP1
|
---|
310 | * @param segmentP2
|
---|
311 | * @param point
|
---|
312 | * @return segmentP1 if it is the closest point, segmentP2 if it is the closest point,
|
---|
313 | * a new point if closest point is between segmentP1 and segmentP2.
|
---|
314 | */
|
---|
315 | public static EastNorth closestPointToSegment(EastNorth segmentP1, EastNorth segmentP2, EastNorth point) {
|
---|
316 |
|
---|
317 | double ldx = segmentP2.getX() - segmentP1.getX();
|
---|
318 | double ldy = segmentP2.getY() - segmentP1.getY();
|
---|
319 |
|
---|
320 | if (ldx == 0 && ldy == 0) //segment zero length
|
---|
321 | return segmentP1;
|
---|
322 |
|
---|
323 | double pdx = point.getX() - segmentP1.getX();
|
---|
324 | double pdy = point.getY() - segmentP1.getY();
|
---|
325 |
|
---|
326 | double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy);
|
---|
327 |
|
---|
328 | if (offset <= 0)
|
---|
329 | return segmentP1;
|
---|
330 | else if (offset >= 1)
|
---|
331 | return segmentP2;
|
---|
332 | else
|
---|
333 | return new EastNorth(segmentP1.getX() + ldx * offset, segmentP1.getY() + ldy * offset);
|
---|
334 | }
|
---|
335 |
|
---|
336 | public static EastNorth closestPointToLine(EastNorth lineP1, EastNorth lineP2, EastNorth point) {
|
---|
337 | double ldx = lineP2.getX() - lineP1.getX();
|
---|
338 | double ldy = lineP2.getY() - lineP1.getY();
|
---|
339 |
|
---|
340 | if (ldx == 0 && ldy == 0) //segment zero length
|
---|
341 | return lineP1;
|
---|
342 |
|
---|
343 | double pdx = point.getX() - lineP1.getX();
|
---|
344 | double pdy = point.getY() - lineP1.getY();
|
---|
345 |
|
---|
346 | double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy);
|
---|
347 | return new EastNorth(lineP1.getX() + ldx * offset, lineP1.getY() + ldy * offset);
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * This method tests if secondNode is clockwise to first node.
|
---|
352 | * @param commonNode starting point for both vectors
|
---|
353 | * @param firstNode first vector end node
|
---|
354 | * @param secondNode second vector end node
|
---|
355 | * @return true if first vector is clockwise before second vector.
|
---|
356 | */
|
---|
357 | public static boolean angleIsClockwise(EastNorth commonNode, EastNorth firstNode, EastNorth secondNode) {
|
---|
358 | double dy1 = (firstNode.getY() - commonNode.getY());
|
---|
359 | double dy2 = (secondNode.getY() - commonNode.getY());
|
---|
360 | double dx1 = (firstNode.getX() - commonNode.getX());
|
---|
361 | double dx2 = (secondNode.getX() - commonNode.getX());
|
---|
362 |
|
---|
363 | return dy1 * dx2 - dx1 * dy2 > 0;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Tests if two polygons intersect.
|
---|
368 | * @param first
|
---|
369 | * @param second
|
---|
370 | * @return intersection kind
|
---|
371 | * TODO: test segments, not only points
|
---|
372 | * TODO: is O(N*M), should use sweep for better performance.
|
---|
373 | */
|
---|
374 | public static PolygonIntersection polygonIntersection(List<Node> first, List<Node> second) {
|
---|
375 | Set<Node> firstSet = new HashSet<Node>(first);
|
---|
376 | Set<Node> secondSet = new HashSet<Node>(second);
|
---|
377 |
|
---|
378 | int nodesInsideSecond = 0;
|
---|
379 | int nodesOutsideSecond = 0;
|
---|
380 | int nodesInsideFirst = 0;
|
---|
381 | int nodesOutsideFirst = 0;
|
---|
382 |
|
---|
383 | for (Node insideNode : first) {
|
---|
384 | if (secondSet.contains(insideNode)) {
|
---|
385 | continue;
|
---|
386 | //ignore touching nodes.
|
---|
387 | }
|
---|
388 |
|
---|
389 | if (nodeInsidePolygon(insideNode, second)) {
|
---|
390 | nodesInsideSecond ++;
|
---|
391 | }
|
---|
392 | else {
|
---|
393 | nodesOutsideSecond ++;
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | for (Node insideNode : second) {
|
---|
398 | if (firstSet.contains(insideNode)) {
|
---|
399 | continue;
|
---|
400 | //ignore touching nodes.
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (nodeInsidePolygon(insideNode, first)) {
|
---|
404 | nodesInsideFirst ++;
|
---|
405 | }
|
---|
406 | else {
|
---|
407 | nodesOutsideFirst ++;
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | if (nodesInsideFirst == 0) {
|
---|
412 | if (nodesInsideSecond == 0){
|
---|
413 | if (nodesOutsideFirst + nodesInsideSecond > 0)
|
---|
414 | return PolygonIntersection.OUTSIDE;
|
---|
415 | else
|
---|
416 | //all nodes common
|
---|
417 | return PolygonIntersection.CROSSING;
|
---|
418 | } else
|
---|
419 | return PolygonIntersection.FIRST_INSIDE_SECOND;
|
---|
420 | }
|
---|
421 | else
|
---|
422 | {
|
---|
423 | if (nodesInsideSecond == 0)
|
---|
424 | return PolygonIntersection.SECOND_INSIDE_FIRST;
|
---|
425 | else
|
---|
426 | return PolygonIntersection.CROSSING;
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Tests if point is inside a polygon. The polygon can be self-intersecting. In such case the contains function works in xor-like manner.
|
---|
432 | * @param polygonNodes list of nodes from polygon path.
|
---|
433 | * @param point the point to test
|
---|
434 | * @return true if the point is inside polygon.
|
---|
435 | */
|
---|
436 | public static boolean nodeInsidePolygon(Node point, List<Node> polygonNodes) {
|
---|
437 | if (polygonNodes.size() < 2)
|
---|
438 | return false;
|
---|
439 |
|
---|
440 | boolean inside = false;
|
---|
441 | Node p1, p2;
|
---|
442 |
|
---|
443 | //iterate each side of the polygon, start with the last segment
|
---|
444 | Node oldPoint = polygonNodes.get(polygonNodes.size() - 1);
|
---|
445 |
|
---|
446 | for (Node newPoint : polygonNodes) {
|
---|
447 | //skip duplicate points
|
---|
448 | if (newPoint.equals(oldPoint)) {
|
---|
449 | continue;
|
---|
450 | }
|
---|
451 |
|
---|
452 | //order points so p1.lat <= p2.lat;
|
---|
453 | if (newPoint.getEastNorth().getY() > oldPoint.getEastNorth().getY()) {
|
---|
454 | p1 = oldPoint;
|
---|
455 | p2 = newPoint;
|
---|
456 | } else {
|
---|
457 | p1 = newPoint;
|
---|
458 | p2 = oldPoint;
|
---|
459 | }
|
---|
460 |
|
---|
461 | //test if the line is crossed and if so invert the inside flag.
|
---|
462 | if ((newPoint.getEastNorth().getY() < point.getEastNorth().getY()) == (point.getEastNorth().getY() <= oldPoint.getEastNorth().getY())
|
---|
463 | && (point.getEastNorth().getX() - p1.getEastNorth().getX()) * (p2.getEastNorth().getY() - p1.getEastNorth().getY())
|
---|
464 | < (p2.getEastNorth().getX() - p1.getEastNorth().getX()) * (point.getEastNorth().getY() - p1.getEastNorth().getY()))
|
---|
465 | {
|
---|
466 | inside = !inside;
|
---|
467 | }
|
---|
468 |
|
---|
469 | oldPoint = newPoint;
|
---|
470 | }
|
---|
471 |
|
---|
472 | return inside;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * returns area of a closed way in square meters
|
---|
477 | * (approximate(?), but should be OK for small areas)
|
---|
478 | */
|
---|
479 | public static double closedWayArea(Way way) {
|
---|
480 |
|
---|
481 | //http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
|
---|
482 | double area = 0;
|
---|
483 | Node lastN = null;
|
---|
484 | for (Node n : way.getNodes()) {
|
---|
485 | if (lastN != null) {
|
---|
486 | n.getEastNorth().getX();
|
---|
487 |
|
---|
488 | area += (calcX(n) * calcY(lastN)) - (calcY(n) * calcX(lastN));
|
---|
489 | }
|
---|
490 | lastN = n;
|
---|
491 | }
|
---|
492 | return Math.abs(area/2);
|
---|
493 | }
|
---|
494 |
|
---|
495 | protected static double calcX(Node p1){
|
---|
496 | double lat1, lon1, lat2, lon2;
|
---|
497 | double dlon, dlat;
|
---|
498 |
|
---|
499 | lat1 = p1.getCoor().lat() * Math.PI / 180.0;
|
---|
500 | lon1 = p1.getCoor().lon() * Math.PI / 180.0;
|
---|
501 | lat2 = lat1;
|
---|
502 | lon2 = 0;
|
---|
503 |
|
---|
504 | dlon = lon2 - lon1;
|
---|
505 | dlat = lat2 - lat1;
|
---|
506 |
|
---|
507 | double a = (Math.pow(Math.sin(dlat/2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2), 2));
|
---|
508 | double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
---|
509 | return 6367000 * c;
|
---|
510 | }
|
---|
511 |
|
---|
512 | protected static double calcY(Node p1){
|
---|
513 | double lat1, lon1, lat2, lon2;
|
---|
514 | double dlon, dlat;
|
---|
515 |
|
---|
516 | lat1 = p1.getCoor().lat() * Math.PI / 180.0;
|
---|
517 | lon1 = p1.getCoor().lon() * Math.PI / 180.0;
|
---|
518 | lat2 = 0;
|
---|
519 | lon2 = lon1;
|
---|
520 |
|
---|
521 | dlon = lon2 - lon1;
|
---|
522 | dlat = lat2 - lat1;
|
---|
523 |
|
---|
524 | double a = (Math.pow(Math.sin(dlat/2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2), 2));
|
---|
525 | double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
---|
526 | return 6367000 * c;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | }
|
---|