@Before
  public void init() {
    // create a configuration
    conf = HadoopUtils.createConfiguration();

    // set this to a relatively high value - it will be bumped back down in
    // testMoreWorkersThanMapSlots
    conf.setInt("mapred.tasktracker.map.tasks.maximum", 100);

    // create a metadata
    metadata = new MrsImagePyramidMetadata();
    metadata.setTilesize(tileSize);
    metadata.setTileType(DataBuffer.TYPE_FLOAT);
  }
Beispiel #2
0
  @Override
  public void postBuild(Progress p, boolean buildPyramid)
      throws IOException, JobFailedException, JobCancelledException {
    if (buildPyramid) {
      MrsImagePyramid pyramid = MrsImagePyramid.open(_outputName, getProviderProperties());
      MrsImagePyramidMetadata metadata = pyramid.getMetadata();

      Aggregator aggregator;
      if (metadata.getClassification() == Classification.Continuous) {
        aggregator = new MeanAggregator();
      } else {
        aggregator = new ModeAggregator();
      }
      BuildPyramidSpark.build(
          _outputName, aggregator, createConfiguration(), getProviderProperties());
    }
    p.complete();
  }
  private void setupConfig(final Job job, final MrsImageDataProvider provider)
      throws DataProviderException {
    try {
      Configuration conf = job.getConfiguration();
      DataProviderFactory.saveProviderPropertiesToConfig(provider.getProviderProperties(), conf);
      context.save(conf);
      // Add the input pyramid metadata to the job configuration
      for (final String input : context.getInputs()) {
        MrsImagePyramid pyramid;
        try {
          pyramid = MrsImagePyramid.open(input, context.getProviderProperties());
        } catch (IOException e) {
          throw new DataProviderException("Failure opening input image pyramid: " + input, e);
        }
        final MrsImagePyramidMetadata metadata = pyramid.getMetadata();
        log.debug(
            "In HadoopUtils.setupMrsPyramidInputFormat, loading pyramid for "
                + input
                + " pyramid instance is "
                + pyramid
                + " metadata instance is "
                + metadata);

        String image = metadata.getName(context.getZoomLevel());
        // if we don't have this zoom level, use the max, then we'll decimate/subsample that one
        if (image == null) {
          log.error(
              "Could not get image in setupMrsPyramidInputFormat at zoom level "
                  + context.getZoomLevel()
                  + " for "
                  + pyramid);
          image = metadata.getName(metadata.getMaxZoomLevel());
        }

        HadoopUtils.setMetadata(conf, metadata);
      }
    } catch (IOException e) {
      throw new DataProviderException(
          "Failure configuring map/reduce job " + context.toString(), e);
    }
  }
  @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;
  }