source: josm/trunk/src/org/openstreetmap/josm/data/imagery/OffsetBookmark.java@ 12139

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

see #14773 - checkstyle

  • Property svn:eol-style set to native
File size: 9.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.List;
10import java.util.ListIterator;
11import java.util.Map;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.Preferences;
15import org.openstreetmap.josm.data.Preferences.pref;
16import org.openstreetmap.josm.data.Preferences.writeExplicitly;
17import org.openstreetmap.josm.data.coor.EastNorth;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.projection.Projection;
20import org.openstreetmap.josm.data.projection.Projections;
21import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
22import org.openstreetmap.josm.gui.layer.ImageryLayer;
23
24/**
25 * Class to save a displacement of background imagery as a bookmark.
26 *
27 * Known offset bookmarks will be stored in the preferences and can be
28 * restored by the user in later sessions.
29 */
30public class OffsetBookmark {
31 private static final List<OffsetBookmark> allBookmarks = new ArrayList<>();
32
33 @pref private String projection_code;
34 @pref private String imagery_name;
35 @pref private String name;
36 @pref @writeExplicitly private double dx, dy;
37 @pref private double center_lon, center_lat;
38
39 public boolean isUsable(ImageryLayer layer) {
40 if (projection_code == null) return false;
41 if (!Main.getProjection().toCode().equals(projection_code) && !hasCenter()) return false;
42 return layer.getInfo().getName().equals(imagery_name);
43 }
44
45 /**
46 * Construct new empty OffsetBookmark.
47 *
48 * Only used for preferences handling.
49 */
50 public OffsetBookmark() {
51 // do nothing
52 }
53
54 public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy) {
55 this(projectionCode, imageryName, name, dx, dy, 0, 0);
56 }
57
58 public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy, double centerLon, double centerLat) {
59 this.projection_code = projectionCode;
60 this.imagery_name = imageryName;
61 this.name = name;
62 this.dx = dx;
63 this.dy = dy;
64 this.center_lon = centerLon;
65 this.center_lat = centerLat;
66 }
67
68 public OffsetBookmark(Collection<String> list) {
69 List<String> array = new ArrayList<>(list);
70 this.projection_code = array.get(0);
71 this.imagery_name = array.get(1);
72 this.name = array.get(2);
73 this.dx = Double.parseDouble(array.get(3));
74 this.dy = Double.parseDouble(array.get(4));
75 if (array.size() >= 7) {
76 this.center_lon = Double.parseDouble(array.get(5));
77 this.center_lat = Double.parseDouble(array.get(6));
78 }
79 if (projection_code == null) {
80 Main.error(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projection_code, name));
81 }
82 }
83
84 public String getProjectionCode() {
85 return projection_code;
86 }
87
88 public String getName() {
89 return name;
90 }
91
92 public String getImageryName() {
93 return imagery_name;
94 }
95
96 /**
97 * Get displacement in EastNorth coordinates of the original projection.
98 *
99 * @return the displacement
100 * @see #getProjectionCode()
101 */
102 public EastNorth getDisplacement() {
103 return new EastNorth(dx, dy);
104 }
105
106 /**
107 * Get displacement in EastNorth coordinates of a given projection.
108 *
109 * Displacement will be converted to the given projection, with respect to the
110 * center (reference point) of this bookmark.
111 * @param proj the projection
112 * @return the displacement, converted to that projection
113 */
114 public EastNorth getDisplacement(Projection proj) {
115 if (proj.toCode().equals(projection_code)) {
116 return getDisplacement();
117 }
118 LatLon center = getCenter();
119 Projection offsetProj = Projections.getProjectionByCode(projection_code);
120 EastNorth centerEN = offsetProj.latlon2eastNorth(center);
121 EastNorth shiftedEN = centerEN.add(getDisplacement());
122 LatLon shifted = offsetProj.eastNorth2latlon(shiftedEN);
123 EastNorth centerEN2 = proj.latlon2eastNorth(center);
124 EastNorth shiftedEN2 = proj.latlon2eastNorth(shifted);
125 return shiftedEN2.subtract(centerEN2);
126 }
127
128 /**
129 * Get center/reference point of the bookmark.
130 *
131 * Basically this is the place where it was created and is valid.
132 * The center may be unrecorded (see {@link #hasCenter()}, in which
133 * case a dummy center (0,0) will be returned.
134 * @return the center
135 */
136 public LatLon getCenter() {
137 return new LatLon(center_lat, center_lon);
138 }
139
140 /**
141 * Check if bookmark has a valid center.
142 * @return true if bookmark has a valid center
143 */
144 public boolean hasCenter() {
145 return center_lat != 0 || center_lon != 0;
146 }
147
148 public void setProjectionCode(String projectionCode) {
149 this.projection_code = projectionCode;
150 }
151
152 public void setName(String name) {
153 this.name = name;
154 }
155
156 public void setImageryName(String imageryName) {
157 this.imagery_name = imageryName;
158 }
159
160 public void setDisplacement(EastNorth displacement) {
161 this.dx = displacement.east();
162 this.dy = displacement.north();
163 }
164
165 public static void loadBookmarks() {
166 List<OffsetBookmark> bookmarks = Main.pref.getListOfStructs("imagery.offsetbookmarks", null, OffsetBookmark.class);
167 if (bookmarks == null) {
168 loadBookmarksOld();
169 saveBookmarks();
170 } else {
171 allBookmarks.addAll(bookmarks);
172 }
173 }
174
175 // migration code - remove Nov. 2017
176 private static void loadBookmarksOld() {
177 for (Collection<String> c : Main.pref.getArray("imagery.offsets",
178 Collections.<Collection<String>>emptySet())) {
179 allBookmarks.add(new OffsetBookmark(c));
180 }
181 }
182
183 public static void saveBookmarks() {
184 Main.pref.putListOfStructs("imagery.offsetbookmarks", allBookmarks, OffsetBookmark.class);
185 }
186
187 /**
188 * Returns all bookmarks.
189 * @return all bookmarks (unmodifiable collection)
190 * @since 11651
191 */
192 public static List<OffsetBookmark> getBookmarks() {
193 return Collections.unmodifiableList(allBookmarks);
194 }
195
196 /**
197 * Returns the number of bookmarks.
198 * @return the number of bookmarks
199 * @since 11651
200 */
201 public static int getBookmarksSize() {
202 return allBookmarks.size();
203 }
204
205 /**
206 * Adds a bookmark.
207 * @param ob bookmark to add
208 * @return {@code true}
209 * @since 11651
210 */
211 public static boolean addBookmark(OffsetBookmark ob) {
212 return allBookmarks.add(ob);
213 }
214
215 /**
216 * Removes a bookmark.
217 * @param ob bookmark to remove
218 * @return {@code true} if this list contained the specified element
219 * @since 11651
220 */
221 public static boolean removeBookmark(OffsetBookmark ob) {
222 return allBookmarks.remove(ob);
223 }
224
225 /**
226 * Returns the bookmark at the given index.
227 * @param index bookmark index
228 * @return the bookmark at the given index
229 * @throws IndexOutOfBoundsException if the index is out of range
230 * (<tt>index &lt; 0 || index &gt;= size()</tt>)
231 * @since 11651
232 */
233 public static OffsetBookmark getBookmarkByIndex(int index) {
234 return allBookmarks.get(index);
235 }
236
237 public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
238 for (OffsetBookmark b : allBookmarks) {
239 if (b.isUsable(layer) && name.equals(b.name))
240 return b;
241 }
242 return null;
243 }
244
245 public static void bookmarkOffset(String name, AbstractTileSourceLayer layer) {
246 LatLon center;
247 if (Main.isDisplayingMapView()) {
248 center = Main.getProjection().eastNorth2latlon(Main.map.mapView.getCenter());
249 } else {
250 center = LatLon.ZERO;
251 }
252 OffsetBookmark nb = new OffsetBookmark(
253 Main.getProjection().toCode(), layer.getInfo().getName(),
254 name, layer.getDisplaySettings().getDx(), layer.getDisplaySettings().getDy(), center.lon(), center.lat());
255 for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator(); it.hasNext();) {
256 OffsetBookmark b = it.next();
257 if (b.isUsable(layer) && name.equals(b.name)) {
258 it.set(nb);
259 saveBookmarks();
260 return;
261 }
262 }
263 allBookmarks.add(nb);
264 saveBookmarks();
265 }
266
267 /**
268 * Converts the offset bookmark to a properties map.
269 *
270 * The map contains all the information to restore the offset bookmark.
271 * @return properties map of all data
272 * @see #fromPropertiesMap(java.util.Map)
273 * @since 12134
274 */
275 public Map<String, String> toPropertiesMap() {
276 return Preferences.serializeStruct(this, OffsetBookmark.class);
277 }
278
279 /**
280 * Creates an offset bookmark from a properties map.
281 * @param properties the properties map
282 * @return corresponding offset bookmark
283 * @see #toPropertiesMap()
284 * @since 12134
285 */
286 public static OffsetBookmark fromPropertiesMap(Map<String, String> properties) {
287 return Preferences.deserializeStruct(properties, OffsetBookmark.class);
288 }
289}
Note: See TracBrowser for help on using the repository browser.