ingo@1052: package de.intevation.flys.utils;
ingo@1052:
ingo@1106: import java.io.IOException;
ingo@1106: import java.io.File;
ingo@1106: import java.io.Serializable;
ingo@1106: import java.net.MalformedURLException;
ingo@1106: import java.util.ArrayList;
ingo@1106: import java.util.HashMap;
ingo@1106: import java.util.List;
ingo@1106: import java.util.Map;
ingo@1106:
ingo@1052: import com.vividsolutions.jts.geom.Coordinate;
ingo@1052: import com.vividsolutions.jts.geom.Geometry;
ingo@1052:
ingo@1106: import org.opengis.feature.simple.SimpleFeature;
ingo@1106: import org.opengis.feature.simple.SimpleFeatureType;
ingo@1108: import org.opengis.referencing.FactoryException;
ingo@1108: import org.opengis.referencing.NoSuchAuthorityCodeException;
ingo@1107: import org.opengis.referencing.crs.CoordinateReferenceSystem;
ingo@1106:
ingo@1106: import org.geotools.data.DataStoreFactorySpi;
ingo@1106: import org.geotools.data.FeatureStore;
ingo@1106: import org.geotools.data.DefaultTransaction;
ingo@1106: import org.geotools.data.Transaction;
ingo@1106: import org.geotools.data.shapefile.ShapefileDataStore;
ingo@1106: import org.geotools.data.shapefile.ShapefileDataStoreFactory;
ingo@1106: import org.geotools.feature.FeatureIterator;
ingo@1106: import org.geotools.feature.FeatureCollection;
ingo@1106: import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
ingo@1106: import org.geotools.geojson.feature.FeatureJSON;
ingo@1108: import org.geotools.referencing.CRS;
ingo@1052:
ingo@1052:
ingo@1052: public class GeometryUtils {
ingo@1052:
ingo@1052: private GeometryUtils() {
ingo@1052: }
ingo@1052:
ingo@1052:
ingo@1052: /**
ingo@1052: * Returns the boundary of Geometry geom in OpenLayers
ingo@1052: * representation.
ingo@1052: *
ingo@1052: * @param geom The geometry.
ingo@1052: *
ingo@1052: * @return the OpenLayers boundary of geom.
ingo@1052: */
ingo@1052: public static String jtsBoundsToOLBounds(Geometry geom) {
ingo@1052: Coordinate[] c = geom != null ? geom.getCoordinates() : null;
ingo@1052:
ingo@1052: if (c == null || c.length < 2) {
ingo@1052: return null;
ingo@1052: }
ingo@1052:
ingo@1052: return "" + c[0].x + " " + c[1].y + " " + c[1].x + " " + c[0].y;
ingo@1052: }
ingo@1106:
ingo@1106:
ingo@1107: public static SimpleFeatureType buildFeatureType(
ingo@1108: String name, String srs, Class geometryType)
ingo@1107: {
ingo@1108: try {
ingo@1108: SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
ingo@1108: CoordinateReferenceSystem crs = CRS.decode(srs);
ingo@1106:
ingo@1108: builder.setName("flys");
ingo@1108: builder.setNamespaceURI("http://www.intevation.de/");
ingo@1108: builder.setCRS(crs);
ingo@1108: builder.setSRS(srs);
ingo@1106:
ingo@1108: builder.add("geometry", geometryType, crs);
ingo@1106:
ingo@1108: return builder.buildFeatureType();
ingo@1108: }
ingo@1108: catch (NoSuchAuthorityCodeException nsae) {
ingo@1108: }
ingo@1108: catch (FactoryException fe) {
ingo@1108: }
ingo@1108:
ingo@1108: return null;
ingo@1106: }
ingo@1106:
ingo@1106:
ingo@1106: public static List parseGeoJSON(
ingo@1106: String geojson, SimpleFeatureType ft
ingo@1106: ) {
ingo@1106: List collection = new ArrayList();
ingo@1106:
ingo@1106: try {
ingo@1106: FeatureJSON fjson = new FeatureJSON();
ingo@1106: fjson.setFeatureType(ft);
ingo@1106:
ingo@1106: FeatureIterator iterator =
ingo@1106: fjson.streamFeatureCollection(geojson);
ingo@1106:
ingo@1106: while (iterator.hasNext()) {
ingo@1106: collection.add(iterator.next());
ingo@1106: }
ingo@1106: }
ingo@1106: catch (IOException ioe) {
ingo@1106: // TODO handle exception
ingo@1106: }
ingo@1106:
ingo@1106: return collection;
ingo@1106: }
ingo@1106:
ingo@1106:
ingo@1106: public static boolean writeShapefile(
ingo@1106: File shape,
ingo@1106: SimpleFeatureType featureType,
ingo@1106: FeatureCollection collection)
ingo@1106: throws MalformedURLException, IOException
ingo@1106: {
ingo@1106: Map params = new HashMap();
ingo@1106: params.put("url", shape.toURI().toURL());
ingo@1106: params.put("create spatial index", Boolean.TRUE);
ingo@1106:
ingo@1106: DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
ingo@1106:
ingo@1106: ShapefileDataStore newDataStore =
ingo@1106: (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
ingo@1106: newDataStore.createSchema(featureType);
ingo@1106:
ingo@1106: Transaction transaction = new DefaultTransaction("create");
ingo@1106:
ingo@1106: String typeName = newDataStore.getTypeNames()[0];
ingo@1106:
ingo@1106: FeatureStore featureStore =
ingo@1106: (FeatureStore)
ingo@1106: newDataStore.getFeatureSource(typeName);
ingo@1106:
ingo@1106: featureStore.setTransaction(transaction);
ingo@1106:
ingo@1106: featureStore.addFeatures(collection);
ingo@1106: transaction.commit();
ingo@1106:
ingo@1106: return true;
ingo@1106: }
ingo@1052: }
ingo@1052: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :