source: osm/applications/editors/josm/plugins/smed2/src/seamap/Map.java@ 29152

Last change on this file since 29152 was 29152, checked in by malcolmh, 14 years ago

save

File size: 3.7 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.*;
18import s57.S57obj;
19import s57.S57obj.*;
20import s57.S57val;
21import s57.S57val.*;
22
23public class Map {
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 Feature {
40 public long id;
41 public Fflag flag;
42 public ArrayList<Long> refs;
43 public Obj type;
44 public EnumMap<Att, AttItem> atts;
45 public EnumMap<Obj, HashMap<Integer, EnumMap<Att, AttItem>>> objs;
46
47 Feature() {
48 clean();
49 }
50
51 void clean() {
52 id = 0;
53 flag = Fflag.UNKN;
54 refs = new ArrayList<Long>();
55 type = Obj.UNKOBJ;
56 atts = new EnumMap<Att, AttItem>(Att.class);
57 objs = new EnumMap<Obj, HashMap<Integer, EnumMap<Att, AttItem>>>(Obj.class);
58 }
59 }
60
61 public class Coord {
62 double lat;
63 double lon;
64
65 Coord(double ilat, double ilon) {
66 lat = ilat;
67 lon = ilon;
68 }
69 }
70
71 public HashMap<Long, Coord> nodes;
72 public HashMap<Long, ArrayList<Long>> ways;
73 public HashMap<Long, ArrayList<Long>> mpolys;
74 public ArrayList<Feature> features;
75
76 private Feature feature;
77
78 public Map() {
79 nodes = new HashMap<Long, Coord>();
80 ways = new HashMap<Long, ArrayList<Long>>();
81 mpolys = new HashMap<Long, ArrayList<Long>>();
82 feature = new Feature();
83 features = new ArrayList<Feature>();
84 features.add(feature);
85 }
86
87 public void addNode(long id, double lat, double lon) {
88 nodes.put(id, new Coord(lat, lon));
89 if (feature.type == Obj.UNKOBJ) {
90 feature.clean();
91 } else {
92 feature = new Feature();
93 features.add(feature);
94 }
95 }
96
97 public void addWay(long id) {
98 ways.put(id, new ArrayList<Long>());
99 if (feature.type == Obj.UNKOBJ) {
100 feature.clean();
101 } else {
102 feature = new Feature();
103 features.add(feature);
104 }
105 }
106
107 public void addToWay(long way, long node) {
108 ways.get(way).add(node);
109 }
110
111 public void addMpoly(long id) {
112 mpolys.put(id, new ArrayList<Long>());
113 }
114
115 public void addToMpoly(long id, long way) {
116 mpolys.get(id).add(way);
117 }
118
119 public void addTag(String key, String val) {
120 String subkeys[] = key.split(":");
121 if ((subkeys.length > 1) && subkeys[0].equals("seamark")) {
122 Obj obj = S57obj.enumType(subkeys[1]);
123 if ((subkeys.length > 2) && (obj != Obj.UNKOBJ)) {
124 int idx = 0;
125 Att att = Att.UNKATT;
126 try {
127 idx = Integer.parseInt(subkeys[2]);
128 if (subkeys.length == 4) {
129 att = s57.S57att.enumAttribute(subkeys[3], obj);
130 }
131 } catch (Exception e) {
132 att = S57att.enumAttribute(subkeys[2], obj);
133 }
134 HashMap<Integer, EnumMap<Att, AttItem>> items = feature.objs.get(obj);
135 if (items == null) {
136 items = new HashMap<Integer, EnumMap<Att, AttItem>>();
137 feature.objs.put(obj, items);
138 }
139 EnumMap<Att, AttItem> atts = items.get(idx);
140 if (atts == null) {
141 atts = new EnumMap<Att, AttItem>(Att.class);
142 }
143 AttVal attval = S57val.convertValue(val, att);
144 atts.put(att, new AttItem(attval.conv, attval.val));
145 } else {
146 if (subkeys[1].equals("type")) {
147 feature.type = S57obj.enumType(val);
148 } else {
149 Att att = S57att.enumAttribute(subkeys[1], Obj.UNKOBJ);
150 if (att != Att.UNKATT) {
151 AttVal attval = S57val.convertValue(val, att);
152 feature.atts.put(att, new AttItem(attval.conv, attval.val));
153 }
154 }
155 }
156 }
157 }
158}
Note: See TracBrowser for help on using the repository browser.