source: osm/applications/editors/josm/plugins/smed2/src/panels/PanelS57.java@ 30298

Last change on this file since 30298 was 30298, checked in by malcolmh, 12 years ago

save

File size: 7.5 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 panels;
11
12import java.awt.Color;
13import java.awt.Dimension;
14import java.io.*;
15import java.util.ArrayList;
16import java.util.HashMap;
17import java.util.Map;
18import java.util.Scanner;
19
20import javax.swing.JFileChooser;
21import javax.swing.JPanel;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.data.Bounds;
25import org.openstreetmap.josm.data.coor.LatLon;
26import org.openstreetmap.josm.data.osm.*;
27import org.openstreetmap.josm.gui.layer.OsmDataLayer;
28
29import s57.S57att;
30import s57.S57dec;
31import s57.S57obj;
32import s57.S57map;
33import s57.S57val;
34import s57.S57att.Att;
35import s57.S57map.*;
36import s57.S57obj.*;
37import s57.S57val.AttVal;
38
39public class PanelS57 extends JPanel {
40
41 ArrayList<Obj> types = new ArrayList<Obj>();
42 S57map map;
43 ArrayList<Long> done = new ArrayList<Long>();
44
45 public PanelS57() {
46 setLayout(null);
47 setSize(new Dimension(480, 480));
48 setVisible(false);
49 }
50
51 public void startImport(File inf) throws IOException {
52 JFileChooser ifc = new JFileChooser(Main.pref.get("smed2plugin.file"));
53 FileInputStream in = new FileInputStream(inf);
54 PanelMain.setStatus("Select OSM types file", Color.yellow);
55 ifc.setCurrentDirectory(inf);
56 int returnVal = ifc.showOpenDialog(Main.parent);
57 if (returnVal == JFileChooser.APPROVE_OPTION) {
58 Main.pref.put("smed2plugin.file", ifc.getSelectedFile().getPath());
59 Scanner tin = new Scanner(new FileInputStream(ifc.getSelectedFile()));
60 while (tin.hasNext()) {
61 types.add(S57obj.enumType(tin.next()));
62 }
63 tin.close();
64 }
65 map = new S57map();
66 MapBounds bounds = S57dec.decodeFile(in, types, map);
67
68 in.close();
69
70 DataSet data = new DataSet();
71 data.setUploadDiscouraged(true);
72
73 for (long id : map.index.keySet()) {
74 Feature feature = map.index.get(id);
75 String type = S57obj.stringType(feature.type);
76 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) {
77 if (feature.reln == Rflag.MASTER) {
78 if (feature.geom.prim == Pflag.POINT) {
79 for (Prim prim : feature.geom.elems) {
80 long ref = prim.id;
81 Snode snode;
82 while ((snode = map.nodes.get(ref)) != null) {
83 if (!done.contains(ref)) {
84 Node node = new Node(new LatLon(Math.toDegrees(snode.lat), Math.toDegrees(snode.lon)));
85 node.setOsmId(ref, 1);
86 data.addPrimitive(node);
87 addKeys(node, feature, type);
88 done.add(ref);
89 }
90 ref++;
91 }
92 }
93 }
94 }
95 }
96 }
97 for (long id : map.index.keySet()) {
98 Feature feature = map.index.get(id);
99 String type = S57obj.stringType(feature.type);
100 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) {
101 if (feature.reln == Rflag.MASTER) {
102 if ((feature.geom.prim == Pflag.LINE) || ((feature.geom.prim == Pflag.AREA) && (feature.geom.outers == 1) && (feature.geom.inners == 0))) {
103 GeomIterator git = map.new GeomIterator(feature.geom);
104 while (git.hasComp()) {
105 git.nextComp();
106 while (git.hasEdge()) {
107 git.nextEdge();
108 while (git.hasNode()) {
109 long ref = git.nextRef();
110 Snode snode = map.nodes.get(ref);
111 if (!done.contains(ref)) {
112 Node node = new Node(new LatLon(Math.toDegrees(snode.lat), Math.toDegrees(snode.lon)));
113 node.setOsmId(ref, 1);
114 data.addPrimitive(node);
115 done.add(ref);
116 }
117 }
118 }
119 }
120 git = map.new GeomIterator(feature.geom);
121 while (git.hasComp()) {
122 long edge = git.nextComp();
123 Way way = new Way(edge, 1);
124 data.addPrimitive(way);
125 while (git.hasEdge()) {
126 git.nextEdge();
127 while (git.hasNode()) {
128 long ref = git.nextRef();
129 way.addNode((Node)data.getPrimitiveById(ref, OsmPrimitiveType.NODE));
130 }
131 addKeys(way, feature, type);
132 }
133 done.add(edge);
134 }
135 } else if (feature.geom.prim == Pflag.AREA) {
136 GeomIterator git = map.new GeomIterator(feature.geom);
137 while (git.hasComp()) {
138 git.nextComp();
139 while (git.hasEdge()) {
140 git.nextEdge();
141 while (git.hasNode()) {
142 long ref = git.nextRef();
143 Snode snode = map.nodes.get(ref);
144 if (!done.contains(ref)) {
145 Node node = new Node(new LatLon(Math.toDegrees(snode.lat), Math.toDegrees(snode.lon)));
146 node.setOsmId(ref, 1);
147 data.addPrimitive(node);
148 done.add(ref);
149 }
150 }
151 }
152 }
153 git = map.new GeomIterator(feature.geom);
154 while (git.hasComp()) {
155 git.nextComp();
156 while (git.hasEdge()) {
157 long edge = git.nextEdge();
158 if (!done.contains(edge)) {
159 Way way = new Way(edge, 1);
160 data.addPrimitive(way);
161 while (git.hasNode()) {
162 long ref = git.nextRef(true);
163 way.addNode((Node)data.getPrimitiveById(ref, OsmPrimitiveType.NODE));
164 }
165 done.add(edge);
166 }
167 }
168 }
169 Relation rel = new Relation(map.ref++, 1);
170 data.addPrimitive(rel);
171 git = map.new GeomIterator(feature.geom);
172 int outers = feature.geom.refs.get(0).size;
173 while (git.hasComp()) {
174 git.nextComp();
175 while (git.hasEdge()) {
176 long way = git.nextEdge();
177 if (outers-- > 0) {
178 rel.addMember(new RelationMember("outer", (Way)data.getPrimitiveById(way, OsmPrimitiveType.WAY)));
179 } else {
180 rel.addMember(new RelationMember("inner", (Way)data.getPrimitiveById(way, OsmPrimitiveType.WAY)));
181 }
182 }
183 }
184 addKeys(rel, feature, type);
185 }
186 }
187 }
188 }
189
190 OsmDataLayer layer = new OsmDataLayer(data, "S-57 Import", null);
191 Main.map.mapView.addLayer(layer);
192 Main.map.mapView.zoomTo(new Bounds(bounds.minlat, bounds.minlon, bounds.maxlat, bounds.maxlon));
193 PanelMain.setStatus("Import done", Color.green);
194 }
195
196 void addKeys(OsmPrimitive prim, Feature feature, String type) {
197 HashMap<String,String> keys = new HashMap<String,String>();
198 if (prim instanceof Relation) {
199 keys.put("type", "multipolygon");
200 }
201 keys.put("seamark:type", type);
202 if (feature.type == Obj.SOUNDG) {
203 Snode snode = map.nodes.get(feature.geom.elems.get(0).id);
204 if (snode.flg == S57map.Nflag.DPTH) {
205 keys.put("seamark:sounding:depth", ((Double)((Dnode)snode).val).toString());
206 }
207 }
208 for (Map.Entry<Att, AttVal<?>> item : feature.atts.entrySet()) {
209 String attstr = S57att.stringAttribute(item.getKey());
210 String valstr = S57val.stringValue(item.getValue());
211 if (!attstr.isEmpty() && !valstr.isEmpty()) {
212 keys.put(("seamark:" + type + ":" + attstr), valstr);
213 }
214 }
215 for (Obj obj : feature.objs.keySet()) {
216 ObjTab tab = feature.objs.get(obj);
217 for (int ix : tab.keySet()) {
218 type = S57obj.stringType(obj);
219 AttMap atts = tab.get(ix);
220 for (Map.Entry<Att, AttVal<?>> item : atts.entrySet()) {
221 String attstr = S57att.stringAttribute(item.getKey());
222 String valstr = S57val.stringValue(item.getValue());
223 if (!attstr.isEmpty() && !valstr.isEmpty()) {
224 if ((ix == 0) && (tab.size() == 1)) {
225 keys.put(("seamark:" + type + ":" + attstr), valstr);
226 } else {
227 keys.put(("seamark:" + type + ":" + (ix+1) + ":" + attstr), valstr);
228 }
229 }
230 }
231 }
232 }
233 prim.setKeys(keys);
234 }
235
236
237 public void startExport(File outf) throws IOException {
238
239 }
240}
Note: See TracBrowser for help on using the repository browser.