root / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / gml / GMLFileCreator.java @ 15627
History | View | Annotate | Download (7.5 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.iver.utiles.StringUtilities; |
17 |
import com.vividsolutions.jts.geom.Geometry; |
18 |
|
19 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
20 |
*
|
21 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
22 |
*
|
23 |
* This program is free software; you can redistribute it and/or
|
24 |
* modify it under the terms of the GNU General Public License
|
25 |
* as published by the Free Software Foundation; either version 2
|
26 |
* of the License, or (at your option) any later version.
|
27 |
*
|
28 |
* This program is distributed in the hope that it will be useful,
|
29 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
30 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
31 |
* GNU General Public License for more details.
|
32 |
*
|
33 |
* You should have received a copy of the GNU General Public License
|
34 |
* along with this program; if not, write to the Free Software
|
35 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
36 |
*
|
37 |
* For more information, contact:
|
38 |
*
|
39 |
* Generalitat Valenciana
|
40 |
* Conselleria d'Infraestructures i Transport
|
41 |
* Av. Blasco Ib??ez, 50
|
42 |
* 46010 VALENCIA
|
43 |
* SPAIN
|
44 |
*
|
45 |
* +34 963862235
|
46 |
* gvsig@gva.es
|
47 |
* www.gvsig.gva.es
|
48 |
*
|
49 |
* or
|
50 |
*
|
51 |
* IVER T.I. S.A
|
52 |
* Salamanca 50
|
53 |
* 46005 Valencia
|
54 |
* Spain
|
55 |
*
|
56 |
* +34 963163400
|
57 |
* dac@iver.es
|
58 |
*/
|
59 |
/* CVS MESSAGES:
|
60 |
*
|
61 |
* $Id: GMLFileCreator.java 15627 2007-10-30 10:57:30Z jpiera $
|
62 |
* $Log$
|
63 |
* Revision 1.2.4.2 2007-01-25 16:11:35 jorpiell
|
64 |
* Se han cambiado los imports que hac?an referencia a remoteServices. Esto es as?, porque se han renombrado las clases del driver de GML
|
65 |
*
|
66 |
* Revision 1.3 2006/11/20 08:12:41 jorpiell
|
67 |
* Se ha sustituido el car?cter & por &. El parser de XML lo requiere
|
68 |
*
|
69 |
* Revision 1.2 2006/08/10 12:03:43 jorpiell
|
70 |
* Se usa el nuevo driver de GML de remoteServices
|
71 |
*
|
72 |
* Revision 1.1 2006/07/19 12:29:39 jorpiell
|
73 |
* A?adido el driver de GML
|
74 |
*
|
75 |
*
|
76 |
*/
|
77 |
/**
|
78 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
79 |
*/
|
80 |
public class GMLFileCreator { |
81 |
private String schemaFileName = null; |
82 |
private Writer writer = null; |
83 |
private com.vividsolutions.jts.io.gml2.GMLWriter featureWriter = null; |
84 |
private IProjection projection = null; |
85 |
private String srsName = null; |
86 |
private Rectangle2D extent = null; |
87 |
private File m_File = null; |
88 |
private FieldDescription[] attributes = null; |
89 |
|
90 |
public GMLFileCreator(File file,String schemaFileName) throws IOException{ |
91 |
this.m_File = file;
|
92 |
this.schemaFileName = schemaFileName;
|
93 |
} |
94 |
|
95 |
/**
|
96 |
* This method creates the GML head file. It must be
|
97 |
* called before start to add geometries.
|
98 |
* @throws IOException
|
99 |
*/
|
100 |
public void initialize() throws IOException{ |
101 |
writer = new FileWriter(m_File); |
102 |
writer.write(getInitFile()); |
103 |
writer.write(getBoundedBy()); |
104 |
featureWriter = new com.vividsolutions.jts.io.gml2.GMLWriter();
|
105 |
featureWriter.setPrefix("gml");
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Intert a new Feature
|
110 |
* @param geom
|
111 |
* @throws IOException
|
112 |
*/
|
113 |
public void insertFeature(Geometry geom,Value[] attributes) throws IOException{ |
114 |
writer.write(getInitfeature()); |
115 |
writer.write(getInitGeomAttribute()); |
116 |
featureWriter.write(geom,writer); |
117 |
writer.write(getEndGeomAttribute()); |
118 |
|
119 |
for (int i=0 ; i<attributes.length ; i++){ |
120 |
String attributeName = this.attributes[i].getFieldName(); |
121 |
if (!(attributeName.compareTo(GMLSchemaCreator.GML_GEOMETRY) == 0)){ |
122 |
String attibuteValue = StringUtilities.replace(attributes[i].toString(),"&","&"); |
123 |
writer.write(getAttribute(attributeName,attibuteValue)); |
124 |
} |
125 |
} |
126 |
writer.write(getEndFeature()); |
127 |
} |
128 |
|
129 |
|
130 |
|
131 |
/**
|
132 |
* Writes the GML file
|
133 |
* @throws IOException
|
134 |
*
|
135 |
*/
|
136 |
public void writeFile() throws IOException { |
137 |
writer.write(getEndFile()); |
138 |
writer.close(); |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Sets the projection
|
143 |
* @param extent
|
144 |
* @param proj
|
145 |
*/
|
146 |
public void setBoundedBy(Rectangle2D extent, IProjection proj) { |
147 |
this.extent = extent;
|
148 |
this.projection = proj;
|
149 |
} |
150 |
|
151 |
/**
|
152 |
* Sets the attributes collection
|
153 |
* @param attributes
|
154 |
*/
|
155 |
public void setAttibutes(FieldDescription[] attributes){ |
156 |
this.attributes = attributes;
|
157 |
} |
158 |
|
159 |
/**
|
160 |
* Creates an init Tag for a feature member
|
161 |
* @return
|
162 |
*/
|
163 |
private String getInitfeature(){ |
164 |
StringBuffer string = new StringBuffer(); |
165 |
string.append("<gml:featureMember>\n");
|
166 |
string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.getFeatureName() + ">"); |
167 |
return string.toString();
|
168 |
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* Creates an end Tag for a feature member
|
173 |
* @return
|
174 |
*/
|
175 |
private String getEndFeature(){ |
176 |
StringBuffer string = new StringBuffer(); |
177 |
string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.getFeatureName() + ">"); |
178 |
string.append("</gml:featureMember>");
|
179 |
return string.toString();
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Creates the GML File first line
|
184 |
* @param fileName
|
185 |
* GML file name used to create the schema name
|
186 |
* @return
|
187 |
*/
|
188 |
private String getInitFile(){ |
189 |
StringBuffer string = new StringBuffer(); |
190 |
string.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
|
191 |
string.append("<gml:FeatureCollection ");
|
192 |
string.append("xmlns:" + GMLSchemaCreator.NAMESPACE + "=\"" + GMLSchemaCreator.TARGET_NAMESPACE + "\" "); |
193 |
string.append("xmlns:gml=\"http://www.opengis.net/gml\" ");
|
194 |
string.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
|
195 |
string.append("xsi:schemaLocation=\"" + GMLSchemaCreator.TARGET_NAMESPACE + " " + schemaFileName + "\""); |
196 |
string.append(">");
|
197 |
return string.toString();
|
198 |
} |
199 |
|
200 |
/**
|
201 |
* Creates the GML Attributes description
|
202 |
* @return
|
203 |
*/
|
204 |
private String getInitGeomAttribute(){ |
205 |
StringBuffer string = new StringBuffer(); |
206 |
string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.GML_GEOMETRY + ">"); |
207 |
return string.toString();
|
208 |
} |
209 |
|
210 |
/**
|
211 |
* Creates the GML attribute code
|
212 |
* @param attributeName
|
213 |
* @param attributeValue
|
214 |
* @return
|
215 |
*/
|
216 |
private String getAttribute(String attributeName, String attributeValue){ |
217 |
StringBuffer string = new StringBuffer(); |
218 |
string.append("<" + GMLSchemaCreator.NAMESPACE + ":" + attributeName + ">"); |
219 |
string.append(attributeValue); |
220 |
string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + attributeName + ">\n"); |
221 |
return string.toString();
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Creates the GML Attributes description
|
226 |
*/
|
227 |
private String getEndGeomAttribute(){ |
228 |
StringBuffer string = new StringBuffer(); |
229 |
string.append("</" + GMLSchemaCreator.NAMESPACE + ":" + GMLSchemaCreator.GML_GEOMETRY + ">\n"); |
230 |
return string.toString();
|
231 |
} |
232 |
|
233 |
/**
|
234 |
* Creates the GML File last line
|
235 |
* @return
|
236 |
*/
|
237 |
private String getEndFile(){ |
238 |
StringBuffer string = new StringBuffer(); |
239 |
string.append("</gml:FeatureCollection>");
|
240 |
return string.toString();
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* Creates the boundedBy param
|
245 |
* @return
|
246 |
*/
|
247 |
private String getBoundedBy(){ |
248 |
srsName = GMLBoundedBy.fromGvSigToGML(projection); |
249 |
StringBuffer string = new StringBuffer(); |
250 |
string.append("<gml:boundedBy>");
|
251 |
string.append("<gml:Box srsName=\"" + srsName + "\">"); |
252 |
string.append("<gml:coordinates decimal=\".\" cs=\",\" ");
|
253 |
string.append("ts=\" \">" + extent.getMinX() + "," + extent.getMinY() + " "); |
254 |
string.append(extent.getMaxX() + "," + extent.getMaxY() + "</gml:coordinates>"); |
255 |
string.append("</gml:Box>");
|
256 |
string.append("</gml:boundedBy>");
|
257 |
return string.toString();
|
258 |
} |
259 |
|
260 |
|
261 |
|
262 |
} |