Esempio n. 1
0
  private static void doProcessing() throws GeoAlgorithmExecutionException, IOException {

    final GridCoverage2D gc = openRasterLayer("c:/dem.asc");
    final GTRasterLayer raster = new GTRasterLayer();
    raster.create(gc);

    final FeatureSource fs = openShapefile("c:/poly.shp");
    final GTVectorLayer vector = new GTVectorLayer();
    vector.create(fs);

    final ClipGridAlgorithm alg = new ClipGridAlgorithm();
    final ParametersSet params = alg.getParameters();
    params.getParameter(ClipGridAlgorithm.INPUT).setParameterValue(raster);
    params.getParameter(ClipGridAlgorithm.POLYGONS).setParameterValue(vector);

    final OutputObjectsSet outputs = alg.getOutputObjects();

    final Output twi = outputs.getOutput(ClipGridAlgorithm.RESULT);
    twi.setOutputChannel(new FileOutputChannel("c:/clipped.tif"));

    alg.execute(null, m_OutputFactory);
  }
Esempio n. 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();
  }
Esempio n. 3
0
  private static void doProcessing() throws GeoAlgorithmExecutionException, IOException {
    /*
     * First we need some data.
     * Let's open a DEM
     */
    final GridCoverage2D gc = openRasterLayer("d:\\gisdata\\dem.asc");
    /*
     * To use this data we need to wrap it with an object
     * that implements the IRasterLayer, so SEXTANTE algorithms
     * can access it.
     * Since it is a Geotools object, we will use the Geotools
     * wrapper class GTRasterLayer
     */
    final GTRasterLayer layer = new GTRasterLayer();
    layer.create(gc);

    /*
     * Now we will calculate contour lines from that DEM
     */
    final ContourLinesAlgorithm alg = new ContourLinesAlgorithm();
    final ParametersSet params = alg.getParameters();
    params.getParameter(ContourLinesAlgorithm.LAYER).setParameterValue(layer);
    params.getParameter(ContourLinesAlgorithm.DISTANCE).setParameterValue(new Double(100.));
    params.getParameter(ContourLinesAlgorithm.MIN).setParameterValue(new Double(0.));
    params.getParameter(ContourLinesAlgorithm.MAX).setParameterValue(new Double(10000.));

    /*
     *  This algorithm will generate a new vector layer.
     * We can select "where" to put the result. To do this, we
     * retrieve the output container and set the output channel,
     * which contains information about the destiny of the resulting
     * data. The most common way of using this is setting
     * a FileOutputChannel, which contains the information needed to
     * put the output to a file (basically a filename).
     * If we omit this, a FileOutputChannel will be used,
     * using a temporary filename.
     */
    final OutputObjectsSet outputs = alg.getOutputObjects();
    final Output contours = outputs.getOutput(ContourLinesAlgorithm.RESULT);
    contours.setOutputChannel(new FileOutputChannel("d:\\gisdata\\contours2.shp"));

    /*
     * Execute the algorithm. We use no task monitor,
     * so we will not be able to monitor the progress
     * of the execution. SEXTANTE also provides a DefaultTaskMonitor,
     * which shows a simple progress bar, or you could make your
     * own one, implementing the ITaskMonitor interface
     *
     * The execute method returns true if everything went OK, or false if it
     * was canceled. Since we are not giving the user the chance to
     * cancel it (there is no task monitor), we do not care about the
     * return value.
     *
     * If something goes wrong, it will throw an exception.
     */
    alg.execute(null, m_OutputFactory);

    /*
     * Now the result can be taken from the output container
     */
    final IVectorLayer result = (IVectorLayer) contours.getOutputObject();

    /*
     * And now we can do with it whatever we want.
     * Let's print some information to see that the layer has
     * been correctly created.
     * Before accesing the layer, we have to call the open() method;
     */
    result.open();
    System.out.println(
        "This layer contains " + Integer.toString(result.getShapesCount()) + " lines.");
  }