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

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

save

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