source: osm/applications/editors/josm/plugins/seachartedit/src/scedit/SCeditAction.java@ 32329

Last change on this file since 32329 was 31156, checked in by malcolmh, 10 years ago

clip to map bounds

File size: 7.1 KB
Line 
1/* Copyright 2014 Malcolm Herring
2 *
3 * This is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License.
6 *
7 * For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
8 */
9
10package scedit;
11
12import java.awt.*;
13import java.awt.event.*;
14import java.util.*;
15import java.util.Map.Entry;
16
17import javax.swing.*;
18
19import static org.openstreetmap.josm.tools.I18n.tr;
20
21import org.openstreetmap.josm.actions.JosmAction;
22import org.openstreetmap.josm.gui.MapView;
23import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener;
24import org.openstreetmap.josm.gui.layer.*;
25import org.openstreetmap.josm.data.Bounds;
26import org.openstreetmap.josm.data.SelectionChangedListener;
27import org.openstreetmap.josm.data.coor.LatLon;
28import org.openstreetmap.josm.data.osm.*;
29import org.openstreetmap.josm.data.osm.event.*;
30import org.openstreetmap.josm.Main;
31
32import s57.S57map;
33import s57.S57map.*;
34import panels.PanelS57;
35import panels.PanelMain;
36import panels.ShowFrame;
37
38public class SCeditAction extends JosmAction implements EditLayerChangeListener, SelectionChangedListener {
39 private static String title = tr("SeaChart Editor");
40 public static JFrame editFrame = null;
41 public static ShowFrame showFrame = null;
42 private boolean isOpen = false;
43 public static PanelMain panelMain = null;
44 public static PanelS57 panelS57 = null;
45 public static S57map map = null;
46 public DataSet data = null;
47
48 private final DataSetListener dataSetListener = new DataSetListener() {
49
50 @Override
51 public void dataChanged(DataChangedEvent e) {
52 makeMap();
53 }
54
55 @Override
56 public void nodeMoved(NodeMovedEvent e) {
57 makeMap();
58 }
59
60 @Override
61 public void otherDatasetChange(AbstractDatasetChangedEvent e) {
62 makeMap();
63 }
64
65 @Override
66 public void primitivesAdded(PrimitivesAddedEvent e) {
67 makeMap();
68 }
69
70 @Override
71 public void primitivesRemoved(PrimitivesRemovedEvent e) {
72 makeMap();
73 }
74
75 @Override
76 public void relationMembersChanged(RelationMembersChangedEvent e) {
77 makeMap();
78 }
79
80 @Override
81 public void tagsChanged(TagsChangedEvent e) {
82 makeMap();
83 }
84
85 @Override
86 public void wayNodesChanged(WayNodesChangedEvent e) {
87 makeMap();
88 }
89 };
90
91 public SCeditAction() {
92 super(title, "SC", title, null, true);
93 }
94
95 @Override
96 public void actionPerformed(ActionEvent arg0) {
97 SwingUtilities.invokeLater(new Runnable() {
98 public void run() {
99 if (!isOpen)
100 createFrame();
101 else
102 editFrame.toFront();
103 isOpen = true;
104 }
105 });
106 }
107
108 protected void createFrame() {
109 editFrame = new JFrame(title);
110 editFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
111 editFrame.addWindowListener(new java.awt.event.WindowAdapter() {
112 public void windowClosing(java.awt.event.WindowEvent e) {
113 closeDialog();
114 }
115 });
116 editFrame.setSize(new Dimension(480, 480));
117 editFrame.setLocation(100, 200);
118 editFrame.setResizable(true);
119 editFrame.setAlwaysOnTop(true);
120 editFrame.setVisible(true);
121 panelMain = new PanelMain();
122 editFrame.add(panelMain);
123
124 panelS57 = new PanelS57();
125 editFrame.add(panelS57);
126
127 showFrame = new ShowFrame(tr("Seamark Inspector"));
128 showFrame.setSize(new Dimension(300, 300));
129 showFrame.setLocation(50, 400);
130 showFrame.setResizable(false);
131 showFrame.setAlwaysOnTop(true);
132 showFrame.setEnabled(true);
133 showFrame.setVisible(false);
134
135 // System.out.println("hello");
136 MapView.addEditLayerChangeListener(this);
137 DataSet.addSelectionListener(this);
138 editLayerChanged(Main.main.getEditLayer(), Main.main.getEditLayer());
139 }
140
141 public void closeDialog() {
142 if (isOpen) {
143 MapView.removeEditLayerChangeListener(this);
144 editFrame.setVisible(false);
145 editFrame.dispose();
146 data = null;
147 map = null;
148 }
149 isOpen = false;
150 }
151
152 @Override
153 public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
154 if (oldLayer != null) {
155 oldLayer.data.removeDataSetListener(dataSetListener);
156 }
157 if (newLayer != null) {
158 newLayer.data.addDataSetListener(dataSetListener);
159 data = newLayer.data;
160 makeMap();
161 } else {
162 data = null;
163 map = null;
164 }
165 }
166
167 @Override
168 public void selectionChanged(Collection<? extends OsmPrimitive> selection) {
169 OsmPrimitive nextFeature = null;
170 OsmPrimitive feature = null;
171
172 showFrame.setVisible(false);
173 panelMain.clearMark();
174 if (map != null) {
175 for (OsmPrimitive osm : selection) {
176 nextFeature = osm;
177 if (selection.size() == 1) {
178 if (nextFeature.compareTo(feature) != 0) {
179 feature = nextFeature;
180 Feature id = map.index.get(feature.getUniqueId());
181 if (id != null) {
182 panelMain.parseMark(id);
183 showFrame.setVisible(true);
184 showFrame.showFeature(feature, map);
185 }
186 }
187 } else {
188 showFrame.setVisible(false);
189 PanelMain.setStatus(tr("Select only one object"), Color.orange);
190 }
191 }
192 if (nextFeature == null) {
193 feature = null;
194 panelMain.clearMark();
195 showFrame.setVisible(false);
196 PanelMain.setStatus(tr("Select a map object"), Color.yellow);
197 }
198 }
199 }
200
201 void makeMap() {
202 map = new S57map(true);
203 if (data != null) {
204 double minlat = 90;
205 double maxlat = -90;
206 double minlon = 180;
207 double maxlon = -180;
208 for (Bounds bounds : data.getDataSourceBounds()) {
209 if (bounds.getMinLat() < minlat) {
210 minlat = bounds.getMinLat();
211 }
212 if (bounds.getMaxLat() > maxlat) {
213 maxlat = bounds.getMaxLat();
214 }
215 if (bounds.getMinLon() < minlon) {
216 minlon = bounds.getMinLon();
217 }
218 if (bounds.getMaxLon() > maxlon) {
219 maxlon = bounds.getMaxLon();
220 }
221 }
222 map.addNode(1, maxlat, minlon);
223 map.addNode(2, minlat, minlon);
224 map.addNode(3, minlat, maxlon);
225 map.addNode(4, maxlat, maxlon);
226 map.bounds.minlat = Math.toRadians(minlat);
227 map.bounds.maxlat = Math.toRadians(maxlat);
228 map.bounds.minlon = Math.toRadians(minlon);
229 map.bounds.maxlon = Math.toRadians(maxlon);
230 for (Node node : data.getNodes()) {
231 LatLon coor = node.getCoor();
232 if (coor != null) {
233 map.addNode(node.getUniqueId(), coor.lat(), coor.lon());
234 for (Entry<String, String> entry : node.getKeys().entrySet()) {
235 map.addTag(entry.getKey(), entry.getValue());
236 }
237 map.tagsDone(node.getUniqueId());
238 }
239 }
240 for (Way way : data.getWays()) {
241 if (way.getNodesCount() > 0) {
242 map.addEdge(way.getUniqueId());
243 for (Node node : way.getNodes()) {
244 map.addToEdge((node.getUniqueId()));
245 }
246 for (Entry<String, String> entry : way.getKeys().entrySet()) {
247 map.addTag(entry.getKey(), entry.getValue());
248 }
249 map.tagsDone(way.getUniqueId());
250 }
251 }
252 for (Relation rel : data.getRelations()) {
253 if (rel.isMultipolygon() && (rel.getMembersCount() > 0)) {
254 map.addArea(rel.getUniqueId());
255 for (RelationMember mem : rel.getMembers()) {
256 if (mem.getType() == OsmPrimitiveType.WAY)
257 map.addToArea(mem.getUniqueId(), (mem.getRole().equals("outer")));
258 }
259 for (Entry<String, String> entry : rel.getKeys().entrySet()) {
260 map.addTag(entry.getKey(), entry.getValue());
261 }
262 map.tagsDone(rel.getUniqueId());
263 }
264 }
265 map.mapDone();
266 }
267 }
268}
Note: See TracBrowser for help on using the repository browser.