Tag transform improvements (#678)

This commit is contained in:
Gustl22 2019-02-27 21:34:35 +01:00 committed by Emux
parent 4162d9b488
commit 207993a6e2
No known key found for this signature in database
GPG Key ID: 64ED9980896038C3
11 changed files with 129 additions and 56 deletions

View File

@ -303,10 +303,10 @@
<!-- tag-transform element --> <!-- tag-transform element -->
<xs:complexType name="tag-transform"> <xs:complexType name="tag-transform">
<xs:attribute name="match-k" type="xs:string" use="required" /> <xs:attribute name="k" type="xs:string" use="required" />
<xs:attribute name="match-v" type="xs:string" use="optional" /> <xs:attribute name="v" type="xs:string" use="optional" />
<xs:attribute name="output-k" type="xs:string" use="required" /> <xs:attribute name="k-lib" type="xs:string" use="required" />
<xs:attribute name="output-v" type="xs:string" use="optional" /> <xs:attribute name="v-lib" type="xs:string" use="optional" />
</xs:complexType> </xs:complexType>
<!-- rendertheme element --> <!-- rendertheme element -->

View File

@ -252,12 +252,22 @@ public class RenderTheme implements IRenderTheme {
} }
@Override @Override
public String transformKey(String key) { public String transformBackwardKey(String key) {
return null; return null;
} }
@Override @Override
public Tag transformTag(Tag tag) { public String transformForwardKey(String key) {
return null;
}
@Override
public Tag transformBackwardTag(Tag tag) {
return null;
}
@Override
public Tag transformForwardTag(Tag tag) {
return null; return null;
} }

View File

@ -48,12 +48,22 @@ public class DebugTheme implements IRenderTheme {
} }
@Override @Override
public String transformKey(String key) { public String transformBackwardKey(String key) {
return null; return null;
} }
@Override @Override
public Tag transformTag(Tag tag) { public String transformForwardKey(String key) {
return null;
}
@Override
public Tag transformBackwardTag(Tag tag) {
return null;
}
@Override
public Tag transformForwardTag(Tag tag) {
return null; return null;
} }

View File

@ -3,16 +3,16 @@
version="1" xmlns="http://opensciencemap.org/rendertheme" version="1" xmlns="http://opensciencemap.org/rendertheme"
xsi:schemaLocation="http://opensciencemap.org/rendertheme https://raw.githubusercontent.com/mapsforge/vtm/master/resources/rendertheme.xsd"> xsi:schemaLocation="http://opensciencemap.org/rendertheme https://raw.githubusercontent.com/mapsforge/vtm/master/resources/rendertheme.xsd">
<!--<tag-transform match-k="building" match-v="yes" output-k="kind" output-v="building" />--> <!--<tag-transform k="kind" v="building" k-lib="building" v-lib="yes" />-->
<!--<tag-transform match-k="building:part" match-v="yes" output-k="kind" output-v="building_part" />--> <!--<tag-transform k="kind" v="building_part" k-lib="building:part" v-lib="yes" />-->
<tag-transform match-k="ref" output-k="root_id" /> <tag-transform k="root_id" k-lib="ref" />
<tag-transform match-k="roof:colour" output-k="roof_color" /> <tag-transform k="roof_color" k-lib="roof:colour" />
<tag-transform match-k="roof:direction" output-k="roof_direction" /> <tag-transform k="roof_direction" k-lib="roof:direction" />
<tag-transform match-k="roof:height" output-k="roof_height" /> <tag-transform k="roof_height" k-lib="roof:height" />
<tag-transform match-k="roof:material" output-k="roof_material" /> <tag-transform k="roof_material" k-lib="roof:material" />
<tag-transform match-k="roof:orientation" output-k="roof_orientation" /> <tag-transform k="roof_orientation" k-lib="roof:orientation" />
<tag-transform match-k="roof:shape" output-k="roof_shape" /> <tag-transform k="roof_shape" k-lib="roof:shape" />
<!-- base style for fixed width lines --> <!-- base style for fixed width lines -->
<style-line cap="butt" fix="true" id="fix" width="1.0" /> <style-line cap="butt" fix="true" id="fix" width="1.0" />

View File

