private boolean calculateVolumes() {

    int x, y;
    double z, z2;
    double dVolumePos = 0;
    double dVolumeNeg = 0;
    double dVolume = 0;
    double dDif;
    final double dArea = m_LowerGrid.getWindowCellSize() * m_LowerGrid.getWindowCellSize();

    for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
      for (x = 0; x < m_iNX; x++) {
        z = m_LowerGrid.getCellValueAsDouble(x, y);
        z2 = m_UpperGrid.getCellValueAsDouble(x, y);
        if (!m_LowerGrid.isNoDataValue(z) && !m_UpperGrid.isNoDataValue(z2)) {
          dDif = (z2 - z);
          dVolume += Math.abs(dDif);
          if (dDif > 0) {
            dVolumePos += dDif;
          } else {
            dVolumeNeg += -dDif;
          }
        }
      }
    }

    if (m_Task.isCanceled()) {
      return false;
    } else {
      dVolume *= dArea;
      dVolumePos *= dArea;
      dVolumeNeg *= dArea;

      final DecimalFormat df = new DecimalFormat("##.##");
      final HTMLDoc doc = new HTMLDoc();
      doc.open(Sextante.getText("Volumes"));
      doc.addHeader(Sextante.getText("Volumes"), 2);
      doc.startUnorderedList();
      doc.addListElement(Sextante.getText("Volume_+") + df.format(dVolumePos));
      doc.addListElement(Sextante.getText("Volume_-") + df.format(dVolumeNeg));
      doc.addListElement(Sextante.getText("Total_volume") + df.format(dVolume));
      doc.close();
      addOutputText(VOL, Sextante.getText("Volume"), doc.getHTMLCode());
      return true;
    }
  }
