2008/11/19 Benoît Thiébault <[EMAIL PROTECTED]>:
> I will have a look at this in more details. Could you please point the
> name of the class you refer to when you talk about vector layer of
> polygons ?

I was simply thinking of a FeatureCollection of polygonal features
which you could create, save to a shapefile or database etc. and then
manipulate and display as required.

Here is some code that I have used to create a regular lattice of
features, each with an integer id and a single double value.  No doubt
there is a much easier and more elegant way of doing this :-)

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Polygon;
import java.awt.geom.Rectangle2D;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.CRS;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;


    public FeatureCollection makeRegularLattice(
            Rectangle2D bounds, double cellWidth,
CoordinateReferenceSystem crs) {

        int rows = (int)Math.round((bounds.getHeight()) / cellWidth);
        int cols = (int)Math.round((bounds.getWidth()) / cellWidth);

        double[] xvertices = new double[cols+1];
        for (int i = 0; i <= cols; i++) {
            xvertices[i] = bounds.getMinX() + i*cellWidth;
        }

        double[] yvertices = new double[rows+1];
        for (int i = 0; i < rows; i++) {
            yvertices[i] = bounds.getMinY() + i*cellWidth;
        }

        SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
        typeBuilder.setName("lattice");

        typeBuilder.setCRS(crs);
        typeBuilder.add("shape", Polygon.class,
(CoordinateReferenceSystem) null);
        typeBuilder.add("id", Integer.class);
        typeBuilder.add("somevalue", Double.class);
        SimpleFeatureType type = typeBuilder.buildFeatureType();

        SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
        GeometryFactory gf = new GeometryFactory();

        Coordinate[] cellVertices = new Coordinate[5];
        int[] xind = {0, 1, 1, 0};
        int[] yind = {0, 0, 1, 1};

        FeatureCollection features = FeatureCollections.newCollection();

        int id = 1;
        double valuePlaceHolder = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                for (int v = 0; v < 4; v++) {
                    cellVertices[v] = new Coordinate(xvertices[j +
xind[v]], yvertices[i + yind[v]]);
                }
                cellVertices[4] = new Coordinate(cellVertices[0]);

                Polygon poly =
gf.createPolygon(gf.createLinearRing(cellVertices), null);

                builder.add(poly);
                builder.add(id++);
                builder.add(valuePlaceHolder);

                features.add(builder.buildFeature(null));
            }
        }

        return features;
    }


Hope this helps

Michael

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to