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

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

save

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