Example #1
0
  /** Proceso de calculo de la transformación tasseeledCap */
  public void process() throws InterruptedException {
    if (!exec) return;
    setBuffer();
    GridExtent layerExtent = new GridExtent(rasterSE.getFullRasterExtent(), rasterSE.getCellSize());

    // TODO: Tipo salida float
    rasterResult =
        RasterBuffer.getBuffer(
            IBuffer.TYPE_FLOAT,
            layerExtent.getNX(),
            layerExtent.getNY(),
            matrixParams.length,
            true);

    // Algoritmo TasseledCap. Trasformacion
    double valor = 0;
    int iNX = layerExtent.getNX();
    int iNY = layerExtent.getNY();

    //			 BUFFER TYPE_BYTE
    if (buffer.getDataType() == IBuffer.TYPE_BYTE) {
      for (int i = 0; i < iNY; i++) {
        for (int j = 0; j < iNX; j++) {
          for (int k = 0; k < matrixParams.length; k++) {
            for (int s = 0; s < matrixParams[0].length; s++) {
              valor += (double) (buffer.getElemByte(i, j, s) & 0xff) * matrixParams[k][s];
            }
            rasterResult.setElem(i, j, k, (float) valor);
            valor = 0;
          }
        }
        percent = (int) (i * 100 / iNY);
      }
    }

    //			 BUFFER TYPE_SHORT
    if (buffer.getDataType() == IBuffer.TYPE_SHORT) {
      for (int i = 0; i < iNY; i++) {
        for (int j = 0; j < iNX; j++) {
          for (int k = 0; k < matrixParams.length; k++) {
            for (int s = 0; s < matrixParams[0].length; s++) {
              valor += (double) buffer.getElemShort(i, j, s) * matrixParams[k][s];
            }
            rasterResult.setElem(i, j, k, (float) valor);
            valor = 0;
          }
        }
        percent = (int) (i * 100 / iNY);
      }
    }

    //			BUFFER TYPE INT
    if (buffer.getDataType() == IBuffer.TYPE_INT) {
      for (int i = 0; i < iNY; i++) {
        for (int j = 0; j < iNX; j++) {
          for (int k = 0; k < matrixParams.length; k++) {
            for (int s = 0; s < matrixParams[0].length; s++) {
              valor += (double) buffer.getElemInt(i, j, s) * matrixParams[k][s];
            }
            rasterResult.setElem(i, j, k, (float) valor);
            valor = 0;
          }
        }
        percent = (int) (i * 100 / iNY);
      }
    }

    //			BUFFER TYPE FLOAT
    if (buffer.getDataType() == IBuffer.TYPE_FLOAT) {
      for (int i = 0; i < iNY; i++) {
        for (int j = 0; j < iNX; j++) {
          for (int k = 0; k < matrixParams.length; k++) {
            for (int s = 0; s < matrixParams[0].length; s++) {
              valor += (double) buffer.getElemFloat(i, j, s) * matrixParams[k][s];
            }
            rasterResult.setElem(i, j, k, (float) valor);
            valor = 0;
          }
        }
        percent = (int) (i * 100 / iNY);
      }
    }

    //			BUFFER TYPE DOUBLE
    if (buffer.getDataType() == IBuffer.TYPE_DOUBLE) {
      for (int i = 0; i < iNY; i++) {
        for (int j = 0; j < iNX; j++) {
          for (int k = 0; k < matrixParams.length; k++) {
            for (int s = 0; s < matrixParams[0].length; s++) {
              valor += (double) buffer.getElemDouble(i, j, s) * matrixParams[k][s];
            }
            rasterResult.setElem(i, j, k, (float) valor);
            valor = 0;
          }
        }
        percent = (int) (i * 100 / iNY);
      }
    }

    // Escritura del resultado en disco
    GeoRasterWriter grw = null;
    writerBufferServer = new WriterBufferServer(rasterResult);

    if (fileName == null) return;

    int endIndex = fileName.lastIndexOf(".");
    if (endIndex < 0) endIndex = fileName.length();

    try {
      grw =
          GeoRasterWriter.getWriter(
              writerBufferServer,
              fileName,
              rasterResult.getBandCount(),
              aTransform,
              rasterResult.getWidth(),
              rasterResult.getHeight(),
              rasterResult.getDataType(),
              GeoRasterWriter.getWriter(fileName).getParams(),
              null);
      grw.dataWrite();
      grw.setWkt(rasterSE.getWktProjection());
      grw.writeClose();
      rasterResult.free();
      tasseledCapFinalize(fileName);
    } catch (NotSupportedExtensionException e1) {
      RasterToolsUtil.messageBoxError(
          PluginServices.getText(this, "error_writer_notsupportedextension"), this, e1);
    } catch (RasterDriverException e1) {
      e1.printStackTrace();
    } catch (IOException e) {
      RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
    }
  }
  /** Tarea de recorte */
  public void process() throws InterruptedException {
    IRasterDataSource dsetCopy = null;
    if (rasterSE != null) rasterSE.setReadingData(Thread.currentThread().toString());

    try {
      long t2;
      long t1 = new java.util.Date().getTime();

      insertLineLog(RasterToolsUtil.getText(this, "leyendo_raster"));

      dsetCopy = rasterSE.getDataSource().newDataset();
      BufferFactory bufferFactory = new BufferFactory(dsetCopy);
      bufferFactory.setDrawableBands(drawableBands);

      if (interpolationMethod != BufferInterpolation.INTERPOLATION_Undefined) {
        try {
          if (pValues != null) {
            if (RasterBuffer.isBufferTooBig(
                new double[] {pValues[0], pValues[3], pValues[2], pValues[1]},
                drawableBands.length)) bufferFactory.setReadOnly(true);
            bufferFactory.setAreaOfInterest(
                pValues[0], pValues[3], pValues[2] - pValues[0], pValues[1] - pValues[3]);
          } else if (wcValues != null) {
            // if (RasterBuffer.isBufferTooBig(new double[] {
            // wcValues[0], wcValues[3], wcValues[2], wcValues[1] },
            // rasterSE.getCellSize(), drawableBands.length))
            if (!rasterSE.isActionEnabled(IRasterLayerActions.REMOTE_ACTIONS))
              bufferFactory.setReadOnly(true);
            bufferFactory.setAreaOfInterest(
                wcValues[0],
                wcValues[1],
                Math.abs(wcValues[0] - wcValues[2]),
                Math.abs(wcValues[1] - wcValues[3]));
          }
        } catch (InvalidSetViewException e) {
          RasterToolsUtil.messageBoxError(
              "No se ha podido asignar la vista al inicial el proceso de recorte.", this, e);
        }
        buffer = bufferFactory.getRasterBuf();

        insertLineLog(RasterToolsUtil.getText(this, "interpolando"));

        if (buffer != null)
          buffer =
              ((RasterBuffer) buffer)
                  .getAdjustedWindow(resolutionWidth, resolutionHeight, interpolationMethod);
        else {
          RasterToolsUtil.messageBoxError(
              "El proceso de recorte ha fallado porque el buffer no contenía datos.",
              this,
              new NullPointerException());
          return;
        }
      } else {
        try {
          if (RasterBuffer.isBufferTooBig(
              new double[] {0, 0, resolutionWidth, resolutionHeight}, drawableBands.length))
            bufferFactory.setReadOnly(true);
          if (pValues != null)
            bufferFactory.setAreaOfInterest(
                pValues[0],
                pValues[3],
                Math.abs(pValues[2] - pValues[0]) + 1,
                Math.abs(pValues[1] - pValues[3]) + 1,
                resolutionWidth,
                resolutionHeight);
          else if (wcValues != null)
            bufferFactory.setAreaOfInterest(
                wcValues[0],
                wcValues[1],
                wcValues[2],
                wcValues[3],
                resolutionWidth,
                resolutionHeight);
          buffer = bufferFactory.getRasterBuf();
          if (buffer == null) {
            RasterToolsUtil.messageBoxError(
                "El proceso de recorte ha fallado porque el buffer no contenía datos.",
                this,
                new NullPointerException());
            return;
          }
        } catch (InvalidSetViewException e) {
          RasterToolsUtil.messageBoxError(
              "No se ha podido asignar la vista al inicial el proceso de recorte.", this, e);
        }
      }
      // TODO: FUNCIONALIDAD: Poner los getWriter con la proyección del
      // fichero fuente

      if ((selectedRois != null) && (!bufferFactory.isReadOnly())) {
        if (selectedRois.size() > 0) {
          int despX = 0;
          int despY = 0;
          if (pValues != null) {
            despX = pValues[0];
            despY = pValues[1];
          } else if (wcValues != null) {
            despX =
                (int) dsetCopy.worldToRaster(new Point2D.Double(wcValues[0], wcValues[1])).getX();
            despY =
                (int) dsetCopy.worldToRaster(new Point2D.Double(wcValues[0], wcValues[1])).getY();
          }
          drawOnlyROIs(buffer, selectedRois, despX, despY);
        }
      }

      insertLineLog(RasterToolsUtil.getText(this, "salvando_imagen"));

      String finalFileName = "";
      if (oneLayerPerBand) {
        long[] milis = new long[drawableBands.length];
        String[] fileNames = new String[drawableBands.length];
        for (int i = 0; i < drawableBands.length; i++) {
          fileNames[i] = fileName + "_B" + drawableBands[i] + ".tif";
          writerBufferServer.setBuffer(buffer, i);
          Params p = null;
          if (params == null) p = GeoRasterWriter.getWriter(fileNames[i]).getParams();
          else p = params;
          grw =
              GeoRasterWriter.getWriter(
                  writerBufferServer,
                  fileNames[i],
                  1,
                  affineTransform,
                  buffer.getWidth(),
                  buffer.getHeight(),
                  buffer.getDataType(),
                  p,
                  null);
          grw.setColorBandsInterpretation(new String[] {DatasetColorInterpretation.GRAY_BAND});
          grw.setWkt(dsetCopy.getWktProjection());
          grw.setCancellableRasterDriver(cancellableObj);
          grw.dataWrite();
          grw.writeClose();
          saveToRmf(fileNames[i]);
          t2 = new java.util.Date().getTime();
          milis[i] = (t2 - t1);
          t1 = new java.util.Date().getTime();
        }
        if (incrementableTask != null) {
          incrementableTask.processFinalize();
          incrementableTask = null;
        }
        if (viewName != null) {
          if (RasterToolsUtil.messageBoxYesOrNot("cargar_toc", this)) {
            try {
              for (int i = 0; i < drawableBands.length; i++) {
                FLayer lyr = RasterToolsUtil.loadLayer(viewName, fileNames[i], null);
                if (lyr != null && lyr instanceof FLyrRasterSE)
                  ((FLyrRasterSE) lyr).setRois(rasterSE.getRois());
              }
            } catch (RasterNotLoadException e) {
              RasterToolsUtil.messageBoxError("error_load_layer", this, e);
            }
          }
        }
        for (int i = 0; i < drawableBands.length; i++) {
          if (externalActions != null)
            externalActions.end(
                new Object[] {fileName + "_B" + drawableBands[i] + ".tif", new Long(milis[i])});
        }
      } else {
        if (isUsingFile(fileName)) {
          incrementableTask.hideWindow();
          RasterToolsUtil.messageBoxError("error_opened_file", this);
          return;
        }
        File f = new File(fileName);
        if (f.exists()) {
          f.delete();
        }
        f = null;
        writerBufferServer.setBuffer(buffer, -1);
        if (params == null) {
          finalFileName = fileName + ".tif";
          params = GeoRasterWriter.getWriter(finalFileName).getParams();
        } else finalFileName = fileName;
        grw =
            GeoRasterWriter.getWriter(
                writerBufferServer,
                finalFileName,
                buffer.getBandCount(),
                affineTransform,
                buffer.getWidth(),
                buffer.getHeight(),
                buffer.getDataType(),
                params,
                null);
        if (colorInterp != null) grw.setColorBandsInterpretation(colorInterp.getValues());
        grw.setWkt(dsetCopy.getWktProjection());
        grw.setCancellableRasterDriver(cancellableObj);
        grw.dataWrite();
        grw.writeClose();
        saveToRmf(finalFileName);
        t2 = new java.util.Date().getTime();
        if (incrementableTask != null) {
          incrementableTask.processFinalize();
          incrementableTask = null;
        }
        // Damos tiempo a parar el Thread del incrementable para que no
        // se cuelgue la ventana
        // El tiempo es como mínimo el de un bucle del run de la tarea
        // incrementable
        Thread.sleep(600);
        cutFinalize(finalFileName, (t2 - t1));
      }

    } catch (NotSupportedExtensionException e) {
      RasterToolsUtil.messageBoxError("error_not_suported_extension", this, e);
    } catch (RasterDriverException e) {
      RasterToolsUtil.messageBoxError("error_writer", this, e);
    } catch (IOException e) {
      RasterToolsUtil.messageBoxError("error_georasterwriter", this, e);
    } finally {
      if (rasterSE != null) rasterSE.setReadingData(null);
      if (dsetCopy != null) dsetCopy.close();
      buffer = null;
    }
  }