1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.Main;
|
---|
7 | import org.openstreetmap.josm.data.Bounds;
|
---|
8 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
9 | import org.openstreetmap.josm.data.coor.CachedLatLon;
|
---|
10 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Calculates the total bounding rectangle of a series of {@link OsmPrimitive} objects, using the
|
---|
20 | * EastNorth values as reference.
|
---|
21 | * @author imi
|
---|
22 | */
|
---|
23 | public class BoundingXYVisitor extends AbstractVisitor {
|
---|
24 |
|
---|
25 | private ProjectionBounds bounds = null;
|
---|
26 |
|
---|
27 | public void visit(Node n) {
|
---|
28 | visit(n.getEastNorth());
|
---|
29 | }
|
---|
30 |
|
---|
31 | public void visit(Way w) {
|
---|
32 | if (w.isIncomplete()) return;
|
---|
33 | for (Node n : w.getNodes()) {
|
---|
34 | visit(n);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void visit(Relation e) {
|
---|
39 | // only use direct members
|
---|
40 | for (RelationMember m : e.getMembers()) {
|
---|
41 | if (!m.isRelation()) {
|
---|
42 | m.getMember().visit(this);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void visit(Bounds b) {
|
---|
48 | if(b != null)
|
---|
49 | {
|
---|
50 | visit(b.getMin());
|
---|
51 | visit(b.getMax());
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void visit(ProjectionBounds b) {
|
---|
56 | if(b != null)
|
---|
57 | {
|
---|
58 | visit(b.getMin());
|
---|
59 | visit(b.getMax());
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void visit(LatLon latlon) {
|
---|
64 | if(latlon != null)
|
---|
65 | {
|
---|
66 | if(latlon instanceof CachedLatLon) {
|
---|
67 | visit(((CachedLatLon)latlon).getEastNorth());
|
---|
68 | } else {
|
---|
69 | visit(Main.proj.latlon2eastNorth(latlon));
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void visit(EastNorth eastNorth) {
|
---|
75 | if (eastNorth != null) {
|
---|
76 | if (bounds == null) {
|
---|
77 | bounds = new ProjectionBounds(eastNorth);
|
---|
78 | } else {
|
---|
79 | bounds.extend(eastNorth);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public boolean hasExtend()
|
---|
85 | {
|
---|
86 | return bounds != null && !bounds.getMin().equals(bounds.getMax());
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * @return The bounding box or <code>null</code> if no coordinates have passed
|
---|
91 | */
|
---|
92 | public ProjectionBounds getBounds() {
|
---|
93 | return bounds;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Enlarges the calculated bounding box by 0.002 degrees.
|
---|
98 | * If the bounding box has not been set (<code>min</code> or <code>max</code>
|
---|
99 | * equal <code>null</code>) this method does not do anything.
|
---|
100 | */
|
---|
101 | public void enlargeBoundingBox() {
|
---|
102 | enlargeBoundingBox(Main.pref.getDouble("edit.zoom-enlarge-bbox", 0.002));
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Enlarges the calculated bounding box by the specified number of degrees.
|
---|
107 | * If the bounding box has not been set (<code>min</code> or <code>max</code>
|
---|
108 | * equal <code>null</code>) this method does not do anything.
|
---|
109 | *
|
---|
110 | * @param enlargeDegree
|
---|
111 | */
|
---|
112 | public void enlargeBoundingBox(double enlargeDegree) {
|
---|
113 | if (bounds == null)
|
---|
114 | return;
|
---|
115 | LatLon minLatlon = Main.getProjection().eastNorth2latlon(bounds.getMin());
|
---|
116 | LatLon maxLatlon = Main.getProjection().eastNorth2latlon(bounds.getMax());
|
---|
117 | bounds = new ProjectionBounds(
|
---|
118 | Main.getProjection().latlon2eastNorth(new LatLon(minLatlon.lat() - enlargeDegree, minLatlon.lon() - enlargeDegree)),
|
---|
119 | Main.getProjection().latlon2eastNorth(new LatLon(maxLatlon.lat() + enlargeDegree, maxLatlon.lon() + enlargeDegree)));
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override public String toString() {
|
---|
123 | return "BoundingXYVisitor["+bounds+"]";
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void computeBoundingBox(Collection<? extends OsmPrimitive> primitives) {
|
---|
127 | if (primitives == null) return;
|
---|
128 | for (OsmPrimitive p: primitives) {
|
---|
129 | if (p == null) {
|
---|
130 | continue;
|
---|
131 | }
|
---|
132 | p.visit(this);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|