Add MapElement as a buffer object to pass map elements to TileGenerator

- unify IMapDatabase callback
This commit is contained in:
Hannes Janetzek
2013-04-21 17:44:57 +02:00
parent e0805cdf2d
commit 119f2ac14c
8 changed files with 271 additions and 267 deletions

View File

@@ -1,5 +1,5 @@
/*
* 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
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -14,77 +14,16 @@
*/
package org.oscim.database;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.Tag;
import org.oscim.database.mapfile.MapDatabase;
import org.oscim.core.MapElement;
/**
* Callback methods which can be triggered from the {@link MapDatabase}.
* MapDatabase callbacks (implemented by TileGenerator)
* ____
* NOTE: All parameters passed belong to the caller! i.e. dont hold
* references to any arrays after callback function returns.
*/
public interface IMapDatabaseCallback {
public class WayData{
public WayData(){
}
public WayData(GeometryBuffer geom, Tag[] tags) {
this.geom = geom;
this.tags = tags;
}
public GeometryBuffer geom;
// closed == geometry is polygon
public boolean closed;
// osm layer of the way.
public int layer;
// osm tags of the way.
public Tag[] tags;
// building tags
public int height;
public int minHeight;
// unused
public int priority;
}
/**
* Renders a single point of interest node (POI).
*
* @param layer
* the layer of the node.
* @param tags
* the tags of the node.
*/
void renderPOI(byte layer, Tag[] tags, GeometryBuffer geom);
/**
* Renders water background for the current tile.
*/
void renderWaterBackground();
/**
* Renders a single way or area.
*
*/
void renderWay(WayData way);
// /**
// * TBD: check if way will be rendered before decoding - MUST be called before renderWay!
// *
// * @param tags
// * ...
// * @param closed
// * ...
// * @return true if the way will be rendered (i.e. found match in
// * RenderTheme)
// */
// boolean matchWay(Tag[] tags, boolean closed);
void renderElement(MapElement element);
}

View File

@@ -18,12 +18,11 @@ import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.database.IMapDatabase;
import org.oscim.database.IMapDatabaseCallback;
import org.oscim.database.IMapDatabaseCallback.WayData;
import org.oscim.database.MapOptions;
import org.oscim.database.OpenResult;
import org.oscim.database.QueryResult;
@@ -198,8 +197,9 @@ public class MapDatabase implements IMapDatabase {
private int mTileLongitude;
private int[] mIntBuffer;
private final GeometryBuffer mGeom = new GeometryBuffer(1 << 14, 1 << 8);
private final WayData mWay = new WayData();
//private final GeometryBuffer mElem = new GeometryBuffer(1 << 14, 1 << 8);
//private final WayData mWay = new WayData();
private final MapElement mElem = new MapElement();
private int minLat, minLon;
private Tile mTile;
@@ -668,10 +668,18 @@ public class MapDatabase implements IMapDatabase {
double sinLat = Math.sin(latitude * PI180);
latitude = (int) (Math.log((1.0 + sinLat) / (1.0 - sinLat)) * divy + dy);
mGeom.points[0] = longitude;
mGeom.points[1] = latitude;
mGeom.index[0] = 2;
mapDatabaseCallback.renderPOI(layer, curTags, mGeom);
mElem.clear();
mElem.startPoints();
mElem.addPoint(longitude, latitude);
mElem.set(curTags, layer, MapElement.GEOM_POINT);
mapDatabaseCallback.renderElement(mElem);
// mGeom.points[0] = longitude;
// mGeom.points[1] = latitude;
// mGeom.index[0] = 2;
// mapDatabaseCallback.renderPOI(layer, curTags, mGeom);
}
@@ -687,11 +695,11 @@ public class MapDatabase implements IMapDatabase {
}
//short[] wayLengths = new short[numBlocks];
short[] wayLengths = mGeom.ensureIndexSize(numBlocks, false);
short[] wayLengths = mElem.ensureIndexSize(numBlocks, false);
if (wayLengths.length > numBlocks)
wayLengths[numBlocks] = -1;
mGeom.pointPos = 0;
mElem.pointPos = 0;
// read the way coordinate blocks
for (int coordinateBlock = 0; coordinateBlock < numBlocks; ++coordinateBlock) {
@@ -723,8 +731,8 @@ public class MapDatabase implements IMapDatabase {
mReadBuffer.readSignedInt(buffer, length);
float[] outBuffer = mGeom.ensurePointSize(mGeom.pointPos + length, true);
int pointPos = mGeom.pointPos;
float[] outBuffer = mElem.ensurePointSize(mElem.pointPos + length, true);
int pointPos = mElem.pointPos;
// get the first way node latitude offset (VBE-S)
int wayNodeLatitude = mTileLatitude + buffer[0];
@@ -761,7 +769,7 @@ public class MapDatabase implements IMapDatabase {
}
}
mGeom.pointPos = pointPos;
mElem.pointPos = pointPos;
return cnt;
}
@@ -770,8 +778,8 @@ public class MapDatabase implements IMapDatabase {
int[] buffer = mIntBuffer;
mReadBuffer.readSignedInt(buffer, length);
float[] outBuffer = mGeom.ensurePointSize(mGeom.pointPos + length, true);
int pointPos = mGeom.pointPos;
float[] outBuffer = mElem.ensurePointSize(mElem.pointPos + length, true);
int pointPos = mElem.pointPos;
// get the first way node latitude single-delta offset (VBE-S)
int wayNodeLatitude = mTileLatitude + buffer[0];
@@ -803,7 +811,7 @@ public class MapDatabase implements IMapDatabase {
}
}
mGeom.pointPos = pointPos;
mElem.pointPos = pointPos;
return cnt;
}
@@ -974,18 +982,18 @@ public class MapDatabase implements IMapDatabase {
return false;
// wayDataContainer.textPos = textPos;
int l = mGeom.index[0];
int l = mElem.index[0];
boolean closed = mGeom.points[0] == mGeom.points[l - 2]
&& mGeom.points[1] == mGeom.points[l - 1];
boolean closed = mElem.points[0] == mElem.points[l - 2]
&& mElem.points[1] == mElem.points[l - 1];
projectToTile(mGeom.points, mGeom.index);
mWay.geom = mGeom;
mWay.layer = layer;
mWay.closed = closed;
mWay.tags = curTags;
projectToTile(mElem.points, mElem.index);
mElem.layer = layer;
mapDatabaseCallback.renderWay(mWay);
mElem.geometryType = closed ? MapElement.GEOM_POLY : MapElement.GEOM_LINE;
mElem.tags = curTags;
mapDatabaseCallback.renderElement(mElem);
}
}

