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