From 85257a0ac8ac793de7990eae07b258d26cd4a3ce Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Mon, 6 May 2013 04:38:16 +0200 Subject: [PATCH] add TagSet class --- src/org/oscim/core/TagSet.java | 99 +++++++++++++++++++ src/org/oscim/core/osm/OSMElement.java | 6 +- src/org/oscim/core/osm/OSMMember.java | 6 +- src/org/oscim/core/osm/OSMNode.java | 6 +- src/org/oscim/core/osm/OSMRelation.java | 4 +- src/org/oscim/core/osm/OSMWay.java | 6 +- src/org/oscim/core/osm/TagGroup.java | 31 ------ .../utils/overpass/OverpassAPIReader.java | 28 +++--- 8 files changed, 133 insertions(+), 53 deletions(-) create mode 100644 src/org/oscim/core/TagSet.java delete mode 100644 src/org/oscim/core/osm/TagGroup.java diff --git a/src/org/oscim/core/TagSet.java b/src/org/oscim/core/TagSet.java new file mode 100644 index 00000000..a0545cc2 --- /dev/null +++ b/src/org/oscim/core/TagSet.java @@ -0,0 +1,99 @@ +/* + * Copyright 2013 Hannes Janetzek + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ +package org.oscim.core; + +public class TagSet { + public static TagSet EMPTY_TAG_SET = new TagSet(); + + public Tag[] tags; + public int numTags; + + private TagSet() { + + } + + public TagSet(int count) { + tags = new Tag[count]; + } + + public void clear() { + numTags = 0; + } + + /** find Tag by key - NOTE: key must be internal() */ + public Tag get(String key) { + for (int i = 0; i < numTags; i++) { + if (tags[i].key == key) + return tags[i]; + } + return null; + } + + public void add(Tag tag) { + if (numTags >= tags.length) { + Tag[] tmp = tags; + tags = new Tag[numTags + 4]; + System.arraycopy(tmp, 0, tags, 0, numTags); + } + tags[numTags++] = tag; + } + + public boolean contains(Tag tag) { + for (int i = 0; i < numTags; i++) { + Tag t = tags[i]; + if (t == tag || (t.key == tag.key && (t.value == t.value))) + return true; + } + return false; + } + + public boolean hasKey(String[] keys) { + for (int i = 0; i < numTags; i++) { + Tag t = tags[i]; + for (String key : keys) + if (key == t.key) + return true; + } + return false; + } + + public boolean hasKey(String key) { + for (int i = 0; i < numTags; i++) { + Tag t = tags[i]; + if (key == t.key) + return true; + } + return false; + } + + public boolean hasValue(String[] vals) { + for (int i = 0; i < numTags; i++) { + Tag t = tags[i]; + for (String value : vals) + if (value == t.value) + return true; + } + return false; + } + + public boolean hasValue(String value) { + for (int i = 0; i < numTags; i++) { + Tag t = tags[i]; + if (value == t.value) + return true; + } + return false; + } +} diff --git a/src/org/oscim/core/osm/OSMElement.java b/src/org/oscim/core/osm/OSMElement.java index 666281c3..5edc36e7 100644 --- a/src/org/oscim/core/osm/OSMElement.java +++ b/src/org/oscim/core/osm/OSMElement.java @@ -14,12 +14,14 @@ */ package org.oscim.core.osm; +import org.oscim.core.TagSet; + public abstract class OSMElement { - public final TagGroup tags; + public final TagSet tags; public final long id; - public OSMElement(TagGroup tags, long id) { + public OSMElement(TagSet tags, long id) { assert tags != null; this.tags = tags; this.id = id; diff --git a/src/org/oscim/core/osm/OSMMember.java b/src/org/oscim/core/osm/OSMMember.java index 7e1fc6d1..ddf0e26d 100644 --- a/src/org/oscim/core/osm/OSMMember.java +++ b/src/org/oscim/core/osm/OSMMember.java @@ -15,7 +15,11 @@ package org.oscim.core.osm; public class OSMMember { - + public enum MemberType{ + NODE, + WAY, + RELATIOM + } static final boolean useDebugLabels = true; public final String role; diff --git a/src/org/oscim/core/osm/OSMNode.java b/src/org/oscim/core/osm/OSMNode.java index 7eae59c9..52af395c 100644 --- a/src/org/oscim/core/osm/OSMNode.java +++ b/src/org/oscim/core/osm/OSMNode.java @@ -14,11 +14,15 @@ */ package org.oscim.core.osm; +import org.oscim.core.TagSet; + public class OSMNode extends OSMElement { + //public static EMPTY_NODE = new OSMNode() + public final double lat; public final double lon; - public OSMNode(double lat, double lon, TagGroup tags, long id) { + public OSMNode(double lat, double lon, TagSet tags, long id) { super(tags, id); this.lat = lat; this.lon = lon; diff --git a/src/org/oscim/core/osm/OSMRelation.java b/src/org/oscim/core/osm/OSMRelation.java index fb75cb52..4a54763e 100644 --- a/src/org/oscim/core/osm/OSMRelation.java +++ b/src/org/oscim/core/osm/OSMRelation.java @@ -17,13 +17,15 @@ package org.oscim.core.osm; import java.util.ArrayList; import java.util.List; +import org.oscim.core.TagSet; + public class OSMRelation extends OSMElement { public final List relationMembers; // content added after constructor call - public OSMRelation(TagGroup tags, long id, int initialMemberSize) { + public OSMRelation(TagSet tags, long id, int initialMemberSize) { super(tags, id); this.relationMembers = new ArrayList(initialMemberSize); diff --git a/src/org/oscim/core/osm/OSMWay.java b/src/org/oscim/core/osm/OSMWay.java index e15be68d..ecb66ab5 100644 --- a/src/org/oscim/core/osm/OSMWay.java +++ b/src/org/oscim/core/osm/OSMWay.java @@ -16,14 +16,14 @@ package org.oscim.core.osm; import java.util.List; +import org.oscim.core.TagSet; + public class OSMWay extends OSMElement { public final List nodes; - public OSMWay(TagGroup tags, long id, List nodes) { + public OSMWay(TagSet tags, long id, List nodes) { super(tags, id); - for (OSMNode node : nodes) - assert node != null; this.nodes = nodes; } diff --git a/src/org/oscim/core/osm/TagGroup.java b/src/org/oscim/core/osm/TagGroup.java deleted file mode 100644 index 24090f0d..00000000 --- a/src/org/oscim/core/osm/TagGroup.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2013 Hannes Janetzek - * - * This program is free software: you can redistribute it and/or modify it under the - * terms of the GNU Lesser General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A - * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License along with - * this program. If not, see . - */ -package org.oscim.core.osm; - -import java.util.Map; - -public class TagGroup { - public final static TagGroup EMPTY_TAG_GROUP = new TagGroup(); - - public final Map tags; - - public TagGroup(Map tags) { - this.tags = tags; - } - - private TagGroup(){ - this.tags = null; - } -} diff --git a/src/org/oscim/utils/overpass/OverpassAPIReader.java b/src/org/oscim/utils/overpass/OverpassAPIReader.java index 488cfd7a..1d4355ae 100644 --- a/src/org/oscim/utils/overpass/OverpassAPIReader.java +++ b/src/org/oscim/utils/overpass/OverpassAPIReader.java @@ -15,8 +15,6 @@ package org.oscim.utils.overpass; -import static org.oscim.core.osm.TagGroup.EMPTY_TAG_GROUP; - import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -33,6 +31,8 @@ import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; +import org.oscim.core.Tag; +import org.oscim.core.TagSet; import org.oscim.core.osm.Bound; import org.oscim.core.osm.OSMData; import org.oscim.core.osm.OSMElement; @@ -40,7 +40,6 @@ import org.oscim.core.osm.OSMMember; import org.oscim.core.osm.OSMNode; import org.oscim.core.osm.OSMRelation; import org.oscim.core.osm.OSMWay; -import org.oscim.core.osm.TagGroup; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParseException; @@ -213,7 +212,7 @@ public class OverpassAPIReader { long id = 0; double lat = 0, lon = 0; - TagGroup tags = EMPTY_TAG_GROUP; + TagSet tags = TagSet.EMPTY_TAG_SET; while (jp.nextToken() != JsonToken.END_OBJECT) { @@ -243,7 +242,7 @@ public class OverpassAPIReader { private void parseWay(JsonParser jp) throws JsonParseException, IOException { long id = 0; - TagGroup tags = EMPTY_TAG_GROUP; + TagSet tags = TagSet.EMPTY_TAG_SET; ArrayList wayNodes = new ArrayList(); while (jp.nextToken() != JsonToken.END_OBJECT) { @@ -278,7 +277,7 @@ public class OverpassAPIReader { IOException { long id = 0; - TagGroup tags = EMPTY_TAG_GROUP; + TagSet tags = TagSet.EMPTY_TAG_SET; ArrayList members = new ArrayList(); while (jp.nextToken() != JsonToken.END_OBJECT) { @@ -318,25 +317,26 @@ public class OverpassAPIReader { relationMembersForRelation.put(relation, members); } - private static TagGroup parseTags(JsonParser jp) throws JsonParseException, + private static TagSet parseTags(JsonParser jp) throws JsonParseException, IOException { - Map tagMap = null; + TagSet tags = null; + while (jp.nextToken() != JsonToken.END_OBJECT) { String key = jp.getCurrentName(); jp.nextToken(); String val = jp.getText(); - if (tagMap == null) - tagMap = new HashMap(10); + if (tags== null) + tags= new TagSet(4); - tagMap.put(key, val); + tags.add(new Tag(key, val, false)); } - if (tagMap == null) - return EMPTY_TAG_GROUP; + if (tags== null) + return TagSet.EMPTY_TAG_SET; - return new TagGroup(tagMap); + return tags; } private static void log(String msg) {