1 | /* Copyright 2012 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 seamap;
|
---|
11 |
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.EnumMap;
|
---|
14 | import java.util.HashMap;
|
---|
15 |
|
---|
16 | import s57.S57att;
|
---|
17 | import s57.S57att.Att;
|
---|
18 | import s57.S57obj;
|
---|
19 | import s57.S57obj.*;
|
---|
20 | import s57.S57val;
|
---|
21 | import s57.S57val.*;
|
---|
22 |
|
---|
23 | public class SeaMap {
|
---|
24 |
|
---|
25 | public enum Fflag {
|
---|
26 | UNKN, NODE, WAY, AREA
|
---|
27 | }
|
---|
28 |
|
---|
29 | public class AttItem {
|
---|
30 | Conv conv;
|
---|
31 | Object val;
|
---|
32 |
|
---|
33 | AttItem(Conv iconv, Object ival) {
|
---|
34 | conv = iconv;
|
---|
35 | val = ival;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public class AttMap extends EnumMap<Att, AttItem> {
|
---|
40 | public AttMap() {
|
---|
41 | super(Att.class);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public class ObjTab extends HashMap<Integer, AttMap> {
|
---|
46 | public ObjTab() {
|
---|
47 | super();
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public class ObjMap extends EnumMap<Obj, ObjTab> {
|
---|
52 | public ObjMap() {
|
---|
53 | super(Obj.class);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public class NodeTab extends HashMap<Long, Coord> {
|
---|
58 | public NodeTab() {
|
---|
59 | super();
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public class WayTab extends HashMap<Long, ArrayList<Long>> {
|
---|
64 | public WayTab() {
|
---|
65 | super();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public class MpolyTab extends HashMap<Long, Long> {
|
---|
70 | public MpolyTab() {
|
---|
71 | super();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public class FtrMap extends EnumMap<Obj, ArrayList<Feature>> {
|
---|
76 | public FtrMap() {
|
---|
77 | super(Obj.class);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public class FtrTab extends HashMap<Long, Feature> {
|
---|
82 | public FtrTab() {
|
---|
83 | super();
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public class Feature {
|
---|
88 | public Fflag flag;
|
---|
89 | public long refs;
|
---|
90 | public Obj type;
|
---|
91 | public AttMap atts;
|
---|
92 | public ObjMap objs;
|
---|
93 |
|
---|
94 | Feature() {
|
---|
95 | flag = Fflag.UNKN;
|
---|
96 | refs = 0;
|
---|
97 | type = Obj.UNKOBJ;
|
---|
98 | atts = new AttMap();
|
---|
99 | objs = new ObjMap();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public class Coord {
|
---|
104 | public double lat;
|
---|
105 | public double lon;
|
---|
106 |
|
---|
107 | public Coord(double ilat, double ilon) {
|
---|
108 | lat = ilat;
|
---|
109 | lon = ilon;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public NodeTab nodes;
|
---|
114 | public WayTab ways;
|
---|
115 | public WayTab mpolys;
|
---|
116 | public MpolyTab outers;
|
---|
117 | public FtrMap features;
|
---|
118 | public FtrTab index;
|
---|
119 |
|
---|
120 | private Feature feature;
|
---|
121 | private ArrayList<Long> list;
|
---|
122 | private long mpid;
|
---|
123 |
|
---|
124 | public SeaMap() {
|
---|
125 | nodes = new NodeTab();
|
---|
126 | ways = new WayTab();
|
---|
127 | mpolys = new WayTab();
|
---|
128 | outers = new MpolyTab();
|
---|
129 | feature = new Feature();
|
---|
130 | features = new FtrMap();
|
---|
131 | index = new FtrTab();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void addNode(long id, double lat, double lon) {
|
---|
135 | nodes.put(id, new Coord(Math.toRadians(lat), Math.toRadians(lon)));
|
---|
136 | feature = new Feature();
|
---|
137 | feature.refs = id;
|
---|
138 | feature.flag = Fflag.NODE;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void moveNode(long id, double lat, double lon) {
|
---|
142 | nodes.put(id, new Coord(Math.toRadians(lat), Math.toRadians(lon)));
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void addWay(long id) {
|
---|
146 | list = new ArrayList<Long>();
|
---|
147 | ways.put(id, list);
|
---|
148 | feature = new Feature();
|
---|
149 | feature.refs = id;
|
---|
150 | feature.flag = Fflag.WAY;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void addMpoly(long id) {
|
---|
154 | list = new ArrayList<Long>();
|
---|
155 | mpolys.put(id, list);
|
---|
156 | mpid = id;
|
---|
157 | }
|
---|
158 |
|
---|
159 | public void addToWay(long node) {
|
---|
160 | list.add(node);
|
---|
161 | }
|
---|
162 |
|
---|
163 | public void addToMpoly(long way, boolean outer) {
|
---|
164 | if (outer) {
|
---|
165 | list.add(0, way);
|
---|
166 | outers.put(way, mpid);
|
---|
167 | } else {
|
---|
168 | list.add(way);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void tagsDone(long id) {
|
---|
173 | if ((feature.type != Obj.UNKOBJ) && !((feature.flag == Fflag.WAY) && (list.size() < 2))) {
|
---|
174 | index.put(id, feature);
|
---|
175 | if ((feature.flag == Fflag.WAY) && (list.size() > 0) && (list.get(0).equals(list.get(list.size() - 1)))) {
|
---|
176 | feature.flag = Fflag.AREA;
|
---|
177 | }
|
---|
178 | if (features.get(feature.type) == null) {
|
---|
179 | features.put(feature.type, new ArrayList<Feature>());
|
---|
180 | }
|
---|
181 | features.get(feature.type).add(feature);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | public void addTag(String key, String val) {
|
---|
186 | String subkeys[] = key.split(":");
|
---|
187 | if ((subkeys.length > 1) && subkeys[0].equals("seamark")) {
|
---|
188 | Obj obj = S57obj.enumType(subkeys[1]);
|
---|
189 | if ((subkeys.length > 2) && (obj != Obj.UNKOBJ)) {
|
---|
190 | int idx = 0;
|
---|
191 | Att att = Att.UNKATT;
|
---|
192 | try {
|
---|
193 | idx = Integer.parseInt(subkeys[2]);
|
---|
194 | if (subkeys.length == 4) {
|
---|
195 | att = s57.S57att.enumAttribute(subkeys[3], obj);
|
---|
196 | }
|
---|
197 | } catch (Exception e) {
|
---|
198 | att = S57att.enumAttribute(subkeys[2], obj);
|
---|
199 | }
|
---|
200 | ObjTab items = feature.objs.get(obj);
|
---|
201 | if (items == null) {
|
---|
202 | items = new ObjTab();
|
---|
203 | feature.objs.put(obj, items);
|
---|
204 | }
|
---|
205 | AttMap atts = items.get(idx);
|
---|
206 | if (atts == null) {
|
---|
207 | atts = new AttMap();
|
---|
208 | items.put(idx, atts);
|
---|
209 | }
|
---|
210 | AttVal attval = S57val.convertValue(val, att);
|
---|
211 | if (attval.val != null) atts.put(att, new AttItem(attval.conv, attval.val));
|
---|
212 | } else {
|
---|
213 | if (subkeys[1].equals("type")) {
|
---|
214 | feature.type = S57obj.enumType(val);
|
---|
215 | } else {
|
---|
216 | Att att = S57att.enumAttribute(subkeys[1], Obj.UNKOBJ);
|
---|
217 | if (att != Att.UNKATT) {
|
---|
218 | AttVal attval = S57val.convertValue(val, att);
|
---|
219 | if (attval.val != null) feature.atts.put(att, new AttItem(attval.conv, attval.val));
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|