| 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 |
|
|---|
| 10 | package seamap;
|
|---|
| 11 |
|
|---|
| 12 | import java.util.ArrayList;
|
|---|
| 13 | import java.util.EnumMap;
|
|---|
| 14 | import java.util.HashMap;
|
|---|
| 15 |
|
|---|
| 16 | import s57.S57att;
|
|---|
| 17 | import s57.S57att.Att;
|
|---|
| 18 | import s57.S57obj;
|
|---|
| 19 | import s57.S57obj.*;
|
|---|
| 20 | import s57.S57val;
|
|---|
| 21 | import s57.S57val.*;
|
|---|
| 22 |
|
|---|
| 23 | public 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 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 | private long mpid;
|
|---|
| 123 |
|
|---|
| 124 | public SeaMap() {
|
|---|
| 125 | nodes = new NodeTab();
|
|---|
| 126 | ways = new WayTab();
|
|---|
| 127 | mpolys = new WayTab();
|
|---|
| 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.WAY;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | public void addMpoly(long id) {
|
|---|
| 153 | list = new ArrayList<Long>();
|
|---|
| 154 | mpolys.put(id, list);
|
|---|
| 155 | mpid = id;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | public void addToWay(long node) {
|
|---|
| 159 | list.add(node);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | public void addToMpoly(long way, boolean outer) {
|
|---|
| 163 | if (outer) {
|
|---|
| 164 | list.add(0, way);
|
|---|
| 165 | outers.put(way, mpid);
|
|---|
| 166 | } else {
|
|---|
| 167 | list.add(way);
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | public void tagsDone(long id) {
|
|---|
| 172 | if ((feature.type != Obj.UNKOBJ) && !((feature.flag == Fflag.WAY) && (list.size() < 2))) {
|
|---|
| 173 | index.put(id, feature);
|
|---|
| 174 | if ((feature.flag == Fflag.WAY) && (list.size() > 0) && (list.get(0).equals(list.get(list.size() - 1)))) {
|
|---|
| 175 | feature.flag = Fflag.AREA;
|
|---|
| 176 | }
|
|---|
| 177 | if (features.get(feature.type) == null) {
|
|---|
| 178 | features.put(feature.type, new ArrayList<Feature>());
|
|---|
| 179 | }
|
|---|
| 180 | features.get(feature.type).add(feature);
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | public void addTag(String key, String val) {
|
|---|
| 185 | String subkeys[] = key.split(":");
|
|---|
| 186 | if ((subkeys.length > 1) && subkeys[0].equals("seamark")) {
|
|---|
| 187 | Obj obj = S57obj.enumType(subkeys[1]);
|
|---|
| 188 | if ((subkeys.length > 2) && (obj != Obj.UNKOBJ)) {
|
|---|
| 189 | int idx = 0;
|
|---|
| 190 | Att att = Att.UNKATT;
|
|---|
| 191 | try {
|
|---|
| 192 | idx = Integer.parseInt(subkeys[2]);
|
|---|
| 193 | if (subkeys.length == 4) {
|
|---|
| 194 | att = s57.S57att.enumAttribute(subkeys[3], obj);
|
|---|
| 195 | }
|
|---|
| 196 | } catch (Exception e) {
|
|---|
| 197 | att = S57att.enumAttribute(subkeys[2], obj);
|
|---|
| 198 | }
|
|---|
| 199 | ObjTab items = feature.objs.get(obj);
|
|---|
| 200 | if (items == null) {
|
|---|
| 201 | items = new ObjTab();
|
|---|
| 202 | feature.objs.put(obj, items);
|
|---|
| 203 | }
|
|---|
| 204 | AttMap atts = items.get(idx);
|
|---|
| 205 | if (atts == null) {
|
|---|
| 206 | atts = new AttMap();
|
|---|
| 207 | items.put(idx, atts);
|
|---|
| 208 | }
|
|---|
| 209 | AttVal attval = S57val.convertValue(val, att);
|
|---|
| 210 | if (attval.val != null) atts.put(att, new AttItem(attval.conv, attval.val));
|
|---|
| 211 | } else {
|
|---|
| 212 | if (subkeys[1].equals("type")) {
|
|---|
| 213 | feature.type = S57obj.enumType(val);
|
|---|
| 214 | } else {
|
|---|
| 215 | Att att = S57att.enumAttribute(subkeys[1], Obj.UNKOBJ);
|
|---|
| 216 | if (att != Att.UNKATT) {
|
|---|
| 217 | AttVal attval = S57val.convertValue(val, att);
|
|---|
| 218 | if (attval.val != null) feature.atts.put(att, new AttItem(attval.conv, attval.val));
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|