Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / writers / WriterGT2Shp.java @ 6313

History | View | Annotate | Download (5.21 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.iver.cit.gvsig.fmap.core.FShape;
20
import com.iver.cit.gvsig.fmap.core.IFeature;
21
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
22
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
23
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
24
import com.iver.cit.gvsig.fmap.edition.EditionException;
25
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
26
import com.iver.cit.gvsig.fmap.edition.IWriter;
27
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
28
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter;
29
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
30

    
31
/**
32
 * @author fjp
33
 *
34
 * Example of using a Geotools dataStore to write ONLY
35
 * the modified features. So: you put the theme in editing mode,
36
 * add, modify or delete features and when you come back to
37
 * non editing mode, the changes will be saved into the original
38
 * shapefile.
39
 *
40
 */
41
public class WriterGT2Shp extends AbstractWriter {
42

    
43
        FilterFactory filterFactory = FilterFactory.createFilterFactory();
44
        FLyrVect lyrVect;
45
        boolean bFromShp;
46
        File file;
47
        FeatureStore featStore;
48
        AttributeType[] types;
49
        Transaction t;
50
        int numReg = 0;
51

    
52
        public WriterGT2Shp(FLyrVect lyrVect) throws IOException
53
        {
54
                this.lyrVect = lyrVect;
55
                VectorialEditableAdapter vea = (VectorialEditableAdapter) lyrVect.getSource();
56
                VectorialDriver vd = vea.getOriginalAdapter().getDriver();
57
                bFromShp = false;
58
                if (vd instanceof VectorialFileDriver)
59
                {
60
                        VectorialFileDriver vfd = (VectorialFileDriver) vd;
61
                        file = vfd.getFile();
62
                        String filePath = file.getAbsolutePath();
63
                        if ((filePath.endsWith(".shp"))
64
                                        || (filePath.endsWith(".SHP")))
65
                        {
66
                                bFromShp = true;
67
                        }
68
                }
69

    
70
        }
71

    
72
        /* (non-Javadoc)
73
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
74
         */
75
        public void preProcess() throws EditionException {
76
//                feature attributes creation
77
                URL theUrl;
78
                try {
79
                        theUrl = file.toURL();
80
                        ShapefileDataStore dataStore = new ShapefileDataStore(theUrl);
81
                        String featureName = dataStore.getTypeNames()[0];
82
                        featStore = (FeatureStore) dataStore.getFeatureSource(featureName);
83
                        types = featStore.getSchema().getAttributeTypes();
84
                        t = new DefaultTransaction("handle");
85
                        featStore.setTransaction(t);
86

    
87
                        t.addAuthorization("handle");  // provide authoriztion
88

    
89

    
90
                        // types = new AttributeType[lyrVect.getRecordset().getFieldCount() +1];
91
                } catch (Exception e) {
92
                        e.printStackTrace();
93
                        throw new EditionException(e);
94
                }
95

    
96

    
97
        }
98

    
99
        /* (non-Javadoc)
100
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#process(com.iver.cit.gvsig.fmap.edition.IRowEdited)
101
         */
102
        public void process(IRowEdited row) throws EditionException {
103

    
104
                IFeature feat = (IFeature) row.getLinkedRow();
105
                Object[] values = new Object[types.length];
106
                values[0] = feat.getGeometry().toJTSGeometry();
107
                for (int i=1; i < types.length; i++)
108
                        values[i] = feat.getAttribute(i);
109

    
110
                Filter theFilter = filterFactory.createFidFilter(feat.getID());
111
        try {
112

    
113
                // Aqu? habr?a que mirar si es una modificaci?n, a?adido o borrado
114
                if ((numReg % 2) == 0)
115
                        featStore.modifyFeatures(types, values, theFilter);
116
                else
117
                        featStore.removeFeatures(theFilter);
118
                        numReg++;
119
                } catch (IOException e) {
120
                        e.printStackTrace();
121
                        throw new EditionException(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 EditionException {
133
                try
134
                {
135
                        t.commit(); // commit opperations
136
                }
137
                catch (IOException io){
138
                        try {
139
                                t.rollback();
140
                        } catch (IOException e) {
141
                                e.printStackTrace();
142
                                throw new EditionException(e);
143
                        } // cancel opperations
144
                }
145
                finally {
146
                        try {
147
                                t.close();
148
                        } catch (IOException e) {
149
                                e.printStackTrace();
150
                                throw new EditionException(e);
151
                        } // free resources
152
                }
153

    
154
        }
155

    
156
        public String getName() {
157
                return "Shp Writer from Geotools";
158
        }
159

    
160
        public boolean canWriteGeometry(int gvSIGgeometryType) {
161
                switch (gvSIGgeometryType)
162
                {
163
                case FShape.POINT:
164
                        return true;
165
                case FShape.LINE:
166
                        return true;
167
                case FShape.POLYGON:
168
                        return true;
169
                case FShape.ARC:
170
                        return false;
171
                case FShape.ELLIPSE:
172
                        return false;
173
                case FShape.MULTIPOINT:
174
                        return true;
175
                case FShape.TEXT:
176
                        return false;
177
                }
178
                return false;
179
        }
180

    
181
        public boolean canWriteAttribute(int sqlType) {
182
                switch (sqlType)
183
                {
184
                case Types.DOUBLE:
185
                case Types.FLOAT:
186
                case Types.INTEGER:
187
                case Types.BIGINT:
188
                        return true;
189
                case Types.DATE:
190
                        return true;
191
                case Types.BIT:
192
                case Types.BOOLEAN:
193
                        return true;
194
                case Types.VARCHAR:
195
                case Types.CHAR:
196
                case Types.LONGVARCHAR:
197
                        return true; // TODO: Revisar esto, porque no creo que admita campos muy grandes
198

    
199
                }
200

    
201
                return false;
202
        }
203

    
204
        public void setFlatness(double flatness) {
205
                // TODO Auto-generated method stub
206

    
207
        }
208

    
209
        public void initialize(ITableDefinition tableDefinition) throws EditionException {
210
                super.initialize(tableDefinition);
211
                
212
        }
213

    
214
}