source: josm/trunk/src/org/openstreetmap/josm/data/ProjectionBounds.java@ 12659

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

fix #13922 - zoom only once when downloading several data types

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import org.openstreetmap.josm.data.coor.EastNorth;
5import org.openstreetmap.josm.tools.Utils;
6
7/**
8 * This is a simple data class for "rectangular" areas of the world, given in
9 * east/north min/max values.
10 *
11 * @author imi
12 */
13public class ProjectionBounds {
14 /**
15 * The minimum east coordinate.
16 */
17 public double minEast;
18 /**
19 * The minimum north coordinate.
20 */
21 public double minNorth;
22 /**
23 * The maximum east coordinate.
24 */
25 public double maxEast;
26 /**
27 * The minimum north coordinate.
28 */
29 public double maxNorth;
30
31 /**
32 * Construct bounds out of two points.
33 * @param min min east/north
34 * @param max max east/north
35 */
36 public ProjectionBounds(EastNorth min, EastNorth max) {
37 this.minEast = min.east();
38 this.minNorth = min.north();
39 this.maxEast = max.east();
40 this.maxNorth = max.north();
41 }
42
43 /**
44 * Construct bounds out of a single point.
45 * @param p east/north
46 */
47 public ProjectionBounds(EastNorth p) {
48 this.minEast = this.maxEast = p.east();
49 this.minNorth = this.maxNorth = p.north();
50 }
51
52 /**
53 * Construct bounds out of a center point and east/north dimensions.
54 * @param center center east/north
55 * @param east east dimension
56 * @param north north dimension
57 */
58 public ProjectionBounds(EastNorth center, double east, double north) {
59 this.minEast = center.east()-east/2.0;
60 this.minNorth = center.north()-north/2.0;
61 this.maxEast = center.east()+east/2.0;
62 this.maxNorth = center.north()+north/2.0;
63 }
64
65 /**
66 * Construct bounds out of two points.
67 * @param minEast min east
68 * @param minNorth min north
69 * @param maxEast max east
70 * @param maxNorth max north
71 */
72 public ProjectionBounds(double minEast, double minNorth, double maxEast, double maxNorth) {
73 this.minEast = minEast;
74 this.minNorth = minNorth;
75 this.maxEast = maxEast;
76 this.maxNorth = maxNorth;
77 }
78
79 /**
80 * Extends bounds to include point {@code e}.
81 * @param e east/north to include
82 */
83 public void extend(EastNorth e) {
84 if (e.east() < minEast) {
85 minEast = e.east();
86 }
87 if (e.east() > maxEast) {
88 maxEast = e.east();
89 }
90 if (e.north() < minNorth) {
91 minNorth = e.north();
92 }
93 if (e.north() > maxNorth) {
94 maxNorth = e.north();
95 }
96 }
97
98 /**
99 * Extends bounds to include bounds {@code b}.
100 * @param b bounds to include
101 * @since 11774
102 */
103 public void extend(ProjectionBounds b) {
104 if (b.minEast < minEast) {
105 minEast = b.minEast;
106 }
107 if (b.maxEast > maxEast) {
108 maxEast = b.maxEast;
109 }
110 if (b.minNorth < minNorth) {
111 minNorth = b.minNorth;
112 }
113 if (b.maxNorth > maxNorth) {
114 maxNorth = b.maxNorth;
115 }
116 }
117
118 /**
119 * Returns the center east/north.
120 * @return the center east/north
121 */
122 public EastNorth getCenter() {
123 return new EastNorth((minEast + maxEast) / 2.0, (minNorth + maxNorth) / 2.0);
124 }
125
126 @Override
127 public String toString() {
128 return "ProjectionBounds["+minEast+','+minNorth+','+maxEast+','+maxNorth+']';
129 }
130
131 /**
132 * The two bounds intersect? Compared to java Shape.intersects, if does not use
133 * the interior but the closure. ("&gt;=" instead of "&gt;")
134 * @param b projection bounds
135 * @return {@code true} if the two bounds intersect
136 */
137 public boolean intersects(ProjectionBounds b) {
138 return b.maxEast >= minEast &&
139 b.maxNorth >= minNorth &&
140 b.minEast <= maxEast &&
141 b.minNorth <= maxNorth;
142 }
143
144 /**
145 * Check, if a point is within the bounds.
146 * @param en the point
147 * @return true, if <code>en</code> is within the bounds
148 */
149 public boolean contains(EastNorth en) {
150 return minEast <= en.east() && en.east() <= maxEast &&
151 minNorth <= en.north() && en.north() <= maxNorth;
152 }
153
154 /**
155 * Returns the min east/north.
156 * @return the min east/north
157 */
158 public EastNorth getMin() {
159 return new EastNorth(minEast, minNorth);
160 }
161
162 /**
163 * Returns the max east/north.
164 * @return the max east/north
165 */
166 public EastNorth getMax() {
167 return new EastNorth(maxEast, maxNorth);
168 }
169
170 /**
171 * Determines if the bounds area is not null
172 * @return {@code true} if the area is not null
173 */
174 public boolean hasExtend() {
175 return !Utils.equalsEpsilon(minEast, maxEast) || !Utils.equalsEpsilon(minNorth, maxNorth);
176 }
177}
Note: See TracBrowser for help on using the repository browser.