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