source: josm/trunk/src/org/openstreetmap/josm/data/Bounds.java@ 2612

Last change on this file since 2612 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.geom.Rectangle2D;
7
8import org.openstreetmap.josm.data.coor.LatLon;
9
10/**
11 * This is a simple data class for "rectangular" areas of the world, given in
12 * lat/lon min/max values.
13 *
14 * @author imi
15 */
16public class Bounds {
17 /**
18 * The minimum and maximum coordinates.
19 */
20 private LatLon min, max;
21
22 public LatLon getMin() {
23 return min;
24 }
25
26 public LatLon getMax() {
27 return max;
28 }
29
30 /**
31 * Construct bounds out of two points
32 */
33 public Bounds(LatLon min, LatLon max) {
34 this.min = min;
35 this.max = max;
36 }
37
38 public Bounds(LatLon b) {
39 this.min = b;
40 this.max = b;
41 }
42
43 public Bounds(double minlat, double minlon, double maxlat, double maxlon) {
44 this.min = new LatLon(minlat, minlon);
45 this.max = new LatLon(maxlat, maxlon);
46 }
47
48 public Bounds(double [] coords) {
49 if (coords == null)
50 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "coords"));
51 if (coords.length != 4)
52 throw new IllegalArgumentException(tr("Expected array of length 4, got {0}", coords.length));
53 this.min = new LatLon(coords[0], coords[1]);
54 this.max = new LatLon(coords[2], coords[3]);
55 }
56
57 public Bounds(String asString, String separator) throws IllegalArgumentException {
58 if (asString == null)
59 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "asString"));
60 String[] components = asString.split(separator);
61 if (components.length != 4)
62 throw new IllegalArgumentException(tr("Exactly four doubles excpected in string, got {0}", components.length));
63 double[] values = new double[4];
64 for (int i=0; i<4; i++) {
65 try {
66 values[i] = Double.parseDouble(components[i]);
67 } catch(NumberFormatException e) {
68 throw new IllegalArgumentException(tr("Illegal double value ''{0}''", components[i]));
69 }
70 }
71 this.min = new LatLon(values[0], values[1]);
72 this.max = new LatLon(values[2], values[3]);
73 }
74
75 public Bounds(Bounds other) {
76 this.min = new LatLon(other.min);
77 this.max = new LatLon(other.max);
78 }
79
80 public Bounds(Rectangle2D rect) {
81 this.min = new LatLon(rect.getMinY(), rect.getMinX());
82 this.max = new LatLon(rect.getMaxY(), rect.getMaxX());
83 }
84
85 /**
86 * Creates new bounds around a coordinate pair <code>center</code>. The
87 * new bounds shall have an extension in latitude direction of <code>latExtent</code>,
88 * and in longitude direction of <code>lonExtent</code>.
89 *
90 * @param center the center coordinate pair. Must not be null.
91 * @param latExtent the latitude extent. > 0 required.
92 * @param lonExtent the longitude extent. > 0 required.
93 * @throws IllegalArgumentException thrown if center is null
94 * @throws IllegalArgumentException thrown if latExtent <= 0
95 * @throws IllegalArgumentException thrown if lonExtent <= 0
96 */
97 public Bounds(LatLon center, double latExtent, double lonExtent) {
98 if (center == null)
99 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "center"));
100 if (latExtent <= 0.0)
101 throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0.0 exptected, got {1}", "latExtent", latExtent));
102 if (lonExtent <= 0.0)
103 throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0.0 exptected, got {1}", "lonExtent", lonExtent));
104
105 this.min = new LatLon(
106 center.lat() - latExtent / 2,
107 center.lon() - lonExtent / 2
108 );
109 this.max = new LatLon(
110 center.lat() + latExtent / 2,
111 center.lon() + lonExtent / 2
112 );
113 }
114
115 @Override public String toString() {
116 return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]";
117 }
118
119 /**
120 * @return Center of the bounding box.
121 */
122 public LatLon getCenter()
123 {
124 return min.getCenter(max);
125 }
126
127 /**
128 * Extend the bounds if necessary to include the given point.
129 */
130 public void extend(LatLon ll) {
131 if (ll.lat() < min.lat() || ll.lon() < min.lon()) {
132 min = new LatLon(Math.min(ll.lat(), min.lat()), Math.min(ll.lon(), min.lon()));
133 }
134 if (ll.lat() > max.lat() || ll.lon() > max.lon()) {
135 max = new LatLon(Math.max(ll.lat(), max.lat()), Math.max(ll.lon(), max.lon()));
136 }
137 }
138 /**
139 * Is the given point within this bounds?
140 */
141 public boolean contains(LatLon ll) {
142 if (ll.lat() < min.lat() || ll.lon() < min.lon())
143 return false;
144 if (ll.lat() > max.lat() || ll.lon() > max.lon())
145 return false;
146 return true;
147 }
148
149 /**
150 * Converts the lat/lon bounding box to an object of type Rectangle2D.Double
151 * @return the bounding box to Rectangle2D.Double
152 */
153 public Rectangle2D.Double asRect() {
154 return new Rectangle2D.Double(min.lon(), min.lat(), max.lon()-min.lon(), max.lat()-min.lat());
155 }
156
157 public double getArea() {
158 return (max.lon() - min.lon()) * (max.lat() - min.lat());
159 }
160
161 public String encodeAsString(String separator) {
162 StringBuffer sb = new StringBuffer();
163 sb.append(min.lat()).append(separator).append(min.lon())
164 .append(separator).append(max.lat()).append(separator)
165 .append(max.lon());
166 return sb.toString();
167 }
168
169 @Override
170 public int hashCode() {
171 final int prime = 31;
172 int result = 1;
173 result = prime * result + ((max == null) ? 0 : max.hashCode());
174 result = prime * result + ((min == null) ? 0 : min.hashCode());
175 return result;
176 }
177
178 @Override
179 public boolean equals(Object obj) {
180 if (this == obj)
181 return true;
182 if (obj == null)
183 return false;
184 if (getClass() != obj.getClass())
185 return false;
186 Bounds other = (Bounds) obj;
187 if (max == null) {
188 if (other.max != null)
189 return false;
190 } else if (!max.equals(other.max))
191 return false;
192 if (min == null) {
193 if (other.min != null)
194 return false;
195 } else if (!min.equals(other.min))
196 return false;
197 return true;
198 }
199
200}
Note: See TracBrowser for help on using the repository browser.