sascha@465: package de.intevation.gnv.utils;
sascha@465:
sascha@465: import java.io.File;
sascha@465: import java.io.IOException;
sascha@498: import java.io.Serializable;
sascha@465: import java.net.MalformedURLException;
ingo@760: import java.text.NumberFormat;
tim@649: import java.util.Collection;
sascha@498: import java.util.Date;
sascha@498: import java.util.HashMap;
sascha@498: import java.util.List;
sascha@498: import java.util.Map;
sascha@465:
sascha@498: import org.apache.log4j.Logger;
sascha@498: import org.geotools.data.DataStoreFactorySpi;
sascha@465: import org.geotools.data.DataUtilities;
sascha@465: import org.geotools.data.DefaultTransaction;
sascha@498: import org.geotools.data.FeatureStore;
sascha@498: import org.geotools.data.Transaction;
sascha@465: import org.geotools.data.shapefile.ShapefileDataStore;
sascha@465: import org.geotools.data.shapefile.ShapefileDataStoreFactory;
sascha@498: import org.geotools.feature.FeatureCollection;
sascha@498: import org.geotools.feature.FeatureCollections;
sascha@498: import org.geotools.feature.SchemaException;
sascha@498: import org.geotools.feature.simple.SimpleFeatureBuilder;
sascha@498: import org.geotools.referencing.crs.DefaultGeographicCRS;
sascha@498: import org.opengis.feature.simple.SimpleFeature;
sascha@498: import org.opengis.feature.simple.SimpleFeatureType;
sascha@465:
tim@799: import com.vividsolutions.jts.geom.Geometry;
tim@799: import com.vividsolutions.jts.geom.MultiLineString;
tim@799: import com.vividsolutions.jts.geom.MultiPolygon;
tim@799: import com.vividsolutions.jts.io.ParseException;
tim@799: import com.vividsolutions.jts.io.WKTReader;
tim@799:
tim@799: import de.intevation.gnv.geobackend.base.Result;
tim@799: import de.intevation.gnv.geobackend.base.ResultDescriptor;
tim@799:
sascha@465: /**
sascha@780: * @author Sascha L. Teichmann
sascha@465: */
sascha@465: public final class ShapeFileWriter
sascha@465: {
sascha@465: private static Logger log = Logger.getLogger(
sascha@465: ShapeFileWriter.class);
sascha@465:
ingo@760: private static NumberFormat format = NumberFormat.getInstance();
ingo@760:
ingo@806: /**
ingo@806: * Precision used to format double values.
ingo@806: */
ingo@760: public static final int DOUBLE_PRECISION = 3;
ingo@760:
ingo@761: static {
ingo@761: format.setMaximumFractionDigits(DOUBLE_PRECISION);
ingo@761: }
ingo@761:
sascha@465: private ShapeFileWriter() {
sascha@465: }
sascha@465:
ingo@806:
ingo@806: /**
ingo@806: * Write multilinestrings to shapefile.
ingo@806: *
ingo@806: * @param shapeFile Shapefile.
ingo@806: * @param parameterId The parameter id.
ingo@806: * @param layer The layer.
ingo@806: * @param date The date.
ingo@806: * @param multiLineStrings The multilinestring.
ingo@806: * @return true, if shapefile writing was successful - otherwise false.
ingo@806: */
sascha@465: public static boolean writeMultiLineStringsToFile(
sascha@465: File shapeFile,
sascha@498: Integer parameterId,
sascha@498: Integer layer,
sascha@498: Date date,
sascha@465: List> multiLineStrings
sascha@465: ) {
sascha@498: return writeMultiLineStringsToFile(
sascha@498: shapeFile,
sascha@498: parameterId,
sascha@498: layer,
sascha@498: date,
sascha@778: multiLineStrings,
sascha@498: "isolines");
sascha@465: }
sascha@465:
ingo@806:
ingo@806: /**
ingo@806: * Write multilinestrings to shapefile.
ingo@806: *
ingo@806: * @param shapeFile Shapefile.
ingo@806: * @param parameterId The parameter id.
ingo@806: * @param layer The layer.
ingo@806: * @param date The date.
ingo@806: * @param multiLineStrings The multilinestring.
ingo@806: * @param name A name.
ingo@806: * @return true, if shapefile writing was successful - otherwise false.
ingo@806: */
sascha@465: public static boolean writeMultiLineStringsToFile(
sascha@465: File shapeFile,
sascha@498: Integer parameterId,
sascha@498: Integer layer,
sascha@498: Date date,
sascha@465: List> multiLineStrings,
sascha@498: String name
sascha@465: ) {
sascha@498: Map params = new HashMap();
sascha@465:
sascha@465: try {
sascha@498: params.put("url", shapeFile.toURI().toURL());
sascha@465: }
sascha@465: catch (MalformedURLException mue) {
sascha@465: log.error(mue.getLocalizedMessage(), mue);
sascha@465: return false;
sascha@465: }
sascha@465:
sascha@498: params.put("create spatial index", Boolean.TRUE);
sascha@465:
sascha@465:
sascha@498: if (name == null) {
sascha@498: name = shapeFile.getName();
sascha@498: }
sascha@465:
sascha@498: SimpleFeatureType TYPE;
sascha@778:
sascha@778: try {
sascha@498: TYPE = DataUtilities.createType(
sascha@498: name,
sascha@498: "geom:MultiLineString:srid=4326," +
sascha@778: "PARAMETER:Integer," +
sascha@498: "LAYER:Integer," +
sascha@498: "DATE:Date," +
ingo@760: "VALUE:Double," +
ingo@760: "DESC:String");
sascha@465: }
sascha@465: catch (SchemaException se) {
sascha@465: log.error(se.getLocalizedMessage(), se);
sascha@498: return false;
sascha@465: }
sascha@498:
sascha@498: SimpleFeatureBuilder featureBuilder =
sascha@498: new SimpleFeatureBuilder(TYPE);
sascha@498:
sascha@778: FeatureCollection collection =
sascha@498: FeatureCollections.newCollection();
sascha@498:
sascha@498: for (Pair