Hi, I read this just by chance.

I had no answer from Sextante community then. I had to implement my own
version of the algorithm. However, I ended up not using it since it was no
longer necessary for my project -neither Sextante-.

I sent this implementation to Victor but had no answer. I post it here since
it may be useful to you. However, keep in mind a few considerations:
- I didn't implement it having efficiency in mind. It takes twice or more
than the original implementation to run the algorithm.
- I didn't test it very well since I didn't have to use it at the end.

Hope it helps.

Fernando

--------------

package com.example;

import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateFilter;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.operation.union.CascadedPolygonUnion;

import es.unex.sextante.core.GeoAlgorithm;
import es.unex.sextante.core.Sextante;
import es.unex.sextante.dataObjects.IRasterLayer;
import es.unex.sextante.dataObjects.IVectorLayer;
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
import es.unex.sextante.exceptions.RepeatedParameterNameException;
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
import es.unex.sextante.outputs.OutputVectorLayer;
import es.unex.sextante.rasterWrappers.GridExtent;

public class VectorizeAlgorithmReimplementado extends GeoAlgorithm {

    public static final String LAYER = "LAYER";
    public static final String RESULT = "RESULT";

    private IRasterLayer m_Input;
    private IVectorLayer m_Polygons;

    public void defineCharacteristics() {

        setName(Sextante.getText("Vectorizar_capa_raster"));
        setGroup(Sextante.getText("Vectorizacion"));
        setGeneratesUserDefinedRasterOutput(false);

        try {
            m_Parameters.addInputRasterLayer(LAYER,
                    Sextante.getText("Capa_de_entrada"), true);
            addOutputVectorLayer(RESULT, Sextante.getText("Resultado"),
                    OutputVectorLayer.SHAPE_TYPE_POLYGON);
        } catch (RepeatedParameterNameException e) {
            Sextante.addErrorToLog(e);
        }

    }

    public boolean processAlgorithm() throws GeoAlgorithmExecutionException
{

        String sFields[] = new String[2];
        Class<?> types[] = { Integer.class, Double.class };

        m_Input = m_Parameters.getParameterValueAsRasterLayer(LAYER);

        m_Input.setFullExtent();

        sFields[0] = "ID";
        sFields[1] = m_Input.getName();

        m_Polygons = getNewVectorLayer(RESULT,
Sextante.getText("Resultado"),
                IVectorLayer.SHAPE_TYPE_POLYGON, types, sFields);

        createPolygons();

        return !m_Task.isCanceled();

    }

    private void createPolygons() throws UnsupportedOutputChannelException {

        double dValue;

        int m_iNX = m_Input.getNX();
        int m_iNY = m_Input.getNY();

        Map<Double, List<Polygon>> classes = new HashMap<Double,
List<Polygon>>();
        Map<Double, Integer> ids = new HashMap<Double, Integer>();

        GeometryFactory gridGeometryFactory = new GeometryFactory();

        int id = 0;
        for (int y = 0; y < m_iNY && setProgress(y, m_iNY); y++) {
            for (int x = 0; x < m_iNX; x++) {
                dValue = m_Input.getCellValueAsDouble(x, y);

                Polygon square = (Polygon) gridGeometryFactory
                        .toGeometry(new Envelope(x, x + 1, y, y + 1));

                List<Polygon> clazz = classes.get(dValue);
                if (clazz == null) {
                    clazz = new LinkedList<Polygon>();
                    ids.put(dValue, id);
                    classes.put(dValue, clazz);
                    id++;
                }
                clazz.add(square);
            }
        }

        final GridExtent wge = m_Input.getWindowGridExtent();
        final double halfCellSize = wge.getCellSize() / 2;

        for (Map.Entry<Double, List<Polygon>> entry : classes.entrySet()) {
            double value = entry.getKey();
            List<Polygon> clazz = entry.getValue();

            CascadedPolygonUnion cascadedPolygonUnion = new
CascadedPolygonUnion(
                    clazz);
            Geometry classGeometry = cascadedPolygonUnion.union();

            classGeometry.apply(new CoordinateFilter() {

                @Override
                public void filter(Coordinate arg0) {
                    Point2D translated = wge.getWorldCoordsFromGridCoords(
                            (int) arg0.x, (int) arg0.y);
                    arg0.x = translated.getX() - halfCellSize;
                    arg0.y = translated.getY() + halfCellSize;
                }
            });

            Object values[] = new Object[2];
            values[0] = ids.get(value);
            values[1] = value;

            m_Polygons.addFeature(classGeometry, values);
        }

    }

}


Hi list,
>
> Last March a user called Fernando had a problem with vectorize
> algorithm (see below). I would like to know
> if there is any solution because I have had the same problem.
>
> Thank you,
>
> Ibon
>
> 2011/3/14 Fernando Barbat <fbar...@gmail.com>:
> > Hi list,
> >
> > I am trying to use VectorizeAlgorithm of Sextante 0.6 to vectorize a
> raster
> > (using plain Java and GeoTools 2.6). However, the resulting shapefile is
> not
> > what one would expect. It has two polygons which are not vectorized
> > properly.
> >
> > I can share both raster and Sextante-vectorized version if someone is
> > interested.
> >
> > Any idea?
> >
> > Thanks.
> >
> > Fernando
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://lists.forge.osor.eu/pipermail/sextante-users/attachments/20110930/585870e2/attachment-0001.htm
>
> ------------------------------
>
> _______________________________________________
> Sextante-users mailing list
> Sextante-users@lists.forge.osor.eu
> https://lists.forge.osor.eu/listinfo/sextante-users
>
>
> End of Sextante-users Digest, Vol 36, Issue 10
> **********************************************
>
_______________________________________________
Sextante-users mailing list
Sextante-users@lists.forge.osor.eu
https://lists.forge.osor.eu/listinfo/sextante-users

Responder a