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

Last change on this file since 30317 was 30315, checked in by malcolmh, 11 years ago

save

File size: 7.3 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 FileInputStream in = new FileInputStream(inf);
53 PanelMain.setStatus("Select OSM types file", Color.yellow);
54 JFileChooser ifc = new JFileChooser(Main.pref.get("smed2plugin.typesfile"));
55 int returnVal = ifc.showOpenDialog(Main.parent);
56 if (returnVal == JFileChooser.APPROVE_OPTION) {
57 Main.pref.put("smed2plugin.typesfile", ifc.getSelectedFile().getPath());
58 Scanner tin = new Scanner(new FileInputStream(ifc.getSelectedFile()));
59 while (tin.hasNext()) {
60 Obj type = S57obj.enumType(tin.next());
61 if (type != Obj.UNKOBJ)
62 types.add(type);
63 }
64 tin.close();
65 }
66 map = new S57map();
67 MapBounds bounds = S57dec.decodeFile(in, types, map);
68
69 in.close();
70
71 DataSet data = new DataSet();
72 data.setUploadDiscouraged(true);
73
74 for (long id : map.index.keySet()) {
75 Feature feature = map.index.get(id);
76 String type = S57obj.stringType(feature.type);
77 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) {
78 if (feature.reln == Rflag.MASTER) {
79 if (feature.geom.prim == Pflag.POINT) {
80 for (Prim prim : feature.geom.elems) {
81 long ref = prim.id;
82 Snode snode;
83 while ((snode = map.nodes.get(ref)) != null) {
84 if (!done.contains(ref)) {
85 Node node = new Node(ref, 1);
86 node.setCoor((new LatLon(Math.toDegrees(snode.lat), Math.toDegrees(snode.lon))));
87 data.addPrimitive(node);
88 addKeys(node, feature, type);
89 done.add(ref);
90 }
91 ref++;
92 }
93 }
94 }
95 }
96 }
97 }
98 for (long id : map.index.keySet()) {
99 Feature feature = map.index.get(id);
100 String type = S57obj.stringType(feature.type);
101 if (!type.isEmpty() && (types.isEmpty() || types.contains(feature.type))) {
102 if (feature.reln == Rflag.MASTER) {
103 if ((feature.geom.prim == Pflag.LINE) || ((feature.geom.prim == Pflag.AREA) && (feature.geom.outers == 1) && (feature.geom.inners == 0))) {
104 GeomIterator git = map.new GeomIterator(feature.geom);
105 while (git.hasComp()) {
106 git.nextComp();
107 while (git.hasEdge()) {
108 git.nextEdge();
109 while (git.hasNode()) {
110 long ref = git.nextRef();
111 Snode snode = map.nodes.get(ref);
112 if (!done.contains(ref)) {
113 Node node = new Node(ref, 1);
114 node.setCoor((new LatLon(Math.toDegrees(snode.lat), Math.toDegrees(snode.lon))));
115 data.addPrimitive(node);
116 done.add(ref);
117 }
118 }
119 }
120 }
121 git = map.new GeomIterator(feature.geom);
122 while (git.hasComp()) {
123 long edge = git.nextComp();
124 Way way = new Way(edge, 1);
125 data.addPrimitive(way);
126 while (git.hasEdge()) {
127 git.nextEdge();
128 while (git.hasNode()) {
129 long ref = git.nextRef();
130 way.addNode((Node)data.getPrimitiveById(ref, OsmPrimitiveType.NODE));
131 }
132 }
133 addKeys(way, feature, type);
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(ref, 1);
146 node.setCoor((new LatLon(Math.toDegrees(snode.lat), Math.toDegrees(snode.lon))));
147 data.addPrimitive(node);
148 done.add(ref);
149 }
150 }
151 }
152 }
153 git = map.new GeomIterator(feature.geom);
154 while (git.hasComp()) {
155 long ref = git.nextComp();
156 Way way = new Way(ref, 1);
157 data.addPrimitive(way);
158 while (git.hasEdge()) {
159 git.nextEdge();
160 while (git.hasNode()) {
161 ref = git.nextRef();
162 way.addNode((Node) data.getPrimitiveById(ref, OsmPrimitiveType.NODE));
163 }
164 }
165 }
166 Relation rel = new Relation(map.ref++, 1);
167 data.addPrimitive(rel);
168 git = map.new GeomIterator(feature.geom);
169 int outers = feature.geom.outers;
170 while (git.hasComp()) {
171 long ref = git.nextComp();
172 if (outers-- > 0) {
173 rel.addMember(new RelationMember("outer", (Way) data.getPrimitiveById(ref, OsmPrimitiveType.WAY)));
174 } else {
175 rel.addMember(new RelationMember("inner", (Way) data.getPrimitiveById(ref, OsmPrimitiveType.WAY)));
176 }
177 }
178 addKeys(rel, feature, type);
179 }
180 }
181 }
182 }
183
184 OsmDataLayer layer = new OsmDataLayer(data, "S-57 Import", null);
185 Main.map.mapView.addLayer(layer);
186 Main.map.mapView.zoomTo(new Bounds(bounds.minlat, bounds.minlon, bounds.maxlat, bounds.maxlon));
187 PanelMain.setStatus("Import done", Color.green);
188 }
189
190 void addKeys(OsmPrimitive prim, Feature feature, String type) {
191 HashMap<String,String> keys = new HashMap<String,String>();
192 if (prim instanceof Relation) {
193 keys.put("type", "multipolygon");
194 }
195 keys.put("seamark:type", type);
196 if (feature.type == Obj.SOUNDG) {
197 Snode snode = map.nodes.get(feature.geom.elems.get(0).id);
198 if (snode.flg == S57map.Nflag.DPTH) {
199 keys.put("seamark:sounding:depth", ((Double)((Dnode)snode).val).toString());
200 }
201 }
202 for (Map.Entry<Att, AttVal<?>> item : feature.atts.entrySet()) {
203 String attstr = S57att.stringAttribute(item.getKey());
204 String valstr = S57val.stringValue(item.getValue());
205 if (!attstr.isEmpty() && !valstr.isEmpty()) {
206 keys.put(("seamark:" + type + ":" + attstr), valstr);
207 }
208 }
209 for (Obj obj : feature.objs.keySet()) {
210 ObjTab tab = feature.objs.get(obj);
211 for (int ix : tab.keySet()) {
212 type = S57obj.stringType(obj);
213 AttMap atts = tab.get(ix);
214 for (Map.Entry<Att, AttVal<?>> item : atts.entrySet()) {
215 String attstr = S57att.stringAttribute(item.getKey());
216 String valstr = S57val.stringValue(item.getValue());
217 if (!attstr.isEmpty() && !valstr.isEmpty()) {
218 if ((ix == 0) && (tab.size() == 1)) {
219 keys.put(("seamark:" + type + ":" + attstr), valstr);
220 } else {
221 keys.put(("seamark:" + type + ":" + (ix+1) + ":" + attstr), valstr);
222 }
223 }
224 }
225 }
226 }
227 prim.setKeys(keys);
228 }
229
230
231 public void startExport(File outf) throws IOException {
232
233 }
234}
Note: See TracBrowser for help on using the repository browser.