Skip to content

Commit

Permalink
Label layer: use ZoomLimiter for overzoom (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 authored and devemux86 committed Jun 24, 2018
1 parent c8068d5 commit 9e52e9e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
32 changes: 29 additions & 3 deletions vtm/src/org/oscim/layers/tile/vector/labeling/LabelLayer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright 2012, 2013 Hannes Janetzek
* Copyright 2017 Wolfgang Schramm
* Copyright 2017 devemux86
* Copyright 2017-2018 devemux86
* Copyright 2017 Andrey Novikov
* Copyright 2018 Gustl22
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
Expand All @@ -24,33 +25,48 @@
import org.oscim.layers.Layer;
import org.oscim.layers.tile.MapTile;
import org.oscim.layers.tile.TileManager;
import org.oscim.layers.tile.ZoomLimiter;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.map.Map;
import org.oscim.map.Viewport;
import org.oscim.utils.async.SimpleWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LabelLayer extends Layer implements Map.UpdateListener, TileManager.Listener {
public class LabelLayer extends Layer implements Map.UpdateListener, TileManager.Listener,
ZoomLimiter.IZoomLimiter {

static final Logger log = LoggerFactory.getLogger(LabelLayer.class);

static final String LABEL_DATA = LabelLayer.class.getName();

private static final long MAX_RELABEL_DELAY = 100;

// Sane default to allow render themes to work
private static final int ZOOM_LIMIT = Viewport.MAX_ZOOM_LEVEL;

private final LabelPlacement mLabelPlacer;
private final Worker mWorker;
private final ZoomLimiter mZoomLimiter;

public LabelLayer(Map map, VectorTileLayer l) {
this(map, l, new LabelTileLoaderHook());
}

public LabelLayer(Map map, VectorTileLayer l, VectorTileLayer.TileLoaderThemeHook h) {
this(map, l, h, ZOOM_LIMIT);
}

public LabelLayer(Map map, VectorTileLayer l, VectorTileLayer.TileLoaderThemeHook h,
int zoomLimit) {
super(map);
l.getManager().events.bind(this);
l.addHook(h);

mLabelPlacer = new LabelPlacement(map, l.tileRenderer());
mZoomLimiter = new ZoomLimiter(l.getManager(), map.viewport().getMinZoomLevel(),
map.viewport().getMaxZoomLevel(), zoomLimit);

mLabelPlacer = new LabelPlacement(map, l.tileRenderer(), mZoomLimiter);
mWorker = new Worker(map);
mRenderer = new TextRenderer(mWorker);
}
Expand Down Expand Up @@ -86,6 +102,16 @@ public synchronized boolean isRunning() {
}
}

@Override
public void addZoomLimit() {
mZoomLimiter.addZoomLimit();
}

@Override
public void removeZoomLimit() {
mZoomLimiter.removeZoomLimit();
}

public void clearLabels() {
mWorker.cancel(true);
}
Expand Down
53 changes: 46 additions & 7 deletions vtm/src/org/oscim/layers/tile/vector/labeling/LabelPlacement.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2018 Gustl22
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
Expand All @@ -21,7 +23,9 @@
import org.oscim.layers.tile.MapTile;
import org.oscim.layers.tile.TileRenderer;
import org.oscim.layers.tile.TileSet;
import org.oscim.layers.tile.ZoomLimiter;
import org.oscim.map.Map;
import org.oscim.map.Viewport;
import org.oscim.renderer.bucket.SymbolBucket;
import org.oscim.renderer.bucket.SymbolItem;
import org.oscim.renderer.bucket.TextItem;
Expand All @@ -31,6 +35,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

import static org.oscim.layers.tile.MapTile.State.NEW_DATA;
import static org.oscim.layers.tile.MapTile.State.READY;

Expand All @@ -53,6 +60,7 @@ public final static LabelTileData getLabels(MapTile tile) {
private final TileSet mTileSet = new TileSet();
private final TileRenderer mTileRenderer;
private final Map mMap;
private final ZoomLimiter mZoomLimiter;

/**
* list of current labels
Expand All @@ -67,9 +75,13 @@ public final static LabelTileData getLabels(MapTile tile) {
*/
private int mRelabelCnt;

public LabelPlacement(Map map, TileRenderer tileRenderer) {
/* Zoom level of current tiles (initial value doesn't matter) */
private Integer mZoom = Viewport.MIN_ZOOM_LEVEL;

public LabelPlacement(Map map, TileRenderer tileRenderer, ZoomLimiter zoomLimiter) {
mMap = map;
mTileRenderer = tileRenderer;
mZoomLimiter = zoomLimiter;
}

/**
Expand Down Expand Up @@ -294,7 +306,13 @@ private Label addNodeLabels(MapTile t, Label l, float dx, float dy,
boolean updateLabels(LabelTask work) {

/* get current tiles */
boolean changedTiles = mTileRenderer.getVisibleTiles(mTileSet);
int lastZoom = mZoom;
mZoom = mTileRenderer.getVisibleTiles(mTileSet, true);
boolean changedTiles = mZoom != null;

// set to last zoom in case of tiles didn't change
if (!changedTiles)
mZoom = lastZoom;

if (mTileSet.cnt == 0) {
return false;
Expand All @@ -307,10 +325,31 @@ boolean updateLabels(LabelTask work) {
if (!changedTiles && !changedPos)
return false;

if (mZoom < mZoomLimiter.getMinZoom() || mZoom > mZoomLimiter.getMaxZoom())
return false;

mRelabelCnt++;

MapTile[] tiles = mTileSet.tiles;
int zoom = tiles[0].zoomLevel;
int cnt = mTileSet.cnt;
MapTile[] tiles;
int zoom;
if (mZoom > mZoomLimiter.getZoomLimit()) {
// render from zoom limit tiles (avoid duplicates and null)
Set<MapTile> hashTiles = new HashSet<>();
for (int i = 0; i < cnt; i++) {
MapTile t = mZoomLimiter.getTile(mTileSet.tiles[i]);
if (t == null)
continue;
hashTiles.add(t);
}

cnt = hashTiles.size();
tiles = hashTiles.toArray(new MapTile[cnt]);
zoom = mZoomLimiter.getZoomLimit();
} else {
tiles = mTileSet.tiles;
zoom = mZoom;
}

/* estimation for visible area to be labeled */
int mw = (mMap.getWidth() + Tile.SIZE) / 2;
Expand Down Expand Up @@ -398,7 +437,7 @@ boolean updateLabels(LabelTask work) {
}

/* add way labels */
for (int i = 0, n = mTileSet.cnt; i < n; i++) {
for (int i = 0; i < cnt; i++) {
MapTile t = tiles[i];
if (!t.state(READY | NEW_DATA))
continue;
Expand All @@ -411,7 +450,7 @@ boolean updateLabels(LabelTask work) {
}

/* add caption */
for (int i = 0, n = mTileSet.cnt; i < n; i++) {
for (int i = 0; i < cnt; i++) {
MapTile t = tiles[i];
if (!t.state(READY | NEW_DATA))
continue;
Expand Down Expand Up @@ -453,7 +492,7 @@ boolean updateLabels(LabelTask work) {
}

/* add symbol items */
for (int i = 0, n = mTileSet.cnt; i < n; i++) {
for (int i = 0; i < cnt; i++) {
MapTile t = tiles[i];
if (!t.state(READY | NEW_DATA))
continue;
Expand Down

0 comments on commit 9e52e9e

Please sign in to comment.