simplify UrlTileDataSource initialization
This commit is contained in:
parent
94011804be
commit
6e543303c6
@ -45,7 +45,6 @@ public class BaseMapActivity extends MapActivity {
|
||||
registerMapView(mMapView);
|
||||
|
||||
mTileSource = new OSciMap4TileSource();
|
||||
mTileSource.setOption("url", "http://opensciencemap.org/tiles/vtm");
|
||||
|
||||
if (USE_CACHE) {
|
||||
mCache = new TileCache(this, "cachedir", "testdb");
|
||||
|
@ -57,10 +57,7 @@ public class MainActivity extends AndroidApplication {
|
||||
class GdxMapAndroid extends GdxMap {
|
||||
@Override
|
||||
public void createLayers() {
|
||||
|
||||
TileSource ts = new OSciMap4TileSource();
|
||||
ts.setOption("url", "http://opensciencemap.org/tiles/vtm");
|
||||
|
||||
initDefaultLayers(ts, true, true, true);
|
||||
}
|
||||
}
|
||||
|
@ -19,8 +19,6 @@ package org.oscim.gdx.client;
|
||||
import org.oscim.backend.CanvasAdapter;
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.backend.GLAdapter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.core.MercatorProjection;
|
||||
import org.oscim.gdx.GdxMap;
|
||||
@ -30,6 +28,8 @@ import org.oscim.renderer.MapRenderer;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.oscimap2.OSciMap2TileSource;
|
||||
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.backends.gwt.GwtApplication;
|
||||
@ -108,12 +108,10 @@ class GwtGdxMap extends GdxMap {
|
||||
|
||||
TileSource tileSource;
|
||||
if ("oscimap4".equals(sourceName))
|
||||
tileSource = new OSciMap4TileSource();
|
||||
tileSource = new OSciMap4TileSource(url);
|
||||
else
|
||||
//if ("oscimap2".equals(source))
|
||||
tileSource = new OSciMap2TileSource();
|
||||
|
||||
tileSource.setOption("url", url);
|
||||
tileSource = new OSciMap2TileSource(url);
|
||||
|
||||
initDefaultLayers(tileSource, false, true, true);
|
||||
|
||||
|
@ -18,25 +18,22 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.oscim.tiling.MapTile;
|
||||
import org.oscim.tiling.source.ITileCache;
|
||||
import org.oscim.tiling.source.ITileDataSink;
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.ITileDecoder;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public abstract class UrlTileDataSource implements ITileDataSource {
|
||||
public class UrlTileDataSource implements ITileDataSource {
|
||||
static final Logger log = LoggerFactory.getLogger(UrlTileDataSource.class);
|
||||
|
||||
protected LwHttp mConn;
|
||||
protected final LwHttp mConn;
|
||||
protected final ITileDecoder mTileDecoder;
|
||||
|
||||
public UrlTileDataSource(ITileDecoder tileDecoder, ITileCache cache) {
|
||||
public UrlTileDataSource(TileSource tileSource, ITileDecoder tileDecoder, LwHttp conn) {
|
||||
mTileDecoder = tileDecoder;
|
||||
mConn = conn;
|
||||
}
|
||||
|
||||
private ITileDataSink mSink;
|
||||
|
@ -47,7 +47,7 @@ public class LwHttp {
|
||||
private final static int RESPONSE_EXPECTED_LIVES = 100;
|
||||
private final static long RESPONSE_TIMEOUT = (long) 10E9; // 10 second in nanosecond
|
||||
|
||||
private final static int BUFFER_SIZE = 4096;
|
||||
private final static int BUFFER_SIZE = 8192;
|
||||
private final byte[] buffer = new byte[BUFFER_SIZE];
|
||||
|
||||
private final String mHost;
|
||||
@ -92,7 +92,7 @@ public class LwHttp {
|
||||
|
||||
REQUEST_GET_START = ("GET " + path).getBytes();
|
||||
|
||||
REQUEST_GET_END = ("." + extension + " HTTP/1.1" +
|
||||
REQUEST_GET_END = (extension + " HTTP/1.1" +
|
||||
"\nHost: " + host +
|
||||
"\nConnection: Keep-Alive" +
|
||||
"\n\n").getBytes();
|
||||
@ -162,6 +162,10 @@ public class LwHttp {
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
public byte[] getArray() {
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
@ -292,18 +296,8 @@ public class LwHttp {
|
||||
|
||||
byte[] request = mRequestBuffer;
|
||||
int pos = REQUEST_GET_START.length;
|
||||
int newPos = 0;
|
||||
|
||||
if ((newPos = formatTilePath(tile, request, pos)) == 0) {
|
||||
request[pos++] = '/';
|
||||
pos = writeInt(tile.zoomLevel, pos, request);
|
||||
request[pos++] = '/';
|
||||
pos = writeInt(tile.tileX, pos, request);
|
||||
request[pos++] = '/';
|
||||
pos = writeInt(tile.tileY, pos, request);
|
||||
} else {
|
||||
pos = newPos;
|
||||
}
|
||||
pos = formatTilePath(tile, request, pos);
|
||||
|
||||
int len = REQUEST_GET_END.length;
|
||||
System.arraycopy(REQUEST_GET_END, 0, request, pos, len);
|
||||
@ -402,8 +396,14 @@ public class LwHttp {
|
||||
* @param curPos current position
|
||||
* @return new position
|
||||
*/
|
||||
protected int formatTilePath(Tile tile, byte[] path, int curPos) {
|
||||
return 0;
|
||||
protected int formatTilePath(Tile tile, byte[] request, int pos) {
|
||||
request[pos++] = '/';
|
||||
pos = writeInt(tile.zoomLevel, pos, request);
|
||||
request[pos++] = '/';
|
||||
pos = writeInt(tile.tileX, pos, request);
|
||||
request[pos++] = '/';
|
||||
pos = writeInt(tile.tileY, pos, request);
|
||||
return pos;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,20 +27,22 @@ import org.oscim.tiling.source.ITileCache;
|
||||
import org.oscim.tiling.source.ITileDataSink;
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.ITileDecoder;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.utils.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public abstract class UrlTileDataSource implements ITileDataSource {
|
||||
public class UrlTileDataSource implements ITileDataSource {
|
||||
static final Logger log = LoggerFactory.getLogger(UrlTileDataSource.class);
|
||||
|
||||
protected LwHttp mConn;
|
||||
protected final LwHttp mConn;
|
||||
protected final ITileDecoder mTileDecoder;
|
||||
protected final ITileCache mTileCache;
|
||||
|
||||
public UrlTileDataSource(ITileDecoder tileDecoder, ITileCache tileCache) {
|
||||
public UrlTileDataSource(TileSource tileSource, ITileDecoder tileDecoder, LwHttp conn) {
|
||||
mTileDecoder = tileDecoder;
|
||||
mTileCache = tileCache;
|
||||
mTileCache = tileSource.tileCache;
|
||||
mConn = conn;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,22 +22,21 @@ import java.net.URL;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
|
||||
public abstract class UrlTileSource extends TileSource {
|
||||
private final static String KEY_URL = "url";
|
||||
|
||||
protected URL mUrl;
|
||||
protected final URL mUrl;
|
||||
|
||||
public UrlTileSource(String urlString) {
|
||||
URL url = null;
|
||||
try {
|
||||
url = new URL(urlString);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mUrl = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpenResult open() {
|
||||
if (!options.containsKey(KEY_URL))
|
||||
return new OpenResult("no url set");
|
||||
String urlString = options.get(KEY_URL);
|
||||
try {
|
||||
mUrl = new URL(urlString);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
return new OpenResult("invalid url " + urlString);
|
||||
}
|
||||
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
@ -45,9 +44,4 @@ public abstract class UrlTileSource extends TileSource {
|
||||
public void close() {
|
||||
|
||||
}
|
||||
|
||||
public boolean setUrl(String urlString) {
|
||||
options.put("url", urlString);
|
||||
return open() == OpenResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
@ -16,50 +16,43 @@
|
||||
*/
|
||||
package org.oscim.tiling.source.mapnik;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.common.LwHttp;
|
||||
import org.oscim.tiling.source.common.UrlTileDataSource;
|
||||
import org.oscim.tiling.source.common.UrlTileSource;
|
||||
|
||||
public class MapnikVectorTileSource extends UrlTileSource {
|
||||
|
||||
public MapnikVectorTileSource() {
|
||||
super("http://d1s11ojcu7opje.cloudfront.net/dev/764e0b8d");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TileDataSource(this, mUrl);
|
||||
}
|
||||
LwHttp conn = new LwHttp(mUrl, "image/png", ".vector.pbf", true) {
|
||||
@Override
|
||||
protected int formatTilePath(Tile tile, byte[] path, int pos) {
|
||||
// url formatter for mapbox streets
|
||||
byte[] hexTable = {
|
||||
'0', '1', '2', '3',
|
||||
'4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b',
|
||||
'c', 'd', 'e', 'f'
|
||||
};
|
||||
|
||||
static class TileDataSource extends UrlTileDataSource {
|
||||
|
||||
public TileDataSource(TileSource tileSource, URL url) {
|
||||
super(new TileDecoder(), tileSource.tileCache);
|
||||
|
||||
mConn = new LwHttp(url, "image/png", "vector.pbf", true) {
|
||||
@Override
|
||||
protected int formatTilePath(Tile tile, byte[] path, int pos) {
|
||||
// url formatter for mapbox streets
|
||||
byte[] hexTable = {
|
||||
'0', '1', '2', '3',
|
||||
'4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b',
|
||||
'c', 'd', 'e', 'f'
|
||||
};
|
||||
|
||||
path[pos++] = '/';
|
||||
path[pos++] = hexTable[(tile.tileX) % 16];
|
||||
path[pos++] = hexTable[(tile.tileY) % 16];
|
||||
path[pos++] = '/';
|
||||
pos = LwHttp.writeInt(tile.zoomLevel, pos, path);
|
||||
path[pos++] = '/';
|
||||
pos = LwHttp.writeInt(tile.tileX, pos, path);
|
||||
path[pos++] = '/';
|
||||
pos = LwHttp.writeInt(tile.tileY, pos, path);
|
||||
return pos;
|
||||
}
|
||||
};
|
||||
}
|
||||
path[pos++] = '/';
|
||||
path[pos++] = hexTable[(tile.tileX) % 16];
|
||||
path[pos++] = hexTable[(tile.tileY) % 16];
|
||||
path[pos++] = '/';
|
||||
pos = LwHttp.writeInt(tile.zoomLevel, pos, path);
|
||||
path[pos++] = '/';
|
||||
pos = LwHttp.writeInt(tile.tileX, pos, path);
|
||||
path[pos++] = '/';
|
||||
pos = LwHttp.writeInt(tile.tileY, pos, path);
|
||||
return pos;
|
||||
}
|
||||
};
|
||||
return new UrlTileDataSource(this, new TileDecoder(), conn);
|
||||
}
|
||||
}
|
||||
|
@ -16,10 +16,7 @@
|
||||
*/
|
||||
package org.oscim.tiling.source.oscimap;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.common.LwHttp;
|
||||
import org.oscim.tiling.source.common.UrlTileDataSource;
|
||||
import org.oscim.tiling.source.common.UrlTileSource;
|
||||
@ -30,15 +27,13 @@ import org.oscim.tiling.source.common.UrlTileSource;
|
||||
*/
|
||||
public class OSciMap1TileSource extends UrlTileSource {
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TileDataSource(this, mUrl);
|
||||
public OSciMap1TileSource(String url) {
|
||||
super(url);
|
||||
}
|
||||
|
||||
class TileDataSource extends UrlTileDataSource {
|
||||
public TileDataSource(TileSource tileSource, URL url) {
|
||||
super(new TileDecoder(), tileSource.tileCache);
|
||||
mConn = new LwHttp(url, "application/osmtile", "osmtile", false);
|
||||
}
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
LwHttp conn = new LwHttp(mUrl, "application/osmtile", ".osmtile", false);
|
||||
return new UrlTileDataSource(this, new TileDecoder(), conn);
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ package org.oscim.tiling.source.oscimap2;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.oscim.core.GeometryBuffer.GeometryType;
|
||||
@ -28,7 +27,6 @@ import org.oscim.core.TagSet;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.tiling.source.ITileDataSink;
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.common.LwHttp;
|
||||
import org.oscim.tiling.source.common.PbfDecoder;
|
||||
import org.oscim.tiling.source.common.UrlTileDataSource;
|
||||
@ -38,16 +36,14 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class OSciMap2TileSource extends UrlTileSource {
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TileDataSource(this, mUrl);
|
||||
public OSciMap2TileSource(String url) {
|
||||
super(url);
|
||||
}
|
||||
|
||||
class TileDataSource extends UrlTileDataSource {
|
||||
public TileDataSource(TileSource tileSource, URL url) {
|
||||
super(new TileDecoder(), tileSource.tileCache);
|
||||
mConn = new LwHttp(url, "application/osmtile", "osmtile", false);
|
||||
}
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
LwHttp conn = new LwHttp(mUrl, "application/osmtile", ".osmtile", false);
|
||||
return new UrlTileDataSource(this, new TileDecoder(), conn);
|
||||
}
|
||||
|
||||
static class TileDecoder extends PbfDecoder {
|
||||
|
@ -16,26 +16,25 @@
|
||||
*/
|
||||
package org.oscim.tiling.source.oscimap4;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.common.LwHttp;
|
||||
import org.oscim.tiling.source.common.UrlTileDataSource;
|
||||
import org.oscim.tiling.source.common.UrlTileSource;
|
||||
|
||||
public class OSciMap4TileSource extends UrlTileSource {
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TileDataSource(this, mUrl);
|
||||
public OSciMap4TileSource() {
|
||||
super("http://opensciencemap.org/tiles/vtm");
|
||||
}
|
||||
|
||||
class TileDataSource extends UrlTileDataSource {
|
||||
public TileDataSource(TileSource tileSource, URL url) {
|
||||
super(new TileDecoder(), tileSource.tileCache);
|
||||
//mConn = new LwHttp(url, "application/x-protobuf", "vtm", false);
|
||||
mConn = new LwHttp(url, "image/png", "vtm", false);
|
||||
}
|
||||
public OSciMap4TileSource(String url) {
|
||||
super(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
//LwHttp conn = new LwHttp(url, "application/x-protobuf", ".vtm", false);
|
||||
LwHttp conn = new LwHttp(mUrl, "image/png", ".vtm", false);
|
||||
return new UrlTileDataSource(this, new TileDecoder(), conn);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user