1 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.io.FileOutputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.PrintWriter;
|
---|
7 | import java.io.BufferedReader;
|
---|
8 | import java.io.FileReader;
|
---|
9 | import java.io.File;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashSet;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JMenu;
|
---|
16 | import javax.swing.JMenuBar;
|
---|
17 | import javax.swing.JMenuItem;
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 | import javax.swing.JTextField;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
23 | import org.openstreetmap.josm.data.Bounds;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
27 | import org.openstreetmap.josm.data.osm.Way;
|
---|
28 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
29 | import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
|
---|
30 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
31 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
32 | import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
|
---|
33 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
34 | import org.openstreetmap.josm.io.OsmWriter;
|
---|
35 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
36 | import org.openstreetmap.josm.tools.GBC;
|
---|
37 |
|
---|
38 | public class OsmarenderPlugin extends Plugin {
|
---|
39 |
|
---|
40 | private class Action extends JosmAction {
|
---|
41 |
|
---|
42 | public Action() {
|
---|
43 | super(tr("Osmarender"), null, tr("Osmarender"), null, true);
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void actionPerformed(ActionEvent e) {
|
---|
47 | // get all stuff visible on screen
|
---|
48 | LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight());
|
---|
49 | LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0);
|
---|
50 | Bounds b = new Bounds(bottomLeft, topRight);
|
---|
51 |
|
---|
52 | try {
|
---|
53 | writeGenerated(b);
|
---|
54 | } catch(Exception ex) {
|
---|
55 | //how handle the exception?
|
---|
56 | }
|
---|
57 |
|
---|
58 | CollectBackReferencesVisitor backRefsV = new CollectBackReferencesVisitor(Main.ds, true);
|
---|
59 | DataSet fromDataSet = new DataSet();
|
---|
60 | for (Node n : Main.ds.nodes) {
|
---|
61 | if (n.deleted || n.incomplete) continue;
|
---|
62 | if (n.coor.isWithin(b)) {
|
---|
63 | fromDataSet.nodes.add(n);
|
---|
64 | n.visit(backRefsV);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | for (OsmPrimitive p : new HashSet<OsmPrimitive>(backRefsV.data)) {
|
---|
68 | if (p instanceof Way) {
|
---|
69 | backRefsV.data.addAll(((Way) p).nodes);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | for (OsmPrimitive p : backRefsV.data)
|
---|
73 | fromDataSet.addPrimitive(p);
|
---|
74 |
|
---|
75 | String firefox = Main.pref.get("osmarender.firefox", "firefox");
|
---|
76 | try {
|
---|
77 | // write to plugin dir
|
---|
78 | OsmWriter.output(new FileOutputStream(getPluginDir()+File.separator+"data.osm"), new OsmWriter.All(fromDataSet, true));
|
---|
79 |
|
---|
80 | // get the exec line
|
---|
81 | String exec = firefox;
|
---|
82 | if (System.getProperty("os.name").startsWith("Windows"))
|
---|
83 | exec += " file:///"+getPluginDir().replace('\\','/').replace(" ","%20")+File.separator+"generated.xml\"";
|
---|
84 | else
|
---|
85 | exec += " "+getPluginDir()+File.separator+"generated.xml";
|
---|
86 |
|
---|
87 | // launch up the viewer
|
---|
88 | Runtime.getRuntime().exec(exec);
|
---|
89 | } catch (IOException e1) {
|
---|
90 | JOptionPane.showMessageDialog(Main.parent, tr("Firefox not found. Please set firefox executable in the Map Settings page of the preferences."));
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private JMenuItem osmarenderMenu;
|
---|
96 |
|
---|
97 | public OsmarenderPlugin() throws IOException {
|
---|
98 | osmarenderMenu = MainMenu.add(Main.main.menu.viewMenu, new Action());
|
---|
99 | osmarenderMenu.setVisible(false);
|
---|
100 |
|
---|
101 | // install the xsl and xml file
|
---|
102 | copy("/osmarender.xsl", "osmarender.xsl");
|
---|
103 | copy("/osm-map-features.xml", "osm-map-features.xml");
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
---|
108 | if (oldFrame != null && newFrame == null) {
|
---|
109 | // disable
|
---|
110 | osmarenderMenu.setVisible(false);
|
---|
111 | } else if (oldFrame == null && newFrame != null) {
|
---|
112 | // enable
|
---|
113 | osmarenderMenu.setVisible(true);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override public PreferenceSetting getPreferenceSetting() {
|
---|
118 | return new PreferenceSetting(){
|
---|
119 | private JTextField firefox = new JTextField(10);
|
---|
120 | public void addGui(PreferenceDialog gui) {
|
---|
121 | gui.map.add(new JLabel(tr("osmarender options")), GBC.eol().insets(0,5,0,0));
|
---|
122 | gui.map.add(new JLabel(tr("Firefox executable")), GBC.std().insets(10,5,5,0));
|
---|
123 | gui.map.add(firefox, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
|
---|
124 | firefox.setText(Main.pref.get("osmarender.firefox"));
|
---|
125 | }
|
---|
126 | public boolean ok() {
|
---|
127 | Main.pref.put("osmarender.firefox", firefox.getText());
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 | };
|
---|
131 | }
|
---|
132 |
|
---|
133 | private void writeGenerated(Bounds b) throws IOException {
|
---|
134 | String bounds_tag = "<bounds " +
|
---|
135 | "minlat=\"" + b.min.lat() + "\" " +
|
---|
136 | "maxlat=\"" + b.max.lat() + "\" " +
|
---|
137 | "minlon=\"" + b.min.lon() + "\" " +
|
---|
138 | "maxlon=\"" + b.max.lon() + "\" " + "/>";
|
---|
139 |
|
---|
140 | BufferedReader reader = new BufferedReader(
|
---|
141 | new FileReader( getPluginDir() + File.separator + "osm-map-features.xml") );
|
---|
142 | PrintWriter writer = new PrintWriter( getPluginDir() + File.separator + "generated.xml");
|
---|
143 |
|
---|
144 | // osm-map-fetaures.xml contain two placemark
|
---|
145 | // (bounds_mkr1 and bounds_mkr2). We write the bounds tag
|
---|
146 | // between the two
|
---|
147 | String str = null;
|
---|
148 | while( (str = reader.readLine()) != null ) {
|
---|
149 | if(str.contains("<!--bounds_mkr1-->")) {
|
---|
150 | writer.println(str);
|
---|
151 | writer.println(" " + bounds_tag);
|
---|
152 | while(!str.contains("<!--bounds_mkr2-->")) {
|
---|
153 | str = reader.readLine();
|
---|
154 | }
|
---|
155 | writer.println(str);
|
---|
156 | } else {
|
---|
157 | writer.println(str);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | writer.close();
|
---|
162 | }
|
---|
163 | }
|
---|