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

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

save

File size: 4.9 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 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
123 public SeaMap() {
124 nodes = new NodeTab();
125 ways = new WayTab();
126 mpolys = new WayTab();
127 outers = new MpolyTab();
128 feature = new Feature();
129 features = new FtrMap();
130 index = new FtrTab();
131 }
132
133 public void addNode(long id, double lat, double lon) {
134 nodes.put(id, new Coord(Math.toRadians(lat), Math.toRadians(lon)));
135 feature = new Feature();
136 feature.refs = id;
137 feature.flag = Fflag.NODE;
138 }
139
140 public void moveNode(long id, double lat, double lon) {
141 nodes.put(id, new Coord(Math.toRadians(lat), Math.toRadians(lon)));
142 }
143
144 public void addWay(long id) {
145 list = new ArrayList<Long>();
146 ways.put(id, list);
147 feature = new Feature();
148 feature.refs = id;
149 feature.flag = Fflag.LINE;
150 }
151
152 public void addMpoly(long id) {
153 list = new ArrayList<Long>();
154 mpolys.put(id, list);
155 feature = new Feature();
156 feature.refs = id;
157 feature.flag = Fflag.AREA;
158 }
159
160 public void addToWay(long node) {
161 list.add(node);
162 }
163
164 public void addToMpoly(long way, boolean outer) {
165 if (outer) {
166 list.add(0, way);
167 outers.put(way, feature.refs);
168 } else {
169 list.add(way);
170 }
171 }
172
173 public void tagsDone(long id) {
174 if ((feature.type != Obj.UNKOBJ) && !((feature.flag == Fflag.LINE) && (list.size() < 2))) {
175 index.put(id, feature);
176 if ((feature.flag == Fflag.LINE) && (list.size() > 0) && (list.get(0).equals(list.get(list.size() - 1)))) {
177 feature.flag = Fflag.AREA;
178 }
179 if (features.get(feature.type) == null) {
180 features.put(feature.type, new ArrayList<Feature>());
181 }
182 features.get(feature.type).add(feature);
183 }
184 }
185
186 public void addTag(String key, String val) {
187 String subkeys[] = key.split(":");
188 if ((subkeys.length > 1) && subkeys[0].equals("seamark")) {
189 Obj obj = S57obj.enumType(subkeys[1]);
190 if ((subkeys.length > 2) && (obj != Obj.UNKOBJ)) {
191 int idx = 0;
192 Att att = Att.UNKATT;
193 try {
194 idx = Integer.parseInt(subkeys[2]);
195 if (subkeys.length == 4) {
196 att = s57.S57att.enumAttribute(subkeys[3], obj);
197 }
198 } catch (Exception e) {
199 att = S57att.enumAttribute(subkeys[2], obj);
200 }
201 ObjTab items = feature.objs.get(obj);
202 if (items == null) {
203 items = new ObjTab();
204 feature.objs.put(obj, items);
205 }
206 AttMap atts = items.get(idx);
207 if (atts == null) {
208 atts = new AttMap();
209 items.put(idx, atts);
210 }
211 AttVal attval = S57val.convertValue(val, att);
212 if (attval.val != null) atts.put(att, new AttItem(attval.conv, attval.val));
213 } else {
214 if (subkeys[1].equals("type")) {
215 feature.type = S57obj.enumType(val);
216 } else {
217 Att att = S57att.enumAttribute(subkeys[1], Obj.UNKOBJ);
218 if (att != Att.UNKATT) {
219 AttVal attval = S57val.convertValue(val, att);
220 if (attval.val != null) feature.atts.put(att, new AttItem(attval.conv, attval.val));
221 }
222 }
223 }
224 }
225 }
226}
Note: See TracBrowser for help on using the repository browser.