source: osm/applications/editors/josm/plugins/seachartedit/src/panels/PanelS57.java@ 34508

Last change on this file since 34508 was 34098, checked in by donvip, 7 years ago

update to JOSM 13558/13559

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