svn-gvsig-desktop / trunk / extensions / extGPE-gvSIG / src / org / gvsig / fmap / drivers / gpe / writer / ExportGeometry.java @ 29825
History | View | Annotate | Download (7.48 KB)
1 |
package org.gvsig.fmap.drivers.gpe.writer; |
---|---|
2 |
|
3 |
import java.awt.geom.PathIterator; |
4 |
|
5 |
import org.cresques.cts.ICoordTrans; |
6 |
import org.cresques.cts.IProjection; |
7 |
import org.gvsig.gpe.writer.GPEWriterHandler; |
8 |
import org.gvsig.remoteClient.gml.schemas.XMLElement; |
9 |
|
10 |
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D; |
11 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
12 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
13 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
14 |
import com.iver.cit.gvsig.fmap.core.FShape; |
15 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
16 |
|
17 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
18 |
*
|
19 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
20 |
*
|
21 |
* This program is free software; you can redistribute it and/or
|
22 |
* modify it under the terms of the GNU General Public License
|
23 |
* as published by the Free Software Foundation; either version 2
|
24 |
* of the License, or (at your option) any later version.
|
25 |
*
|
26 |
* This program is distributed in the hope that it will be useful,
|
27 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
28 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
29 |
* GNU General Public License for more details.
|
30 |
*
|
31 |
* You should have received a copy of the GNU General Public License
|
32 |
* along with this program; if not, write to the Free Software
|
33 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
34 |
*
|
35 |
* For more information, contact:
|
36 |
*
|
37 |
* Generalitat Valenciana
|
38 |
* Conselleria d'Infraestructures i Transport
|
39 |
* Av. Blasco Ib??ez, 50
|
40 |
* 46010 VALENCIA
|
41 |
* SPAIN
|
42 |
*
|
43 |
* +34 963862235
|
44 |
* gvsig@gva.es
|
45 |
* www.gvsig.gva.es
|
46 |
*
|
47 |
* or
|
48 |
*
|
49 |
* IVER T.I. S.A
|
50 |
* Salamanca 50
|
51 |
* 46005 Valencia
|
52 |
* Spain
|
53 |
*
|
54 |
* +34 963163400
|
55 |
* dac@iver.es
|
56 |
*/
|
57 |
/* CVS MESSAGES:
|
58 |
*
|
59 |
* $Id$
|
60 |
* $Log$
|
61 |
*
|
62 |
*/
|
63 |
/**
|
64 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
65 |
*/
|
66 |
public class ExportGeometry { |
67 |
private GPEWriterHandler writer = null; |
68 |
//To know if the geometry is multiple
|
69 |
private boolean isMultiple = false; |
70 |
//To reproject geometries
|
71 |
private IProjection projOrig = null; |
72 |
private IProjection projDest = null; |
73 |
private ICoordTrans coordTrans = null; |
74 |
private String crs = null; |
75 |
|
76 |
public ExportGeometry(GPEWriterHandler writer) {
|
77 |
super();
|
78 |
this.writer = writer;
|
79 |
} |
80 |
|
81 |
/**
|
82 |
* It writes a geometry
|
83 |
* @param geom
|
84 |
* The geometry to write
|
85 |
* @param crs
|
86 |
* The coordinates reference system
|
87 |
*/
|
88 |
public void writeGeometry(IGeometry geom){ |
89 |
crs = null;
|
90 |
if (projDest != null){ |
91 |
crs = projDest.getAbrev(); |
92 |
} |
93 |
if (geom instanceof FMultiPoint2D){ |
94 |
FMultiPoint2D multi = (FMultiPoint2D)geom; |
95 |
for (int i=0 ; i<multi.getNumPoints() ; i++){ |
96 |
reproject(multi.getPoint(i)); |
97 |
} |
98 |
writeMultiPoint(multi, crs); |
99 |
return;
|
100 |
} |
101 |
FShape shp = (FShape)geom.getInternalShape(); |
102 |
reproject(shp); |
103 |
int type = shp.getShapeType() % FShape.Z % FShape.M;
|
104 |
|
105 |
if (type == FShape.POINT){
|
106 |
writePoint((FPoint2D)shp, crs); |
107 |
}else if (type==FShape.LINE){ |
108 |
writeLine((FPolyline2D)shp, crs); |
109 |
}else if (type==FShape.POLYGON){ |
110 |
writePolygon((FPolygon2D)shp, crs); |
111 |
} |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Reproject a geometry
|
116 |
* @param shp
|
117 |
*/
|
118 |
private void reproject(FShape shp){ |
119 |
ICoordTrans coordTrans = getCoordTrans(); |
120 |
if (coordTrans != null){ |
121 |
try{
|
122 |
shp.reProject(coordTrans); |
123 |
}catch(Exception e){ |
124 |
//The server is the responsible to reproject
|
125 |
if (projOrig != null){ |
126 |
crs = projOrig.getAbrev(); |
127 |
} |
128 |
} |
129 |
} |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Writes a point in 2D
|
134 |
* @param point
|
135 |
* The point to write
|
136 |
* @param crs
|
137 |
* The coordinates reference system
|
138 |
*/
|
139 |
private void writePoint(FPoint2D point, String crs){ |
140 |
writer.startPoint(null, new CoordinatesSequencePoint(point), crs); |
141 |
writer.endPoint(); |
142 |
} |
143 |
|
144 |
/**
|
145 |
* Writes a multipoint in 2D
|
146 |
* @param point
|
147 |
* The point to write
|
148 |
* @param crs
|
149 |
* The coordinates reference system
|
150 |
*/
|
151 |
private void writeMultiPoint(FMultiPoint2D multi, String crs){ |
152 |
writer.startMultiPoint(null, crs);
|
153 |
for (int i=0 ; i<multi.getNumPoints() ; i++){ |
154 |
FPoint2D point = multi.getPoint(i); |
155 |
writePoint(point, crs); |
156 |
} |
157 |
writer.endMultiPoint(); |
158 |
} |
159 |
|
160 |
/**
|
161 |
* Writes a line in 2D
|
162 |
* @param line
|
163 |
* The line to write
|
164 |
* @param crs
|
165 |
* The coordinates reference system
|
166 |
* @param geometries
|
167 |
* The parsed geometries
|
168 |
*/
|
169 |
private void writeLine(FPolyline2D line, String crs){ |
170 |
boolean isMultipleGeometry = false; |
171 |
if (isMultiple){
|
172 |
writer.startMultiLineString(null, crs);
|
173 |
}else{
|
174 |
isMultipleGeometry = isMultiple(line.getPathIterator(null));
|
175 |
if (isMultipleGeometry){
|
176 |
writer.startMultiLineString(null, crs);
|
177 |
} |
178 |
} |
179 |
CoordinatesSequenceGeneralPath sequence = new CoordinatesSequenceGeneralPath(line.getPathIterator(null)); |
180 |
writer.startLineString(null, sequence, crs);
|
181 |
writer.endLineString(); |
182 |
if (isMultiple || isMultipleGeometry){
|
183 |
while (sequence.hasMoreGeometries()){
|
184 |
sequence.initialize(); |
185 |
writer.startLineString(null, sequence, crs);
|
186 |
writer.endLineString(); |
187 |
} |
188 |
writer.endMultiLineString(); |
189 |
} |
190 |
} |
191 |
|
192 |
/**
|
193 |
* Writes a polygon in 2D
|
194 |
* @param polygon
|
195 |
* The polygon to write
|
196 |
* @param crs
|
197 |
* The coordinates reference system
|
198 |
* @param geometries
|
199 |
* The parsed geometries
|
200 |
*/
|
201 |
private void writePolygon(FPolygon2D polygon, String crs){ |
202 |
boolean isMultipleGeometry = false; |
203 |
if (isMultiple){
|
204 |
writer.startMultiPolygon(null, crs);
|
205 |
}else{
|
206 |
isMultipleGeometry = isMultiple(polygon.getPathIterator(null));
|
207 |
if (isMultipleGeometry){
|
208 |
writer.startMultiPolygon(null, crs);
|
209 |
} |
210 |
} |
211 |
CoordinatesSequenceGeneralPath sequence = new CoordinatesSequenceGeneralPath(polygon.getPathIterator(null)); |
212 |
writer.startPolygon(null, sequence ,crs);
|
213 |
writer.endPolygon(); |
214 |
if (isMultiple || isMultipleGeometry){
|
215 |
while (sequence.hasMoreGeometries()){
|
216 |
sequence.initialize(); |
217 |
writer.startPolygon(null, sequence ,crs);
|
218 |
writer.endPolygon(); |
219 |
} |
220 |
writer.endMultiPolygon(); |
221 |
} |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Return if the geometry is multiple
|
226 |
* @param path
|
227 |
* @return
|
228 |
*/
|
229 |
public boolean isMultiple(PathIterator path){ |
230 |
double[] coords = new double[2]; |
231 |
int type = 0; |
232 |
int numGeometries = 0; |
233 |
while (!path.isDone()){
|
234 |
type = path.currentSegment(coords); |
235 |
switch (type) {
|
236 |
case PathIterator.SEG_MOVETO: |
237 |
numGeometries++; |
238 |
if (numGeometries == 2){ |
239 |
return true; |
240 |
} |
241 |
break;
|
242 |
case PathIterator.SEG_CLOSE: |
243 |
return false; |
244 |
default:
|
245 |
break;
|
246 |
} |
247 |
path.next(); |
248 |
} |
249 |
return false; |
250 |
} |
251 |
|
252 |
/**
|
253 |
* @param projOrig the projOrig to set
|
254 |
*/
|
255 |
public void setProjOrig(IProjection projOrig) { |
256 |
this.projOrig = projOrig;
|
257 |
} |
258 |
|
259 |
/**
|
260 |
* @param projDest the projDest to set
|
261 |
*/
|
262 |
public void setProjDest(IProjection projDest) { |
263 |
this.projDest = projDest;
|
264 |
} |
265 |
|
266 |
/**
|
267 |
* @return the coordTrans
|
268 |
*/
|
269 |
private ICoordTrans getCoordTrans() {
|
270 |
if (coordTrans == null){ |
271 |
if ((projOrig == null) || (projDest == null)){ |
272 |
return null; |
273 |
} |
274 |
coordTrans = projOrig.getCT(projDest); |
275 |
} |
276 |
return coordTrans;
|
277 |
} |
278 |
|
279 |
/**
|
280 |
* @param writer the writer to set
|
281 |
*/
|
282 |
public void setWriter(GPEWriterHandler writer) { |
283 |
this.writer = writer;
|
284 |
} |
285 |
|
286 |
|
287 |
|
288 |
/**
|
289 |
* @param geometry the geometry to set
|
290 |
*/
|
291 |
public void setGeometry(XMLElement geometry) { |
292 |
if (geometry != null){ |
293 |
if (geometry.getEntityType().getName().toLowerCase().indexOf("multi") > 0){ |
294 |
isMultiple = true;
|
295 |
}else{
|
296 |
isMultiple = false;
|
297 |
} |
298 |
} |
299 |
} |
300 |
|
301 |
/**
|
302 |
* @return the isMultiple
|
303 |
*/
|
304 |
public boolean isMultiple() { |
305 |
return isMultiple;
|
306 |
} |
307 |
|
308 |
/**
|
309 |
* @param isMultiple the isMultiple to set
|
310 |
*/
|
311 |
public void setMultiple(boolean isMultiple) { |
312 |
this.isMultiple = isMultiple;
|
313 |
} |
314 |
|
315 |
/**
|
316 |
* @return the projDest
|
317 |
*/
|
318 |
public IProjection getProjDest() {
|
319 |
return projDest;
|
320 |
} |
321 |
} |