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