Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 465:f7038820df2e
Added support to trace rasters to JTS multi polygons and multi line strings.
Write them to shape files with GeoTools.
gnv-artifacts/trunk@526 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 11 Jan 2010 00:29:45 +0000 |
parents | |
children | 4080b57dcb52 |
comparison
equal
deleted
inserted
replaced
464:70df44021a9f | 465:f7038820df2e |
---|---|
1 package de.intevation.gnv.utils; | |
2 | |
3 import java.util.Map; | |
4 import java.util.List; | |
5 import java.util.HashMap; | |
6 | |
7 import java.io.File; | |
8 import java.io.IOException; | |
9 | |
10 import java.net.MalformedURLException; | |
11 | |
12 import com.vividsolutions.jts.geom.MultiPolygon; | |
13 import com.vividsolutions.jts.geom.MultiLineString; | |
14 | |
15 import org.geotools.data.DataUtilities; | |
16 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 | |
25 import org.geotools.data.shapefile.ShapefileDataStore; | |
26 import org.geotools.data.shapefile.ShapefileDataStoreFactory; | |
27 | |
28 import org.apache.log4j.Logger; | |
29 | |
30 /** | |
31 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | |
32 */ | |
33 public final class ShapeFileWriter | |
34 { | |
35 private static Logger log = Logger.getLogger( | |
36 ShapeFileWriter.class); | |
37 | |
38 private ShapeFileWriter() { | |
39 } | |
40 | |
41 public static boolean writeMultiLineStringsToFile( | |
42 File shapeFile, | |
43 List<Pair<Object, MultiLineString>> multiLineStrings | |
44 ) { | |
45 return writeMultiLineStringsToFile(shapeFile, multiLineStrings, null); | |
46 } | |
47 | |
48 public static boolean writeMultiLineStringsToFile( | |
49 File shapeFile, | |
50 List<Pair<Object, MultiLineString>> multiLineStrings, | |
51 String typeName | |
52 ) { | |
53 HashMap params = new HashMap(); | |
54 | |
55 try { | |
56 params.put( | |
57 ShapefileDataStoreFactory.URLP.key, | |
58 shapeFile.toURI().toURL()); | |
59 } | |
60 catch (MalformedURLException mue) { | |
61 log.error(mue.getLocalizedMessage(), mue); | |
62 return false; | |
63 } | |
64 | |
65 boolean success = false; | |
66 Transaction tx = null; | |
67 | |
68 try { | |
69 ShapefileDataStoreFactory sfdsf = | |
70 new ShapefileDataStoreFactory(); | |
71 | |
72 ShapefileDataStore sfds = | |
73 (ShapefileDataStore)sfdsf.createNewDataStore(params); | |
74 | |
75 if (typeName == null) { | |
76 typeName = shapeFile.getName(); | |
77 } | |
78 | |
79 FeatureType featureType = DataUtilities.createType( | |
80 typeName, | |
81 "geom:MultiLineString,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 } | |
104 catch (SchemaException se) { | |
105 log.error(se.getLocalizedMessage(), se); | |
106 } | |
107 catch (IOException ioe) { | |
108 log.error(ioe.getLocalizedMessage(), ioe); | |
109 } | |
110 finally { | |
111 if (!success && tx != null) { | |
112 try { tx.rollback(); } | |
113 catch (IOException ioe) {} | |
114 } | |
115 } | |
116 | |
117 if (tx != null) { | |
118 try { tx.close(); } | |
119 catch (IOException ioe) {} | |
120 } | |
121 | |
122 return success; | |
123 } | |
124 | |
125 public static boolean writeMultiPolygonsToFile( | |
126 File shapeFile, | |
127 Map<Integer, MultiPolygon> multiPolygons | |
128 ) { | |
129 return writeMultiPolygonsToFile(shapeFile, multiPolygons, null); | |
130 } | |
131 | |
132 public static boolean writeMultiPolygonsToFile( | |
133 File shapeFile, | |
134 Map<Integer, MultiPolygon> multiPolygons, | |
135 String typeName | |
136 ) { | |
137 HashMap params = new HashMap(); | |
138 | |
139 try { | |
140 params.put( | |
141 ShapefileDataStoreFactory.URLP.key, | |
142 shapeFile.toURI().toURL()); | |
143 } | |
144 catch (MalformedURLException mue) { | |
145 log.error(mue.getLocalizedMessage(), mue); | |
146 return false; | |
147 } | |
148 | |
149 boolean success = false; | |
150 Transaction tx = null; | |
151 | |
152 try { | |
153 ShapefileDataStoreFactory sfdsf = | |
154 new ShapefileDataStoreFactory(); | |
155 | |
156 ShapefileDataStore sfds = | |
157 (ShapefileDataStore)sfdsf.createNewDataStore(params); | |
158 | |
159 if (typeName == null) { | |
160 typeName = shapeFile.getName(); | |
161 } | |
162 | |
163 FeatureType featureType = DataUtilities.createType( | |
164 typeName, | |
165 "geom:MultiPolygon,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 } | |
189 catch (SchemaException se) { | |
190 log.error(se.getLocalizedMessage(), se); | |
191 } | |
192 catch (IOException ioe) { | |
193 log.error(ioe.getLocalizedMessage(), ioe); | |
194 } | |
195 finally { | |
196 if (!success && tx != null) { | |
197 try { tx.rollback(); } | |
198 catch (IOException ioe) {} | |
199 } | |
200 } | |
201 | |
202 if (tx != null) { | |
203 try { tx.close(); } | |
204 catch (IOException ioe) {} | |
205 } | |
206 | |
207 return success; | |
208 } | |
209 | |
210 private static final Double asDouble(Object a) { | |
211 if (a instanceof Double) | |
212 return (Double)a; | |
213 if (a instanceof Number) | |
214 return Double.valueOf(((Number)a).doubleValue()); | |
215 return 0d; | |
216 } | |
217 } | |
218 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |