Statistics
| Revision:

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

History | View | Annotate | Download (6.28 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
                /* System.out.println("Intento escribir el registro " +
70
                 numRows + " de la capa " + lyrVect.getName()); */
71

    
72
                Geometry theGeom = (Geometry)feature.getDefaultGeometry();
73
                if (theGeom==null){
74
                        theGeom=new NullGeometry();
75
                }
76
                // Revisamos que podemos escribir esa entidad
77
                // En un shpFile, podemos meter pol?gonos, pero que sean como
78
                // lineas. En cambio, en uno de puntos solo se pueden meter puntos
79
                // Con capas de anotaciones ?nicamente se pueden salvar los puntos,
80
                // de momento no hay problema porque est? limitado
81
                // y no se puede tener anotaciones de otro tipo de shape.
82
                if (canWriteGeometry(theGeom.getType()))// || canWriteGeometry(gvSIG_geometryType))
83
                        {
84
                        System.out.println(feature.get("NOMBRE"));
85
                        super.insertFeature(feature);
86
                        fileSize = shpWrite.writeIGeometry(theGeom);
87
                        Rectangle2D boundsShp = theGeom.getBounds2D();
88
                        if (boundsShp!=null){
89
                                if (fullExtent == null) {
90
                                        fullExtent = boundsShp;
91
                                } else {
92
                                        fullExtent.add(boundsShp);
93
                                }
94
                        }
95

    
96
                }
97
        }
98

    
99
        public void updateFeatureType(FeatureType featureType) {
100
                super.updateFeatureType(featureType);
101
                int type=featureType.getGeometryTypes()[0];
102
                shapeType = shpWrite.getShapeType(type);
103
                gvSIG_geometryType = type;
104
                setSupportedGeometryTypes();
105
        }
106

    
107
        public void setFile(File f)
108
        {
109
                shpPath = f.getAbsolutePath();
110

    
111
                shxPath = SHP.getShxFile(f).getAbsolutePath();
112

    
113
                super.setFile(SHP.getDbfFile(f));
114

    
115
//                dbfPath = SHP.getDbfFile(f).getAbsolutePath();
116

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

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

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

    
184
        }
185
}