001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.osm.visitor; 003 004import java.util.Collection; 005 006import org.openstreetmap.josm.Main; 007import org.openstreetmap.josm.data.Bounds; 008import org.openstreetmap.josm.data.ProjectionBounds; 009import org.openstreetmap.josm.data.coor.CachedLatLon; 010import org.openstreetmap.josm.data.coor.EastNorth; 011import org.openstreetmap.josm.data.coor.LatLon; 012import org.openstreetmap.josm.data.osm.Node; 013import org.openstreetmap.josm.data.osm.OsmPrimitive; 014import org.openstreetmap.josm.data.osm.Relation; 015import org.openstreetmap.josm.data.osm.RelationMember; 016import org.openstreetmap.josm.data.osm.Way; 017 018/** 019 * Calculates the total bounding rectangle of a series of {@link OsmPrimitive} objects, using the 020 * EastNorth values as reference. 021 * @author imi 022 */ 023public class BoundingXYVisitor extends AbstractVisitor { 024 025 private ProjectionBounds bounds; 026 027 @Override 028 public void visit(Node n) { 029 visit(n.getEastNorth()); 030 } 031 032 @Override 033 public void visit(Way w) { 034 if (w.isIncomplete()) return; 035 for (Node n : w.getNodes()) { 036 visit(n); 037 } 038 } 039 040 @Override 041 public void visit(Relation e) { 042 // only use direct members 043 for (RelationMember m : e.getMembers()) { 044 if (!m.isRelation()) { 045 m.getMember().accept(this); 046 } 047 } 048 } 049 050 /** 051 * Visiting call for bounds. 052 * @param b bounds 053 */ 054 public void visit(Bounds b) { 055 if (b != null) { 056 visit(b.getMin()); 057 visit(b.getMax()); 058 } 059 } 060 061 /** 062 * Visiting call for projection bounds. 063 * @param b projection bounds 064 */ 065 public void visit(ProjectionBounds b) { 066 if (b != null) { 067 visit(b.getMin()); 068 visit(b.getMax()); 069 } 070 } 071 072 /** 073 * Visiting call for lat/lon. 074 * @param latlon lat/lon 075 */ 076 public void visit(LatLon latlon) { 077 if (latlon != null) { 078 if (latlon instanceof CachedLatLon) { 079 visit(((CachedLatLon) latlon).getEastNorth()); 080 } else { 081 visit(Main.getProjection().latlon2eastNorth(latlon)); 082 } 083 } 084 } 085 086 /** 087 * Visiting call for east/north. 088 * @param eastNorth east/north 089 */ 090 public void visit(EastNorth eastNorth) { 091 if (eastNorth != null) { 092 if (bounds == null) { 093 bounds = new ProjectionBounds(eastNorth); 094 } else { 095 bounds.extend(eastNorth); 096 } 097 } 098 } 099 100 /** 101 * Determines if the visitor has a non null bounds area. 102 * @return {@code true} if the visitor has a non null bounds area 103 * @see ProjectionBounds#hasExtend 104 */ 105 public boolean hasExtend() { 106 return bounds != null && bounds.hasExtend(); 107 } 108 109 /** 110 * @return The bounding box or <code>null</code> if no coordinates have passed 111 */ 112 public ProjectionBounds getBounds() { 113 return bounds; 114 } 115 116 /** 117 * Enlarges the calculated bounding box by 0.002 degrees. 118 * If the bounding box has not been set (<code>min</code> or <code>max</code> 119 * equal <code>null</code>) this method does not do anything. 120 */ 121 public void enlargeBoundingBox() { 122 enlargeBoundingBox(Main.pref.getDouble("edit.zoom-enlarge-bbox", 0.002)); 123 } 124 125 /** 126 * Enlarges the calculated bounding box by the specified number of degrees. 127 * If the bounding box has not been set (<code>min</code> or <code>max</code> 128 * equal <code>null</code>) this method does not do anything. 129 * 130 * @param enlargeDegree number of degrees to enlarge on each side 131 */ 132 public void enlargeBoundingBox(double enlargeDegree) { 133 if (bounds == null) 134 return; 135 LatLon minLatlon = Main.getProjection().eastNorth2latlon(bounds.getMin()); 136 LatLon maxLatlon = Main.getProjection().eastNorth2latlon(bounds.getMax()); 137 bounds = new ProjectionBounds( 138 Main.getProjection().latlon2eastNorth(new LatLon(minLatlon.lat() - enlargeDegree, minLatlon.lon() - enlargeDegree)), 139 Main.getProjection().latlon2eastNorth(new LatLon(maxLatlon.lat() + enlargeDegree, maxLatlon.lon() + enlargeDegree))); 140 } 141 142 /** 143 * Enlarges the bounding box up to <code>maxEnlargePercent</code>, depending on 144 * its size. If the bounding box is small, it will be enlarged more in relation 145 * to its beginning size. The larger the bounding box, the smaller the change, 146 * down to the minimum of 1% enlargement. 147 * 148 * Warning: if the bounding box only contains a single node, no expansion takes 149 * place because a node has no width/height. Use <code>enlargeToMinDegrees</code> 150 * instead. 151 * 152 * Example: You specify enlargement to be up to 100%. 153 * 154 * Bounding box is a small house: enlargement will be 95–100%, i.e. 155 * making enough space so that the house fits twice on the screen in 156 * each direction. 157 * 158 * Bounding box is a large landuse, like a forest: Enlargement will 159 * be 1–10%, i.e. just add a little border around the landuse. 160 * 161 * If the bounding box has not been set (<code>min</code> or <code>max</code> 162 * equal <code>null</code>) this method does not do anything. 163 * 164 * @param maxEnlargePercent maximum enlargement in percentage (100.0 for 100%) 165 */ 166 public void enlargeBoundingBoxLogarithmically(double maxEnlargePercent) { 167 if (bounds == null) 168 return; 169 170 double diffEast = bounds.getMax().east() - bounds.getMin().east(); 171 double diffNorth = bounds.getMax().north() - bounds.getMin().north(); 172 173 double enlargeEast = Math.min(maxEnlargePercent - 10*Math.log(diffEast), 1)/100; 174 double enlargeNorth = Math.min(maxEnlargePercent - 10*Math.log(diffNorth), 1)/100; 175 176 visit(bounds.getMin().add(-enlargeEast/2, -enlargeNorth/2)); 177 visit(bounds.getMax().add(+enlargeEast/2, +enlargeNorth/2)); 178 } 179 180 /** 181 * Specify a degree larger than 0 in order to make the bounding box at least 182 * the specified size in width and height. The value is ignored if the 183 * bounding box is already larger than the specified amount. 184 * 185 * If the bounding box has not been set (<code>min</code> or <code>max</code> 186 * equal <code>null</code>) this method does not do anything. 187 * 188 * If the bounding box contains objects and is to be enlarged, the objects 189 * will be centered within the new bounding box. 190 * 191 * @param size minimum width and height in meter 192 */ 193 public void enlargeToMinSize(double size) { 194 if (bounds == null) 195 return; 196 // convert size from meters to east/north units 197 double enSize = size * Main.map.mapView.getScale() / Main.map.mapView.getDist100Pixel() * 100; 198 visit(bounds.getMin().add(-enSize/2, -enSize/2)); 199 visit(bounds.getMax().add(+enSize/2, +enSize/2)); 200 } 201 202 @Override 203 public String toString() { 204 return "BoundingXYVisitor["+bounds+']'; 205 } 206 207 /** 208 * Compute the bounding box of a collection of primitives. 209 * @param primitives the collection of primitives 210 */ 211 public void computeBoundingBox(Collection<? extends OsmPrimitive> primitives) { 212 if (primitives == null) return; 213 for (OsmPrimitive p: primitives) { 214 if (p == null) { 215 continue; 216 } 217 p.accept(this); 218 } 219 } 220}