svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / writers / WriterGT2Shp.java @ 10626
History | View | Annotate | Download (5.6 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.iver.cit.gvsig.writers; |
5 |
|
6 |
import java.io.File; |
7 |
import java.io.IOException; |
8 |
import java.net.URL; |
9 |
import java.sql.Types; |
10 |
|
11 |
import org.geotools.data.DefaultTransaction; |
12 |
import org.geotools.data.FeatureStore; |
13 |
import org.geotools.data.Transaction; |
14 |
import org.geotools.data.shapefile.ShapefileDataStore; |
15 |
import org.geotools.feature.AttributeType; |
16 |
import org.geotools.filter.Filter; |
17 |
import org.geotools.filter.FilterFactory; |
18 |
|
19 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
20 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException; |
21 |
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
22 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
23 |
import com.iver.cit.gvsig.fmap.core.FShape; |
24 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
25 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
26 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
27 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
28 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
29 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
30 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
31 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
32 |
|
33 |
/**
|
34 |
* @author fjp
|
35 |
*
|
36 |
* Example of using a Geotools dataStore to write ONLY
|
37 |
* the modified features. So: you put the theme in editing mode,
|
38 |
* add, modify or delete features and when you come back to
|
39 |
* non editing mode, the changes will be saved into the original
|
40 |
* shapefile.
|
41 |
*
|
42 |
*/
|
43 |
public class WriterGT2Shp extends AbstractWriter { |
44 |
|
45 |
FilterFactory filterFactory = FilterFactory.createFilterFactory(); |
46 |
FLyrVect lyrVect; |
47 |
boolean bFromShp;
|
48 |
File file;
|
49 |
FeatureStore featStore; |
50 |
AttributeType[] types;
|
51 |
Transaction t; |
52 |
int numReg = 0; |
53 |
|
54 |
public WriterGT2Shp(FLyrVect lyrVect) throws IOException |
55 |
{ |
56 |
this.lyrVect = lyrVect;
|
57 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) lyrVect.getSource(); |
58 |
VectorialDriver vd = vea.getOriginalAdapter().getDriver(); |
59 |
bFromShp = false;
|
60 |
if (vd instanceof VectorialFileDriver) |
61 |
{ |
62 |
VectorialFileDriver vfd = (VectorialFileDriver) vd; |
63 |
file = vfd.getFile(); |
64 |
String filePath = file.getAbsolutePath();
|
65 |
if ((filePath.endsWith(".shp")) |
66 |
|| (filePath.endsWith(".SHP")))
|
67 |
{ |
68 |
bFromShp = true;
|
69 |
} |
70 |
} |
71 |
|
72 |
} |
73 |
|
74 |
/* (non-Javadoc)
|
75 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
|
76 |
*/
|
77 |
public void preProcess() throws StartWriterVisitorException { |
78 |
// feature attributes creation
|
79 |
URL theUrl;
|
80 |
try {
|
81 |
theUrl = file.toURL(); |
82 |
ShapefileDataStore dataStore = new ShapefileDataStore(theUrl);
|
83 |
String featureName = dataStore.getTypeNames()[0]; |
84 |
featStore = (FeatureStore) dataStore.getFeatureSource(featureName); |
85 |
types = featStore.getSchema().getAttributeTypes(); |
86 |
t = new DefaultTransaction("handle"); |
87 |
featStore.setTransaction(t); |
88 |
|
89 |
t.addAuthorization("handle"); // provide authoriztion |
90 |
|
91 |
|
92 |
// types = new AttributeType[lyrVect.getRecordset().getFieldCount() +1];
|
93 |
} catch (IOException e) { |
94 |
throw new StartWriterVisitorException(getName(),e); |
95 |
} |
96 |
|
97 |
|
98 |
} |
99 |
|
100 |
/* (non-Javadoc)
|
101 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#process(com.iver.cit.gvsig.fmap.edition.IRowEdited)
|
102 |
*/
|
103 |
public void process(IRowEdited row) throws ProcessWriterVisitorException { |
104 |
|
105 |
IFeature feat = (IFeature) row.getLinkedRow(); |
106 |
Object[] values = new Object[types.length]; |
107 |
values[0] = feat.getGeometry().toJTSGeometry();
|
108 |
for (int i=1; i < types.length; i++) |
109 |
values[i] = feat.getAttribute(i); |
110 |
|
111 |
Filter theFilter = filterFactory.createFidFilter(feat.getID());
|
112 |
try {
|
113 |
|
114 |
// Aqu? habr?a que mirar si es una modificaci?n, a?adido o borrado
|
115 |
if ((numReg % 2) == 0) |
116 |
featStore.modifyFeatures(types, values, theFilter); |
117 |
else
|
118 |
featStore.removeFeatures(theFilter); |
119 |
numReg++; |
120 |
} catch (IOException e) { |
121 |
throw new ProcessWriterVisitorException(getName(),e); |
122 |
} |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
} |
128 |
|
129 |
/* (non-Javadoc)
|
130 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#postProcess()
|
131 |
*/
|
132 |
public void postProcess() throws StopWriterVisitorException { |
133 |
try
|
134 |
{ |
135 |
t.commit(); // commit opperations
|
136 |
} |
137 |
catch (IOException io){ |
138 |
try {
|
139 |
t.rollback(); |
140 |
} catch (IOException e) { |
141 |
throw new StopWriterVisitorException(getName(),e); |
142 |
} // cancel opperations
|
143 |
} |
144 |
finally {
|
145 |
try {
|
146 |
t.close(); |
147 |
} catch (IOException e) { |
148 |
throw new StopWriterVisitorException(getName(),e); |
149 |
} // free resources
|
150 |
} |
151 |
|
152 |
} |
153 |
|
154 |
public String getName() { |
155 |
return "Shp Writer from Geotools"; |
156 |
} |
157 |
|
158 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
159 |
switch (gvSIGgeometryType)
|
160 |
{ |
161 |
case FShape.POINT:
|
162 |
return true; |
163 |
case FShape.LINE:
|
164 |
return true; |
165 |
case FShape.POLYGON:
|
166 |
return true; |
167 |
case FShape.ARC:
|
168 |
return false; |
169 |
case FShape.ELLIPSE:
|
170 |
return false; |
171 |
case FShape.MULTIPOINT:
|
172 |
return true; |
173 |
case FShape.TEXT:
|
174 |
return false; |
175 |
} |
176 |
return false; |
177 |
} |
178 |
|
179 |
public boolean canWriteAttribute(int sqlType) { |
180 |
switch (sqlType)
|
181 |
{ |
182 |
case Types.DOUBLE: |
183 |
case Types.FLOAT: |
184 |
case Types.INTEGER: |
185 |
case Types.BIGINT: |
186 |
return true; |
187 |
case Types.DATE: |
188 |
return true; |
189 |
case Types.BIT: |
190 |
case Types.BOOLEAN: |
191 |
return true; |
192 |
case Types.VARCHAR: |
193 |
case Types.CHAR: |
194 |
case Types.LONGVARCHAR: |
195 |
return true; // TODO: Revisar esto, porque no creo que admita campos muy grandes |
196 |
|
197 |
} |
198 |
|
199 |
return false; |
200 |
} |
201 |
|
202 |
public void setFlatness(double flatness) { |
203 |
// TODO Auto-generated method stub
|
204 |
|
205 |
} |
206 |
|
207 |
public void initialize(ITableDefinition tableDefinition) throws InitializeWriterException { |
208 |
super.initialize(tableDefinition);
|
209 |
|
210 |
} |
211 |
|
212 |
public boolean canAlterTable() { |
213 |
// TODO Auto-generated method stub
|
214 |
return false; |
215 |
} |
216 |
|
217 |
public boolean canSaveEdits() { |
218 |
// TODO Auto-generated method stub
|
219 |
return true; |
220 |
} |
221 |
|
222 |
} |