Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / shp / ShpFeaturesWriter.java @ 21724

History | View | Annotate | Download (5.98 KB)

1
package org.gvsig.data.datastores.vectorial.file.shp;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.IOException;
6
import java.nio.channels.FileChannel;
7

    
8
import org.gvsig.data.DataException;
9
import org.gvsig.data.OpenException;
10
import org.gvsig.data.ReadException;
11
import org.gvsig.data.WriteException;
12
import org.gvsig.data.datastores.vectorial.file.dbf.DBFFeaturesWriter;
13
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFileHeader;
14
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHP;
15
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHPFileWrite;
16
import org.gvsig.data.vectorial.Feature;
17
import org.gvsig.data.vectorial.FeatureStore;
18
import org.gvsig.data.vectorial.FeatureType;
19
import org.gvsig.data.vectorial.InitializeWriterException;
20
import org.gvsig.data.vectorial.UnsupportedEncodingException;
21
import org.gvsig.fmap.geom.Geometry;
22
import org.gvsig.fmap.geom.primitive.NullGeometry;
23

    
24
public class ShpFeaturesWriter extends DBFFeaturesWriter {
25
        private File shpFile;
26
        private String shxPath;
27
        private String shpPath;
28
        private int shapeType;
29
        private int gvSIG_geometryType;
30
        private SHPFileWrite shpWrite;
31
        private int[] supportedGeometryTypes;
32
        private boolean bWriteHeaders=true;
33
        private Rectangle2D fullExtent;
34
        private int fileSize;
35

    
36
        public void init(FeatureStore store) throws InitializeWriterException {
37
                super.init(store);
38
                SHPStoreParameters parameters=(SHPStoreParameters)store.getParameters();
39
                shpFile=parameters.getFile();
40
                setFile(shpFile);
41

    
42
                try {
43
                        shpWrite = new SHPFileWrite((FileChannel) getWriteChannel(shpPath),
44
                                        (FileChannel) super.getWriteChannel(shxPath));
45
                } catch (IOException e) {
46
                        throw new InitializeWriterException(SHPStore.DATASTORE_NAME,e);
47
                }
48
                updateFeatureType(store.getDefaultFeatureType());
49
        }
50

    
51
        public void postProcess() throws OpenException, WriteException {
52
                super.postProcess();
53
                if (fullExtent == null)
54
                        fullExtent = new Rectangle2D.Double();
55
                        shpWrite.writeHeaders(fullExtent,
56
                                        shapeType, numRows, fileSize);
57
        }
58

    
59
        public void preProcess() throws WriteException, ReadException {
60
                super.preProcess();
61
                if (bWriteHeaders){
62
                                shpWrite.writeHeaders(new Rectangle2D.Double(),
63
                                        shapeType, 0, 0);
64
                }
65
                fullExtent = null;
66
        }
67

    
68
        public void insertFeature(Feature feature) throws WriteException, UnsupportedEncodingException {
69

    
70
                Geometry theGeom = (Geometry)feature.getDefaultGeometry();
71
                if (theGeom==null){
72
                        FeatureType type = feature.getType();
73
                        theGeom=(Geometry) type.get(type.getDefaultGeometry()).getDefaultValue();
74
                        if (theGeom == null){
75
                                theGeom = new NullGeometry();
76
                        }
77
                } 
78
                if (!canWriteGeometry(theGeom.getType())){
79
                        throw new WriteException("UnsupportedGeometryType: "+ theGeom.getGeometryType().getName(),SHPStore.DATASTORE_NAME);
80
                }
81
                super.insertFeature(feature);
82
                fileSize = shpWrite.writeIGeometry(theGeom);
83
                Rectangle2D boundsShp = theGeom.getBounds2D();
84
                if (boundsShp!=null){
85
                        if (fullExtent == null) {
86
                                fullExtent = boundsShp;
87
                        } else {
88
                                fullExtent.add(boundsShp);
89
                        }
90
                }
91

    
92
        }
93

    
94
        public void updateFeatureType(FeatureType featureType) {
95
                super.updateFeatureType(featureType);
96
                int type=featureType.getGeometryTypes()[0];
97
                shapeType = shpWrite.getShapeType(type);
98
                gvSIG_geometryType = type;
99
                setSupportedGeometryTypes();
100
        }
101

    
102
        public void setFile(File f)
103
        {
104
                shpPath = f.getAbsolutePath();
105

    
106
                shxPath = SHP.getShxFile(f).getAbsolutePath();
107

    
108
                super.setFile(SHP.getDbfFile(f));
109

    
110
//                dbfPath = SHP.getDbfFile(f).getAbsolutePath();
111

    
112
                shpFile = f;
113
        }
114
        private void setSupportedGeometryTypes() {
115
                switch (gvSIG_geometryType % Geometry.TYPES.Z)
116
                {
117
                case Geometry.TYPES.POINT:
118
                        supportedGeometryTypes = new int[] {Geometry.TYPES.POINT, Geometry.TYPES.NULL };
119
                        break;
120
                case Geometry.TYPES.MULTIPOINT:
121
                        supportedGeometryTypes = new int[] {Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL };
122
                        break;
123
                case Geometry.TYPES.CURVE:
124
                        supportedGeometryTypes = new int[] {Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE,
125
                                Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE, Geometry.TYPES.SURFACE, Geometry.TYPES.NULL };
126
                        break;
127
                case Geometry.TYPES.SURFACE:
128
                        supportedGeometryTypes = new int[] {Geometry.TYPES.ELLIPSE,
129
                                Geometry.TYPES.CIRCLE, Geometry.TYPES.SURFACE, Geometry.TYPES.NULL };
130
                        break;
131

    
132
                default:
133
                        supportedGeometryTypes = new int[] {};
134
                }
135
        }
136
        public boolean canWriteGeometry(int gvSIGgeometryType) {
137
                for (int i=0; i < supportedGeometryTypes.length; i++)
138
                {
139
                        if (gvSIGgeometryType == supportedGeometryTypes[i] ||
140
                                gvSIGgeometryType == (supportedGeometryTypes[i] | Geometry.TYPES.Z))
141
                                return true;
142
                }
143
                return false;
144
        }
145
        public static void create(SHPStoreParameters parameters, FeatureType featureType)throws OpenException, WriteException {
146
                int fileLength = 100;
147
                DbaseFileHeader myHeader = DbaseFileHeader.createDbaseHeader(featureType);
148
                try {
149
                        DBFFeaturesWriter.create(parameters,featureType);
150
                        FileChannel shpChannel=(FileChannel) getWriteChannel(parameters.getFile().getAbsolutePath());
151
                        FileChannel shxChannel=(FileChannel) getWriteChannel(SHP.getShxFile(parameters.getFile()).getAbsolutePath());
152
                        SHPFileWrite shpWrite = new SHPFileWrite(shpChannel,shxChannel);
153
                        int shapeType = 0;
154
                        if (featureType.getGeometryTypes().length > 0){
155
                                shapeType= shpWrite.getShapeType(featureType.getGeometryTypes()[0]);
156
                        }
157
                        myHeader.setNumRecords(0);
158
                                Rectangle2D fullExtent = new Rectangle2D.Double();
159
                        shpWrite.writeHeaders(fullExtent,
160
                                        shapeType, 0, fileLength);
161
                        shpChannel.close();
162
                        shxChannel.close();
163
                } catch (IOException e) {
164
                        throw new InitializeWriterException("SHP AbstractFeature Writer",e);
165
                }
166
        }
167

    
168
        /* (non-Javadoc)
169
         * @see org.gvsig.data.datastores.vectorial.file.dbf.DBFFeaturesWriter#dispose()
170
         */
171
        public void dispose() throws DataException {
172
                super.dispose();
173
                if (this.shpWrite != null){
174
                        this.shpWrite.close();
175
                        this.shpWrite = null;
176
                }
177
                this.fullExtent = null;
178

    
179
        }
180
}