oscim.core.Tag: cleanups

- consistent String constants
This commit is contained in:
Hannes Janetzek 2013-09-25 05:50:54 +02:00
parent 2db1cf8b6c
commit c05710fcdb
9 changed files with 71 additions and 82 deletions

View File

@ -68,8 +68,8 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
// replacement for variable value tags that should not be matched by RenderTheme // replacement for variable value tags that should not be matched by RenderTheme
// FIXME make this general, maybe subclass tags // FIXME make this general, maybe subclass tags
private static final Tag mTagEmptyName = new Tag(Tag.TAG_KEY_NAME, null, false); private static final Tag mTagEmptyName = new Tag(Tag.KEY_NAME, null, false);
private static final Tag mTagEmptyHouseNr = new Tag(Tag.TAG_KEY_HOUSE_NUMBER, null, false); private static final Tag mTagEmptyHouseNr = new Tag(Tag.KEY_HOUSE_NUMBER, null, false);
private IRenderTheme renderTheme; private IRenderTheme renderTheme;
private int renderLevels; private int renderLevels;
@ -208,12 +208,12 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
for (int i = 0; i < in.numTags; i++) { for (int i = 0; i < in.numTags; i++) {
String key = tags[i].key; String key = tags[i].key;
if (key == Tag.TAG_KEY_NAME) { if (key == Tag.KEY_NAME) {
if (tags[i].value != null) { if (tags[i].value != null) {
mTagName = tags[i]; mTagName = tags[i];
tags[i] = mTagEmptyName; tags[i] = mTagEmptyName;
} }
} else if (key == Tag.TAG_KEY_HOUSE_NUMBER) { } else if (key == Tag.KEY_HOUSE_NUMBER) {
if (tags[i].value != null) { if (tags[i].value != null) {
mTagHouseNr = tags[i]; mTagHouseNr = tags[i];
tags[i] = mTagEmptyHouseNr; tags[i] = mTagEmptyHouseNr;
@ -221,7 +221,7 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
} else if (mTile.zoomLevel > 16) { } else if (mTile.zoomLevel > 16) {
// FIXME, allow overlays to intercept // FIXME, allow overlays to intercept
// this, or use a theme option for this // this, or use a theme option for this
if (key == Tag.TAG_KEY_BUILDING) if (key == Tag.KEY_BUILDING)
mRenderBuildingModel = true; mRenderBuildingModel = true;
else if (key == Tag.KEY_HEIGHT) { else if (key == Tag.KEY_HEIGHT) {
try { try {
@ -381,10 +381,10 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
private String textValueForKey(Text text) { private String textValueForKey(Text text) {
String value = null; String value = null;
if (text.textKey == Tag.TAG_KEY_NAME) { if (text.textKey == Tag.KEY_NAME) {
if (mTagName != null) if (mTagName != null)
value = mTagName.value; value = mTagName.value;
} else if (text.textKey == Tag.TAG_KEY_HOUSE_NUMBER) { } else if (text.textKey == Tag.KEY_HOUSE_NUMBER) {
if (mTagHouseNr != null) if (mTagHouseNr != null)
value = mTagHouseNr.value; value = mTagHouseNr.value;
} }

View File

@ -1,5 +1,6 @@
/* /*
* Copyright 2010, 2011, 2012 mapsforge.org * Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2013 Hannes Janetzek
* *
* This program is free software: you can redistribute it and/or modify it under the * 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 * terms of the GNU Lesser General Public License as published by the Free Software
@ -15,47 +16,32 @@
package org.oscim.core; package org.oscim.core;
/** /**
* A tag represents an immutable key-value pair. * A tag represents an immutable key-value pair. Keys are always intern().
*/ */
// TODO: use own stringshare method instead of internalized strings
public class Tag { public class Tag {
private static final char KEY_VALUE_SEPARATOR = '='; /** The key of the house number OpenStreetMap tag. */
/** public static final String KEY_HOUSE_NUMBER = "addr:housenumber";
* The key of the house number OpenStreetMap tag.
*/
public static final String TAG_KEY_HOUSE_NUMBER = "addr:housenumber";
/** /** The key of the name OpenStreetMap tag. */
* The key of the name OpenStreetMap tag. public static final String KEY_NAME = "name";
*/
public static final String TAG_KEY_NAME = "name";
/** /** The key of the reference OpenStreetMap tag. */
* The key of the reference OpenStreetMap tag. public static final String KEY_REF = "ref";
*/
public static final String TAG_KEY_REF = "ref";
/** /** The key of the elevation OpenStreetMap tag. */
* The key of the elevation OpenStreetMap tag. public static final String KEY_ELE = "ele";
*/
public static final String TAG_KEY_ELE = "ele";
public static final String TAG_KEY_AMENITY = "amenity";
/**
* The key of the elevation OpenStreetMap tag.
*/
public static final String TAG_KEY_BUILDING = "building";
public static final String TAG_KEY_HIGHWAY = "highway";
public static final String TAG_KEY_LANDUSE = "landuse";
public static final String VALUE_YES = "yes";
public static final String VALUE_NO = "no";
public static final String KEY_AMENITY = "amenity";
public static final String KEY_BUILDING = "building";
public static final String KEY_HIGHWAY = "highway";
public static final String KEY_LANDUSE = "landuse";
public static final String KEY_HEIGHT = "height"; public static final String KEY_HEIGHT = "height";
public static final String KEY_MIN_HEIGHT = "min_height"; public static final String KEY_MIN_HEIGHT = "min_height";
public static final String VALUE_YES = "yes";
public static final String VALUE_NO = "no";
/** /**
* The key of this tag. * The key of this tag.
*/ */
@ -66,23 +52,12 @@ public class Tag {
*/ */
public String value; public String value;
private int hashCodeValue = 0; /**
* true when value is intern().
*/
private final boolean intern; private final boolean intern;
/** private int hashCodeValue = 0;
* @param tag
* the textual representation of the tag.
*/
public Tag(String tag) {
int splitPosition = tag.indexOf(KEY_VALUE_SEPARATOR);
if (splitPosition < 0) {
System.out.println("TAG:" + tag);
}
this.key = tag.substring(0, splitPosition).intern();
this.value = tag.substring(splitPosition + 1).intern();
this.intern = true;
}
/** /**
* @param key * @param key
@ -165,4 +140,17 @@ public class Tag {
result = 31 * result + ((this.value == null) ? 0 : this.value.hashCode()); result = 31 * result + ((this.value == null) ? 0 : this.value.hashCode());
return result; return result;
} }
/**
* @param tag
* the textual representation of the tag.
*/
public static Tag parse(String tag) {
int splitPosition = tag.indexOf("=");
if (splitPosition < 0) {
return new Tag(tag, "");
}
return new Tag(tag.substring(0, splitPosition), tag.substring(splitPosition + 1));
}
} }

View File

@ -61,8 +61,8 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
// replacement for variable value tags that should not be matched by RenderTheme // replacement for variable value tags that should not be matched by RenderTheme
// FIXME make this general, maybe subclass tags // FIXME make this general, maybe subclass tags
private static final Tag mTagEmptyName = new Tag(Tag.TAG_KEY_NAME, null, false); private static final Tag mTagEmptyName = new Tag(Tag.KEY_NAME, null, false);
private static final Tag mTagEmptyHouseNr = new Tag(Tag.TAG_KEY_HOUSE_NUMBER, null, false); private static final Tag mTagEmptyHouseNr = new Tag(Tag.KEY_HOUSE_NUMBER, null, false);
// private final MapElement mDebugWay, mDebugPoint; // private final MapElement mDebugWay, mDebugPoint;
@ -187,12 +187,12 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
for (int i = 0; i < in.numTags; i++) { for (int i = 0; i < in.numTags; i++) {
String key = tags[i].key; String key = tags[i].key;
if (tags[i].key == Tag.TAG_KEY_NAME) { if (tags[i].key == Tag.KEY_NAME) {
if (tags[i].value != null) { if (tags[i].value != null) {
mTagName = tags[i]; mTagName = tags[i];
tags[i] = mTagEmptyName; tags[i] = mTagEmptyName;
} }
} else if (tags[i].key == Tag.TAG_KEY_HOUSE_NUMBER) { } else if (tags[i].key == Tag.KEY_HOUSE_NUMBER) {
if (tags[i].value != null) { if (tags[i].value != null) {
mTagHouseNr = tags[i]; mTagHouseNr = tags[i];
tags[i] = mTagEmptyHouseNr; tags[i] = mTagEmptyHouseNr;
@ -200,7 +200,7 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
} else if (mTile.zoomLevel > 16) { } else if (mTile.zoomLevel > 16) {
// FIXME, allow overlays to intercept // FIXME, allow overlays to intercept
// this, or use a theme option for this // this, or use a theme option for this
if (key == Tag.TAG_KEY_BUILDING) if (key == Tag.KEY_BUILDING)
mRenderBuildingModel = true; mRenderBuildingModel = true;
else if (key == Tag.KEY_HEIGHT) { else if (key == Tag.KEY_HEIGHT) {
@ -369,10 +369,10 @@ public class VectorTileLoader extends TileLoader implements IRenderTheme.Callbac
private String textValueForKey(Text text) { private String textValueForKey(Text text) {
String value = null; String value = null;
if (text.textKey == Tag.TAG_KEY_NAME) { if (text.textKey == Tag.KEY_NAME) {
if (mTagName != null) if (mTagName != null)
value = mTagName.value; value = mTagName.value;
} else if (text.textKey == Tag.TAG_KEY_HOUSE_NUMBER) { } else if (text.textKey == Tag.KEY_HOUSE_NUMBER) {
if (mTagHouseNr != null) if (mTagHouseNr != null)
value = mTagHouseNr.value; value = mTagHouseNr.value;
} }

View File

@ -503,7 +503,7 @@ public class MapDatabase implements ITileDataSource {
// check if the POI has a name // check if the POI has a name
if ((featureByte & POI_FEATURE_NAME) != 0) { if ((featureByte & POI_FEATURE_NAME) != 0) {
String str = mReadBuffer.readUTF8EncodedString(); String str = mReadBuffer.readUTF8EncodedString();
mElem.tags.add(new Tag(Tag.TAG_KEY_NAME, str, false)); mElem.tags.add(new Tag(Tag.KEY_NAME, str, false));
} }
// check if the POI has a house number // check if the POI has a house number
@ -801,30 +801,30 @@ public class MapDatabase implements ITileDataSource {
if (hasName) { if (hasName) {
int textPos = mReadBuffer.readUnsignedInt(); int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos); String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
mElem.tags.add(new Tag(Tag.TAG_KEY_NAME, str, false)); mElem.tags.add(new Tag(Tag.KEY_NAME, str, false));
} }
if (hasHouseNr) { if (hasHouseNr) {
int textPos = mReadBuffer.readUnsignedInt(); int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos); String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
mElem.tags.add(new Tag(Tag.TAG_KEY_HOUSE_NUMBER, str, false)); mElem.tags.add(new Tag(Tag.KEY_HOUSE_NUMBER, str, false));
} }
if (hasRef) { if (hasRef) {
int textPos = mReadBuffer.readUnsignedInt(); int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos); String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
mElem.tags.add(new Tag(Tag.TAG_KEY_REF, str, false)); mElem.tags.add(new Tag(Tag.KEY_REF, str, false));
} }
} else { } else {
if (hasName) { if (hasName) {
String str = mReadBuffer.readUTF8EncodedString(); String str = mReadBuffer.readUTF8EncodedString();
mElem.tags.add(new Tag(Tag.TAG_KEY_NAME, str, false)); mElem.tags.add(new Tag(Tag.KEY_NAME, str, false));
} }
if (hasHouseNr) { if (hasHouseNr) {
String str = mReadBuffer.readUTF8EncodedString(); String str = mReadBuffer.readUTF8EncodedString();
mElem.tags.add(new Tag(Tag.TAG_KEY_HOUSE_NUMBER, str, false)); mElem.tags.add(new Tag(Tag.KEY_HOUSE_NUMBER, str, false));
} }
if (hasRef) { if (hasRef) {
String str = mReadBuffer.readUTF8EncodedString(); String str = mReadBuffer.readUTF8EncodedString();
mElem.tags.add(new Tag(Tag.TAG_KEY_REF, str, false)); mElem.tags.add(new Tag(Tag.KEY_REF, str, false));
} }
} }
if ((featureByte & WAY_FEATURE_LABEL_POSITION) != 0) if ((featureByte & WAY_FEATURE_LABEL_POSITION) != 0)

View File

@ -171,7 +171,7 @@ final class RequiredFields {
if (tag == null) { if (tag == null) {
return new OpenResult("POI tag must not be null: " + currentTagId); return new OpenResult("POI tag must not be null: " + currentTagId);
} }
poiTags[currentTagId] = new Tag(tag); poiTags[currentTagId] = Tag.parse(tag);
} }
mapFileInfoBuilder.poiTags = poiTags; mapFileInfoBuilder.poiTags = poiTags;
return OpenResult.SUCCESS; return OpenResult.SUCCESS;
@ -227,7 +227,7 @@ final class RequiredFields {
if (tag == null) { if (tag == null) {
return new OpenResult("way tag must not be null: " + currentTagId); return new OpenResult("way tag must not be null: " + currentTagId);
} }
wayTags[currentTagId] = new Tag(tag); wayTags[currentTagId] = Tag.parse(tag);
} }
mapFileInfoBuilder.wayTags = wayTags; mapFileInfoBuilder.wayTags = wayTags;
return OpenResult.SUCCESS; return OpenResult.SUCCESS;

View File

@ -175,7 +175,7 @@ public class TileDecoder extends PbfDecoder {
for (int i = 0; i < keys.size(); i++) { for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i); String key = keys.get(i);
if (!key.startsWith(Tag.TAG_KEY_NAME)) if (!key.startsWith(Tag.KEY_NAME))
continue; continue;
int len = key.length(); int len = key.length();
if (len == 4) { if (len == 4) {
@ -222,7 +222,7 @@ public class TileDecoder extends PbfDecoder {
if (keyIdx == matchedLocal) { if (keyIdx == matchedLocal) {
hasName = true; hasName = true;
f.elem.tags.add(new Tag(Tag.TAG_KEY_NAME, val, false)); f.elem.tags.add(new Tag(Tag.KEY_NAME, val, false));
} else { } else {
key = keys.get(keyIdx); key = keys.get(keyIdx);
@ -231,7 +231,7 @@ public class TileDecoder extends PbfDecoder {
} }
if (!hasName && fallbackName != null) if (!hasName && fallbackName != null)
f.elem.tags.add(new Tag(Tag.TAG_KEY_NAME, fallbackName, false)); f.elem.tags.add(new Tag(Tag.KEY_NAME, fallbackName, false));
// FIXME extract layer tag here // FIXME extract layer tag here
f.elem.setLayer(5); f.elem.setLayer(5);

View File

@ -130,19 +130,20 @@ public class TileDecoder extends PbfDecoder {
String tagString = decodeString(); String tagString = decodeString();
if (tagString == null || tagString.length() == 0) { if (tagString == null || tagString.length() == 0) {
curTags[mCurTagCnt++] = new Tag(Tag.TAG_KEY_NAME, "..."); curTags[mCurTagCnt++] = new Tag(Tag.KEY_NAME, "...");
return false; return false;
} }
Tag tag = tagHash.get(tagString); Tag tag = tagHash.get(tagString);
if (tag == null) { if (tag == null) {
if (tagString.startsWith(Tag.TAG_KEY_NAME)) if (tagString.startsWith(Tag.KEY_NAME))
tag = new Tag(Tag.TAG_KEY_NAME, tagString.substring(5), false); tag = new Tag(Tag.KEY_NAME, tagString.substring(5), false);
else else
tag = new Tag(tagString); tag = Tag.parse(tagString);
tagHash.put(tagString, tag); if (tag != null)
tagHash.put(tagString, tag);
} }
if (mCurTagCnt >= MAX_TILE_TAGS) { if (mCurTagCnt >= MAX_TILE_TAGS) {

View File

@ -155,7 +155,7 @@ public class OSciMap2TileSource extends UrlTileSource {
String key = Tags.keys[mSArray[curTag]]; String key = Tags.keys[mSArray[curTag]];
Tag tag; Tag tag;
if (key == Tag.TAG_KEY_NAME) if (key == Tag.KEY_NAME)
tag = new Tag(key, tagString, false); tag = new Tag(key, tagString, false);
else else
tag = new Tag(key, tagString, true); tag = new Tag(key, tagString, true);

View File

@ -209,12 +209,12 @@ public class TileDecoder extends PbfDecoder {
// FIXME filter out all variable tags // FIXME filter out all variable tags
// might depend on theme though // might depend on theme though
if (key == Tag.TAG_KEY_NAME if (key == Tag.KEY_NAME
|| key == Tag.KEY_HEIGHT || key == Tag.KEY_HEIGHT
|| key == Tag.KEY_MIN_HEIGHT || key == Tag.KEY_MIN_HEIGHT
|| key == Tag.TAG_KEY_HOUSE_NUMBER || key == Tag.KEY_HOUSE_NUMBER
|| key == Tag.TAG_KEY_REF || key == Tag.KEY_REF
|| key == Tag.TAG_KEY_ELE) || key == Tag.KEY_ELE)
tag = new Tag(key, val, false); tag = new Tag(key, val, false);
else else
tag = new Tag(key, val, true); tag = new Tag(key, val, true);