root / tags / v1_1_Build_1005 / extensions / extCAD / src / com / iver / cit / gvsig / writers / WriterGT2.java @ 12355
History | View | Annotate | Download (8.14 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.iver.cit.gvsig.writers; |
5 |
|
6 |
import java.io.IOException; |
7 |
import java.sql.Types; |
8 |
|
9 |
import org.geotools.data.DataUtilities; |
10 |
import org.geotools.data.DefaultTransaction; |
11 |
import org.geotools.data.FeatureReader; |
12 |
import org.geotools.data.FeatureStore; |
13 |
import org.geotools.data.Transaction; |
14 |
import org.geotools.factory.FactoryConfigurationError; |
15 |
import org.geotools.feature.AttributeType; |
16 |
import org.geotools.feature.AttributeTypeFactory; |
17 |
import org.geotools.feature.Feature; |
18 |
import org.geotools.feature.FeatureType; |
19 |
import org.geotools.feature.FeatureTypeBuilder; |
20 |
import org.geotools.feature.IllegalAttributeException; |
21 |
import org.geotools.feature.SchemaException; |
22 |
import org.geotools.filter.Filter; |
23 |
import org.geotools.filter.FilterFactory; |
24 |
|
25 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
26 |
import com.hardcode.gdbms.engine.values.NullValue; |
27 |
import com.iver.cit.gvsig.fmap.core.FShape; |
28 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
29 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
30 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
31 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
32 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
33 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
34 |
import com.vividsolutions.jts.geom.Geometry; |
35 |
import com.vividsolutions.jts.geom.MultiLineString; |
36 |
import com.vividsolutions.jts.geom.MultiPoint; |
37 |
import com.vividsolutions.jts.geom.MultiPolygon; |
38 |
import com.vividsolutions.jts.geom.Point; |
39 |
|
40 |
/**
|
41 |
* @author fjp
|
42 |
*
|
43 |
* Example of using a Geotools featureStore to write features
|
44 |
* Example of use: Inside the extension, open a dataStore and
|
45 |
* get the featureStore. Then create this class with it.
|
46 |
*
|
47 |
*/
|
48 |
public class WriterGT2 extends AbstractWriter { |
49 |
|
50 |
FilterFactory filterFactory = FilterFactory.createFilterFactory(); |
51 |
FeatureStore featStore; |
52 |
AttributeType[] types;
|
53 |
Transaction t; |
54 |
int numReg = 0; |
55 |
public static Class getClassBySqlTYPE(int type) |
56 |
{ |
57 |
switch (type)
|
58 |
{ |
59 |
case Types.SMALLINT: |
60 |
return Integer.class; |
61 |
case Types.INTEGER: |
62 |
return Integer.class; |
63 |
case Types.BIGINT: |
64 |
return Integer.class; |
65 |
case Types.BOOLEAN: |
66 |
return Boolean.class; |
67 |
case Types.DECIMAL: |
68 |
return Double.class; |
69 |
case Types.DOUBLE: |
70 |
return Double.class; |
71 |
case Types.FLOAT: |
72 |
return Double.class; |
73 |
case Types.CHAR: |
74 |
return Character.class; |
75 |
case Types.VARCHAR: |
76 |
return String.class; |
77 |
case Types.LONGVARCHAR: |
78 |
return String.class; |
79 |
} |
80 |
return NullValue.class;
|
81 |
} |
82 |
|
83 |
public static FeatureType getFeatureType(FLyrVect layer, String geomField, String featName) throws DriverException, com.iver.cit.gvsig.fmap.DriverException, FactoryConfigurationError, SchemaException { |
84 |
|
85 |
Class geomType = findBestGeometryClass(layer.getShapeType());
|
86 |
// geomType = Geometry.class;
|
87 |
AttributeType geom = AttributeTypeFactory.newAttributeType(geomField, geomType); |
88 |
int numFields = layer.getRecordset().getFieldCount() + 1; |
89 |
AttributeType[] att = new AttributeType[numFields]; |
90 |
att[0] = geom;
|
91 |
for (int i=1; i < numFields; i++) |
92 |
{ |
93 |
att[i] = AttributeTypeFactory.newAttributeType( |
94 |
layer.getRecordset().getFieldName(i-1),
|
95 |
getClassBySqlTYPE(layer.getRecordset().getFieldType(i-1)));
|
96 |
} |
97 |
FeatureType featType = FeatureTypeBuilder.newFeatureType(att,featName); |
98 |
return featType;
|
99 |
} |
100 |
|
101 |
public static final Class findBestGeometryClass(int layerType) { |
102 |
Class best = Geometry.class;
|
103 |
switch (layerType)
|
104 |
{ |
105 |
case FShape.LINE:
|
106 |
best = MultiLineString.class; |
107 |
break;
|
108 |
case FShape.MULTIPOINT:
|
109 |
best = MultiPoint.class; |
110 |
break;
|
111 |
case FShape.POINT:
|
112 |
best = Point.class;
|
113 |
break;
|
114 |
case FShape.POLYGON:
|
115 |
best = MultiPolygon.class; |
116 |
break;
|
117 |
case FShape.MULTI:
|
118 |
best = Geometry.class; |
119 |
break;
|
120 |
default:
|
121 |
throw new RuntimeException("Unknown gvSigShapeType->GeometryClass : " + layerType); |
122 |
} |
123 |
return best;
|
124 |
} |
125 |
|
126 |
|
127 |
public WriterGT2(FeatureStore featureStore, boolean writeAllFeatures) throws IOException |
128 |
{ |
129 |
this.featStore = featureStore;
|
130 |
this.bWriteAll = writeAllFeatures;
|
131 |
} |
132 |
|
133 |
/* (non-Javadoc)
|
134 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
|
135 |
*/
|
136 |
public void preProcess() throws EditionException { |
137 |
try {
|
138 |
types = featStore.getSchema().getAttributeTypes(); |
139 |
t = new DefaultTransaction("handle"); |
140 |
featStore.setTransaction(t); |
141 |
|
142 |
t.addAuthorization("handle"); // provide authoriztion |
143 |
|
144 |
|
145 |
} catch (Exception e) { |
146 |
e.printStackTrace(); |
147 |
throw new EditionException(e); |
148 |
} |
149 |
|
150 |
|
151 |
} |
152 |
|
153 |
/* (non-Javadoc)
|
154 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#process(com.iver.cit.gvsig.fmap.edition.IRowEdited)
|
155 |
*/
|
156 |
public void process(IRowEdited row) throws EditionException { |
157 |
|
158 |
IFeature feat = (IFeature) row.getLinkedRow(); |
159 |
// FeatureType featType = featStore.getSchema();
|
160 |
// TODO: OJO CON EL ORDEN DE LOS CAMPOS, QUE NO ES EL MISMO
|
161 |
Object[] values = new Object[types.length]; |
162 |
values[0] = feat.getGeometry().toJTSGeometry();
|
163 |
for (int i=1; i < types.length; i++) |
164 |
values[i] = feat.getAttribute(i-1);
|
165 |
|
166 |
Filter theFilter = filterFactory.createFidFilter(feat.getID());
|
167 |
try {
|
168 |
// System.out.println("Escribiendo numReg=" + numReg + " con STATUS=" + row.getStatus());
|
169 |
switch (row.getStatus())
|
170 |
{ |
171 |
case IRowEdited.STATUS_ADDED:
|
172 |
Feature featGT2 = featStore.getSchema().create(values); |
173 |
FeatureReader reader = DataUtilities.reader( |
174 |
new Feature[] {featGT2}); |
175 |
featStore.addFeatures(reader); |
176 |
break;
|
177 |
case IRowEdited.STATUS_MODIFIED:
|
178 |
featStore.modifyFeatures(types, values, theFilter); |
179 |
break;
|
180 |
case IRowEdited.STATUS_ORIGINAL:
|
181 |
if (bWriteAll)
|
182 |
{ |
183 |
featGT2 = featStore.getSchema().create(values); |
184 |
reader = DataUtilities.reader( |
185 |
new Feature[] {featGT2}); |
186 |
featStore.addFeatures(reader); |
187 |
} |
188 |
break;
|
189 |
case IRowEdited.STATUS_DELETED:
|
190 |
featStore.removeFeatures(theFilter); |
191 |
break;
|
192 |
} |
193 |
|
194 |
|
195 |
numReg++; |
196 |
} catch (IOException e) { |
197 |
e.printStackTrace(); |
198 |
throw new EditionException(e); |
199 |
} catch (IllegalAttributeException e) {
|
200 |
e.printStackTrace(); |
201 |
throw new EditionException(e); |
202 |
} |
203 |
|
204 |
|
205 |
|
206 |
|
207 |
} |
208 |
|
209 |
/* (non-Javadoc)
|
210 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#postProcess()
|
211 |
*/
|
212 |
public void postProcess() throws EditionException { |
213 |
try
|
214 |
{ |
215 |
t.commit(); // commit opperations
|
216 |
} |
217 |
catch (IOException io){ |
218 |
try {
|
219 |
t.rollback(); |
220 |
} catch (IOException e) { |
221 |
e.printStackTrace(); |
222 |
throw new EditionException(e); |
223 |
} // cancel opperations
|
224 |
} |
225 |
finally {
|
226 |
try {
|
227 |
t.close(); |
228 |
} catch (IOException e) { |
229 |
e.printStackTrace(); |
230 |
throw new EditionException(e); |
231 |
} // free resources
|
232 |
} |
233 |
|
234 |
} |
235 |
|
236 |
public String getName() { |
237 |
return "JDBC Writer from Geotools"; |
238 |
} |
239 |
|
240 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
241 |
switch (gvSIGgeometryType)
|
242 |
{ |
243 |
case FShape.POINT:
|
244 |
return true; |
245 |
case FShape.LINE:
|
246 |
return true; |
247 |
case FShape.POLYGON:
|
248 |
return true; |
249 |
case FShape.ARC:
|
250 |
return false; |
251 |
case FShape.ELLIPSE:
|
252 |
return false; |
253 |
case FShape.MULTIPOINT:
|
254 |
return true; |
255 |
case FShape.TEXT:
|
256 |
return false; |
257 |
} |
258 |
return false; |
259 |
} |
260 |
|
261 |
public boolean canWriteAttribute(int sqlType) { |
262 |
switch (sqlType)
|
263 |
{ |
264 |
case Types.DOUBLE: |
265 |
case Types.FLOAT: |
266 |
case Types.INTEGER: |
267 |
case Types.BIGINT: |
268 |
return true; |
269 |
case Types.DATE: |
270 |
return true; |
271 |
case Types.BIT: |
272 |
case Types.BOOLEAN: |
273 |
return true; |
274 |
case Types.VARCHAR: |
275 |
case Types.CHAR: |
276 |
case Types.LONGVARCHAR: |
277 |
return true; // TODO: Revisar esto, porque no creo que admita campos muy grandes |
278 |
|
279 |
} |
280 |
|
281 |
return false; |
282 |
} |
283 |
|
284 |
public void setFlatness(double flatness) { |
285 |
// TODO Auto-generated method stub
|
286 |
|
287 |
} |
288 |
|
289 |
public void initialize(ITableDefinition tableDefinition) throws EditionException { |
290 |
super.initialize(tableDefinition);
|
291 |
|
292 |
} |
293 |
|
294 |
public boolean canAlterTable() { |
295 |
// TODO Auto-generated method stub
|
296 |
return false; |
297 |
} |
298 |
|
299 |
public boolean canSaveEdits() { |
300 |
// TODO Auto-generated method stub
|
301 |
return true; |
302 |
} |
303 |
|
304 |
} |