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

Last change on this file since 29198 was 29198, checked in by malcolmh, 13 years ago

save

File size: 4.6 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, 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 FtrMap extends EnumMap<Obj, ArrayList<Feature>> {
70 public FtrMap() {
71 super(Obj.class);
72 }
73 }
74
75 public class FtrTab extends HashMap<Long, Feature> {
76 public FtrTab() {
77 super();
78 }
79 }
80
81 public class Feature {
82 public Fflag flag;
83 public long refs;
84 public Obj type;
85 public AttMap atts;
86 public ObjMap objs;
87
88 Feature() {
89 flag = Fflag.UNKN;
90 refs = 0;
91 type = Obj.UNKOBJ;
92 atts = new AttMap();
93 objs = new ObjMap();
94 }
95 }
96
97 public class Coord {
98 public double lat;
99 public double lon;
100
101 public Coord(double ilat, double ilon) {
102 lat = ilat;
103 lon = ilon;
104 }
105 }
106
107 public NodeTab nodes;
108 public WayTab ways;
109 public WayTab mpolys;
110 public FtrMap features;
111 public FtrTab index;
112
113 private Feature feature;
114 private ArrayList<Long> list;
115
116 public SeaMap() {
117 nodes = new NodeTab();
118 ways = new WayTab();
119 mpolys = new WayTab();
120 feature = new Feature();
121 features = new FtrMap();
122 index = new FtrTab();
123 }
124
125 public void addNode(long id, double lat, double lon) {
126 nodes.put(id, new Coord(Math.toRadians(lat), Math.toRadians(lon)));
127 feature = new Feature();
128 feature.refs = id;
129 feature.flag = Fflag.NODE;
130 }
131
132 public void moveNode(long id, double lat, double lon) {
133 nodes.put(id, new Coord(Math.toRadians(lat), Math.toRadians(lon)));
134 }
135
136 public void addWay(long id) {
137 list = new ArrayList<Long>();
138 ways.put(id, list);
139 feature = new Feature();
140 feature.refs = id;
141 feature.flag = Fflag.WAY;
142 }
143
144 public void addMpoly(long id) {
145 list = new ArrayList<Long>();
146 mpolys.put(id, list);
147 }
148
149 public void addToWay(long node) {
150 list.add(node);
151 }
152
153 public void addToMpoly(long way, boolean outer) {
154 if (outer)
155 list.add(0, way);
156 else
157 list.add(way);
158 }
159
160 public void tagsDone(long id) {
161 if ((feature.type != Obj.UNKOBJ) && !((feature.flag == Fflag.WAY) && (list.size() < 2))) {
162 index.put(id, feature);
163 if ((feature.flag == Fflag.WAY) && (list.size() > 0) && (list.get(0).equals(list.get(list.size() - 1)))) {
164 feature.flag = Fflag.AREA;
165 }
166 if (features.get(feature.type) == null) {
167 features.put(feature.type, new ArrayList<Feature>());
168 }
169 features.get(feature.type).add(feature);
170 }
171 }
172
173 public void addTag(String key, String val) {
174 String subkeys[] = key.split(":");
175 if ((subkeys.length > 1) && subkeys[0].equals("seamark")) {
176 Obj obj = S57obj.enumType(subkeys[1]);
177 if ((subkeys.length > 2) && (obj != Obj.UNKOBJ)) {
178 int idx = 0;
179 Att att = Att.UNKATT;
180 try {
181 idx = Integer.parseInt(subkeys[2]);
182 if (subkeys.length == 4) {
183 att = s57.S57att.enumAttribute(subkeys[3], obj);
184 }
185 } catch (Exception e) {
186 att = S57att.enumAttribute(subkeys[2], obj);
187 }
188 ObjTab items = feature.objs.get(obj);
189 if (items == null) {
190 items = new ObjTab();
191 feature.objs.put(obj, items);
192 }
193 AttMap atts = items.get(idx);
194 if (atts == null) {
195 atts = new AttMap();
196 items.put(idx, atts);
197 }
198 AttVal attval = S57val.convertValue(val, att);
199 if (attval.val != null) atts.put(att, new AttItem(attval.conv, attval.val));
200 } else {
201 if (subkeys[1].equals("type")) {
202 feature.type = S57obj.enumType(val);
203 } else {
204 Att att = S57att.enumAttribute(subkeys[1], Obj.UNKOBJ);
205 if (att != Att.UNKATT) {
206 AttVal attval = S57val.convertValue(val, att);
207 if (attval.val != null) feature.atts.put(att, new AttItem(attval.conv, attval.val));
208 }
209 }
210 }
211 }
212 }
213}
Note: See TracBrowser for help on using the repository browser.