1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.ListIterator;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.projection.Mercator;
|
---|
16 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
17 | import org.openstreetmap.josm.data.projection.ProjectionInfo;
|
---|
18 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
19 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
20 |
|
---|
21 | public class OffsetBookmark {
|
---|
22 | public static List<OffsetBookmark> allBookmarks = new ArrayList<OffsetBookmark>();
|
---|
23 |
|
---|
24 | public Projection proj;
|
---|
25 | public String layerName;
|
---|
26 | public String name;
|
---|
27 | public double dx, dy;
|
---|
28 | public double centerX, centerY;
|
---|
29 |
|
---|
30 | public boolean isUsable(ImageryLayer layer) {
|
---|
31 | if (proj == null) return false;
|
---|
32 | if (!Main.proj.toCode().equals(proj.toCode())) return false;
|
---|
33 | return layer.getInfo().getName().equals(layerName);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy) {
|
---|
37 | this(proj, layerName, name, dx, dy, 0, 0);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public OffsetBookmark(Projection proj, String layerName, String name, double dx, double dy, double centerX, double centerY) {
|
---|
41 | this.proj = proj;
|
---|
42 | this.layerName = layerName;
|
---|
43 | this.name = name;
|
---|
44 | this.dx = dx;
|
---|
45 | this.dy = dy;
|
---|
46 | this.centerX = centerX;
|
---|
47 | this.centerY = centerY;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public OffsetBookmark(Collection<String> list) {
|
---|
51 | ArrayList<String> array = new ArrayList<String>(list);
|
---|
52 | String projectionStr = array.get(0);
|
---|
53 | proj = ProjectionInfo.getProjectionByCode(projectionStr);
|
---|
54 | if (proj == null) {
|
---|
55 | for (Projection proj : Projections.getProjections()) {
|
---|
56 | if (proj.getCacheDirectoryName().equals(projectionStr)) {
|
---|
57 | this.proj = proj;
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | this.layerName = array.get(1);
|
---|
63 | this.name = array.get(2);
|
---|
64 | this.dx = Double.valueOf(array.get(3));
|
---|
65 | this.dy = Double.valueOf(array.get(4));
|
---|
66 | if (array.size() >= 7) {
|
---|
67 | this.centerX = Double.valueOf(array.get(5));
|
---|
68 | this.centerY = Double.valueOf(array.get(6));
|
---|
69 | }
|
---|
70 | // Mercator scale factor migration
|
---|
71 | if (proj instanceof Mercator && Math.abs(this.dx) < 1E-3 && Math.abs(this.dy) < 1E-3) {
|
---|
72 | this.dx *= 6378137;
|
---|
73 | this.dy *= 6378137;
|
---|
74 | }
|
---|
75 | if (proj == null) {
|
---|
76 | System.err.println(tr("Projection ''{0}'' is not found, bookmark ''{1}'' is not usable", projectionStr, name));
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public ArrayList<String> getInfoArray() {
|
---|
81 | ArrayList<String> res = new ArrayList<String>(7);
|
---|
82 | if (proj != null) {
|
---|
83 | res.add(proj.toCode());
|
---|
84 | } else {
|
---|
85 | res.add("");
|
---|
86 | }
|
---|
87 | res.add(layerName);
|
---|
88 | res.add(name);
|
---|
89 | res.add(String.valueOf(dx));
|
---|
90 | res.add(String.valueOf(dy));
|
---|
91 | if (this.centerX != 0 || this.centerY != 0) {
|
---|
92 | res.add(String.valueOf(centerX));
|
---|
93 | res.add(String.valueOf(centerY));
|
---|
94 | }
|
---|
95 | return res;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public static void loadBookmarks() {
|
---|
99 | for(Collection<String> c : Main.pref.getArray("imagery.offsets",
|
---|
100 | Collections.<Collection<String>>emptySet())) {
|
---|
101 | allBookmarks.add(new OffsetBookmark(c));
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public static void saveBookmarks() {
|
---|
106 | LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>();
|
---|
107 | for (OffsetBookmark b : allBookmarks) {
|
---|
108 | coll.add(b.getInfoArray());
|
---|
109 | }
|
---|
110 | Main.pref.putArray("imagery.offsets", coll);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
|
---|
114 | for (OffsetBookmark b : allBookmarks) {
|
---|
115 | if (b.isUsable(layer) && name.equals(b.name))
|
---|
116 | return b;
|
---|
117 | }
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public static void bookmarkOffset(String name, ImageryLayer layer) {
|
---|
122 | LatLon center;
|
---|
123 | if (Main.map != null && Main.map.mapView != null) {
|
---|
124 | center = Main.proj.eastNorth2latlon(Main.map.mapView.getCenter());
|
---|
125 | } else {
|
---|
126 | center = new LatLon(0,0);
|
---|
127 | }
|
---|
128 | OffsetBookmark nb = new OffsetBookmark(
|
---|
129 | Main.proj, layer.getInfo().getName(),
|
---|
130 | name, layer.getDx(), layer.getDy(), center.lon(), center.lat());
|
---|
131 | for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator();it.hasNext();) {
|
---|
132 | OffsetBookmark b = it.next();
|
---|
133 | if (b.isUsable(layer) && name.equals(b.name)) {
|
---|
134 | it.set(nb);
|
---|
135 | saveBookmarks();
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | allBookmarks.add(nb);
|
---|
140 | saveBookmarks();
|
---|
141 | }
|
---|
142 | }
|
---|