Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / gml / GMLFileCreator.java @ 10627

History | View | Annotate | Download (7.12 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

    
11
import com.hardcode.gdbms.engine.values.Value;
12
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
13
import com.iver.utiles.StringUtilities;
14
import com.vividsolutions.jts.geom.Geometry;
15

    
16
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
17
 *
18
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
19
 *
20
 * This program is free software; you can redistribute it and/or
21
 * modify it under the terms of the GNU General Public License
22
 * as published by the Free Software Foundation; either version 2
23
 * of the License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
33
 *
34
 * For more information, contact:
35
 *
36
 *  Generalitat Valenciana
37
 *   Conselleria d'Infraestructures i Transport
38
 *   Av. Blasco Ib??ez, 50
39
 *   46010 VALENCIA
40
 *   SPAIN
41
 *
42
 *      +34 963862235
43
 *   gvsig@gva.es
44
 *      www.gvsig.gva.es
45
 *
46
 *    or
47
 *
48
 *   IVER T.I. S.A
49
 *   Salamanca 50
50
 *   46005 Valencia
51
 *   Spain
52
 *
53
 *   +34 963163400
54
 *   dac@iver.es
55
 */
56
/* CVS MESSAGES:
57
 *
58
 * $Id: GMLFileCreator.java 10627 2007-03-06 17:10:21Z caballero $
59
 * $Log$
60
 * Revision 1.4  2007-03-06 17:08:56  caballero
61
 * Exceptions
62
 *
63
 * Revision 1.3  2006/11/20 08:12:41  jorpiell
64
 * Se ha sustituido el car?cter & por &. El parser de XML lo requiere
65
 *
66
 * Revision 1.2  2006/08/10 12:03:43  jorpiell
67
 * Se usa el nuevo driver de GML de remoteServices
68
 *
69
 * Revision 1.1  2006/07/19 12:29:39  jorpiell
70
 * A?adido el driver de GML
71
 *
72
 *
73
 */
74
/**
75
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
76
 */
77
public class GMLFileCreator {
78
        private String schemaFileName = null;
79
        private Writer writer = null;
80
        private com.vividsolutions.jts.io.gml2.GMLWriter featureWriter = null;
81
        private IProjection projection = null;
82
        private String srsName = null;
83
        private Rectangle2D extent = null;
84
        private File m_File = null;
85
        private FieldDescription[] attributes = null;
86

    
87
        public GMLFileCreator(File file,String schemaFileName) throws IOException{
88
                this.m_File = file;
89
                this.schemaFileName = schemaFileName;
90
        }
91

    
92
        /**
93
         * This method creates the GML head file. It must be
94
         * called before start to add geometries.
95
         * @throws IOException
96
         */
97
        public void initialize() throws IOException{
98
                writer = new FileWriter(m_File);
99
                writer.write(getInitFile());
100
                writer.write(getBoundedBy());
101
                featureWriter = new com.vividsolutions.jts.io.gml2.GMLWriter();
102
                featureWriter.setPrefix("gml");
103
        }
104

    
105
        /**
106
         * Intert a new Feature
107
         * @param geom
108
         * @throws IOException
109
         */
110
        public void insertFeature(Geometry geom,Value[] attributes) throws IOException{
111
                        writer.write(getInitfeature());
112
                        writer.write(getInitGeomAttribute());
113
                        featureWriter.write(geom,writer);
114
                        writer.write(getEndGeomAttribute());
115

    
116
                        for (int i=0 ; i<attributes.length ; i++){
117
                                String attributeName = this.attributes[i].getFieldName();
118
                                String attibuteValue = StringUtilities.replace(attributes[i].toString(),"&","&amp;");
119
                                writer.write(getAttribute(attributeName,attibuteValue));
120
                        }
121

    
122
                        writer.write(getEndFeature());
123
        }
124

    
125

    
126

    
127
        /**
128
         * Writes the GML file
129
         * @throws IOException
130
         *
131
         */
132
        public void writeFile() throws IOException {
133
                writer.write(getEndFile());
134
                writer.close();
135
        }
136

    
137
        /**
138
         * Sets the projection
139
         * @param extent
140
         * @param proj
141
         */
142
        public void setBoundedBy(Rectangle2D extent, IProjection proj) {
143
                this.extent = extent;
144
                this.projection = proj;
145
        }
146

    
147
        /**
148
         * Sets the attributes collection
149
         * @param attributes
150
         */
151
        public void setAttibutes(FieldDescription[] attributes){
152
                this.attributes = attributes;
153
        }
154

    
155
        /**
156
         *  Creates an init Tag for a feature member
157
         * @return
158
         */
159
        private String getInitfeature(){
160
                StringBuffer string = new StringBuffer();
161
                string.append("<gml:featureMember>\n");
162
                string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.getFeatureName() + ">");
163
                return string.toString();
164

    
165
        }
166

    
167
        /**
168
         * Creates an end Tag for a feature member
169
         * @return
170
         */
171
        private String getEndFeature(){
172
                StringBuffer string = new StringBuffer();
173
                string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.getFeatureName() + ">");
174
                string.append("</gml:featureMember>");
175
                return string.toString();
176
        }
