metatile cache added

This commit is contained in:
simon 2020-09-09 13:28:14 +03:00
parent 2851fd38cd
commit ce2b0eacbf
3 changed files with 29 additions and 19 deletions

View file

@ -58,7 +58,11 @@ public class OsmAndHttpServer extends NanoHTTPD {
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry<String, ApiEndpoint> e = it.next(); Map.Entry<String, ApiEndpoint> e = it.next();
if (uri.startsWith(e.getKey())) { if (uri.startsWith(e.getKey())) {
try {
return e.getValue().process(session, uri); return e.getValue().process(session, uri);
} catch (Exception exception) {
LOG.error("SERVER ERROR: " + exception.getMessage());
}
} }
} }
return ErrorResponses.response404; return ErrorResponses.response404;

View file

@ -85,14 +85,13 @@ public class ServerFragment extends BaseOsmAndFragment {
} }
private void initServer() { private void initServer() {
TrafficStats.setThreadStatsTag(THREAD_ID); TrafficStats.setThreadStatsTag(THREAD_ID);
String hostname = getDeviceAddress(); String hostname = getDeviceAddress();
try { try {
server = new OsmAndHttpServer(hostname, port); server = new OsmAndHttpServer(hostname, port);
server.start((MapActivity) getActivity()); server.start((MapActivity) getActivity());
initialized = true; initialized = true;
updateTextView("Server started at: " + server.getUrl()); updateTextView("Server started at " + server.getUrl());
} catch (IOException e) { } catch (IOException e) {
Toast.makeText(requireContext(), Toast.makeText(requireContext(),
e.getLocalizedMessage(), e.getLocalizedMessage(),

View file

@ -21,7 +21,7 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
private static final int TILE_DENSITY = 2; private static final int TILE_DENSITY = 2;
private static final int TIMEOUT_STEP = 500; private static final int TIMEOUT_STEP = 500;
private static final int TIMEOUT = 5000; private static final int TIMEOUT = 5000;
private static final int METATILE_SIZE = 1; private static final int METATILE_SIZE = 2;
private static final int MAX_CACHE_SIZE = 4; private static final int MAX_CACHE_SIZE = 4;
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class); private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
@ -43,8 +43,10 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
} }
public Bitmap getSubtile(int x, int y) { public Bitmap getSubtile(int x, int y) {
// TODO cut subtitle return Bitmap.createBitmap(bmp,
return bmp; (x - sx) * TILE_SIZE_PX * TILE_DENSITY,
(y - sy) * TILE_SIZE_PX * TILE_DENSITY,
TILE_SIZE_PX * TILE_DENSITY, TILE_SIZE_PX * TILE_DENSITY);
} }
} }
@ -76,19 +78,24 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
int zoom = Integer.parseInt(prms[1]); int zoom = Integer.parseInt(prms[1]);
int x = Integer.parseInt(prms[2]); int x = Integer.parseInt(prms[2]);
int y = Integer.parseInt(prms[3]); int y = Integer.parseInt(prms[3]);
//incorrect condition
// for (MetaTileCache r : cache) { MetaTileCache res = null;
// if (r.zoom == zoom && r.ex >= x && r.ey >= y && r.sx <= x && r.sy <= y) { for (MetaTileCache r : cache) {
// res = r; if (r.zoom == zoom && r.ex >= x && r.ey >= y && r.sx <= x && r.sy <= y) {
// } res = r;
// } }
MetaTileCache res = requestMetatile(x, y, zoom); }
if (res == null) { if (res == null) {
res = requestMetatile(x, y, zoom);
if (res == null) {
LOG.error("SERVER: Cannot request metatile");
return OsmAndHttpServer.ErrorResponses.response500; return OsmAndHttpServer.ErrorResponses.response500;
} }
}
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = res.bmp; Bitmap bmp = res.getSubtile(x, y);
if (bmp == null) { if (bmp == null) {
LOG.error("SERVER: Cannot cut bitmap");
return OsmAndHttpServer.ErrorResponses.response500; return OsmAndHttpServer.ErrorResponses.response500;
} }
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
@ -109,7 +116,8 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
.setLocation(lat, lon) .setLocation(lat, lon)
.setMapDensity(TILE_DENSITY).density(TILE_DENSITY) .setMapDensity(TILE_DENSITY).density(TILE_DENSITY)
.setZoom(zoom) .setZoom(zoom)
.setPixelDimensions(TILE_SIZE_PX * TILE_DENSITY * METATILE_SIZE, TILE_SIZE_PX * TILE_DENSITY * METATILE_SIZE, 0.5f, 0.5f).build(); .setPixelDimensions(TILE_SIZE_PX * TILE_DENSITY * METATILE_SIZE,
TILE_SIZE_PX * TILE_DENSITY * METATILE_SIZE, 0.5f, 0.5f).build();
mapActivity.getMapView().setCurrentViewport(rotatedTileBox); mapActivity.getMapView().setCurrentViewport(rotatedTileBox);
int timeout = 0; int timeout = 0;
try { try {
@ -127,9 +135,8 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
res.sy = my; res.sy = my;
res.ey = my + METATILE_SIZE - 1; res.ey = my + METATILE_SIZE - 1;
res.zoom = zoom; res.zoom = zoom;
RotatedTileBox tilebox = mapActivity.getMapView().getBufferImgLoc(); Bitmap tempBmp = mapActivity.getMapView().getBufferBitmap();
res.bmp = mapActivity.getMapView().getBufferBitmap(); res.bmp = tempBmp.copy(tempBmp.getConfig(), true);
LOG.debug(mapActivity.getMapView().getBufferImgLoc());
addToMemoryCache(res); addToMemoryCache(res);
} }
return res; return res;