comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 498:4080b57dcb52

Upgraded to Geotools 2.5.8. Write polygons and line strings to shape files. gnv-artifacts/trunk@578 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 19 Jan 2010 21:23:28 +0000
parents f7038820df2e
children 4fc97074eb90
comparison
equal deleted inserted replaced
497:75306d856004 498:4080b57dcb52
1 package de.intevation.gnv.utils; 1 package de.intevation.gnv.utils;
2 2
3 import java.util.Map; 3 import com.vividsolutions.jts.geom.MultiLineString;
4 import java.util.List; 4 import com.vividsolutions.jts.geom.MultiPolygon;
5 import java.util.HashMap;
6 5
7 import java.io.File; 6 import java.io.File;
8 import java.io.IOException; 7 import java.io.IOException;
8 import java.io.Serializable;
9 9
10 import java.net.MalformedURLException; 10 import java.net.MalformedURLException;
11 11
12 import com.vividsolutions.jts.geom.MultiPolygon; 12 import java.util.Date;
13 import com.vividsolutions.jts.geom.MultiLineString; 13 import java.util.HashMap;
14 14 import java.util.List;
15 import java.util.Map;
16
17 import org.apache.log4j.Logger;
18
19 import org.geotools.data.DataStoreFactorySpi;
15 import org.geotools.data.DataUtilities; 20 import org.geotools.data.DataUtilities;
21 import org.geotools.data.DefaultTransaction;
22 import org.geotools.data.FeatureStore;
16 import org.geotools.data.Transaction; 23 import org.geotools.data.Transaction;
17 import org.geotools.data.DefaultTransaction;
18 import org.geotools.data.FeatureWriter;
19
20 import org.geotools.feature.Feature;
21 import org.geotools.feature.FeatureType;
22 import org.geotools.feature.SchemaException;
23 import org.geotools.feature.IllegalAttributeException;
24 24
25 import org.geotools.data.shapefile.ShapefileDataStore; 25 import org.geotools.data.shapefile.ShapefileDataStore;
26 import org.geotools.data.shapefile.ShapefileDataStoreFactory; 26 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
27 27
28 import org.apache.log4j.Logger; 28 import org.geotools.feature.FeatureCollection;
29 import org.geotools.feature.FeatureCollections;
30 import org.geotools.feature.SchemaException;
31
32 import org.geotools.feature.simple.SimpleFeatureBuilder;
33
34 import org.geotools.referencing.crs.DefaultGeographicCRS;
35
36 import org.opengis.feature.simple.SimpleFeature;
37 import org.opengis.feature.simple.SimpleFeatureType;
29 38
30 /** 39 /**
31 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) 40 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
32 */ 41 */
33 public final class ShapeFileWriter 42 public final class ShapeFileWriter
38 private ShapeFileWriter() { 47 private ShapeFileWriter() {
39 } 48 }
40 49
41 public static boolean writeMultiLineStringsToFile( 50 public static boolean writeMultiLineStringsToFile(
42 File shapeFile, 51 File shapeFile,
52 Integer parameterId,
53 Integer layer,
54 Date date,
43 List<Pair<Object, MultiLineString>> multiLineStrings 55 List<Pair<Object, MultiLineString>> multiLineStrings
44 ) { 56 ) {
45 return writeMultiLineStringsToFile(shapeFile, multiLineStrings, null); 57 return writeMultiLineStringsToFile(
58 shapeFile,
59 parameterId,
60 layer,
61 date,
62 multiLineStrings,
63 "isolines");
46 } 64 }
47 65
48 public static boolean writeMultiLineStringsToFile( 66 public static boolean writeMultiLineStringsToFile(
49 File shapeFile, 67 File shapeFile,
68 Integer parameterId,
69 Integer layer,
70 Date date,
50 List<Pair<Object, MultiLineString>> multiLineStrings, 71 List<Pair<Object, MultiLineString>> multiLineStrings,
51 String typeName 72 String name
52 ) { 73 ) {
53 HashMap params = new HashMap(); 74 Map<String, Serializable> params = new HashMap<String, Serializable>();
54 75
55 try { 76 try {
56 params.put( 77 params.put("url", shapeFile.toURI().toURL());
57 ShapefileDataStoreFactory.URLP.key,
58 shapeFile.toURI().toURL());
59 } 78 }
60 catch (MalformedURLException mue) { 79 catch (MalformedURLException mue) {
61 log.error(mue.getLocalizedMessage(), mue); 80 log.error(mue.getLocalizedMessage(), mue);
62 return false; 81 return false;
63 } 82 }
64 83
65 boolean success = false; 84 params.put("create spatial index", Boolean.TRUE);
66 Transaction tx = null; 85
67 86
68 try { 87 if (name == null) {
69 ShapefileDataStoreFactory sfdsf = 88 name = shapeFile.getName();
70 new ShapefileDataStoreFactory(); 89 }
71 90
72 ShapefileDataStore sfds = 91 SimpleFeatureType TYPE;
73 (ShapefileDataStore)sfdsf.createNewDataStore(params); 92
74 93 try {
75 if (typeName == null) { 94 TYPE = DataUtilities.createType(
76 typeName = shapeFile.getName(); 95 name,
77 } 96 "geom:MultiLineString:srid=4326," +
78 97 "PARAMETER:Integer," +
79 FeatureType featureType = DataUtilities.createType( 98 "LAYER:Integer," +
80 typeName, 99 "DATE:Date," +
81 "geom:MultiLineString,VALUE:Double"); 100 "VALUE:Double");
82
83 sfds.createSchema(featureType);
84
85 tx = new DefaultTransaction();
86
87 FeatureWriter featureWriter = sfds.getFeatureWriter(
88 typeName, tx);
89
90 for (Pair<Object, MultiLineString> pair: multiLineStrings) {
91 log.debug("00000000000000000 -> " + pair.getA());
92 Feature feature = featureWriter.next();
93 feature.setAttribute("geom", pair.getB());
94 feature.setAttribute("VALUE", asDouble(pair.getA()));
95 featureWriter.write();
96 }
97
98 tx.commit();
99 success = true;
100 }
101 catch (IllegalAttributeException iae) {
102 log.error(iae.getLocalizedMessage(), iae);
103 } 101 }
104 catch (SchemaException se) { 102 catch (SchemaException se) {
105 log.error(se.getLocalizedMessage(), se); 103 log.error(se.getLocalizedMessage(), se);
106 } 104 return false;
105 }
106
107 SimpleFeatureBuilder featureBuilder =
108 new SimpleFeatureBuilder(TYPE);
109
110 FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
111 FeatureCollections.newCollection();
112
113 for (Pair<Object, MultiLineString> pair: multiLineStrings) {
114 featureBuilder.add(pair.getB());
115 featureBuilder.add(parameterId);
116 featureBuilder.add(layer);
117 featureBuilder.add(date);
118 featureBuilder.add(pair.getA());
119 SimpleFeature feature = featureBuilder.buildFeature(null);
120 collection.add(feature);
121 }
122
123 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
124
125 Transaction transaction = null;
126
127 boolean success = false;
128 try {
129 ShapefileDataStore newDataStore =
130 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
131
132 newDataStore.createSchema(TYPE);
133 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
134
135 transaction = new DefaultTransaction("create");
136
137 String typeName = newDataStore.getTypeNames()[0];
138
139 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
140 (FeatureStore<SimpleFeatureType, SimpleFeature>)
141 newDataStore.getFeatureSource(typeName);
142
143 featureStore.setTransaction(transaction);
144
145 featureStore.addFeatures(collection);
146 transaction.commit();
147 success = true;
148 }
107 catch (IOException ioe) { 149 catch (IOException ioe) {
108 log.error(ioe.getLocalizedMessage(), ioe); 150 log.error(ioe.getLocalizedMessage(), ioe);
109 } 151 }
110 finally { 152 finally {
111 if (!success && tx != null) { 153 if (transaction != null) {
112 try { tx.rollback(); } 154 if (!success) {
155 try { transaction.rollback(); }
156 catch (IOException ioe) {}
157 }
158 try { transaction.close(); }
113 catch (IOException ioe) {} 159 catch (IOException ioe) {}
114 } 160 }
115 } 161 }
116 162
117 if (tx != null) {
118 try { tx.close(); }
119 catch (IOException ioe) {}
120 }
121
122 return success; 163 return success;
123 } 164 }
124 165
125 public static boolean writeMultiPolygonsToFile( 166 public static boolean writeMultiPolygonsToFile(
126 File shapeFile, 167 File shapeFile,
168 Integer parameterId,
169 Integer layer,
170 Date date,
127 Map<Integer, MultiPolygon> multiPolygons 171 Map<Integer, MultiPolygon> multiPolygons
128 ) { 172 ) {
129 return writeMultiPolygonsToFile(shapeFile, multiPolygons, null); 173 return writeMultiPolygonsToFile(
174 shapeFile,
175 parameterId,
176 layer,
177 date,
178 multiPolygons,
179 "polygons");
130 } 180 }
131 181
132 public static boolean writeMultiPolygonsToFile( 182 public static boolean writeMultiPolygonsToFile(
133 File shapeFile, 183 File shapeFile,
184 Integer parameterId,
185 Integer layer,
186 Date date,
134 Map<Integer, MultiPolygon> multiPolygons, 187 Map<Integer, MultiPolygon> multiPolygons,
135 String typeName 188 String name
136 ) { 189 ) {
137 HashMap params = new HashMap(); 190 Map<String, Serializable> params = new HashMap<String, Serializable>();
138 191
139 try { 192 try {
140 params.put( 193 params.put("url", shapeFile.toURI().toURL());
141 ShapefileDataStoreFactory.URLP.key,
142 shapeFile.toURI().toURL());
143 } 194 }
144 catch (MalformedURLException mue) { 195 catch (MalformedURLException mue) {
145 log.error(mue.getLocalizedMessage(), mue); 196 log.error(mue.getLocalizedMessage(), mue);
146 return false; 197 return false;
147 } 198 }
148 199
149 boolean success = false; 200 params.put("create spatial index", Boolean.TRUE);
150 Transaction tx = null; 201
151 202
152 try { 203 if (name == null) {
153 ShapefileDataStoreFactory sfdsf = 204 name = shapeFile.getName();
154 new ShapefileDataStoreFactory(); 205 }
155 206
156 ShapefileDataStore sfds = 207 SimpleFeatureType TYPE;
157 (ShapefileDataStore)sfdsf.createNewDataStore(params); 208
158 209 try {
159 if (typeName == null) { 210 TYPE = DataUtilities.createType(
160 typeName = shapeFile.getName(); 211 name,
161 } 212 "geom:MultiPolygon:srid=4326," +
162 213 "PARAMETER:Integer," +
163 FeatureType featureType = DataUtilities.createType( 214 "LAYER:Integer," +
164 typeName, 215 "DATE:Date," +
165 "geom:MultiPolygon,CLASS:Integer"); 216 "CLASS:Integer");
166
167 sfds.createSchema(featureType);
168
169 tx = new DefaultTransaction();
170
171 FeatureWriter featureWriter = sfds.getFeatureWriter(
172 typeName, tx);
173
174 for (Map.Entry<Integer, MultiPolygon> entry:
175 multiPolygons.entrySet()
176 ) {
177 Feature feature = featureWriter.next();
178 feature.setAttribute("geom", entry.getValue());
179 feature.setAttribute("CLASS", entry.getKey());
180 featureWriter.write();
181 }
182
183 tx.commit();
184 success = true;
185 }
186 catch (IllegalAttributeException iae) {
187 log.error(iae.getLocalizedMessage(), iae);
188 } 217 }
189 catch (SchemaException se) { 218 catch (SchemaException se) {
190 log.error(se.getLocalizedMessage(), se); 219 log.error(se.getLocalizedMessage(), se);
191 } 220 return false;
221 }
222
223 SimpleFeatureBuilder featureBuilder =
224 new SimpleFeatureBuilder(TYPE);
225
226 FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
227 FeatureCollections.newCollection();
228
229 for (Map.Entry<Integer, MultiPolygon> entry:
230 multiPolygons.entrySet()
231 ) {
232 featureBuilder.add(entry.getValue());
233 featureBuilder.add(parameterId);
234 featureBuilder.add(layer);
235 featureBuilder.add(date);
236 featureBuilder.add(entry.getKey());
237 SimpleFeature feature = featureBuilder.buildFeature(null);
238 collection.add(feature);
239 }
240
241 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
242
243 Transaction transaction = null;
244
245 boolean success = false;
246 try {
247 ShapefileDataStore newDataStore =
248 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
249
250 newDataStore.createSchema(TYPE);
251 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
252
253 transaction = new DefaultTransaction("create");
254
255 String typeName = newDataStore.getTypeNames()[0];
256
257 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
258 (FeatureStore<SimpleFeatureType, SimpleFeature>)
259 newDataStore.getFeatureSource(typeName);
260
261 featureStore.setTransaction(transaction);
262
263 featureStore.addFeatures(collection);
264 transaction.commit();
265 success = true;
266 }
192 catch (IOException ioe) { 267 catch (IOException ioe) {
193 log.error(ioe.getLocalizedMessage(), ioe); 268 log.error(ioe.getLocalizedMessage(), ioe);
194 } 269 }
195 finally { 270 finally {
196 if (!success && tx != null) { 271 if (transaction != null) {
197 try { tx.rollback(); } 272 if (!success) {
273 try { transaction.rollback(); }
274 catch (IOException ioe) {}
275 }
276 try { transaction.close(); }
198 catch (IOException ioe) {} 277 catch (IOException ioe) {}
199 } 278 }
200 } 279 }
201 280
202 if (tx != null) {
203 try { tx.close(); }
204 catch (IOException ioe) {}
205 }
206
207 return success; 281 return success;
208 } 282 }
209 283
210 private static final Double asDouble(Object a) { 284 private static final Double asDouble(Object a) {
211 if (a instanceof Double) 285 if (a instanceof Double) {
212 return (Double)a; 286 return (Double)a;
213 if (a instanceof Number) 287 }
288 if (a instanceof Number) {
214 return Double.valueOf(((Number)a).doubleValue()); 289 return Double.valueOf(((Number)a).doubleValue());
290 }
215 return 0d; 291 return 0d;
216 } 292 }
217 } 293 }
218 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 294 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org