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

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