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