177

    
178
        /**
179
         * Creates the GML File first line
180
         * @param fileName
181
         * GML file name used to create the schema name
182
         * @return
183
         */
184
        private String getInitFile(){
185
                StringBuffer string = new StringBuffer();
186
                string.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
187
                string.append("<gml:FeatureCollection ");
188
                string.append("xmlns:" + GMLSchemaCreator.NAMESPACE + "=\"" + GMLSchemaCreator.TARGET_NAMESPACE + "\" ");
189
                string.append("xmlns:gml=\"http://www.opengis.net/gml\" ");
190
                string.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
191
                string.append("xsi:schemaLocation=\"" + GMLSchemaCreator.TARGET_NAMESPACE + " " + schemaFileName + "\"");
192
                string.append(">");
193
                return string.toString();
194
        }
195

    
196
        /**
197
         * Creates the GML Attributes description
198
         * @return
199
         */
200
        private String getInitGeomAttribute(){
201
                StringBuffer string = new StringBuffer();
202
                string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.GML_GEOMETRY + ">");
203
                return string.toString();
204
        }
205

    
206
        /**
207
         * Creates the GML attribute code
208
         * @param attributeName
209
         * @param attributeValue
210
         * @return
211
         */
212
        private String getAttribute(String attributeName, String attributeValue){
213
                StringBuffer string = new StringBuffer();
214
                string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + attributeName + ">");
215
                string.append(attributeValue);
216
                string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + attributeName + ">\n");
217
                return string.toString();
218
        }
219

    
220
        /**
221
         * Creates the GML Attributes description
222
         */
223
        private String getEndGeomAttribute(){
224
                StringBuffer string = new StringBuffer();
225
                string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.GML_GEOMETRY + ">\n");
226
                return string.toString();
227
        }
228

    
229
        /**
230
         * Creates the GML File last line
231
         * @return
232
         */
233
        private String getEndFile(){
234
                StringBuffer string = new StringBuffer();
235
                string.append("</gml:FeatureCollection>");
236
                return string.toString();
237
        }
238

    
239
        /**
240
         * Creates the boundedBy param
241
         * @return
242
         */
243
        private String getBoundedBy(){
244
                srsName = GMLBoundedBy.fromGvSigToGML(projection);
245
                StringBuffer string = new StringBuffer();
246
                string.append("<gml:boundedBy>");
247
            string.append("<gml:Box srsName=\"" + srsName + "\">");
248
            string.append("<gml:coordinates decimal=\".\" cs=\",\" ");
249
            string.append("ts=\" \">" + extent.getMinX() + "," + extent.getMinY() + " ");
250
            string.append(extent.getMaxX() + "," + extent.getMaxY() + "</gml:coordinates>");
251
            string.append("</gml:Box>");
252
            string.append("</gml:boundedBy>");
253
            return string.toString();
254
        }
255

    
256

    
257

    
258
}