view flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 1110:563e015f0f22

Export floodplain to shapefile and add its file path to WSPLGEN job. flys-artifacts/trunk@2616 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 31 Aug 2011 08:53:30 +0000
parents 5b1198c27d43
children faca1825818e
line wrap: on
line source
package de.intevation.flys.utils;

import java.io.IOException;
import java.io.File;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;

import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.FeatureStore;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.referencing.CRS;


public class GeometryUtils {

    private GeometryUtils() {
    }


    /**
     * Returns the boundary of Geometry <i>geom</i> in OpenLayers
     * representation.
     *
     * @param geom The geometry.
     *
     * @return the OpenLayers boundary of <i>geom</i>.
     */
    public static String jtsBoundsToOLBounds(Geometry geom) {
        Coordinate[] c = geom != null ? geom.getCoordinates() : null;

        if (c == null || c.length < 2) {
            return null;
        }

        return "" + c[0].x + " " + c[1].y + " " + c[1].x + " " + c[0].y;
    }


    public static SimpleFeatureType buildFeatureType(
        String name, String srs, Class geometryType)
    {
        try {
            SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
            CoordinateReferenceSystem crs    = CRS.decode(srs);

            builder.setName("flys");
            builder.setNamespaceURI("http://www.intevation.de/");
            builder.setCRS(crs);
            builder.setSRS(srs);

            builder.add("geometry", geometryType, crs);

            return builder.buildFeatureType();
        }
        catch (NoSuchAuthorityCodeException nsae) {
        }
        catch (FactoryException fe) {
        }

        return null;
    }


    public static List<SimpleFeature> parseGeoJSON(
        String geojson, SimpleFeatureType ft
    ) {
        List<SimpleFeature> collection = new ArrayList<SimpleFeature>();

        try {
            FeatureJSON fjson = new FeatureJSON();
            fjson.setFeatureType(ft);

            FeatureIterator<SimpleFeature> iterator =
                fjson.streamFeatureCollection(geojson);

            while (iterator.hasNext()) {
                collection.add(iterator.next());
            }
        }
        catch (IOException ioe) {
            // TODO handle exception
        }

        return collection;
    }


    public static boolean writeShapefile(
        File              shape,
        SimpleFeatureType featureType,
        FeatureCollection collection)
    throws MalformedURLException, IOException
    {
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put("url", shape.toURI().toURL());
        params.put("create spatial index", Boolean.TRUE);

        DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();

        ShapefileDataStore newDataStore =
            (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
        newDataStore.createSchema(featureType);

        Transaction transaction = new DefaultTransaction("create");

        String typeName = newDataStore.getTypeNames()[0];

        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
            (FeatureStore<SimpleFeatureType, SimpleFeature>)
                newDataStore.getFeatureSource(typeName);

        featureStore.setTransaction(transaction);

        featureStore.addFeatures(collection);
        transaction.commit();

        return true;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org