1 | package org.openstreetmap.josm.data.osm.visitor;
|
---|
2 |
|
---|
3 | import org.openstreetmap.josm.Main;
|
---|
4 | import org.openstreetmap.josm.data.Bounds;
|
---|
5 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
6 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
7 | import org.openstreetmap.josm.data.osm.Node;
|
---|
8 | import org.openstreetmap.josm.data.osm.Way;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Calculates the total bounding rectangle of a serie of OsmPrimitives, using the EastNorth values
|
---|
12 | * as reference.
|
---|
13 | * @author imi
|
---|
14 | */
|
---|
15 | public class BoundingXYVisitor implements Visitor {
|
---|
16 |
|
---|
17 | public EastNorth min, max;
|
---|
18 |
|
---|
19 | public void visit(Node n) {
|
---|
20 | visit(n.eastNorth);
|
---|
21 | }
|
---|
22 |
|
---|
23 | public void visit(LineSegment ls) {
|
---|
24 | if (!ls.incomplete) {
|
---|
25 | visit(ls.from);
|
---|
26 | visit(ls.to);
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | public void visit(Way t) {
|
---|
31 | for (LineSegment ls : t.segments)
|
---|
32 | visit(ls);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void visit(EastNorth eastNorth) {
|
---|
36 | if (eastNorth != null) {
|
---|
37 | if (min == null)
|
---|
38 | min = eastNorth;
|
---|
39 | else if (eastNorth.east() < min.east() || eastNorth.north() < min.north())
|
---|
40 | min = new EastNorth(Math.min(min.east(), eastNorth.east()), Math.min(min.north(), eastNorth.north()));
|
---|
41 |
|
---|
42 | if (max == null)
|
---|
43 | max = eastNorth;
|
---|
44 | else if (eastNorth.east() > max.east() || eastNorth.north() > max.north())
|
---|
45 | max = new EastNorth(Math.max(max.east(), eastNorth.east()), Math.max(max.north(), eastNorth.north()));
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * @return The bounding box or <code>null</code> if no coordinates have passed
|
---|
51 | */
|
---|
52 | public Bounds getBounds() {
|
---|
53 | if (min == null || max == null)
|
---|
54 | return null;
|
---|
55 | return new Bounds(Main.pref.getProjection().eastNorth2latlon(min), Main.pref.getProjection().eastNorth2latlon(max));
|
---|
56 | }
|
---|
57 | }
|
---|