source: osm/applications/editors/josm/plugins/smed2/src/seamap/SeaMap.java@ 29215

Last change on this file since 29215 was 29215, checked in by malcolmh, 12 years ago

save

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