comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 799:feeaf5aec552

ISSUE213: Wrong Geometrytype used for the generation of an Layer with Multipolygon-Geometries gnv-artifacts/trunk@881 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 06 Apr 2010 11:56:53 +0000
parents c4156275c1e1
children 2cea76f1112e
comparison
equal deleted inserted replaced
798:6cff63d0c434 799:feeaf5aec552
1 package de.intevation.gnv.utils; 1 package de.intevation.gnv.utils;
2
3 import com.vividsolutions.jts.geom.Geometry;
4 import com.vividsolutions.jts.geom.MultiLineString;
5 import com.vividsolutions.jts.geom.MultiPolygon;
6
7 import com.vividsolutions.jts.io.ParseException;
8 import com.vividsolutions.jts.io.WKTReader;
9
10 import de.intevation.gnv.geobackend.base.Result;
11 import de.intevation.gnv.geobackend.base.ResultDescriptor;
12 2
13 import java.io.File; 3 import java.io.File;
14 import java.io.IOException; 4 import java.io.IOException;
15 import java.io.Serializable; 5 import java.io.Serializable;
16
17 import java.net.MalformedURLException; 6 import java.net.MalformedURLException;
18
19 import java.text.NumberFormat; 7 import java.text.NumberFormat;
20
21 import java.util.Collection; 8 import java.util.Collection;
22 import java.util.Date; 9 import java.util.Date;
23 import java.util.HashMap; 10 import java.util.HashMap;
24 import java.util.List; 11 import java.util.List;
25 import java.util.Map; 12 import java.util.Map;
26 13
27 import org.apache.log4j.Logger; 14 import org.apache.log4j.Logger;
28
29 import org.geotools.data.DataStoreFactorySpi; 15 import org.geotools.data.DataStoreFactorySpi;
30 import org.geotools.data.DataUtilities; 16 import org.geotools.data.DataUtilities;
31 import org.geotools.data.DefaultTransaction; 17 import org.geotools.data.DefaultTransaction;
32 import org.geotools.data.FeatureStore; 18 import org.geotools.data.FeatureStore;
33 import org.geotools.data.Transaction; 19 import org.geotools.data.Transaction;
34
35 import org.geotools.data.shapefile.ShapefileDataStore; 20 import org.geotools.data.shapefile.ShapefileDataStore;
36 import org.geotools.data.shapefile.ShapefileDataStoreFactory; 21 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
37
38 import org.geotools.feature.FeatureCollection; 22 import org.geotools.feature.FeatureCollection;
39 import org.geotools.feature.FeatureCollections; 23 import org.geotools.feature.FeatureCollections;
40 import org.geotools.feature.SchemaException; 24 import org.geotools.feature.SchemaException;
41
42 import org.geotools.feature.simple.SimpleFeatureBuilder; 25 import org.geotools.feature.simple.SimpleFeatureBuilder;
43
44 import org.geotools.referencing.crs.DefaultGeographicCRS; 26 import org.geotools.referencing.crs.DefaultGeographicCRS;
45
46 import org.opengis.feature.simple.SimpleFeature; 27 import org.opengis.feature.simple.SimpleFeature;
47 import org.opengis.feature.simple.SimpleFeatureType; 28 import org.opengis.feature.simple.SimpleFeatureType;
29
30 import com.vividsolutions.jts.geom.Geometry;
31 import com.vividsolutions.jts.geom.MultiLineString;
32 import com.vividsolutions.jts.geom.MultiPolygon;
33 import com.vividsolutions.jts.io.ParseException;
34 import com.vividsolutions.jts.io.WKTReader;
35
36 import de.intevation.gnv.geobackend.base.Result;
37 import de.intevation.gnv.geobackend.base.ResultDescriptor;
48 38
49 /** 39 /**
50 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> 40 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
51 */ 41 */
52 public final class ShapeFileWriter 42 public final class ShapeFileWriter
198 multiPolygons, 188 multiPolygons,
199 "polygons"); 189 "polygons");
200 } 190 }
201 191
202 192
203 public static String writeDataToFile(File shapeFile, 193 public static boolean writeDataToFile(File shapeFile,
204 String name, 194 String name,
205 Collection<Result> data){ 195 Collection<Result> data,
206 String geomType = null; 196 String geometryType){
207 197
208 WKTReader wktReader = new WKTReader(); 198 WKTReader wktReader = new WKTReader();
209 199
210 Map<String, Serializable> params = new HashMap<String, Serializable>(); 200 Map<String, Serializable> params = new HashMap<String, Serializable>();
211 201
212 try { 202 try {
213 params.put("url", shapeFile.toURI().toURL()); 203 params.put("url", shapeFile.toURI().toURL());
214 } 204 }
215 catch (MalformedURLException mue) { 205 catch (MalformedURLException mue) {
216 log.error(mue.getLocalizedMessage(), mue); 206 log.error(mue.getLocalizedMessage(), mue);
217 return null; 207 return false;
218 } 208 }
219 209
220 params.put("create spatial index", Boolean.TRUE); 210 params.put("create spatial index", Boolean.TRUE);
221 211
222 212
235 Geometry g = wktReader.read(result.getString(0)); 225 Geometry g = wktReader.read(result.getString(0));
236 ResultDescriptor rd = result.getResultDescriptor(); 226 ResultDescriptor rd = result.getResultDescriptor();
237 int columns = rd.getColumnCount(); 227 int columns = rd.getColumnCount();
238 if (type == null){ 228 if (type == null){
239 try { 229 try {
240 geomType = g.getGeometryType().toUpperCase(); 230 String schema = "geom:"+geometryType+":srid=4326";
241 String schema = "geom:"+g.getGeometryType()+":srid=4326";
242 for (int i = 1; i < columns; i++){ 231 for (int i = 1; i < columns; i++){
243 schema+=","+rd.getColumnName(i)+ 232 schema+=","+rd.getColumnName(i)+
244 ":"+rd.getColumnClassName(i); 233 ":"+rd.getColumnClassName(i);
245 } 234 }
246 type = DataUtilities.createType(name, schema); 235 type = DataUtilities.createType(name, schema);
247 } 236 }
248 catch (SchemaException se) { 237 catch (SchemaException se) {
249 log.error(se.getLocalizedMessage(), se); 238 log.error(se.getLocalizedMessage(), se);
250 return null; 239 return false;
251 } 240 }
252 featureBuilder = new SimpleFeatureBuilder(type); 241 featureBuilder = new SimpleFeatureBuilder(type);
253 } 242 }
254 featureBuilder.add(g); 243 featureBuilder.add(g);
255 for (int i = 1; i < columns; i++){ 244 for (int i = 1; i < columns; i++){
302 try { transaction.close(); } 291 try { transaction.close(); }
303 catch (IOException ioe) {} 292 catch (IOException ioe) {}
304 } 293 }
305 } 294 }
306 295
307 return geomType; 296 return true;
308 297
309 } 298 }
310 299
311 public static boolean writeMultiPolygonsToFile( 300 public static boolean writeMultiPolygonsToFile(
312 File shapeFile, 301 File shapeFile,

http://dive4elements.wald.intevation.org