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

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

save

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