refactor: split S3DB Layer/TileLoader

This commit is contained in:
Hannes Janetzek 2014-03-08 03:09:49 +01:00
parent aeff737250
commit f51fb210b8
2 changed files with 118 additions and 116 deletions

View File

@ -1,23 +1,12 @@
package org.oscim.layers.tile.s3db;
import java.util.concurrent.CancellationException;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Color;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.MercatorProjection;
import org.oscim.layers.tile.MapTile;
import org.oscim.layers.tile.TileLayer;
import org.oscim.layers.tile.TileLoader;
import org.oscim.layers.tile.TileManager;
import org.oscim.layers.tile.TileRenderer;
import org.oscim.map.Map;
import org.oscim.renderer.ExtrusionRenderer;
import org.oscim.renderer.GLViewport;
import org.oscim.renderer.elements.ElementLayers;
import org.oscim.renderer.elements.ExtrusionLayer;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.TileSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -40,8 +29,8 @@ public class S3DBLayer extends TileLayer {
}
@Override
protected S3DBLoader createLoader() {
return new S3DBLoader(getManager());
protected S3DBTileLoader createLoader() {
return new S3DBTileLoader(getManager(), mTileSource);
}
static class S3DBRenderer extends TileRenderer {
@ -64,112 +53,10 @@ public class S3DBLayer extends TileLayer {
}
}
class S3DBLoader extends TileLoader {
private MapTile mTile;
/** current TileDataSource used by this MapTileLoader */
private ITileDataSource mTileDataSource;
private ExtrusionLayer mLayers;
private ExtrusionLayer mRoofs;
private float mGroundScale;
public S3DBLoader(TileManager tileManager) {
super(tileManager);
mTileDataSource = mTileSource.getDataSource();
}
@Override
public void cleanup() {
mTileDataSource.destroy();
}
@Override
protected boolean loadTile(MapTile tile) {
mTile = tile;
double lat = MercatorProjection.toLatitude(tile.y);
mGroundScale = (float) MercatorProjection
.groundResolution(lat, 1 << mTile.zoomLevel);
mLayers = new ExtrusionLayer(0, mGroundScale, Color.get(255, 255, 250));
mRoofs = new ExtrusionLayer(0, mGroundScale, Color.get(218, 220, 220));
mLayers.next = mRoofs;
ElementLayers layers = new ElementLayers();
layers.setExtrusionLayers(mLayers);
tile.data = layers;
try {
/* query database, which calls process() callback */
mTileDataSource.query(mTile, this);
} catch (CancellationException e) {
log.debug("{}", e);
return false;
} catch (Exception e) {
log.debug("{}", e);
return false;
}
return true;
}
String COLOR_KEY = "c";
String ROOF_KEY = "roof";
@Override
public void process(MapElement element) {
//log.debug("TAG {}", element.tags);
if (element.type != GeometryType.TRIS) {
log.debug("wrong type " + element.type);
return;
}
boolean isRoof = element.tags.containsKey(ROOF_KEY);
int c = 0;
if (element.tags.containsKey(COLOR_KEY)) {
c = getColor(element.tags.getValue(COLOR_KEY), isRoof);
}
if (c == 0) {
if (isRoof)
mRoofs.add(element);
else
mLayers.add(element);
return;
}
for (ExtrusionLayer l = mLayers; l != null; l = (ExtrusionLayer) l.next) {
if (l.color == c) {
l.add(element);
return;
}
}
ExtrusionLayer l = new ExtrusionLayer(0, mGroundScale, c);
l.next = mRoofs.next;
mRoofs.next = l;
l.add(element);
}
@Override
public void completed(QueryResult result) {
super.completed(result);
}
@Override
public void setTileImage(Bitmap bitmap) {
}
}
/**
* Colors from OSM2World, except roof-red
*/
private int getColor(String color, boolean roof) {
static int getColor(String color, boolean roof) {
try {
return Color.parseColor(color);

View File

@ -0,0 +1,115 @@
package org.oscim.layers.tile.s3db;
import java.util.concurrent.CancellationException;
import org.oscim.backend.canvas.Color;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.MercatorProjection;
import org.oscim.layers.tile.MapTile;
import org.oscim.layers.tile.TileLoader;
import org.oscim.layers.tile.TileManager;
import org.oscim.renderer.elements.ElementLayers;
import org.oscim.renderer.elements.ExtrusionLayer;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.TileSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class S3DBTileLoader extends TileLoader {
static final Logger log = LoggerFactory.getLogger(S3DBTileLoader.class);
/** current TileDataSource used by this MapTileLoader */
private ITileDataSource mTileDataSource;
private ExtrusionLayer mLayers;
private ExtrusionLayer mRoofs;
private float mGroundScale;
public S3DBTileLoader(TileManager tileManager, TileSource tileSource) {
super(tileManager);
mTileDataSource = tileSource.getDataSource();
}
@Override
public void cleanup() {
mTileDataSource.destroy();
}
@Override
protected boolean loadTile(MapTile tile) {
mTile = tile;
double lat = MercatorProjection.toLatitude(tile.y);
mGroundScale = (float) MercatorProjection
.groundResolution(lat, 1 << mTile.zoomLevel);
mLayers = new ExtrusionLayer(0, mGroundScale, Color.get(255, 255, 250));
mRoofs = new ExtrusionLayer(0, mGroundScale, Color.get(218, 220, 220));
mLayers.next = mRoofs;
ElementLayers layers = new ElementLayers();
layers.setExtrusionLayers(mLayers);
tile.data = layers;
try {
/* query database, which calls process() callback */
mTileDataSource.query(mTile, this);
} catch (CancellationException e) {
log.debug("{}", e);
return false;
} catch (Exception e) {
log.debug("{}", e);
return false;
}
return true;
}
String COLOR_KEY = "c";
String ROOF_KEY = "roof";
@Override
public void process(MapElement element) {
//log.debug("TAG {}", element.tags);
if (element.type != GeometryType.TRIS) {
log.debug("wrong type " + element.type);
return;
}
boolean isRoof = element.tags.containsKey(ROOF_KEY);
int c = 0;
if (element.tags.containsKey(COLOR_KEY)) {
c = S3DBLayer.getColor(element.tags.getValue(COLOR_KEY), isRoof);
}
if (c == 0) {
if (isRoof)
mRoofs.add(element);
else
mLayers.add(element);
return;
}
for (ExtrusionLayer l = mLayers; l != null; l = (ExtrusionLayer) l.next) {
if (l.color == c) {
l.add(element);
return;
}
}
ExtrusionLayer l = new ExtrusionLayer(0, mGroundScale, c);
l.next = mRoofs.next;
mRoofs.next = l;
l.add(element);
}
@Override
public void completed(QueryResult result) {
mLayers = null;
mRoofs = null;
super.completed(result);
}
}