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

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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