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