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.*;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.HashMap;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.Scanner;
|
---|
19 |
|
---|
20 | import javax.swing.JFileChooser;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.data.Bounds;
|
---|
25 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
26 | import org.openstreetmap.josm.data.osm.*;
|
---|
27 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
28 |
|
---|
29 | import s57.S57att;
|
---|
30 | import s57.S57dec;
|
---|
31 | import s57.S57obj;
|
---|
32 | import s57.S57map;
|
---|
33 | import s57.S57val;
|
---|
34 | import s57.S57att.Att;
|
---|
35 | import s57.S57map.*;
|
---|
36 | import s57.S57obj.*;
|
---|
37 | import s57.S57val.AttVal;
|
---|
38 |
|
---|
39 | public class PanelS57 extends JPanel {
|
---|
40 |
|
---|
41 | ArrayList<Obj> types = new ArrayList<Obj>();
|
---|
42 | S57map map;
|
---|
43 | HashMap<Long, Long> done = new HashMap<Long, 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, 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.containsKey(ref)) {
|
---|
85 | Node node = new Node(0, 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.put(ref, node.getUniqueId());
|
---|
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.containsKey(ref)) {
|
---|
113 | Node node = new Node(0, 1);
|
---|
114 | node.setCoor((new LatLon(Math.toDegrees(snode.lat), Math.toDegrees(snode.lon))));
|
---|
115 | data.addPrimitive(node);
|
---|
116 | done.put(ref, node.getUniqueId());
|
---|
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(0, 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(done.get(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.containsKey(ref)) {
|
---|
145 | Node node = new Node(0, 1);
|
---|
146 | node.setCoor((new LatLon(Math.toDegrees(snode.lat), Math.toDegrees(snode.lon))));
|
---|
147 | data.addPrimitive(node);
|
---|
148 | done.put(ref, node.getUniqueId());
|
---|
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(0, 1);
|
---|
157 | done.put(ref, way.getUniqueId());
|
---|
158 | data.addPrimitive(way);
|
---|
159 | while (git.hasEdge()) {
|
---|
160 | git.nextEdge();
|
---|
161 | while (git.hasNode()) {
|
---|
162 | ref = git.nextRef();
|
---|
163 | way.addNode((Node) data.getPrimitiveById(done.get(ref), OsmPrimitiveType.NODE));
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | Relation rel = new Relation(0, 1);
|
---|
168 | data.addPrimitive(rel);
|
---|
169 | git = map.new GeomIterator(feature.geom);
|
---|
170 | int outers = feature.geom.outers;
|
---|
171 | while (git.hasComp()) {
|
---|
172 | long ref = git.nextComp();
|
---|
173 | if (outers-- > 0) {
|
---|
174 | rel.addMember(new RelationMember("outer", (Way) data.getPrimitiveById(done.get(ref), OsmPrimitiveType.WAY)));
|
---|
175 | } else {
|
---|
176 | rel.addMember(new RelationMember("inner", (Way) data.getPrimitiveById(done.get(ref), OsmPrimitiveType.WAY)));
|
---|
177 | }
|
---|
178 | }
|
---|
179 | addKeys(rel, feature, type);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | OsmDataLayer layer = new OsmDataLayer(data, "S-57 Import", null);
|
---|
186 | Main.map.mapView.addLayer(layer);
|
---|
187 | Main.map.mapView.zoomTo(new Bounds(bounds.minlat, bounds.minlon, bounds.maxlat, bounds.maxlon));
|
---|
188 | PanelMain.setStatus("Import done", Color.green);
|
---|
189 | }
|
---|
190 |
|
---|
191 | void addKeys(OsmPrimitive prim, Feature feature, String type) {
|
---|
192 | HashMap<String,String> keys = new HashMap<String,String>();
|
---|
193 | if (prim instanceof Relation) {
|
---|
194 | keys.put("type", "multipolygon");
|
---|
195 | }
|
---|
196 | keys.put("seamark:type", type);
|
---|
197 | if (feature.type == Obj.SOUNDG) {
|
---|
198 | Snode snode = map.nodes.get(feature.geom.elems.get(0).id);
|
---|
199 | if (snode.flg == S57map.Nflag.DPTH) {
|
---|
200 | keys.put("seamark:sounding:depth", ((Double)((Dnode)snode).val).toString());
|
---|
201 | }
|
---|
202 | }
|
---|
203 | for (Map.Entry<Att, AttVal<?>> item : feature.atts.entrySet()) {
|
---|
204 | String attstr = S57att.stringAttribute(item.getKey());
|
---|
205 | String valstr = S57val.stringValue(item.getValue());
|
---|
206 | if (!attstr.isEmpty() && !valstr.isEmpty()) {
|
---|
207 | keys.put(("seamark:" + type + ":" + attstr), valstr);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | for (Obj obj : feature.objs.keySet()) {
|
---|
211 | ObjTab tab = feature.objs.get(obj);
|
---|
212 | for (int ix : tab.keySet()) {
|
---|
213 | type = S57obj.stringType(obj);
|
---|
214 | AttMap atts = tab.get(ix);
|
---|
215 | for (Map.Entry<Att, AttVal<?>> item : atts.entrySet()) {
|
---|
216 | String attstr = S57att.stringAttribute(item.getKey());
|
---|
217 | String valstr = S57val.stringValue(item.getValue());
|
---|
218 | if (!attstr.isEmpty() && !valstr.isEmpty()) {
|
---|
219 | if ((ix == 0) && (tab.size() == 1)) {
|
---|
220 | keys.put(("seamark:" + type + ":" + attstr), valstr);
|
---|
221 | } else {
|
---|
222 | keys.put(("seamark:" + type + ":" + (ix+1) + ":" + attstr), valstr);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 | prim.setKeys(keys);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | public void startExport(File outf) throws IOException {
|
---|
233 |
|
---|
234 | }
|
---|
235 | }
|
---|