Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / gml / GMLFileCreator.java @ 6433

History | View | Annotate | Download (6.93 KB)

1
package com.iver.cit.gvsig.fmap.edition.writers.gml;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.FileWriter;
6
import java.io.IOException;
7
import java.io.Writer;
8

    
9
import org.cresques.cts.IProjection;
10
import org.geotools.xml.gml.GMLSchema;
11

    
12
import com.hardcode.gdbms.engine.values.Value;
13
import com.iver.cit.gvsig.fmap.core.IFeature;
14
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
15
import com.iver.cit.gvsig.fmap.edition.EditionException;
16
import com.vividsolutions.jts.geom.Geometry;
17

    
18
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
19
 *
20
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
21
 *
22
 * This program is free software; you can redistribute it and/or
23
 * modify it under the terms of the GNU General Public License
24
 * as published by the Free Software Foundation; either version 2
25
 * of the License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
35
 *
36
 * For more information, contact:
37
 *
38
 *  Generalitat Valenciana
39
 *   Conselleria d'Infraestructures i Transport
40
 *   Av. Blasco Ib??ez, 50
41
 *   46010 VALENCIA
42
 *   SPAIN
43
 *
44
 *      +34 963862235
45
 *   gvsig@gva.es
46
 *      www.gvsig.gva.es
47
 *
48
 *    or
49
 *
50
 *   IVER T.I. S.A
51
 *   Salamanca 50
52
 *   46005 Valencia
53
 *   Spain
54
 *
55
 *   +34 963163400
56
 *   dac@iver.es
57
 */
58
/* CVS MESSAGES:
59
 *
60
 * $Id: GMLFileCreator.java 6433 2006-07-19 12:30:09Z jorpiell $
61
 * $Log$
62
 * Revision 1.1  2006-07-19 12:29:39  jorpiell
63
 * A?adido el driver de GML
64
 *
65
 *
66
 */
67
/**
68
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
69
 */
70
public class GMLFileCreator {
71
        private String schemaFileName = null;
72
        private Writer writer = null;
73
        private com.vividsolutions.jts.io.gml2.GMLWriter featureWriter = null;
74
        private IProjection projection = null;
75
        private String srsName = null;
76
        private Rectangle2D extent = null;
77
        private File m_File = null;
78
        private FieldDescription[] attributes = null;
79
                
80
        public GMLFileCreator(File file,String schemaFileName) throws IOException{
81
                this.m_File = file;
82
                this.schemaFileName = schemaFileName;
83
        }
84
        
85
        /**
86
         * This method creates the GML head file. It must be 
87
         * called before start to add geometries.
88
         * @throws IOException
89
         */
90
        public void initialize() throws IOException{                
91
                writer = new FileWriter(m_File);
92
                writer.write(getInitFile());
93
                writer.write(getBoundedBy());
94
                featureWriter = new com.vividsolutions.jts.io.gml2.GMLWriter();
95
                featureWriter.setPrefix("gml");
96
        }
97
        
98
        /**
99
         * Intert a new Feature
100
         * @param geom
101
         * @throws IOException
102
         */
103
        public void insertFeature(Geometry geom,Value[] attributes) throws IOException{
104
                        writer.write(getInitfeature());
105
                        writer.write(getInitGeomAttribute());
106
                        featureWriter.write(geom,writer);
107
                        writer.write(getEndGeomAttribute());
108
                        
109
                        for (int i=0 ; i<attributes.length ; i++){
110
                                String attributeName = this.attributes[i].getFieldName();
111
                                String attibuteValue = attributes[i].toString();
112
                                writer.write(getAttribute(attributeName,attibuteValue));
113
                        }
114
                        
115
                        writer.write(getEndFeature());        
116
        }
117
        
118
        
119

    
120
        /**
121
         * Writes the GML file
122
         * @throws IOException 
123
         *
124
         */
125
        public void writeFile() throws IOException {
126
                writer.write(getEndFile());
127
                writer.close();                
128
        }
129

    
130
        /**
131
         * Sets the projection
132
         * @param extent
133
         * @param proj
134
         */
135
        public void setBoundedBy(Rectangle2D extent, IProjection proj) {
136
                this.extent = extent;
137
                this.projection = proj;                
138
        }
139
        
140
        /**
141
         * Sets the attributes collection
142
         * @param attributes
143
         */
144
        public void setAttibutes(FieldDescription[] attributes){
145
                this.attributes = attributes;
146
        }
147
        
148
        /**
149
         *  Creates an init Tag for a feature member
150
         * @return
151
         */
152
        private String getInitfeature(){
153
                StringBuffer string = new StringBuffer();
154
                string.append("<gml:featureMember>\n");
155
                string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.getFeatureName() + ">");
156
                return string.toString();
157
                
158
        }
