add vtm-jeo
This commit is contained in:
74
vtm-jeo/src/org/oscim/theme/carto/MatcherFeature.java
Normal file
74
vtm-jeo/src/org/oscim/theme/carto/MatcherFeature.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package org.oscim.theme.carto;
|
||||
|
||||
import static java.lang.System.out;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jeo.feature.BasicFeature;
|
||||
import org.oscim.core.Tag;
|
||||
import org.oscim.core.TagSet;
|
||||
|
||||
//imitate Feature behaviour for tags and zoom-level
|
||||
class MatcherFeature extends BasicFeature {
|
||||
TagSet mTags;
|
||||
Integer mZoom;
|
||||
|
||||
void setTags(TagSet tags) {
|
||||
mTags = tags;
|
||||
}
|
||||
|
||||
void setZoom(int zoom) {
|
||||
mZoom = Integer.valueOf(zoom);
|
||||
}
|
||||
|
||||
protected MatcherFeature() {
|
||||
super("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String key) {
|
||||
//out.println("get(" + key + ")");
|
||||
|
||||
if (key.equals("zoom"))
|
||||
return mZoom;
|
||||
|
||||
Tag t = mTags.get(key.intern());
|
||||
if (t == null)
|
||||
return null;
|
||||
|
||||
//out.println("value: " + t.value);
|
||||
|
||||
return t.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, Object val) {
|
||||
out.println("EEEK put()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> list() {
|
||||
out.println("EEEK list()");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> map() {
|
||||
out.println("EEEK map()");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(int arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int arg0, Object arg1) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
266
vtm-jeo/src/org/oscim/theme/carto/RenderTheme.java
Normal file
266
vtm-jeo/src/org/oscim/theme/carto/RenderTheme.java
Normal file
@@ -0,0 +1,266 @@
|
||||
package org.oscim.theme.carto;
|
||||
|
||||
import static java.lang.System.out;
|
||||
import static org.jeo.map.CartoCSS.BACKGROUND_COLOR;
|
||||
import static org.jeo.map.CartoCSS.OPACITY;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jeo.carto.Carto;
|
||||
import org.jeo.map.CartoCSS;
|
||||
import org.jeo.map.RGB;
|
||||
import org.jeo.map.Rule;
|
||||
import org.jeo.map.RuleList;
|
||||
import org.jeo.map.Selector;
|
||||
import org.jeo.map.Style;
|
||||
import org.oscim.core.GeometryBuffer.GeometryType;
|
||||
import org.oscim.core.MapElement;
|
||||
import org.oscim.core.Tag;
|
||||
import org.oscim.core.TagSet;
|
||||
import org.oscim.theme.IRenderTheme;
|
||||
import org.oscim.theme.renderinstruction.Area;
|
||||
import org.oscim.theme.renderinstruction.Line;
|
||||
import org.oscim.theme.renderinstruction.RenderInstruction;
|
||||
|
||||
public class RenderTheme implements IRenderTheme {
|
||||
|
||||
final String STYLE = "" +
|
||||
|
||||
"[building = 'yes'] {" +
|
||||
" z: 1;" +
|
||||
" polygon-fill: #eee;" +
|
||||
" [zoom >= 16] {" +
|
||||
" polygon-fill: #c00;" +
|
||||
" }" +
|
||||
"}" +
|
||||
|
||||
"[admin_level = '2'] {" +
|
||||
" line-color: #000;" +
|
||||
" line-width: 1;" +
|
||||
" z: 1;" +
|
||||
"}" +
|
||||
|
||||
"[admin_level = '2'] {" +
|
||||
" line-color: #000;" +
|
||||
" line-width: 1;" +
|
||||
" z: 1;" +
|
||||
"}" +
|
||||
|
||||
"[admin_level = '4'] {" +
|
||||
" line-color: #aaa;" +
|
||||
" line-width: 1;" +
|
||||
" z: 2;" +
|
||||
"}" +
|
||||
|
||||
"[highway = 'motorway'] {" +
|
||||
" line-color: #a00;" +
|
||||
" z: 10;" +
|
||||
"}" +
|
||||
|
||||
"[highway = 'primary'] {" +
|
||||
" line-color: #aa0;" +
|
||||
" z: 11;" +
|
||||
"}" +
|
||||
|
||||
"[highway = 'residential'],[highway = 'road'],[highway = 'secondary'] {" +
|
||||
" line-color: #fff;" +
|
||||
" z: 12;" +
|
||||
"}" +
|
||||
|
||||
" [landuse = 'forest'] {" +
|
||||
" polygon-fill: #0a0;" +
|
||||
" z: 2;" +
|
||||
"}" +
|
||||
|
||||
"[natural = 'water'] {" +
|
||||
" polygon-fill: #00a;" +
|
||||
" z: 3;" +
|
||||
"}";
|
||||
|
||||
private Style mStyle;
|
||||
private RuleList mRules;
|
||||
|
||||
MatcherFeature mMatchFeature = new MatcherFeature();
|
||||
private int mBackground;
|
||||
|
||||
public RenderTheme() {
|
||||
|
||||
try {
|
||||
mStyle = loadStyle();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// get map background
|
||||
RuleList rules = mStyle.getRules().selectByName("Map", false);
|
||||
if (!rules.isEmpty()) {
|
||||
Rule rule = rules.collapse();
|
||||
RGB bgColor = rule.color(null, BACKGROUND_COLOR, null);
|
||||
if (bgColor != null) {
|
||||
bgColor = bgColor.alpha(rule.number(null, OPACITY, 1f));
|
||||
mBackground = color(bgColor);
|
||||
}
|
||||
}
|
||||
|
||||
mRules = mStyle.getRules();
|
||||
|
||||
//out.println(mRules);
|
||||
//out.println();
|
||||
if (mRules.get(1).equals(mRules.get(2)))
|
||||
out.println("ok");
|
||||
|
||||
for (Rule r : mRules)
|
||||
out.println(formatRule(r, 0));
|
||||
}
|
||||
|
||||
class StyleSet {
|
||||
int level;
|
||||
RenderInstruction[] ri = new RenderInstruction[2];
|
||||
}
|
||||
|
||||
Map<Rule, StyleSet> mStyleSets = new HashMap<Rule, StyleSet>();
|
||||
int mCurLevel = 0;
|
||||
|
||||
public String formatRule(Rule r, int indent) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String pad = "";
|
||||
for (int i = 0; i < indent; i++)
|
||||
pad += " ";
|
||||
|
||||
sb.append(pad);
|
||||
|
||||
for (Selector s : r.getSelectors()) {
|
||||
sb.append(RuleDebug.formatSelector(s));
|
||||
sb.append(",");
|
||||
}
|
||||
|
||||
if (sb.length() > 0)
|
||||
sb.setLength(sb.length() - 1);
|
||||
|
||||
sb.append(pad).append(" {").append("\n");
|
||||
|
||||
StyleSet s = new StyleSet();
|
||||
RGB l = null;
|
||||
RGB p = null;
|
||||
if (r.properties().containsKey(CartoCSS.LINE_COLOR)) {
|
||||
l = r.color(null, CartoCSS.LINE_COLOR, RGB.black);
|
||||
}
|
||||
if (r.properties().containsKey(CartoCSS.POLYGON_FILL)) {
|
||||
p = r.color(null, CartoCSS.POLYGON_FILL, RGB.black);
|
||||
}
|
||||
|
||||
if (p != null) {
|
||||
s.ri[0] = new Area(mCurLevel++, color(p));
|
||||
}
|
||||
|
||||
if (l != null) {
|
||||
s.ri[1] = new Line(mCurLevel++, color(l), 1);
|
||||
}
|
||||
|
||||
if (p != null || l != null) {
|
||||
mStyleSets.put(r, s);
|
||||
out.println("put " + s.ri[0] + s.ri[1]);
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Object> e : r.properties().entrySet()) {
|
||||
sb.append(pad).append(" ").append(e.getKey()).append(": ").append(e.getValue())
|
||||
.append(";\n");
|
||||
}
|
||||
|
||||
for (Rule nested : r.nested()) {
|
||||
sb.append(formatRule(nested, indent + 2)).append("\n");
|
||||
}
|
||||
|
||||
sb.append(pad).append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
Style loadStyle() throws IOException {
|
||||
return Carto.parse(STYLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized RenderInstruction[] matchElement(GeometryType type, TagSet tags,
|
||||
int zoomLevel) {
|
||||
MatcherFeature f = mMatchFeature;
|
||||
|
||||
f.setTags(tags);
|
||||
f.setZoom(zoomLevel);
|
||||
|
||||
RuleList rules = mRules.match(f);
|
||||
|
||||
Rule r = rules.collapse();
|
||||
|
||||
//out.println(r);
|
||||
if (rules.isEmpty())
|
||||
return null;
|
||||
|
||||
int z = r.number(f, "z", 0f).intValue();
|
||||
|
||||
if (type == GeometryType.POLY) {
|
||||
RGB c = r.color(f, CartoCSS.POLYGON_FILL, RGB.black);
|
||||
out.println(z + " " + c);
|
||||
return new RenderInstruction[] {
|
||||
new Area(z, color(c))
|
||||
};
|
||||
|
||||
} else if (type == GeometryType.LINE) {
|
||||
RGB c = r.color(f, CartoCSS.LINE_COLOR, RGB.black);
|
||||
float width = r.number(f, CartoCSS.LINE_WIDTH, 2f);
|
||||
//out.println(z + " " + c);
|
||||
|
||||
return new RenderInstruction[] {
|
||||
new Line(100 + z, color(c), width)
|
||||
};
|
||||
|
||||
} else if (type == GeometryType.POINT) {
|
||||
//RGB c = r.color(f, CartoCSS.MARKER_FILL, RGB.black);
|
||||
//out.println(c);
|
||||
//return new RenderInstruction[] {
|
||||
// new Caption(color(c), width)
|
||||
//};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int color(RGB rgb) {
|
||||
return rgb.getAlpha() << 24
|
||||
| rgb.getRed() << 16
|
||||
| rgb.getGreen() << 8
|
||||
| rgb.getBlue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLevels() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMapBackground() {
|
||||
return mBackground;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scaleTextSize(float scaleFactor) {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
RenderTheme t = new RenderTheme();
|
||||
|
||||
MapElement e = new MapElement();
|
||||
e.startPolygon();
|
||||
e.tags.add(new Tag("building", "yes"));
|
||||
|
||||
t.matchElement(GeometryType.POLY, e.tags, 16);
|
||||
t.matchElement(GeometryType.POLY, e.tags, 15);
|
||||
}
|
||||
|
||||
}
|
||||
74
vtm-jeo/src/org/oscim/theme/carto/RuleDebug.java
Normal file
74
vtm-jeo/src/org/oscim/theme/carto/RuleDebug.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package org.oscim.theme.carto;
|
||||
|
||||
import static java.lang.System.out;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.jeo.filter.Filter;
|
||||
import org.jeo.map.Rule;
|
||||
import org.jeo.map.Selector;
|
||||
|
||||
public class RuleDebug {
|
||||
|
||||
static void printRule(Rule r, int level) {
|
||||
|
||||
out.println("> " + level + " >");
|
||||
out.println(formatRule(r, level));
|
||||
}
|
||||
|
||||
public static String formatRule(Rule r, int indent) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String pad = "";
|
||||
for (int i = 0; i < indent; i++) {
|
||||
pad += " ";
|
||||
};
|
||||
|
||||
sb.append(pad);
|
||||
for (Selector s : r.getSelectors()) {
|
||||
sb.append(formatSelector(s));
|
||||
sb.append(",");
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
sb.setLength(sb.length() - 1);
|
||||
}
|
||||
sb.append(pad).append(" {").append("\n");
|
||||
|
||||
for (Map.Entry<String, Object> e : r.properties().entrySet()) {
|
||||
sb.append(pad).append(" ").append(e.getKey()).append(": ").append(e.getValue())
|
||||
.append(";\n");
|
||||
}
|
||||
|
||||
for (Rule nested : r.nested()) {
|
||||
sb.append(nested.toString(indent + 2)).append("\n");
|
||||
}
|
||||
|
||||
sb.append(pad).append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String formatSelector(Selector s) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
if (s.getName() != null) {
|
||||
sb.append(s.getName());
|
||||
}
|
||||
if (s.getId() != null) {
|
||||
sb.append("#").append(s.getId());
|
||||
}
|
||||
for (String c : s.getClasses()) {
|
||||
sb.append(".").append(c);
|
||||
}
|
||||
if (s.getFilter() != null && s.getFilter() != Filter.TRUE) {
|
||||
sb.append("[").append(s.getFilter()).append("]");
|
||||
}
|
||||
if (s.getAttachment() != null) {
|
||||
sb.append("::").append(s.getAttachment());
|
||||
}
|
||||
|
||||
if (s.isWildcard()) {
|
||||
sb.append("*");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user