Exemple #2
0
  /**
   * Returns the help associated with a given geoalgorithm as a html-formatted string
   *
   * @param alg the geoalgorithm
   * @param sFilename the filename where help for the passed algorithm is stored
   * @return a html-formatted string with help for the given algorithm
   */
  public static String getHelpAsHTMLCode(final GeoAlgorithm alg, final String sFilename) {

    HelpElement element;
    final ArrayList list = open(sFilename);
    HashMap elements;
    final String sPath =
        "file:///" + sFilename.substring(0, sFilename.lastIndexOf(File.separator)) + File.separator;

    if (list != null) {
      elements = createMap(list);
    } else {
      elements = new HashMap();
    }

    final HTMLDoc doc = new HTMLDoc();

    doc.open(alg.getName());
    doc.addHeader(Sextante.getText(alg.getName()), 1);

    doc.addHeader(Sextante.getText("Description"), 2);

    element = (HelpElement) elements.get("DESCRIPTION");
    if (element != null) {
      doc.addParagraph(element.getTextAsFormattedHTML());
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    doc.addHeader(Sextante.getText("Parameters"), 2);

    final ParametersSet params = alg.getParameters();

    doc.startUnorderedList();
    for (int i = 0; i < params.getNumberOfParameters(); i++) {
      final Parameter param = params.getParameter(i);
      String sParam = param.getParameterDescription();
      sParam = "<b>" + sParam + "[" + getParameterTypeName(param) + "]: </b>";
      element = (HelpElement) elements.get(param.getParameterName());
      if (element != null) {
        sParam = sParam + element.getTextAsFormattedHTML();
      }
      doc.addListElement(sParam);
      if (element != null) {
        for (int j = 0; j < element.getImages().size(); j++) {
          final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
          doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
        }
      }
    }

    doc.closeUnorderedList();

    doc.addHeader(Sextante.getText("Outputs"), 2);

    element = (HelpElement) elements.get("OUTPUT_DESCRIPTION");
    if (element != null) {
      doc.addParagraph(element.getTextAsFormattedHTML());
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    doc.startUnorderedList();
    final OutputObjectsSet oo = alg.getOutputObjects();
    String sOutputType = "";
    for (int i = 0; i < oo.getOutputObjectsCount(); i++) {
      final Output out = oo.getOutput(i);
      String sOutput = out.getDescription();
      if (out instanceof OutputRasterLayer) {
        sOutputType = Sextante.getText("Raster_Layer");
      } else if (out instanceof Output3DRasterLayer) {
        sOutputType = Sextante.getText("3D_Raster_layer");
      } else if (out instanceof OutputVectorLayer) {
        sOutputType = Sextante.getText("Vector_Layer");
        final OutputVectorLayer ovl = (OutputVectorLayer) out;
        switch (ovl.getShapeType()) {
          case OutputVectorLayer.SHAPE_TYPE_UNDEFINED:
          default:
            sOutputType = sOutputType + " - " + Sextante.getText("Any_type");
            break;
          case OutputVectorLayer.SHAPE_TYPE_LINE:
            sOutputType = sOutputType + " - " + Sextante.getText("Line");
            break;
          case OutputVectorLayer.SHAPE_TYPE_POLYGON:
            sOutputType = sOutputType + " - " + Sextante.getText("Polygon");
            break;
          case OutputVectorLayer.SHAPE_TYPE_POINT:
            sOutputType = sOutputType + " - " + Sextante.getText("Point");
            break;
        }
      } else if (out instanceof OutputTable) {
        sOutputType = Sextante.getText("Table");
      } else if (out instanceof OutputChart) {
        sOutputType = Sextante.getText("graph-chart");
      } else if (out instanceof OutputText) {
        sOutputType = Sextante.getText("Text");
      } else if (out instanceof OutputNumericalValue) {
        sOutputType = Sextante.getText("Numerical_value");
      }
      sOutput = "<b>" + sOutput + "[" + sOutputType + "]: </b>";
      element = (HelpElement) elements.get(out.getName());
      if (element != null) {
        sOutput = sOutput + element.getTextAsFormattedHTML();
      }
      doc.addListElement(sOutput);
      if (element != null) {
        for (int j = 0; j < element.getImages().size(); j++) {
          final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
          doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
        }
      }
    }
    doc.closeUnorderedList();

    doc.addHeader(Sextante.getText("Additional_information"), 2);

    element = (HelpElement) elements.get("ADDITIONAL_INFO");
    if (element != null) {
      doc.addParagraph(element.getTextAsFormattedHTML());
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    doc.addHeader(Sextante.getText("Command_line"), 2);

    String sText = alg.getCommandLineHelp();
    sText = sText.replaceAll("\n", "<br>");
    sText = sText.replace("   ", " &nbsp ");
    doc.addCourierText(sText);

    doc.addParagraph("");

    element = (HelpElement) elements.get("EXTENSION_AUTHOR");
    if (element != null) {
      doc.addParagraph(
          "<i>" + Sextante.getText("Algorithm_created_by") + " " + element.getText() + "</i>");
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    element = (HelpElement) elements.get("HELP_AUTHOR");
    if (element != null) {
      doc.addParagraph(
          "<i>" + Sextante.getText("Help_file_created_by") + " " + element.getText() + "</i>");
      for (int j = 0; j < element.getImages().size(); j++) {
        final ImageAndDescription iad = (ImageAndDescription) element.getImages().get(j);
        doc.addImageAndDescription(sPath + iad.getFilename(), iad.getDescription());
      }
    }

    doc.close();

    return doc.getHTMLCode();
  }
  private void createResults() throws GeoAlgorithmExecutionException {

    int i, j;
    int iPt;
    int iID = 1;
    double dMin;
    double dDist;
    DemandPoint dpt;
    Coordinate pt = null;
    LineString line;
    final Coordinate coords[] = new Coordinate[2];
    final Object values[] = new Object[8];
    String sOfferName = null;
    final String[] sNames = {
      Sextante.getText("ID"),
      Sextante.getText("Demand_point"),
      Sextante.getText("X_demand"),
      Sextante.getText("Y_demand"),
      Sextante.getText("Resource"),
      Sextante.getText("X_resource"),
      Sextante.getText("Y_resource"),
      Sextante.getText("Distance")
    };
    final Class[] types = {
      Integer.class,
      String.class,
      Double.class,
      Double.class,
      String.class,
      Double.class,
      Double.class,
      Double.class
    };
    final SimpleStats stats = new SimpleStats();
    final IVectorLayer spiderWebLayer =
        getNewVectorLayer(
            SPIDER, Sextante.getText("Conexions"), IVectorLayer.SHAPE_TYPE_LINE, types, sNames);
    final IVectorLayer newPoints =
        getNewVectorLayer(
            NEWLOCATIONS,
            Sextante.getText("Result"),
            IVectorLayer.SHAPE_TYPE_POINT,
            new Class[] {Integer.class},
            new String[] {"ID"});
    final GeometryFactory gf = new GeometryFactory();
    for (i = 0; i < m_Demand.size(); i++) {
      dpt = (DemandPoint) m_Demand.get(i);
      dMin = Double.MAX_VALUE;
      for (j = 0; j < m_iOffer; j++) {
        if (m_dDist[i][j] < dMin) {
          dMin = m_dDist[i][j];
          pt = (Coordinate) m_Offer.get(j);
          sOfferName = Sextante.getText("Preexistent") + Integer.toString(j);
        }
      }
      for (j = 0; j < m_iBestSolution.length; j++) {
        iPt = m_iBestSolution[j];
        if (m_dDist[i][iPt + m_iOffer] < dMin) {
          dMin = m_dDist[i][iPt + m_iOffer];
          pt = (Coordinate) m_Candidates.get(iPt);
          sOfferName = Sextante.getText("Candidate") + Integer.toString(iPt);
        }
      }

      stats.addValue(dMin);
      coords[0] = pt;
      coords[1] = new Coordinate(dpt.x, dpt.y);
      line = gf.createLineString(coords);
      values[0] = new Integer(iID++);
      values[1] = Integer.toString(i);
      values[2] = new Double(dpt.x);
      values[3] = new Double(dpt.y);
      values[4] = sOfferName;
      values[5] = new Double(pt.x);
      values[6] = new Double(pt.y);
      dDist = Math.sqrt(Math.pow(pt.x - dpt.x, 2) + Math.pow(pt.y - dpt.y, 2));
      values[7] = new Double(dDist);
      spiderWebLayer.addFeature(line, values);
    }

    for (j = 0; j < m_iBestSolution.length; j++) {
      iPt = m_iBestSolution[j];
      final Coordinate solutionPt = (Coordinate) m_Candidates.get(iPt);
      newPoints.addFeature(gf.createPoint(solutionPt), new Object[] {new Integer(j + 1)});
    }

    final DecimalFormat df = new DecimalFormat("##.###");
    final HTMLDoc doc = new HTMLDoc();
    doc.open(Sextante.getText("Result"));
    doc.addHeader(Sextante.getText("Statistics_of_global_solution"), 2);
    doc.startUnorderedList();
    doc.addListElement(
        Sextante.getText("Objective_function") + ": " + df.format(Math.abs(m_dObjective)));
    doc.addListElement(Sextante.getText("Mean_distance") + ": " + df.format(stats.getMean()));
    doc.addListElement(
        Sextante.getText("Mean_squared_distance") + ": " + df.format(stats.getRMS()));
    doc.addListElement(Sextante.getText("Min_distance") + ": " + df.format(stats.getMin()));
    doc.addListElement(Sextante.getText("Maximum_distance") + ": " + df.format(stats.getMax()));
    doc.addListElement(Sextante.getText("Variance") + ": " + df.format(stats.getVariance()));
    doc.addListElement(Sextante.getText("Sum_of_distances") + ": " + df.format(stats.getSum()));
    doc.addListElement(
        Sextante.getText("Coefficient_of_variation") + ": " + df.format(stats.getCoeffOfVar()));
    doc.closeUnorderedList();
    doc.close();

    addOutputText(RESULT, Sextante.getText("Statistics"), doc.getHTMLCode());

    addOutputNumericalValue(OBJ_FUNCTION_VALUE, m_dObjective);
    addOutputNumericalValue(MEAN_DIST, stats.getMean());
    addOutputNumericalValue(MEAN_SQUARED_DISTANCE, stats.getRMS());
    addOutputNumericalValue(MIN_DISTANCE, stats.getMin());
    addOutputNumericalValue(MAX_DISTANCE, stats.getMax());
    addOutputNumericalValue(VARIANCE, stats.getVariance());
    addOutputNumericalValue(SUM_OF_DISTANCES, stats.getSum());
    addOutputNumericalValue(COEFF_OF_VARIATION, stats.getMean());
  }