View File

@@ -23,12 +23,11 @@ import java.util.Arrays;
import org.oscim.core.BoundingBox;
import org.oscim.core.GeoPoint;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.database.IMapDatabase;
import org.oscim.database.IMapDatabaseCallback;
import org.oscim.database.IMapDatabaseCallback.WayData;
import org.oscim.database.MapInfo;
import org.oscim.database.MapOptions;
import org.oscim.database.OpenResult;
@@ -75,7 +74,8 @@ public class MapDatabase implements IMapDatabase {
private final boolean debug = false;
private LwHttp lwHttp;
private final WayData mWay = new WayData();
//private final WayData mWay = new WayData();
private final MapElement mElem = new MapElement();
@Override
public QueryResult executeQuery(JobTile tile, IMapDatabaseCallback mapDatabaseCallback) {
@@ -211,7 +211,7 @@ public class MapDatabase implements IMapDatabase {
}
// /////////////// hand sewed tile protocol buffers decoder ///////////////
private final int MAX_WAY_COORDS = 1 << 14;
//private final int MAX_WAY_COORDS = 1 << 14;
// overall bytes of content processed
private int mBytesProcessed;
@@ -240,7 +240,7 @@ public class MapDatabase implements IMapDatabase {
//private float[] mTmpCoords;
//private short[] mIndices = new short[10];
private final GeometryBuffer mGeom = new GeometryBuffer(MAX_WAY_COORDS, 128);
//private final GeometryBuffer mElem = new GeometryBuffer(MAX_WAY_COORDS, 128);
private Tag[][] mElementTags;
@@ -315,9 +315,9 @@ public class MapDatabase implements IMapDatabase {
}
private int decodeWayIndices(int indexCnt) throws IOException {
mGeom.index = decodeShortArray(indexCnt, mGeom.index);
mElem.index = decodeShortArray(indexCnt, mElem.index);
short[] index = mGeom.index;
short[] index = mElem.index;
int coordCnt = 0;
for (int i = 0; i < indexCnt; i++) {
@@ -348,13 +348,13 @@ public class MapDatabase implements IMapDatabase {
int coordCnt = 0;
if (type == TAG_TILE_POINT){
coordCnt = 2;
mGeom.index[0] = 2;
mElem.index[0] = 2;
}
mWay.layer = 5;
mWay.priority = 0;
mWay.height = 0;
mWay.minHeight = 0;
mElem.layer = 5;
mElem.priority = 0;
mElem.height = 0;
mElem.minHeight = 0;
while (mBytesProcessed < end) {
// read tag and wire type
@@ -391,19 +391,19 @@ public class MapDatabase implements IMapDatabase {
break;
case TAG_ELEM_LAYER:
mWay.layer = decodeVarint32();
mElem.layer = decodeVarint32();
break;
case TAG_ELEM_HEIGHT:
mWay.height= decodeVarint32();
mElem.height= decodeVarint32();
break;
case TAG_ELEM_MIN_HEIGHT:
mWay.minHeight = decodeVarint32();
mElem.minHeight = decodeVarint32();
break;
case TAG_ELEM_PRIORITY:
mWay.priority = decodeVarint32();
mElem.priority = decodeVarint32();
break;
default:
@@ -419,23 +419,22 @@ public class MapDatabase implements IMapDatabase {
return false;
}
mWay.geom = mGeom;
mWay.tags = tags;
mElem.tags = tags;
switch (type) {
case TAG_TILE_LINE:
mWay.closed = false;
mMapGenerator.renderWay(mWay);
mElem.geometryType= MapElement.GEOM_LINE;
break;
case TAG_TILE_POLY:
mWay.closed = true;
mMapGenerator.renderWay(mWay);
mElem.geometryType= MapElement.GEOM_POLY;
break;
case TAG_TILE_POINT:
mMapGenerator.renderPOI((byte) mWay.layer, tags, mGeom);
mElem.geometryType= MapElement.GEOM_POINT;
break;
}
mMapGenerator.renderElement(mElem);
return true;
}
@@ -506,7 +505,7 @@ public class MapDatabase implements IMapDatabase {
float scale = mScaleFactor;
float[] coords = mGeom.ensurePointSize(nodes, false);
float[] coords = mElem.ensurePointSize(nodes, false);
// if (nodes * 2 > coords.length) {
// Log.d(TAG, mTile + " increase way coord buffer to " + (nodes * 2));

View File

@@ -37,11 +37,11 @@ import java.util.Map;
import org.oscim.core.BoundingBox;
import org.oscim.core.GeoPoint;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.database.IMapDatabase;
import org.oscim.database.IMapDatabaseCallback;
import org.oscim.database.IMapDatabaseCallback.WayData;
import org.oscim.database.MapInfo;
import org.oscim.database.MapOptions;
import org.oscim.database.OpenResult;
@@ -103,7 +103,7 @@ public class MapDatabase implements IMapDatabase {
private static final int MAX_TAGS_CACHE = 100;
private final WayData mWay = new WayData();
private final MapElement mElement = new MapElement();
private static Map<String, Tag> tagHash = Collections
.synchronizedMap(new LinkedHashMap<String, Tag>(
@@ -454,12 +454,13 @@ public class MapDatabase implements IMapDatabase {
if (layer == 0)
layer = 5;
mWay.geom = mGeom;
mWay.tags = tags;
mWay.layer = layer;
mWay.closed = polygon;
mElement.set(tags, layer, polygon ? MapElement.GEOM_POLY : MapElement.GEOM_LINE);
mMapGenerator.renderWay(mWay);
// mElement.tags = tags;
// mElement.layer = layer;
// mElement.closed = polygon;
mMapGenerator.renderElement(mElement);
return true;
}
@@ -535,8 +536,12 @@ public class MapDatabase implements IMapDatabase {
coords[cnt++] = Tile.SIZE - lastY / scale;
}
mGeom.index[0] = (short)numNodes;
mMapGenerator.renderPOI(layer, tags, mGeom);
mElement.index[0] = (short)numNodes;
mElement.set(tags, layer, MapElement.GEOM_POINT);
mMapGenerator.renderElement(mElement);
//mMapGenerator.renderPOI(layer, tags, mGeom);
return cnt;
}

View File

@@ -14,19 +14,21 @@
*/
package org.oscim.database.test;
import static org.oscim.core.MapElement.GEOM_LINE;
import static org.oscim.core.MapElement.GEOM_POINT;
import static org.oscim.core.MapElement.GEOM_POLY;
import org.oscim.core.BoundingBox;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.database.IMapDatabase;
import org.oscim.database.IMapDatabaseCallback;
import org.oscim.database.IMapDatabaseCallback.WayData;
import org.oscim.database.MapInfo;
import org.oscim.database.MapOptions;
import org.oscim.database.OpenResult;
import org.oscim.database.QueryResult;
import org.oscim.generator.JobTile;
/**
*
*
@@ -56,13 +58,10 @@ public class MapDatabase implements IMapDatabase {
private boolean mOpenFile = false;
private final WayData mWay;
private final GeometryBuffer mGeom;
private final MapElement mElem;
public MapDatabase() {
mGeom = new GeometryBuffer(1, 1);
mWay = new WayData();
mWay.geom = mGeom;
mElem = new MapElement();
}
@Override
@@ -70,100 +69,91 @@ public class MapDatabase implements IMapDatabase {
IMapDatabaseCallback mapDatabaseCallback) {
int size = Tile.SIZE;
GeometryBuffer g = mGeom;
MapElement e = mElem;
float x1 = -1;
float y1 = -1;
float x2 = size + 1;
float y2 = size + 1;
g.clear();
g.startPolygon();
g.addPoint(x1, y1);
g.addPoint(x2, y1);
g.addPoint(x2, y2);
g.addPoint(x1, y2);
e.clear();
e.startPolygon();
e.addPoint(x1, y1);
e.addPoint(x2, y1);
e.addPoint(x2, y2);
e.addPoint(x1, y2);
y1 = 40;
y2 = size - 40;
x1 = 40;
x2 = size - 40;
g.startHole();
g.addPoint(x1, y1);
g.addPoint(x2, y1);
g.addPoint(x2, y2);
g.addPoint(x1, y2);
e.startHole();
e.addPoint(x1, y1);
e.addPoint(x2, y1);
e.addPoint(x2, y2);
e.addPoint(x1, y2);
mWay.tags = mTags;
mWay.layer = 0;
mWay.closed = true;
e.set(mTags, 0, GEOM_POLY);
mapDatabaseCallback.renderElement(e);
mapDatabaseCallback.renderWay(mWay);
g.clear();
e.clear();
// middle horizontal
g.startLine();
g.addPoint(0, size / 2);
g.addPoint(size, size / 2);
e.startLine();
e.addPoint(0, size / 2);
e.addPoint(size, size / 2);
// center up
g.startLine();
g.addPoint(size / 2, -size / 2);
g.addPoint(size / 2, size / 2);
e.startLine();
e.addPoint(size / 2, -size / 2);
e.addPoint(size / 2, size / 2);
// center down
g.startLine();
g.addPoint(size / 2, size / 2);
g.addPoint(size / 2, size / 2 + size);
e.startLine();
e.addPoint(size / 2, size / 2);
e.addPoint(size / 2, size / 2 + size);
mWay.closed = false;
mWay.layer = 0;
mWay.tags = mTagsWay;
mapDatabaseCallback.renderWay(mWay);
e.set(mTagsWay, 0, GEOM_LINE);
mapDatabaseCallback.renderElement(e);
g.clear();
e.clear();
// left-top to center
g.startLine();
g.addPoint(size / 2, size / 2);
g.addPoint(10, 10);
e.startLine();
e.addPoint(size / 2, size / 2);
e.addPoint(10, 10);
g.startLine();
g.addPoint(0, 10);
g.addPoint(size, 10);
e.startLine();
e.addPoint(0, 10);
e.addPoint(size, 10);
g.startLine();
g.addPoint(10, 0);
g.addPoint(10, size);
e.startLine();
e.addPoint(10, 0);
e.addPoint(10, size);
mWay.closed = false;
mWay.layer = 1;
mWay.tags = mTagsWay;
mapDatabaseCallback.renderWay(mWay);
e.set(mTagsWay, 1, GEOM_LINE);
mapDatabaseCallback.renderElement(e);
g.clear();
g.startPolygon();
e.clear();
e.startPolygon();
float r = size / 2;
for (int i = 0; i < 360; i += 4) {
double d = Math.toRadians(i);
g.addPoint(r + (float) Math.cos(d) * (r - 40), r + (float) Math.sin(d) * (r - 40));
e.addPoint(r + (float) Math.cos(d) * (r - 40), r + (float) Math.sin(d) * (r - 40));
}
mWay.closed = true;
mWay.layer = 1;
mWay.tags = mTagsBoundary;
mapDatabaseCallback.renderWay(mWay);
e.set(mTagsBoundary, 1, GEOM_LINE);
mapDatabaseCallback.renderElement(e);
e.clear();
e.startPoints();
e.addPoint(size/2, size/2);
g.clear();
g.startPoints();
g.addPoint(size/2, size/2);
mTagsPlace[1] = new Tag("name", tile.toString());
mapDatabaseCallback.renderPOI((byte)0, mTagsPlace, g);
e.set(mTagsPlace, 0, GEOM_POINT);
mapDatabaseCallback.renderElement(e);
return QueryResult.SUCCESS;
}