[Mapproxy-java] SF.net SVN: mapproxy:[31] tms
Brought to you by:
pborissow
From: <pbo...@us...> - 2011-05-16 11:59:20
|
Revision: 31 http://mapproxy.svn.sourceforge.net/mapproxy/?rev=31&view=rev Author: pborissow Date: 2011-05-16 11:59:14 +0000 (Mon, 16 May 2011) Log Message: ----------- - Fixed bug in Layer when format was jpg - Added TileServer class - Updated Capabilities and ServiceResponse classes to use the new TileServer class Modified Paths: -------------- core/Capabilities.java core/Layer.java core/ServiceResponse.java kml/server/KMLServer.java Added Paths: ----------- tms/TileServer.java Modified: core/Capabilities.java =================================================================== --- core/Capabilities.java 2011-05-15 16:25:34 UTC (rev 30) +++ core/Capabilities.java 2011-05-16 11:59:14 UTC (rev 31) @@ -178,26 +178,7 @@ /** Returns an XML document suitable for a TileMapService request. */ private String TMS(){ - - String url = service.getURL(); - - StringBuffer xml = new StringBuffer(); - xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); - xml.append("<TileMapService version=\"1.0.0\">"); - xml.append("<Title>" + service.getTitle() + "</Title>"); - xml.append("<Abstract>" + service.getDescription() + "</Abstract>"); - xml.append("<TileMaps>"); - for (Layer layer : service.getLayers()){ - - xml.append( - "<TileMap title=\"" + layer.getName() + "\" " + - "srs=\"" + layer.getSRS()[0] + "\" " + - "profile=\"global-geodetic\" " + - "href=\"" + url + layer.getName() + "/\"/>"); - } - xml.append("</TileMaps>"); - xml.append("</TileMapService>"); - return xml.toString(); + return new mapproxy.tms.TileServer(service).getCapabilities(); } Modified: core/Layer.java =================================================================== --- core/Layer.java 2011-05-15 16:25:34 UTC (rev 30) +++ core/Layer.java 2011-05-16 11:59:14 UTC (rev 31) @@ -259,6 +259,7 @@ //if (format.equalsIgnoreCase("image/PNG24")) format = "jpeg"; //<-- This is crazy! If I return a 24 bit PNG, ArcMap doesn't display anything. If I return a JPEG, works like a champ if (!format.toLowerCase().startsWith("image/")) format = "image/" + format; + if (format.equalsIgnoreCase("image/jpg")) format = "image/jpeg"; if (format.equalsIgnoreCase("image/PNG24") && fileCache!=null) format = "image/png"; //format = "image/png"; req.params.set("format", format); Modified: core/ServiceResponse.java =================================================================== --- core/ServiceResponse.java 2011-05-15 16:25:34 UTC (rev 30) +++ core/ServiceResponse.java 2011-05-16 11:59:14 UTC (rev 31) @@ -312,7 +312,6 @@ //Set local variables String url = this.url.toString(); -System.out.println(url); //Check whether this request is in the http cache mapproxy.tms.TileRequest req = new mapproxy.tms.TileRequest(url); @@ -324,7 +323,11 @@ String format = req.format; contentType = "image/"+format; + //Update content type for jpeg + if (format.equalsIgnoreCase("jpg")) contentType = "image/jpeg"; + + //Find requested layer in the mapproxy service definition Layer layer = service.getLayer(req.layer); FileCache fileCache = (FileCache) layer.getParam("cache"); @@ -564,8 +567,8 @@ private void getKMLCapabilities(boolean getBytes) { key = "KML Capabilities"; - //contentType = "application/vnd.google-earth.kml+xml"; - contentType = "text/xml"; + contentType = "application/vnd.google-earth.kml+xml"; + //contentType = "text/xml"; if (!getBytes) return; @@ -580,8 +583,8 @@ private void getKML(boolean getBytes) { key = url.getPath(); - //contentType = "application/vnd.google-earth.kml+xml"; - contentType = "text/xml"; + contentType = "application/vnd.google-earth.kml+xml"; + //contentType = "text/xml"; if (!getBytes) return; @@ -629,69 +632,32 @@ if (!getBytes) return; + //Update Service URL String url = this.url.toString(); if (url.contains("?")) url = url.substring(0, url.indexOf("?")); if (!url.endsWith("/")) url += "/"; - - TileGrid grid = (TileGrid) layer.getParam("grid"); - if (grid!=null){ - grid = new TileGrid(4326, new int[]{256,256}); + String lyr = url.substring(url.substring(0, url.length()-1).lastIndexOf("/")+1); + if (lyr.endsWith("/")) lyr = lyr.substring(0, lyr.length()-1); + if (java.net.URLDecoder.decode(lyr).equalsIgnoreCase(layerName)){ + url = url.substring(0, url.lastIndexOf(lyr)); } - int[] tileSize = grid.tile_size; - int tileWidth = tileSize[0]; - int tileHeight = tileSize[1]; - String tileFormat = "png"; - FileCache fileCache = (FileCache) layer.getParam("cache"); - if (fileCache!=null){ - tileFormat = fileCache.file_ext; - } - - String mimeType = "image/"+tileFormat.toLowerCase(); - if (tileFormat.equalsIgnoreCase("jpg")) mimeType = "image/jpeg"; - - //Get Capabilities - StringBuffer xml = new StringBuffer(); - xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); - xml.append("<TileMap version=\"1.0.0\" tilemapservice=\"" + url + "1.0.0/\">"); - xml.append("<Title>" + layerName + "</Title>"); - xml.append("<Abstract>" + layer.getDescription() + "</Abstract>"); - xml.append("<SRS>EPSG:4326</SRS>"); - xml.append("<BoundingBox minx=\"-180.000000\" miny=\"-90.000000\" maxx=\"180.000000\" maxy=\"90.000000\" />"); - xml.append("<Origin x=\"-180.000000\" y=\"-90.000000\" />"); - xml.append("<TileFormat width=\"" + tileWidth + "\" height=\"" + tileHeight + "\" mime-type=\"" + mimeType + "\" extension=\"" + tileFormat + "\" />"); - xml.append("<TileSets>"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/0\" units-per-pixel=\"0.70312500000000000000\" order=\"0\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/1\" units-per-pixel=\"0.35156250000000000000\" order=\"1\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/2\" units-per-pixel=\"0.17578125000000000000\" order=\"2\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/3\" units-per-pixel=\"0.08789062500000000000\" order=\"3\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/4\" units-per-pixel=\"0.04394531250000000000\" order=\"4\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/5\" units-per-pixel=\"0.02197265625000000000\" order=\"5\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/6\" units-per-pixel=\"0.01098632812500000000\" order=\"6\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/7\" units-per-pixel=\"0.00549316406250000000\" order=\"7\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/8\" units-per-pixel=\"0.00274658203125000000\" order=\"8\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/9\" units-per-pixel=\"0.00137329101562500000\" order=\"9\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/10\" units-per-pixel=\"0.00068664550781250000\" order=\"10\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/11\" units-per-pixel=\"0.00034332275390625000\" order=\"11\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/12\" units-per-pixel=\"0.00017166137695312500\" order=\"12\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/13\" units-per-pixel=\"0.00008583068847656250\" order=\"13\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/14\" units-per-pixel=\"0.00004291534423828125\" order=\"14\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/15\" units-per-pixel=\"0.00002145767211914062\" order=\"15\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/16\" units-per-pixel=\"0.00001072883605957031\" order=\"16\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/17\" units-per-pixel=\"0.00000536441802978516\" order=\"17\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/18\" units-per-pixel=\"0.00000268220901489258\" order=\"18\" />"); - xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/19\" units-per-pixel=\"0.00000134110450744629\" order=\"19\" />"); - xml.append("</TileSets>"); - xml.append("</TileMap>"); + String orgURL = service.getURL(); + service.setURL(url); + rsp = getBytes(new mapproxy.tms.TileServer(service).getLayerInfo(layer)); + service.setURL(orgURL); + } + + private byte[] getBytes(String str){ try{ - rsp = xml.toString().getBytes("UTF-8"); + return str.getBytes("UTF-8"); } catch(Exception e){ - rsp = xml.toString().getBytes(); + return str.getBytes(); } } Modified: kml/server/KMLServer.java =================================================================== --- kml/server/KMLServer.java 2011-05-15 16:25:34 UTC (rev 30) +++ kml/server/KMLServer.java 2011-05-16 11:59:14 UTC (rev 31) @@ -104,15 +104,18 @@ tile_coord[2] = tile_coord[2]+1; //<-- This wasn't in the original mapproxy code. //Note that this tile_coord and the subtile coord is later updated in the kml + + TileGrid grid = (TileGrid) layer.getParam("grid"); + if (grid==null) grid = new TileGrid(4326, new int[]{256,256}); boolean initial_level = false; if (tile_coord[2] == 0) initial_level = true; - double[] bbox = _tile_bbox(tile_coord, (TileGrid) layer.getParam("grid")); + double[] bbox = _tile_bbox(tile_coord, grid); SubTile tile = new SubTile(tile_coord, bbox); - SubTile[] subtiles = _get_subtiles(tile_coord, layer); + SubTile[] subtiles = _get_subtiles(tile_coord, grid); String format = "jpg"; @@ -210,7 +213,7 @@ //************************************************************************** /* Create four `SubTile` for the next level of `tile`. */ - private SubTile[] _get_subtiles(int[] tile, Layer layer){ + private SubTile[] _get_subtiles(int[] tile, TileGrid grid){ //Layer layer //x, y, z = tile int x = tile[0]; @@ -220,7 +223,7 @@ java.util.ArrayList<SubTile> subtiles = new java.util.ArrayList<SubTile>(); for (int[] coord : new int[][]{new int[]{x*2, y*2, z+1}, new int[]{x*2+1, y*2, z+1}, new int[]{x*2+1, y*2+1, z+1}, new int[]{x*2, y*2+1, z+1}}){ - double[] bbox = _tile_bbox(coord, (TileGrid) layer.getParam("grid")); + double[] bbox = _tile_bbox(coord, grid); if (bbox != null) subtiles.add(new SubTile(coord, bbox)); } Added: tms/TileServer.java =================================================================== --- tms/TileServer.java (rev 0) +++ tms/TileServer.java 2011-05-16 11:59:14 UTC (rev 31) @@ -0,0 +1,187 @@ +package mapproxy.tms; +import mapproxy.core.*; +import mapproxy.core.cache.FileCache; +import mapproxy.core.grid.TileGrid; + +//****************************************************************************** +//** TileServer Class +//****************************************************************************** +/** + * A Tile Server. Supports strict TMS and non-TMS requests. The difference is + * the support for profiles. The our internal tile cache starts with one tile + * at the first level (like KML, etc.), but the global-geodetic and + * global-mercator start with two and four tiles. The ``tile_request`` should + * set ``use_profiles`` accordingly (eg. False if first level is one tile) + * + ******************************************************************************/ + +public class TileServer { + + private Service service; + + + //************************************************************************** + //** Constructor + //************************************************************************** + /** Creates a new instance of TileServer. */ + + public TileServer(Service service){ + this.service = service; + } + + //************************************************************************** + //** TMS Capabilities + //************************************************************************** + /** Returns an XML document suitable for a TileMapService request. */ + + public String getCapabilities(){ + + String url = service.getURL(); + + StringBuffer xml = new StringBuffer(); + xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); + xml.append("<TileMapService version=\"1.0.0\">"); + xml.append("<Title>" + service.getTitle() + "</Title>"); + xml.append("<Abstract>" + service.getDescription() + "</Abstract>"); + xml.append("<TileMaps>"); + for (Layer layer : service.getLayers()){ + + xml.append( + "<TileMap title=\"" + layer.getName() + "\" " + + "srs=\"" + layer.getSRS()[0] + "\" " + //layer.grid.srs_name + "profile=\"global-geodetic\" " + //layer.grid.profile + "href=\"" + url + layer.getName() + "/\"/>"); + } + xml.append("</TileMaps>"); + xml.append("</TileMapService>"); + return xml.toString(); + } + + + //************************************************************************** + //** getLayerInfo + //************************************************************************** + /** Used to construct a response to a TMS Capabilities request for a given + * layer. + */ + public String getLayerInfo(Layer layer) { + + String layerName = layer.getName(); + + + String url = service.getURL(); + if (url.contains("?")) url = url.substring(0, url.indexOf("?")); + if (!url.endsWith("/")) url += "/"; + + + url += layerName + "/"; + + + TileGrid grid = (TileGrid) layer.getParam("grid"); + if (grid!=null){ + grid = new TileGrid(4326, new double[]{-180,-90,180,90}, new int[]{256,256}, null, true, 21); + } + + int[] tileSize = grid.tile_size; + int tileWidth = tileSize[0]; + int tileHeight = tileSize[1]; + + String tileFormat = "png"; + FileCache fileCache = (FileCache) layer.getParam("cache"); + if (fileCache!=null){ + tileFormat = fileCache.file_ext; + } + + String mimeType = "image/"+tileFormat.toLowerCase(); + if (tileFormat.equalsIgnoreCase("jpg")) mimeType = "image/jpeg"; + + + //Get Capabilities + StringBuffer xml = new StringBuffer(); + xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); + xml.append("<TileMap version=\"1.0.0\" tilemapservice=\"" + url + "1.0.0/\">"); + xml.append("<Title>" + layerName + "</Title>"); + xml.append("<Abstract>" + layer.getDescription() + "</Abstract>"); + xml.append("<SRS>EPSG:4326</SRS>"); + xml.append("<BoundingBox minx=\"-180.000000\" miny=\"-90.000000\" maxx=\"180.000000\" maxy=\"90.000000\" />"); + xml.append("<Origin x=\"-180.000000\" y=\"-90.000000\" />"); + xml.append("<TileFormat width=\"" + tileWidth + "\" height=\"" + tileHeight + "\" mime-type=\"" + mimeType + "\" extension=\"" + tileFormat + "\" />"); + xml.append("<TileSets>"); + + for (int i=0; i<grid.levels; i++){ + if (i>0){ + int level = i-1; + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/" + level + "\" units-per-pixel=\"" + formatNum(grid.resolution(i)) + "\" order=\"" + level + "\" />"); + } + } + + /* + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/0\" units-per-pixel=\"0.70312500000000000000\" order=\"0\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/1\" units-per-pixel=\"0.35156250000000000000\" order=\"1\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/2\" units-per-pixel=\"0.17578125000000000000\" order=\"2\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/3\" units-per-pixel=\"0.08789062500000000000\" order=\"3\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/4\" units-per-pixel=\"0.04394531250000000000\" order=\"4\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/5\" units-per-pixel=\"0.02197265625000000000\" order=\"5\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/6\" units-per-pixel=\"0.01098632812500000000\" order=\"6\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/7\" units-per-pixel=\"0.00549316406250000000\" order=\"7\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/8\" units-per-pixel=\"0.00274658203125000000\" order=\"8\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/9\" units-per-pixel=\"0.00137329101562500000\" order=\"9\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/10\" units-per-pixel=\"0.00068664550781250000\" order=\"10\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/11\" units-per-pixel=\"0.00034332275390625000\" order=\"11\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/12\" units-per-pixel=\"0.00017166137695312500\" order=\"12\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/13\" units-per-pixel=\"0.00008583068847656250\" order=\"13\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/14\" units-per-pixel=\"0.00004291534423828125\" order=\"14\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/15\" units-per-pixel=\"0.00002145767211914062\" order=\"15\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/16\" units-per-pixel=\"0.00001072883605957031\" order=\"16\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/17\" units-per-pixel=\"0.00000536441802978516\" order=\"17\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/18\" units-per-pixel=\"0.00000268220901489258\" order=\"18\" />"); + xml.append("<TileSet href=\"" + url + "1.0.0/" + layerName + "/19\" units-per-pixel=\"0.00000134110450744629\" order=\"19\" />"); + + */ + xml.append("</TileSets>"); + xml.append("</TileMap>"); + + return xml.toString(); + + } + + + private static final int MAX_LENGTH = ("0.70312500000000000000".length()); + + private static String formatNum(double number) { + int digitsAvailable = MAX_LENGTH - 2; + if (Math.abs(number) < Math.pow(10, digitsAvailable) + && Math.abs(number) > Math.pow(10, -digitsAvailable)) { + String format = "0."; + double temp = number; + for (int i = 0; i < digitsAvailable; i++) { + if ((temp /= 10) < 1) { + format += "#"; + } + } + String r = new java.text.DecimalFormat(format).format(number); + while (r.length() < MAX_LENGTH){ + r+= "0"; + } + return r; + } + String format = "0."; + for (int i = 0; i < digitsAvailable; i++) { + format += "#"; + } + String r = new java.text.DecimalFormat(format + "E0").format(number); + int lastLength = r.length() + 1; + while (r.length() > MAX_LENGTH && lastLength > r.length()) { + lastLength = r.length(); + r = r.replaceAll("\\.?[0-9]E", "E"); + } + + while (r.length() < MAX_LENGTH){ + r+= "0"; + } + + return r; + } + + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |