@SuppressWarnings("static-method") @GET @Produces("image/*") @Path("{version}/{raster}/{z}/{x}/{y}.{format}") public Response getTile( @PathParam("version") final String version, @PathParam("raster") String pyramid, @PathParam("z") final Integer z, @PathParam("x") final Integer x, @PathParam("y") final Integer y, @PathParam("format") final String format, @QueryParam("color-scale-name") final String colorScaleName, @QueryParam("color-scale") final String colorScale, @QueryParam("min") final Double min, @QueryParam("max") final Double max, @DefaultValue("1") @QueryParam("maskMax") final Double maskMax, @QueryParam("mask") final String mask) { final ImageRenderer renderer; Raster raster; try { renderer = (ImageRenderer) ImageHandlerFactory.getHandler(format, ImageRenderer.class); // TODO: Need to construct provider properties from the WebRequest using // a new security layer and pass those properties. // Apply mask if requested ProviderProperties providerProperties = SecurityUtils.getProviderProperties(); if (mask != null && !mask.isEmpty()) { raster = renderer.renderImage(pyramid, x, y, z, mask, maskMax, providerProperties); } else { raster = renderer.renderImage(pyramid, x, y, z, providerProperties); } if (!(renderer instanceof TiffImageRenderer) && raster.getNumBands() != 3 && raster.getNumBands() != 4) { ColorScale cs = null; if (colorScaleName != null) { cs = ColorScaleManager.fromName(colorScaleName, props); } else if (colorScale != null) { cs = ColorScaleManager.fromJSON(colorScale); } // else // { // cs = ColorScaleManager.fromPyramid(pyramid, driver); // } final double[] extrema = renderer.getExtrema(); // Check for min/max override values from the request if (min != null) { extrema[0] = min; } if (max != null) { extrema[1] = max; } raster = ((ColorScaleApplier) ImageHandlerFactory.getHandler(format, ColorScaleApplier.class)) .applyColorScale(raster, cs, extrema, renderer.getDefaultValues()); } // Apply mask if requested // if (mask != null && !mask.isEmpty()) // { // try // { // final MrsImagePyramidMetadata maskMetadata = service.getMetadata(mask); // // final Raster maskRaster = renderer.renderImage(mask, x, y, z, props, driver); // final WritableRaster wr = RasterUtils.makeRasterWritable(raster); // // final int band = 0; // final double nodata = maskMetadata.getDefaultValue(band); // // for (int w = 0; w < maskRaster.getWidth(); w++) // { // for (int h = 0; h < maskRaster.getHeight(); h++) // { // final double maskPixel = maskRaster.getSampleDouble(w, h, band); // if (maskPixel > maskMax || Double.compare(maskPixel, nodata) == 0) // { // wr.setSample(w, h, band, nodata); // } // } // } // } // catch (final TileNotFoundException ex) // { // raster = RasterUtils.createEmptyRaster(raster.getWidth(), raster.getHeight(), // raster // .getNumBands(), raster.getTransferType(), 0); // } // } return ((ImageResponseWriter) ImageHandlerFactory.getHandler(format, ImageResponseWriter.class)) .write(raster, renderer.getDefaultValues()) .build(); } catch (final IllegalArgumentException e) { return Response.status(Status.BAD_REQUEST) .entity("Unsupported image format - " + format) .build(); } catch (final IOException e) { return Response.status(Status.NOT_FOUND).entity("Tile map not found - " + pyramid).build(); } catch (final MrsImageException e) { return Response.status(Status.NOT_FOUND) .entity("Tile map not found - " + pyramid + ": " + z) .build(); } catch (final TileNotFoundException e) { // return Response.status(Status.NOT_FOUND).entity("Tile not found").build(); try { final MrsImagePyramidMetadata metadata = service.getMetadata(pyramid); return createEmptyTile( ((ImageResponseWriter) ImageHandlerFactory.getHandler(format, ImageResponseWriter.class)), metadata.getTilesize(), metadata.getTilesize()); } catch (final Exception e1) { log.error( "Exception occurred creating blank tile " + pyramid + "/" + z + "/" + x + "/" + y + "." + format, e1); } } catch (final ColorScale.BadJSONException e) { return Response.status(Status.NOT_FOUND).entity("Unable to parse color scale JSON").build(); } catch (final ColorScale.BadSourceException e) { return Response.status(Status.NOT_FOUND).entity("Unable to open color scale file").build(); } catch (final ColorScale.BadXMLException e) { return Response.status(Status.NOT_FOUND).entity("Unable to parse color scale XML").build(); } catch (final ColorScale.ColorScaleException e) { return Response.status(Status.NOT_FOUND).entity("Unable to open color scale").build(); } catch (final Exception e) { log.error( "Exception occurred getting tile " + pyramid + "/" + z + "/" + x + "/" + y + "." + format, e); } return Response.status(Status.INTERNAL_SERVER_ERROR).entity(GENERAL_ERROR).build(); }
protected static Document mrsPyramidMetadataToTileMapXml( final String raster, final String url, final MrsImagePyramidMetadata mpm) throws ParserConfigurationException { /* * String tileMap = "<?xml version='1.0' encoding='UTF-8' ?>" + * "<TileMap version='1.0.0' tilemapservice='http://localhost/mrgeo-services/api/tms/1.0.0'>" + * " <Title>AfPk Elevation V2</Title>" + " <Abstract>A test of V2 MrsImagePyramid.</Abstract>" * + " <SRS>EPSG:4326</SRS>" + " <BoundingBox minx='68' miny='33' maxx='72' maxy='35' />" + * " <Origin x='68' y='33' />" + * " <TileFormat width='512' height='512' mime-type='image/tiff' extension='tif' />" + * " <TileSets profile='global-geodetic'>" + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/1' units-per-pixel='0.3515625' order='1' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/2' units-per-pixel='0.17578125' order='2' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/3' units-per-pixel='0.08789063' order='3' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/4' units-per-pixel='0.08789063' order='4' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/5' units-per-pixel='0.08789063' order='5' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/6' units-per-pixel='0.08789063' order='6' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/7' units-per-pixel='0.08789063' order='7' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/8' units-per-pixel='0.08789063' order='8' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/9' units-per-pixel='0.08789063' order='9' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/10' units-per-pixel='0.08789063' order='10' />" * + " </TileSets>" + "</TileMap>"; */ final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements final Document doc = docBuilder.newDocument(); final Element rootElement = doc.createElement("TileMap"); doc.appendChild(rootElement); final Attr v = doc.createAttribute("version"); v.setValue(VERSION); rootElement.setAttributeNode(v); final Attr tilemapservice = doc.createAttribute("tilemapservice"); tilemapservice.setValue(normalizeUrl(normalizeUrl(url).replace(raster, ""))); rootElement.setAttributeNode(tilemapservice); // child elements final Element title = doc.createElement("Title"); title.setTextContent(raster); rootElement.appendChild(title); final Element abst = doc.createElement("Abstract"); abst.setTextContent(""); rootElement.appendChild(abst); final Element srs = doc.createElement("SRS"); srs.setTextContent(SRS); rootElement.appendChild(srs); final Element bbox = doc.createElement("BoundingBox"); rootElement.appendChild(bbox); final Attr minx = doc.createAttribute("minx"); minx.setValue(String.valueOf(mpm.getBounds().getMinX())); bbox.setAttributeNode(minx); final Attr miny = doc.createAttribute("miny"); miny.setValue(String.valueOf(mpm.getBounds().getMinY())); bbox.setAttributeNode(miny); final Attr maxx = doc.createAttribute("maxx"); maxx.setValue(String.valueOf(mpm.getBounds().getMaxX())); bbox.setAttributeNode(maxx); final Attr maxy = doc.createAttribute("maxy"); maxy.setValue(String.valueOf(mpm.getBounds().getMaxY())); bbox.setAttributeNode(maxy); final Element origin = doc.createElement("Origin"); rootElement.appendChild(origin); final Attr x = doc.createAttribute("x"); x.setValue(String.valueOf(mpm.getBounds().getMinX())); origin.setAttributeNode(x); final Attr y = doc.createAttribute("y"); y.setValue(String.valueOf(mpm.getBounds().getMinY())); origin.setAttributeNode(y); final Element tileformat = doc.createElement("TileFormat"); rootElement.appendChild(tileformat); final Attr w = doc.createAttribute("width"); w.setValue(String.valueOf(mpm.getTilesize())); tileformat.setAttributeNode(w); final Attr h = doc.createAttribute("height"); h.setValue(String.valueOf(mpm.getTilesize())); tileformat.setAttributeNode(h); final Attr mt = doc.createAttribute("mime-type"); mt.setValue("image/tiff"); tileformat.setAttributeNode(mt); final Attr ext = doc.createAttribute("extension"); ext.setValue("tif"); tileformat.setAttributeNode(ext); final Element tilesets = doc.createElement("TileSets"); rootElement.appendChild(tilesets); final Attr profile = doc.createAttribute("profile"); profile.setValue("global-geodetic"); tilesets.setAttributeNode(profile); for (int i = 0; i <= mpm.getMaxZoomLevel(); i++) { final Element tileset = doc.createElement("TileSet"); tilesets.appendChild(tileset); final Attr href = doc.createAttribute("href"); href.setValue(normalizeUrl(normalizeUrl(url)) + "/" + i); tileset.setAttributeNode(href); final Attr upp = doc.createAttribute("units-per-pixel"); upp.setValue(String.valueOf(180d / 256d / Math.pow(2, i))); tileset.setAttributeNode(upp); final Attr order = doc.createAttribute("order"); order.setValue(String.valueOf(i)); tileset.setAttributeNode(order); } return doc; }