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.util.Collection;
|
---|
7 | import java.util.HashSet;
|
---|
8 |
|
---|
9 | import javax.swing.AbstractAction;
|
---|
10 | import javax.swing.JMenu;
|
---|
11 | import javax.swing.JMenuBar;
|
---|
12 | import javax.swing.JMenuItem;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.Bounds;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
|
---|
23 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
24 | import org.openstreetmap.josm.io.OsmWriter;
|
---|
25 | import org.openstreetmap.josm.plugins.Plugin;
|
---|
26 |
|
---|
27 |
|
---|
28 | public class OsmarenderPlugin extends Plugin {
|
---|
29 |
|
---|
30 | private class Action extends AbstractAction {
|
---|
31 |
|
---|
32 | public Action() {
|
---|
33 | super("Osmarender");
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void actionPerformed(ActionEvent e) {
|
---|
37 | // get all stuff visible on screen
|
---|
38 | LatLon bottomLeft = Main.map.mapView.getLatLon(0,Main.map.mapView.getHeight());
|
---|
39 | LatLon topRight = Main.map.mapView.getLatLon(Main.map.mapView.getWidth(), 0);
|
---|
40 | Bounds b = new Bounds(bottomLeft, topRight);
|
---|
41 | Collection<Node> nodes = new HashSet<Node>();
|
---|
42 | DataSet fromDataSet = new DataSet();
|
---|
43 | AddVisitor adder = new AddVisitor(fromDataSet);
|
---|
44 | for (Node n : Main.ds.nodes) {
|
---|
45 | if (n.coor.isWithin(b)) {
|
---|
46 | n.visit(adder);
|
---|
47 | nodes.add(n);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | Collection<Segment> segments = new HashSet<Segment>();
|
---|
51 | for (Segment s : Main.ds.segments) {
|
---|
52 | if (nodes.contains(s.from) || nodes.contains(s.to)) {
|
---|
53 | s.visit(adder);
|
---|
54 | segments.add(s);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | for (Way w : Main.ds.ways) {
|
---|
58 | for (Segment s : w.segments) {
|
---|
59 | if (segments.contains(s)) {
|
---|
60 | w.visit(adder);
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | try {
|
---|
67 | // store the stuff in data.osm
|
---|
68 | OsmWriter.output(new FileOutputStream(getPluginDir()+"data.osm"), new OsmWriter.All(fromDataSet, true));
|
---|
69 |
|
---|
70 | // launch up the viewer
|
---|
71 | String firefox = Main.pref.get("osmarender.firefox", "firefox");
|
---|
72 | Runtime.getRuntime().exec(firefox+" "+getPluginDir()+"osm-map-features.xml");
|
---|
73 | } catch (IOException e1) {
|
---|
74 | JOptionPane.showMessageDialog(Main.parent, "Could not launch Osmarender.");
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private JMenu view;
|
---|
80 | private JMenuItem osmarenderMenu = new JMenuItem(new Action());
|
---|
81 |
|
---|
82 | public OsmarenderPlugin() throws IOException {
|
---|
83 | JMenuBar menu = Main.main.mainMenu;
|
---|
84 | view = null;
|
---|
85 | for (int i = 0; i < menu.getMenuCount(); ++i) {
|
---|
86 | if (menu.getMenu(i) != null && tr("View").equals(menu.getMenu(i).getName())) {
|
---|
87 | view = menu.getMenu(i);
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | if (view == null) {
|
---|
92 | view = new JMenu(tr("View"));
|
---|
93 | Main.main.mainMenu.add(view, 1);
|
---|
94 | view.setVisible(false);
|
---|
95 | }
|
---|
96 | view.add(osmarenderMenu);
|
---|
97 | osmarenderMenu.setVisible(false);
|
---|
98 |
|
---|
99 | // install the xsl and xml file
|
---|
100 | copy("/osmarender.xsl", "osmarender.xsl");
|
---|
101 | copy("/osm-map-features.xml", "osm-map-features.xml");
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
---|
106 | if (oldFrame != null && newFrame == null) {
|
---|
107 | // disable
|
---|
108 | osmarenderMenu.setVisible(false);
|
---|
109 | if (view.getMenuComponentCount() == 1)
|
---|
110 | view.setVisible(false);
|
---|
111 | } else if (oldFrame == null && newFrame != null) {
|
---|
112 | // enable
|
---|
113 | osmarenderMenu.setVisible(true);
|
---|
114 | if (view.getMenuComponentCount() == 1)
|
---|
115 | view.setVisible(true);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|