add TagSet class

This commit is contained in:
Hannes Janetzek 2013-05-06 04:38:16 +02:00
parent 2ff67d078d
commit 85257a0ac8
8 changed files with 133 additions and 53 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<OSMMember> 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<OSMMember>(initialMemberSize);

View File

@ -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<OSMNode> nodes;
public OSMWay(TagGroup tags, long id, List<OSMNode> nodes) {
public OSMWay(TagSet tags, long id, List<OSMNode> nodes) {
super(tags, id);
for (OSMNode node : nodes)
assert node != null;
this.nodes = nodes;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package org.oscim.core.osm;
import java.util.Map;
public class TagGroup {
public final static TagGroup EMPTY_TAG_GROUP = new TagGroup();
public final Map<String, String> tags;
public TagGroup(Map<String,String> tags) {
this.tags = tags;
}
private TagGroup(){
this.tags = null;
}
}

View File

@ -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<OSMNode> wayNodes = new ArrayList<OSMNode>();
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<TmpRelation> members = new ArrayList<TmpRelation>();
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<String, String> 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<String, String>(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) {