1 | package org.openstreetmap.josm.gui.layer;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.io.File;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.LinkedList;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.Icon;
|
---|
15 | import javax.swing.JColorChooser;
|
---|
16 | import javax.swing.JFileChooser;
|
---|
17 | import javax.swing.JMenuItem;
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 | import javax.swing.JSeparator;
|
---|
20 | import javax.swing.filechooser.FileFilter;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.actions.GpxExportAction;
|
---|
24 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
25 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
26 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
27 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
28 | import org.openstreetmap.josm.data.osm.Node;
|
---|
29 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
30 | import org.openstreetmap.josm.data.osm.Way;
|
---|
31 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
32 | import org.openstreetmap.josm.gui.MapView;
|
---|
33 | import org.openstreetmap.josm.gui.dialogs.LayerList;
|
---|
34 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
35 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
36 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * A layer holding data from a gps source.
|
---|
40 | * The data is read only.
|
---|
41 | *
|
---|
42 | * @author imi
|
---|
43 | */
|
---|
44 | public class RawGpsLayer extends Layer implements PreferenceChangedListener {
|
---|
45 |
|
---|
46 | public class ConvertToDataLayerAction extends AbstractAction {
|
---|
47 | public ConvertToDataLayerAction() {
|
---|
48 | super("Convert to data layer", ImageProvider.get("converttoosm"));
|
---|
49 | }
|
---|
50 | public void actionPerformed(ActionEvent e) {
|
---|
51 | DataSet ds = new DataSet();
|
---|
52 | for (Collection<GpsPoint> c : data) {
|
---|
53 | Way w = new Way();
|
---|
54 | Node start = null;
|
---|
55 | for (GpsPoint p : c) {
|
---|
56 | Node end = new Node(p.latlon);
|
---|
57 | ds.nodes.add(end);
|
---|
58 | if (start != null) {
|
---|
59 | Segment segment = new Segment(start,end);
|
---|
60 | w.segments.add(segment);
|
---|
61 | ds.segments.add(segment);
|
---|
62 | }
|
---|
63 | start = end;
|
---|
64 | }
|
---|
65 | ds.ways.add(w);
|
---|
66 | }
|
---|
67 | Main.main.addLayer(new OsmDataLayer(ds, "Data Layer", true));
|
---|
68 | Main.main.removeLayer(RawGpsLayer.this);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static class GpsPoint {
|
---|
73 | public final LatLon latlon;
|
---|
74 | public final EastNorth eastNorth;
|
---|
75 | public final String time;
|
---|
76 | public GpsPoint(LatLon ll, String t) {
|
---|
77 | latlon = ll;
|
---|
78 | eastNorth = Main.proj.latlon2eastNorth(ll);
|
---|
79 | time = t;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * A list of ways which containing a list of points.
|
---|
85 | */
|
---|
86 | public final Collection<Collection<GpsPoint>> data;
|
---|
87 |
|
---|
88 | public RawGpsLayer(Collection<Collection<GpsPoint>> data, String name) {
|
---|
89 | super(name);
|
---|
90 | this.data = data;
|
---|
91 | Main.pref.listener.add(this);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Return a static icon.
|
---|
96 | */
|
---|
97 | @Override public Icon getIcon() {
|
---|
98 | return ImageProvider.get("layer", "rawgps");
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override public void paint(Graphics g, MapView mv) {
|
---|
102 | String gpsCol = Main.pref.get("color.gps point");
|
---|
103 | String gpsColSpecial = Main.pref.get("color.layer "+name);
|
---|
104 | if (!gpsColSpecial.equals(""))
|
---|
105 | g.setColor(ColorHelper.html2color(gpsColSpecial));
|
---|
106 | else if (!gpsCol.equals(""))
|
---|
107 | g.setColor(ColorHelper.html2color(gpsCol));
|
---|
108 | else
|
---|
109 | g.setColor(Color.GRAY);
|
---|
110 | Point old = null;
|
---|
111 | for (Collection<GpsPoint> c : data) {
|
---|
112 | if (!Main.pref.getBoolean("draw.rawgps.lines.force"))
|
---|
113 | old = null;
|
---|
114 | for (GpsPoint p : c) {
|
---|
115 | Point screen = mv.getPoint(p.eastNorth);
|
---|
116 | if (Main.pref.getBoolean("draw.rawgps.lines") && old != null)
|
---|
117 | g.drawLine(old.x, old.y, screen.x, screen.y);
|
---|
118 | else
|
---|
119 | g.drawRect(screen.x, screen.y, 0, 0);
|
---|
120 | old = screen;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override public String getToolTipText() {
|
---|
126 | int points = 0;
|
---|
127 | for (Collection<GpsPoint> c : data)
|
---|
128 | points += c.size();
|
---|
129 | return data.size()+" tracks, "+points+" points.";
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override public void mergeFrom(Layer from) {
|
---|
133 | RawGpsLayer layer = (RawGpsLayer)from;
|
---|
134 | data.addAll(layer.data);
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override public boolean isMergable(Layer other) {
|
---|
138 | return other instanceof RawGpsLayer;
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override public void visitBoundingBox(BoundingXYVisitor v) {
|
---|
142 | for (Collection<GpsPoint> c : data)
|
---|
143 | for (GpsPoint p : c)
|
---|
144 | v.visit(p.eastNorth);
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override public Object getInfoComponent() {
|
---|
148 | StringBuilder b = new StringBuilder();
|
---|
149 | int points = 0;
|
---|
150 | for (Collection<GpsPoint> c : data) {
|
---|
151 | b.append(" a track with "+c.size()+" points<br>");
|
---|
152 | points += c.size();
|
---|
153 | }
|
---|
154 | b.append("</html>");
|
---|
155 | return "<html>"+name+" consists of "+data.size()+" tracks ("+points+" points)<br>"+b.toString();
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override public Component[] getMenuEntries() {
|
---|
159 | JMenuItem color = new JMenuItem("Customize Color", ImageProvider.get("colorchooser"));
|
---|
160 | color.addActionListener(new ActionListener(){
|
---|
161 | public void actionPerformed(ActionEvent e) {
|
---|
162 | String col = Main.pref.get("color.layer "+name, Main.pref.get("color.gps point", ColorHelper.color2html(Color.gray)));
|
---|
163 | JColorChooser c = new JColorChooser(ColorHelper.html2color(col));
|
---|
164 | Object[] options = new Object[]{"OK", "Cancel", "Default"};
|
---|
165 | int answer = JOptionPane.showOptionDialog(Main.parent, c, "Choose a color", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
|
---|
166 | switch (answer) {
|
---|
167 | case 0:
|
---|
168 | Main.pref.put("color.layer "+name, ColorHelper.color2html(c.getColor()));
|
---|
169 | break;
|
---|
170 | case 1:
|
---|
171 | return;
|
---|
172 | case 2:
|
---|
173 | Main.pref.put("color.layer "+name, null);
|
---|
174 | break;
|
---|
175 | }
|
---|
176 | Main.map.repaint();
|
---|
177 | }
|
---|
178 | });
|
---|
179 |
|
---|
180 | JMenuItem tagimage = new JMenuItem("Import images", ImageProvider.get("tagimages"));
|
---|
181 | tagimage.addActionListener(new ActionListener(){
|
---|
182 | public void actionPerformed(ActionEvent e) {
|
---|
183 | JFileChooser fc = new JFileChooser(Main.pref.get("tagimages.lastdirectory"));
|
---|
184 | fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
---|
185 | fc.setMultiSelectionEnabled(true);
|
---|
186 | fc.setAcceptAllFileFilterUsed(false);
|
---|
187 | fc.setFileFilter(new FileFilter(){
|
---|
188 | @Override public boolean accept(File f) {
|
---|
189 | return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg");
|
---|
190 | }
|
---|
191 | @Override public String getDescription() {
|
---|
192 | return "JPEG images (*.jpg)";
|
---|
193 | }
|
---|
194 | });
|
---|
195 | fc.showOpenDialog(Main.parent);
|
---|
196 | File[] sel = fc.getSelectedFiles();
|
---|
197 | if (sel == null || sel.length == 0)
|
---|
198 | return;
|
---|
199 | LinkedList<File> files = new LinkedList<File>();
|
---|
200 | addRecursiveFiles(files, sel);
|
---|
201 | Main.pref.put("tagimages.lastdirectory", fc.getCurrentDirectory().getPath());
|
---|
202 | GeoImageLayer.create(files, RawGpsLayer.this);
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void addRecursiveFiles(LinkedList<File> files, File[] sel) {
|
---|
206 | for (File f : sel) {
|
---|
207 | if (f.isDirectory())
|
---|
208 | addRecursiveFiles(files, f.listFiles());
|
---|
209 | else if (f.getName().toLowerCase().endsWith(".jpg"))
|
---|
210 | files.add(f);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | });
|
---|
214 | return new Component[]{
|
---|
215 | new JMenuItem(new LayerList.ShowHideLayerAction(this)),
|
---|
216 | new JMenuItem(new LayerList.DeleteLayerAction(this)),
|
---|
217 | new JSeparator(),
|
---|
218 | new JMenuItem(new GpxExportAction(this)),
|
---|
219 | color,
|
---|
220 | tagimage,
|
---|
221 | new JMenuItem(new ConvertToDataLayerAction()),
|
---|
222 | new JSeparator(),
|
---|
223 | new JMenuItem(new LayerListPopup.InfoAction(this))};
|
---|
224 | }
|
---|
225 |
|
---|
226 | public void preferenceChanged(String key, String newValue) {
|
---|
227 | if (Main.map != null && (key.equals("draw.rawgps.lines") || key.equals("draw.rawgps.lines.force")))
|
---|
228 | Main.map.repaint();
|
---|
229 | }
|
---|
230 |
|
---|
231 | @Override public void layerRemoved() {
|
---|
232 | Main.pref.listener.remove(this);
|
---|
233 | }
|
---|
234 | }
|
---|