Tag transform improvements (#678)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright 2012 Hannes Janetzek
|
||||
* Copyright 2016 Andrey Novikov
|
||||
* Copyright 2017-2018 Gustl22
|
||||
* Copyright 2017-2019 Gustl22
|
||||
* Copyright 2018 devemux86
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
@@ -66,7 +66,7 @@ public class MapElement extends GeometryBuffer {
|
||||
* @return height in meters, if present
|
||||
*/
|
||||
public Float getHeight(IRenderTheme theme) {
|
||||
String res = theme != null ? theme.transformKey(Tag.KEY_HEIGHT) : Tag.KEY_HEIGHT;
|
||||
String res = theme != null ? theme.transformBackwardKey(Tag.KEY_HEIGHT) : Tag.KEY_HEIGHT;
|
||||
String v = tags.getValue(res != null ? res : Tag.KEY_HEIGHT);
|
||||
if (v != null)
|
||||
return Float.parseFloat(v);
|
||||
@@ -77,7 +77,7 @@ public class MapElement extends GeometryBuffer {
|
||||
* @return minimum height in meters, if present
|
||||
*/
|
||||
public Float getMinHeight(IRenderTheme theme) {
|
||||
String res = theme != null ? theme.transformKey(Tag.KEY_MIN_HEIGHT) : Tag.KEY_MIN_HEIGHT;
|
||||
String res = theme != null ? theme.transformBackwardKey(Tag.KEY_MIN_HEIGHT) : Tag.KEY_MIN_HEIGHT;
|
||||
String v = tags.getValue(res != null ? res : Tag.KEY_MIN_HEIGHT);
|
||||
if (v != null)
|
||||
return Float.parseFloat(v);
|
||||
|
||||
@@ -273,7 +273,7 @@ public class BuildingLayer extends Layer implements TileLoaderThemeHook, ZoomLim
|
||||
protected String getKeyOrDefault(String key) {
|
||||
if (mTileLayer.getTheme() == null)
|
||||
return key;
|
||||
String res = mTileLayer.getTheme().transformKey(key);
|
||||
String res = mTileLayer.getTheme().transformBackwardKey(key);
|
||||
return res != null ? res : key;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
* Copyright 2013 Hannes Janetzek
|
||||
* Copyright 2017 devemux86
|
||||
* Copyright 2018 Gustl22
|
||||
* Copyright 2018-2019 Gustl22
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
*
|
||||
@@ -66,14 +66,35 @@ public interface IRenderTheme {
|
||||
void scaleTextSize(float scaleFactor);
|
||||
|
||||
/**
|
||||
* @return the transformed tag key of this RenderTheme.
|
||||
* Transform internal key to tile source key.
|
||||
* e.g. for lazy fetched tag values via tile source key.
|
||||
* Use when tile source and internal keys have 1-1 relation.
|
||||
*
|
||||
* @return the backwards transformed tag key.
|
||||
*/
|
||||
String transformKey(String key);
|
||||
String transformBackwardKey(String key);
|
||||
|
||||
/**
|
||||
* @return the transformed tag of this RenderTheme.
|
||||
* Transform tile source key to internal key.
|
||||
*
|
||||
* @return the forward transformed tag key.
|
||||
*/
|
||||
Tag transformTag(Tag tag);
|
||||
String transformForwardKey(String key);
|
||||
|
||||
/**
|
||||
* Transform internal tag to tile source tag.
|
||||
* Use when tile source and internal tags have 1-1 relation.
|
||||
*
|
||||
* @return the backwards transformed tag.
|
||||
*/
|
||||
Tag transformBackwardTag(Tag tag);
|
||||
|
||||
/**
|
||||
* Transform tile source tag to internal tag.
|
||||
*
|
||||
* @return the forward transformed tag.
|
||||
*/
|
||||
Tag transformForwardTag(Tag tag);
|
||||
|
||||
class ThemeException extends IllegalArgumentException {
|
||||
public ThemeException(String string) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* Copyright 2014 Hannes Janetzek
|
||||
* Copyright 2017 Longri
|
||||
* Copyright 2017 devemux86
|
||||
* Copyright 2018 Gustl22
|
||||
* Copyright 2018-2019 Gustl22
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
*
|
||||
@@ -26,6 +26,7 @@ import org.oscim.theme.rule.Rule;
|
||||
import org.oscim.theme.rule.Rule.Element;
|
||||
import org.oscim.theme.rule.Rule.RuleVisitor;
|
||||
import org.oscim.theme.styles.RenderStyle;
|
||||
import org.oscim.utils.ArrayUtils;
|
||||
import org.oscim.utils.LRUCache;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -47,8 +48,8 @@ public class RenderTheme implements IRenderTheme {
|
||||
private final Rule[] mRules;
|
||||
private final boolean mMapsforgeTheme;
|
||||
|
||||
private final Map<String, String> mTransformKeyMap;
|
||||
private final Map<Tag, Tag> mTransformTagMap;
|
||||
private final Map<String, String> mTransformBackwardKeyMap, mTransformForwardKeyMap;
|
||||
private final Map<Tag, Tag> mTransformBackwardTagMap, mTransformForwardTagMap;
|
||||
|
||||
class RenderStyleCache {
|
||||
final int matchType;
|
||||
@@ -105,8 +106,10 @@ public class RenderTheme implements IRenderTheme {
|
||||
mRules = rules;
|
||||
mMapsforgeTheme = mapsforgeTheme;
|
||||
|
||||
mTransformKeyMap = transformKeyMap;
|
||||
mTransformTagMap = transformTagMap;
|
||||
mTransformForwardKeyMap = transformKeyMap;
|
||||
mTransformBackwardKeyMap = ArrayUtils.swap(transformKeyMap);
|
||||
mTransformForwardTagMap = transformTagMap;
|
||||
mTransformBackwardTagMap = ArrayUtils.swap(transformTagMap);
|
||||
|
||||
mStyleCache = new RenderStyleCache[3];
|
||||
mStyleCache[0] = new RenderStyleCache(Element.NODE);
|
||||
@@ -292,16 +295,30 @@ public class RenderTheme implements IRenderTheme {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String transformKey(String key) {
|
||||
if (mTransformKeyMap != null)
|
||||
return mTransformKeyMap.get(key);
|
||||
public String transformBackwardKey(String key) {
|
||||
if (mTransformBackwardKeyMap != null)
|
||||
return mTransformBackwardKeyMap.get(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag transformTag(Tag tag) {
|
||||
if (mTransformTagMap != null)
|
||||
return mTransformTagMap.get(tag);
|
||||
public String transformForwardKey(String key) {
|
||||
if (mTransformForwardKeyMap != null)
|
||||
return mTransformForwardKeyMap.get(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag transformBackwardTag(Tag tag) {
|
||||
if (mTransformBackwardTagMap != null)
|
||||
return mTransformBackwardTagMap.get(tag);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag transformForwardTag(Tag tag) {
|
||||
if (mTransformForwardTagMap != null)
|
||||
return mTransformForwardTagMap.get(tag);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Copyright 2016-2019 devemux86
|
||||
* Copyright 2016-2017 Longri
|
||||
* Copyright 2016 Andrey Novikov
|
||||
* Copyright 2018 Gustl22
|
||||
* Copyright 2018-2019 Gustl22
|
||||
* Copyright 2018 Izumi Kawashima
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
@@ -1268,40 +1268,40 @@ public class XmlThemeBuilder extends DefaultHandler {
|
||||
}
|
||||
|
||||
private void tagTransform(String localName, Attributes attributes) {
|
||||
String matchKey, matchValue, outputKey, outputValue;
|
||||
matchKey = matchValue = outputKey = outputValue = null;
|
||||
String k, v, libK, libV;
|
||||
k = v = libK = libV = null;
|
||||
|
||||
for (int i = 0; i < attributes.getLength(); i++) {
|
||||
String name = attributes.getLocalName(i);
|
||||
String value = attributes.getValue(i);
|
||||
|
||||
switch (name) {
|
||||
case "match-k":
|
||||
matchKey = value;
|
||||
case "k":
|
||||
k = value;
|
||||
break;
|
||||
case "match-v":
|
||||
matchValue = value;
|
||||
case "v":
|
||||
v = value;
|
||||
break;
|
||||
case "output-k":
|
||||
outputKey = value;
|
||||
case "k-lib":
|
||||
libK = value;
|
||||
break;
|
||||
case "output-v":
|
||||
outputValue = value;
|
||||
case "v-lib":
|
||||
libV = value;
|
||||
break;
|
||||
default:
|
||||
logUnknownAttribute(localName, name, value, i);
|
||||
}
|
||||
}
|
||||
|
||||
if (matchKey == null || matchKey.isEmpty() || outputKey == null || outputKey.isEmpty()) {
|
||||
if (k == null || k.isEmpty() || libK == null || libK.isEmpty()) {
|
||||
log.debug("empty key in element " + localName);
|
||||
return;
|
||||
}
|
||||
|
||||
if (matchValue == null && outputValue == null) {
|
||||
mTransformKeyMap.put(matchKey, outputKey);
|
||||
if (v == null && libV == null) {
|
||||
mTransformKeyMap.put(k, libK);
|
||||
} else {
|
||||
mTransformTagMap.put(new Tag(matchKey, matchValue), new Tag(outputKey, outputValue));
|
||||
mTransformTagMap.put(new Tag(k, v), new Tag(libK, libV));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
*/
|
||||
package org.oscim.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ArrayUtils {
|
||||
|
||||
public static <T> void reverse(T[] data) {
|
||||
@@ -189,6 +192,18 @@ public class ArrayUtils {
|
||||
return neg ? -val : val;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the Map with swapped keys and values
|
||||
*/
|
||||
public static <K, V> Map<V, K> swap(Map<K, V> map) {
|
||||
if (map == null)
|
||||
return null;
|
||||
Map<V, K> swap = new HashMap<>();
|
||||
for (Map.Entry<K, V> entry : map.entrySet())
|
||||
swap.put(entry.getValue(), entry.getKey());
|
||||
return swap;
|
||||
}
|
||||
|
||||
public static boolean withinRange(float[] vec, float min, float max) {
|
||||
for (int i = 0, n = vec.length; i < n; i++) {
|
||||
float v = vec[i];
|
||||
|
||||
Reference in New Issue
Block a user