source: josm/trunk/src/org/openstreetmap/josm/data/osm/visitor/BoundingXYVisitor.java@ 2667

Last change on this file since 2667 was 2578, checked in by jttt, 14 years ago

Encalupse OsmPrimitive.incomplete

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