@ -3,10 +3,10 @@
version="1" xmlns="http://opensciencemap.org/rendertheme" version="1" xmlns="http://opensciencemap.org/rendertheme"
xsi:schemaLocation="http://opensciencemap.org/rendertheme https://raw.githubusercontent.com/mapsforge/vtm/master/resources/rendertheme.xsd"> xsi:schemaLocation="http://opensciencemap.org/rendertheme https://raw.githubusercontent.com/mapsforge/vtm/master/resources/rendertheme.xsd">
<tag-transform match-k="height" output-k="render_height" /> <tag-transform k="render_height" k-lib="height" />
<tag-transform match-k="min_height" output-k="render_min_height" /> <tag-transform k="render_min_height" k-lib="min_height" />
<!--<tag-transform match-k="building" match-v="yes" output-k="layer" output-v="building" />--> <!--<tag-transform k="layer" v="building" k-lib="building" v-lib="yes" />-->
<!--<tag-transform match-k="building:part" match-v="yes" output-k="layer" output-v="building:part" />--> <!--<tag-transform k="layer" v="building:part" k-lib="building:part" v-lib="yes" />-->
<!--###### TEXT styles ######--> <!--###### TEXT styles ######-->

View File

@ -1,7 +1,7 @@
/* /*
* Copyright 2012 Hannes Janetzek * Copyright 2012 Hannes Janetzek
* Copyright 2016 Andrey Novikov * Copyright 2016 Andrey Novikov
* Copyright 2017-2018 Gustl22 * Copyright 2017-2019 Gustl22
* Copyright 2018 devemux86 * Copyright 2018 devemux86
* *
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * 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 * @return height in meters, if present
*/ */
public Float getHeight(IRenderTheme theme) { 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); String v = tags.getValue(res != null ? res : Tag.KEY_HEIGHT);
if (v != null) if (v != null)
return Float.parseFloat(v); return Float.parseFloat(v);
@ -77,7 +77,7 @@ public class MapElement extends GeometryBuffer {
* @return minimum height in meters, if present * @return minimum height in meters, if present
*/ */
public Float getMinHeight(IRenderTheme theme) { 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); String v = tags.getValue(res != null ? res : Tag.KEY_MIN_HEIGHT);
if (v != null) if (v != null)
return Float.parseFloat(v); return Float.parseFloat(v);

View File

@ -273,7 +273,7 @@ public class BuildingLayer extends Layer implements TileLoaderThemeHook, ZoomLim
protected String getKeyOrDefault(String key) { protected String getKeyOrDefault(String key) {
if (mTileLayer.getTheme() == null) if (mTileLayer.getTheme() == null)
return key; return key;
String res = mTileLayer.getTheme().transformKey(key); String res = mTileLayer.getTheme().transformBackwardKey(key);
return res != null ? res : key; return res != null ? res : key;
} }

View File

@ -2,7 +2,7 @@
* Copyright 2010, 2011, 2012 mapsforge.org * Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2013 Hannes Janetzek * Copyright 2013 Hannes Janetzek
* Copyright 2017 devemux86 * Copyright 2017 devemux86
* Copyright 2018 Gustl22 * Copyright 2018-2019 Gustl22
* *
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
* *
@ -66,14 +66,35 @@ public interface IRenderTheme {
void scaleTextSize(float scaleFactor); 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 { class ThemeException extends IllegalArgumentException {
public ThemeException(String string) { public ThemeException(String string) {

View File

@ -2,7 +2,7 @@
* Copyright 2014 Hannes Janetzek * Copyright 2014 Hannes Janetzek
* Copyright 2017 Longri * Copyright 2017 Longri
* Copyright 2017 devemux86 * Copyright 2017 devemux86
* Copyright 2018 Gustl22 * Copyright 2018-2019 Gustl22
* *
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * 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.Element;
import org.oscim.theme.rule.Rule.RuleVisitor; import org.oscim.theme.rule.Rule.RuleVisitor;
import org.oscim.theme.styles.RenderStyle; import org.oscim.theme.styles.RenderStyle;
import org.oscim.utils.ArrayUtils;
import org.oscim.utils.LRUCache; import org.oscim.utils.LRUCache;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -47,8 +48,8 @@ public class RenderTheme implements IRenderTheme {
private final Rule[] mRules; private final Rule[] mRules;
private final boolean mMapsforgeTheme; private final boolean mMapsforgeTheme;
private final Map<String, String> mTransformKeyMap; private final Map<String, String> mTransformBackwardKeyMap, mTransformForwardKeyMap;
private final Map<Tag, Tag> mTransformTagMap; private final Map<Tag, Tag> mTransformBackwardTagMap, mTransformForwardTagMap;
class RenderStyleCache { class RenderStyleCache {
final int matchType; final int matchType;
@ -105,8 +106,10 @@ public class RenderTheme implements IRenderTheme {
mRules = rules; mRules = rules;
mMapsforgeTheme = mapsforgeTheme; mMapsforgeTheme = mapsforgeTheme;
mTransformKeyMap = transformKeyMap; mTransformForwardKeyMap = transformKeyMap;
mTransformTagMap = transformTagMap; mTransformBackwardKeyMap = ArrayUtils.swap(transformKeyMap);
mTransformForwardTagMap = transformTagMap;
mTransformBackwardTagMap = ArrayUtils.swap(transformTagMap);
mStyleCache = new RenderStyleCache[3]; mStyleCache = new RenderStyleCache[3];
mStyleCache[0] = new RenderStyleCache(Element.NODE); mStyleCache[0] = new RenderStyleCache(Element.NODE);
@ -292,16 +295,30 @@ public class RenderTheme implements IRenderTheme {
} }
@Override @Override
public String transformKey(String key) { public String transformBackwardKey(String key) {
if (mTransformKeyMap != null) if (mTransformBackwardKeyMap != null)
return mTransformKeyMap.get(key); return mTransformBackwardKeyMap.get(key);
return null; return null;
} }
@Override @Override
public Tag transformTag(Tag tag) { public String transformForwardKey(String key) {
if (mTransformTagMap != null) if (mTransformForwardKeyMap != null)
return mTransformTagMap.get(tag); 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; return null;
} }

View File

@ -4,7 +4,7 @@
* Copyright 2016-2019 devemux86 * Copyright 2016-2019 devemux86
* Copyright 2016-2017 Longri * Copyright 2016-2017 Longri
* Copyright 2016 Andrey Novikov * Copyright 2016 Andrey Novikov
* Copyright 2018 Gustl22 * Copyright 2018-2019 Gustl22
* Copyright 2018 Izumi Kawashima * Copyright 2018 Izumi Kawashima
* *
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * 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) { private void tagTransform(String localName, Attributes attributes) {
String matchKey, matchValue, outputKey, outputValue; String k, v, libK, libV;
matchKey = matchValue = outputKey = outputValue = null; k = v = libK = libV = null;
for (int i = 0; i < attributes.getLength(); i++) { for (int i = 0; i < attributes.getLength(); i++) {
String name = attributes.getLocalName(i); String name = attributes.getLocalName(i);
String value = attributes.getValue(i); String value = attributes.getValue(i);
switch (name) { switch (name) {
case "match-k": case "k":
matchKey = value; k = value;
break; break;
case "match-v": case "v":
matchValue = value; v = value;
break; break;
case "output-k": case "k-lib":
outputKey = value; libK = value;
break; break;
case "output-v": case "v-lib":
outputValue = value; libV = value;
break; break;
default: default:
logUnknownAttribute(localName, name, value, i); 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); log.debug("empty key in element " + localName);
return; return;
} }
if (matchValue == null && outputValue == null) { if (v == null && libV == null) {
mTransformKeyMap.put(matchKey, outputKey); mTransformKeyMap.put(k, libK);
} else { } else {
mTransformTagMap.put(new Tag(matchKey, matchValue), new Tag(outputKey, outputValue)); mTransformTagMap.put(new Tag(k, v), new Tag(libK, libV));
} }
} }

View File

@ -17,6 +17,9 @@
*/ */
package org.oscim.utils; package org.oscim.utils;
import java.util.HashMap;
import java.util.Map;
public class ArrayUtils { public class ArrayUtils {
public static <T> void reverse(T[] data) { public static <T> void reverse(T[] data) {
@ -189,6 +192,18 @@ public class ArrayUtils {
return neg ? -val : val; 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) { public static boolean withinRange(float[] vec, float min, float max) {
for (int i = 0, n = vec.length; i < n; i++) { for (int i = 0, n = vec.length; i < n; i++) {
float v = vec[i]; float v = vec[i];