159
        
160
        /**
161
         * Creates an end Tag for a feature member
162
         * @return
163
         */
164
        private String getEndFeature(){
165
                StringBuffer string = new StringBuffer();
166
                string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.getFeatureName() + ">");
167
                string.append("</gml:featureMember>");
168
                return string.toString();
169
        }
170
        
171
        /**
172
         * Creates the GML File first line
173
         * @param fileName
174
         * GML file name used to create the schema name
175
         * @return
176
         */
177
        private String getInitFile(){
178
                StringBuffer string = new StringBuffer();
179
                string.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
180
                string.append("<gml:FeatureCollection ");
181
                string.append("xmlns:" + GMLSchemaCreator.NAMESPACE + "=\"" + GMLSchemaCreator.TARGET_NAMESPACE + "\" ");
182
                string.append("xmlns:gml=\"http://www.opengis.net/gml\" ");
183
                string.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "); 
184
                string.append("xsi:schemaLocation=\"" + GMLSchemaCreator.TARGET_NAMESPACE + " " + schemaFileName + "\"");
185
                string.append(">");
186
                return string.toString();
187
        }
188
        
189
        /**
190
         * Creates the GML Attributes description
191
         * @return
192
         */
193
        private String getInitGeomAttribute(){
194
                StringBuffer string = new StringBuffer();
195
                string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.GML_GEOMETRY + ">");
196
                return string.toString();
197
        }
198
        
199
        /**
200
         * Creates the GML attribute code
201
         * @param attributeName
202
         * @param attributeValue
203
         * @return
204
         */
205
        private String getAttribute(String attributeName, String attributeValue){
206
                StringBuffer string = new StringBuffer();
207
                string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + attributeName + ">");
208
                string.append(attributeValue);
209
                string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + attributeName + ">\n");
210
                return string.toString();
211
        }
212
        
213
        /**
214
         * Creates the GML Attributes description
215
         */
216
        private String getEndGeomAttribute(){
217
                StringBuffer string = new StringBuffer();
218
                string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.GML_GEOMETRY + ">\n");
219
                return string.toString();
220
        }
221

    
222
        /**
223
         * Creates the GML File last line
224
         * @return
225
         */
226
        private String getEndFile(){
227
                StringBuffer string = new StringBuffer();
228
                string.append("</gml:FeatureCollection>");
229
                return string.toString();
230
        }
231
        
232
        /**
233
         * Creates the boundedBy param
234
         * @return
235
         */
236
        private String getBoundedBy(){
237
                srsName = GMLBoundedBy.getTag(projection);
238
                StringBuffer string = new StringBuffer();
239
                string.append("<gml:boundedBy>");
240
            string.append("<gml:Box srsName=\"" + srsName + "\">");
241
            string.append("<gml:coordinates decimal=\".\" cs=\",\" ");
242
            string.append("ts=\" \">" + extent.getMinX() + "," + extent.getMinY() + " ");
243
            string.append(extent.getMaxX() + "," + extent.getMaxY() + "</gml:coordinates>");
244
            string.append("</gml:Box>");
245
            string.append("</gml:boundedBy>");
246
            return string.toString();            
247
        }
248
        
249
        
250

    
251
}