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

Last change on this file since 12630 was 12630, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.visitor;
3
4import java.util.Collection;
5
6import org.openstreetmap.josm.Main;
7import org.openstreetmap.josm.data.Bounds;
8import org.openstreetmap.josm.data.ProjectionBounds;
9import org.openstreetmap.josm.data.coor.EastNorth;
10import org.openstreetmap.josm.data.coor.ILatLon;
11import org.openstreetmap.josm.data.coor.LatLon;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.RelationMember;
16import org.openstreetmap.josm.data.osm.Way;
17import org.openstreetmap.josm.gui.MainApplication;
18import org.openstreetmap.josm.gui.MapFrame;
19
20/**
21 * Calculates the total bounding rectangle of a series of {@link OsmPrimitive} objects, using the
22 * EastNorth values as reference.
23 * @author imi
24 */
25public class BoundingXYVisitor extends AbstractVisitor {
26
27 private ProjectionBounds bounds;
28
29 @Override
30 public void visit(Node n) {
31 visit((ILatLon) n);
32 }
33
34 @Override
35 public void visit(Way w) {
36 if (w.isIncomplete()) return;
37 for (Node n : w.getNodes()) {
38 visit(n);
39 }
40 }
41
42 @Override
43 public void visit(Relation e) {
44 // only use direct members
45 for (RelationMember m : e.getMembers()) {
46 if (!m.isRelation()) {
47 m.getMember().accept(this);
48 }
49 }
50 }
51
52 /**
53 * Visiting call for bounds.
54 * @param b bounds
55 */
56 public void visit(Bounds b) {
57 if (b != null) {
58 b.visitEdge(Main.getProjection(), this::visit);
59 }
60 }
61
62 /**
63 * Visiting call for projection bounds.
64 * @param b projection bounds
65 */
66 public void visit(ProjectionBounds b) {
67 if (b != null) {
68 visit(b.getMin());
69 visit(b.getMax());
70 }
71 }
72
73 /**
74 * Visiting call for lat/lon.
75 * @param latlon lat/lon
76 */
77 public void visit(LatLon latlon) {
78 if (latlon != null) {
79 visit((ILatLon) latlon);
80 }
81 }
82
83 private void visit(ILatLon latlon) {
84 visit(latlon.getEastNorth());
85 }
86
87 /**
88 * Visiting call for east/north.
89 * @param eastNorth east/north
90 */
91 public void visit(EastNorth eastNorth) {
92 if (eastNorth != null) {
93 if (bounds == null) {
94 bounds = new ProjectionBounds(eastNorth);
95 } else {
96 bounds.extend(eastNorth);
97 }
98 }
99 }
100
101 /**
102 * Determines if the visitor has a non null bounds area.
103 * @return {@code true} if the visitor has a non null bounds area
104 * @see ProjectionBounds#hasExtend
105 */
106 public boolean hasExtend() {
107 return bounds != null && bounds.hasExtend();
108 }
109
110 /**
111 * @return The bounding box or <code>null</code> if no coordinates have passed
112 */
113 public ProjectionBounds getBounds() {
114 return bounds;
115 }
116
117 /**
118 * Enlarges the calculated bounding box by 0.002 degrees.
119 * If the bounding box has not been set (<code>min</code> or <code>max</code>
120 * equal <code>null</code>) this method does not do anything.
121 */
122 public void enlargeBoundingBox() {
123 enlargeBoundingBox(Main.pref.getDouble("edit.zoom-enlarge-bbox", 0.002));
124 }
125
126 /**
127 * Enlarges the calculated bounding box by the specified number of degrees.
128 * If the bounding box has not been set (<code>min</code> or <code>max</code>
129 * equal <code>null</code>) this method does not do anything.
130 *
131 * @param enlargeDegree number of degrees to enlarge on each side
132 */
133 public void enlargeBoundingBox(double enlargeDegree) {
134 if (bounds == null)
135 return;
136 LatLon minLatlon = Main.getProjection().eastNorth2latlon(bounds.getMin());
137 LatLon maxLatlon = Main.getProjection().eastNorth2latlon(bounds.getMax());
138 bounds = new ProjectionBounds(new LatLon(
139 Math.max(-90, minLatlon.lat() - enlargeDegree),
140 Math.max(-180, minLatlon.lon() - enlargeDegree)).getEastNorth(),
141 new LatLon(
142 Math.min(90, maxLatlon.lat() + enlargeDegree),
143 Math.min(180, maxLatlon.lon() + enlargeDegree)).getEastNorth());
144 }
145
146 /**
147 * Enlarges the bounding box up to <code>maxEnlargePercent</code>, depending on
148 * its size. If the bounding box is small, it will be enlarged more in relation
149 * to its beginning size. The larger the bounding box, the smaller the change,
150 * down to the minimum of 1% enlargement.
151 *
152 * Warning: if the bounding box only contains a single node, no expansion takes
153 * place because a node has no width/height. Use <code>enlargeToMinDegrees</code>
154 * instead.
155 *
156 * Example: You specify enlargement to be up to 100%.
157 *
158 * Bounding box is a small house: enlargement will be 95–100%, i.e.
159 * making enough space so that the house fits twice on the screen in
160 * each direction.
161 *
162 * Bounding box is a large landuse, like a forest: Enlargement will
163 * be 1–10%, i.e. just add a little border around the landuse.
164 *
165 * If the bounding box has not been set (<code>min</code> or <code>max</code>
166 * equal <code>null</code>) this method does not do anything.
167 *
168 * @param maxEnlargePercent maximum enlargement in percentage (100.0 for 100%)
169 */
170 public void enlargeBoundingBoxLogarithmically(double maxEnlargePercent) {
171 if (bounds == null)
172 return;
173
174 double diffEast = bounds.getMax().east() - bounds.getMin().east();
175 double diffNorth = bounds.getMax().north() - bounds.getMin().north();
176
177 double enlargeEast = Math.min(maxEnlargePercent - 10*Math.log(diffEast), 1)/100;
178 double enlargeNorth = Math.min(maxEnlargePercent - 10*Math.log(diffNorth), 1)/100;
179
180 visit(bounds.getMin().add(-enlargeEast/2, -enlargeNorth/2));
181 visit(bounds.getMax().add(+enlargeEast/2, +enlargeNorth/2));
182 }
183
184 /**
185 * Specify a degree larger than 0 in order to make the bounding box at least
186 * the specified size in width and height. The value is ignored if the
187 * bounding box is already larger than the specified amount.
188 *
189 * If the bounding box has not been set (<code>min</code> or <code>max</code>
190 * equal <code>null</code>) this method does not do anything.
191 *
192 * If the bounding box contains objects and is to be enlarged, the objects
193 * will be centered within the new bounding box.
194 *
195 * @param size minimum width and height in meter
196 */
197 public void enlargeToMinSize(double size) {
198 if (bounds == null)
199 return;
200 // convert size from meters to east/north units
201 MapFrame map = MainApplication.getMap();
202 double enSize = size * map.mapView.getScale() / map.mapView.getDist100Pixel() * 100;
203 visit(bounds.getMin().add(-enSize/2, -enSize/2));
204 visit(bounds.getMax().add(+enSize/2, +enSize/2));
205 }
206
207 @Override
208 public String toString() {
209 return "BoundingXYVisitor["+bounds+']';
210 }
211
212 /**
213 * Compute the bounding box of a collection of primitives.
214 * @param primitives the collection of primitives
215 */
216 public void computeBoundingBox(Collection<? extends OsmPrimitive> primitives) {
217 if (primitives == null) return;
218 for (OsmPrimitive p: primitives) {
219 if (p == null) {
220 continue;
221 }
222 p.accept(this);
223 }
224 }
225}
Note: See TracBrowser for help on using the repository browser.