Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / writers / WriterGT2Shp.java @ 4313

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

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

    
42
        FilterFactory filterFactory = FilterFactory.createFilterFactory();
43
        FLyrVect lyrVect;
44
        boolean bFromShp;
45
        File file;
46
        FeatureStore featStore;
47
        AttributeType[] types;
48
        Transaction t;
49
        int numReg = 0;
50
        
51
        public WriterGT2Shp(FLyrVect lyrVect) throws IOException
52
        {
53
                this.lyrVect = lyrVect;
54
                VectorialEditableAdapter vea = (VectorialEditableAdapter) lyrVect.getSource();
55
                VectorialDriver vd = vea.getOriginalAdapter().getDriver();
56
                bFromShp = false;
57
                if (vd instanceof VectorialFileDriver)
58
                {
59
                        VectorialFileDriver vfd = (VectorialFileDriver) vd;
60
                        file = vfd.getFile();
61
                        String filePath = file.getAbsolutePath(); 
62
                        if ((filePath.endsWith(".shp"))
63
                                        || (filePath.endsWith(".SHP")))
64
                        {
65
                                bFromShp = true;
66
                        }
67
                }
68
                
69
        }
70
        
71
        /* (non-Javadoc)
72
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
73
         */
74
        public void preProcess() throws EditionException {
75
//                feature attributes creation
76
                URL theUrl;
77
                try {
78
                        theUrl = file.toURL();
79
                        ShapefileDataStore dataStore = new ShapefileDataStore(theUrl);
80
                        String featureName = dataStore.getTypeNames()[0];
81
                        featStore = (FeatureStore) dataStore.getFeatureSource(featureName);
82
                        types = featStore.getSchema().getAttributeTypes();
83
                        t = new DefaultTransaction("handle");
84
                        featStore.setTransaction(t);
85
              
86
                        t.addAuthorization("handle");  // provide authoriztion
87
                        
88
                        
89
                        // types = new AttributeType[lyrVect.getRecordset().getFieldCount() +1];
90
                } catch (Exception e) {
91
                        e.printStackTrace();
92
                        throw new EditionException(e);
93
                }
94

    
95

    
96
        }
97

    
98
        /* (non-Javadoc)
99
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#process(com.iver.cit.gvsig.fmap.edition.IRowEdited)
100
         */
101
        public void process(IRowEdited row) throws EditionException {
102
                
103
                IFeature feat = (IFeature) row.getLinkedRow();
104
                Object[] values = new Object[types.length];
105
                values[0] = feat.getGeometry().toJTSGeometry();
106
                for (int i=1; i < types.length; i++)
107
                        values[i] = feat.getAttribute(i);
108

    
109
                Filter theFilter = filterFactory.createFidFilter(feat.getID()); 
110
        try {
111
                
112
                // Aqu? habr?a que mirar si es una modificaci?n, a?adido o borrado
113
                if ((numReg % 2) == 0)
114
                        featStore.modifyFeatures(types, values, theFilter);
115
                else
116
                        featStore.removeFeatures(theFilter);
117
                        numReg++;
118
                } catch (IOException e) {
119
                        e.printStackTrace();
120
                        throw new EditionException(e);
121
                }
122
                                
123
                
124
                        
125

    
126
        }
127

    
128
        /* (non-Javadoc)
129
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#postProcess()
130
         */
131
        public void postProcess() throws EditionException {
132
                try
133
                {
134
                        t.commit(); // commit opperations
135
                }
136
                catch (IOException io){
137
                        try {
138
                                t.rollback();
139
                        } catch (IOException e) {
140
                                e.printStackTrace();
141
                                throw new EditionException(e);
142
                        } // cancel opperations
143
                }
144
                finally {
145
                        try {
146
                                t.close();
147
                        } catch (IOException e) {
148
                                e.printStackTrace();
149
                                throw new EditionException(e);
150
                        } // free resources
151
                }
152

    
153
        }
154

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

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

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

    
198
                }
199
                
200
                return false;
201
        }
202
        
203
}