svn-gvsig-desktop / tags / v1_9_Build_1250 / extensions / extGPE-gvSIG / src / org / gvsig / fmap / drivers / gpe / writer / ExportGeometry.java @ 31459
History | View | Annotate | Download (8 KB)
1 | 27093 | jpiera | package org.gvsig.fmap.drivers.gpe.writer; |
---|---|---|---|
2 | |||
3 | import java.awt.geom.PathIterator; |
||
4 | 31459 | csanchez | import java.awt.geom.Rectangle2D; |
5 | 27093 | jpiera | |
6 | import org.cresques.cts.ICoordTrans; |
||
7 | import org.cresques.cts.IProjection; |
||
8 | import org.gvsig.gpe.writer.GPEWriterHandler; |
||
9 | import org.gvsig.remoteClient.gml.schemas.XMLElement; |
||
10 | |||
11 | import com.iver.cit.gvsig.fmap.core.FMultiPoint2D; |
||
12 | import com.iver.cit.gvsig.fmap.core.FPoint2D; |
||
13 | import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
||
14 | import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
||
15 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
16 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
17 | |||
18 | 31459 | csanchez | /* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
19 | 27093 | jpiera | *
|
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 | 31459 | csanchez | * Av. Blasco Ib��ez, 50
|
41 | 27093 | jpiera | * 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$
|
||
61 | * $Log$
|
||
62 | *
|
||
63 | */
|
||
64 | /**
|
||
65 | 31459 | csanchez | * @author Jorge Piera LLodr� (jorge.piera@iver.es)
|
66 | 27093 | jpiera | */
|
67 | public class ExportGeometry { |
||
68 | private GPEWriterHandler writer = null; |
||
69 | //To know if the geometry is multiple
|
||
70 | private boolean isMultiple = false; |
||
71 | //To reproject geometries
|
||
72 | private IProjection projOrig = null; |
||
73 | private IProjection projDest = null; |
||
74 | private ICoordTrans coordTrans = null; |
||
75 | private String crs = null; |
||
76 | |||
77 | public ExportGeometry(GPEWriterHandler writer) {
|
||
78 | super();
|
||
79 | this.writer = writer;
|
||
80 | } |
||
81 | |||
82 | 31459 | csanchez | |
83 | 27093 | jpiera | /**
|
84 | * It writes a geometry
|
||
85 | * @param geom
|
||
86 | * The geometry to write
|
||
87 | * @param crs
|
||
88 | * The coordinates reference system
|
||
89 | */
|
||
90 | public void writeGeometry(IGeometry geom){ |
||
91 | crs = null;
|
||
92 | if (projDest != null){ |
||
93 | crs = projDest.getAbrev(); |
||
94 | } |
||
95 | if (geom instanceof FMultiPoint2D){ |
||
96 | FMultiPoint2D multi = (FMultiPoint2D)geom; |
||
97 | for (int i=0 ; i<multi.getNumPoints() ; i++){ |
||
98 | reproject(multi.getPoint(i)); |
||
99 | } |
||
100 | writeMultiPoint(multi, crs); |
||
101 | return;
|
||
102 | } |
||
103 | FShape shp = (FShape)geom.getInternalShape(); |
||
104 | reproject(shp); |
||
105 | 29825 | jpiera | int type = shp.getShapeType() % FShape.Z % FShape.M;
|
106 | |||
107 | if (type == FShape.POINT){
|
||
108 | 27093 | jpiera | writePoint((FPoint2D)shp, crs); |
109 | 29825 | jpiera | }else if (type==FShape.LINE){ |
110 | 27133 | jpiera | writeLine((FPolyline2D)shp, crs); |
111 | 29825 | jpiera | }else if (type==FShape.POLYGON){ |
112 | 27133 | jpiera | writePolygon((FPolygon2D)shp, crs); |
113 | 27093 | jpiera | } |
114 | } |
||
115 | |||
116 | /**
|
||
117 | * Reproject a geometry
|
||
118 | * @param shp
|
||
119 | */
|
||
120 | private void reproject(FShape shp){ |
||
121 | ICoordTrans coordTrans = getCoordTrans(); |
||
122 | if (coordTrans != null){ |
||
123 | try{
|
||
124 | shp.reProject(coordTrans); |
||
125 | }catch(Exception e){ |
||
126 | //The server is the responsible to reproject
|
||
127 | if (projOrig != null){ |
||
128 | crs = projOrig.getAbrev(); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /**
|
||
135 | * Writes a point in 2D
|
||
136 | * @param point
|
||
137 | * The point to write
|
||
138 | * @param crs
|
||
139 | * The coordinates reference system
|
||
140 | */
|
||
141 | private void writePoint(FPoint2D point, String crs){ |
||
142 | writer.startPoint(null, new CoordinatesSequencePoint(point), crs); |
||
143 | writer.endPoint(); |
||
144 | } |
||
145 | |||
146 | /**
|
||
147 | * Writes a multipoint in 2D
|
||
148 | * @param point
|
||
149 | * The point to write
|
||
150 | * @param crs
|
||
151 | * The coordinates reference system
|
||
152 | */
|
||
153 | private void writeMultiPoint(FMultiPoint2D multi, String crs){ |
||
154 | writer.startMultiPoint(null, crs);
|
||
155 | for (int i=0 ; i<multi.getNumPoints() ; i++){ |
||
156 | FPoint2D point = multi.getPoint(i); |
||
157 | writePoint(point, crs); |
||
158 | } |
||
159 | writer.endMultiPoint(); |
||
160 | } |
||
161 | |||
162 | /**
|
||
163 | * Writes a line in 2D
|
||
164 | * @param line
|
||
165 | * The line to write
|
||
166 | * @param crs
|
||
167 | * The coordinates reference system
|
||
168 | * @param geometries
|
||
169 | * The parsed geometries
|
||
170 | */
|
||
171 | 27133 | jpiera | private void writeLine(FPolyline2D line, String crs){ |
172 | boolean isMultipleGeometry = false; |
||
173 | 27093 | jpiera | if (isMultiple){
|
174 | writer.startMultiLineString(null, crs);
|
||
175 | 27133 | jpiera | }else{
|
176 | isMultipleGeometry = isMultiple(line.getPathIterator(null));
|
||
177 | if (isMultipleGeometry){
|
||
178 | writer.startMultiLineString(null, crs);
|
||
179 | } |
||
180 | 27093 | jpiera | } |
181 | 27133 | jpiera | CoordinatesSequenceGeneralPath sequence = new CoordinatesSequenceGeneralPath(line.getPathIterator(null)); |
182 | 27093 | jpiera | writer.startLineString(null, sequence, crs);
|
183 | 27133 | jpiera | writer.endLineString(); |
184 | if (isMultiple || isMultipleGeometry){
|
||
185 | while (sequence.hasMoreGeometries()){
|
||
186 | sequence.initialize(); |
||
187 | writer.startLineString(null, sequence, crs);
|
||
188 | writer.endLineString(); |
||
189 | } |
||
190 | 27093 | jpiera | writer.endMultiLineString(); |
191 | } |
||
192 | } |
||
193 | |||
194 | /**
|
||
195 | * Writes a polygon in 2D
|
||
196 | * @param polygon
|
||
197 | * The polygon to write
|
||
198 | * @param crs
|
||
199 | * The coordinates reference system
|
||
200 | * @param geometries
|
||
201 | * The parsed geometries
|
||
202 | */
|
||
203 | 27133 | jpiera | private void writePolygon(FPolygon2D polygon, String crs){ |
204 | boolean isMultipleGeometry = false; |
||
205 | 27093 | jpiera | if (isMultiple){
|
206 | writer.startMultiPolygon(null, crs);
|
||
207 | 27133 | jpiera | }else{
|
208 | isMultipleGeometry = isMultiple(polygon.getPathIterator(null));
|
||
209 | if (isMultipleGeometry){
|
||
210 | writer.startMultiPolygon(null, crs);
|
||
211 | } |
||
212 | } |
||
213 | CoordinatesSequenceGeneralPath sequence = new CoordinatesSequenceGeneralPath(polygon.getPathIterator(null)); |
||
214 | writer.startPolygon(null, sequence ,crs);
|
||
215 | writer.endPolygon(); |
||
216 | if (isMultiple || isMultipleGeometry){
|
||
217 | while (sequence.hasMoreGeometries()){
|
||
218 | sequence.initialize(); |
||
219 | writer.startPolygon(null, sequence ,crs);
|
||
220 | writer.endPolygon(); |
||
221 | } |
||
222 | 27093 | jpiera | writer.endMultiPolygon(); |
223 | } |
||
224 | } |
||
225 | 27133 | jpiera | |
226 | /**
|
||
227 | * Return if the geometry is multiple
|
||
228 | * @param path
|
||
229 | * @return
|
||
230 | */
|
||
231 | public boolean isMultiple(PathIterator path){ |
||
232 | double[] coords = new double[2]; |
||
233 | int type = 0; |
||
234 | int numGeometries = 0; |
||
235 | while (!path.isDone()){
|
||
236 | type = path.currentSegment(coords); |
||
237 | switch (type) {
|
||
238 | case PathIterator.SEG_MOVETO: |
||
239 | numGeometries++; |
||
240 | if (numGeometries == 2){ |
||
241 | return true; |
||
242 | } |
||
243 | break;
|
||
244 | case PathIterator.SEG_CLOSE: |
||
245 | return false; |
||
246 | default:
|
||
247 | break;
|
||
248 | } |
||
249 | path.next(); |
||
250 | } |
||
251 | return false; |
||
252 | } |
||
253 | 27093 | jpiera | |
254 | /**
|
||
255 | * @param projOrig the projOrig to set
|
||
256 | */
|
||
257 | public void setProjOrig(IProjection projOrig) { |
||
258 | this.projOrig = projOrig;
|
||
259 | } |
||
260 | |||
261 | /**
|
||
262 | * @param projDest the projDest to set
|
||
263 | */
|
||
264 | public void setProjDest(IProjection projDest) { |
||
265 | this.projDest = projDest;
|
||
266 | } |
||
267 | |||
268 | /**
|
||
269 | * @return the coordTrans
|
||
270 | */
|
||
271 | private ICoordTrans getCoordTrans() {
|
||
272 | if (coordTrans == null){ |
||
273 | if ((projOrig == null) || (projDest == null)){ |
||
274 | return null; |
||
275 | } |
||
276 | coordTrans = projOrig.getCT(projDest); |
||
277 | } |
||
278 | return coordTrans;
|
||
279 | } |
||
280 | |||
281 | /**
|
||
282 | * @param writer the writer to set
|
||
283 | */
|
||
284 | public void setWriter(GPEWriterHandler writer) { |
||
285 | this.writer = writer;
|
||
286 | } |
||
287 | |||
288 | |||
289 | |||
290 | /**
|
||
291 | * @param geometry the geometry to set
|
||
292 | */
|
||
293 | public void setGeometry(XMLElement geometry) { |
||
294 | if (geometry != null){ |
||
295 | if (geometry.getEntityType().getName().toLowerCase().indexOf("multi") > 0){ |
||
296 | isMultiple = true;
|
||
297 | }else{
|
||
298 | isMultiple = false;
|
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /**
|
||
304 | * @return the isMultiple
|
||
305 | */
|
||
306 | public boolean isMultiple() { |
||
307 | return isMultiple;
|
||
308 | } |
||
309 | |||
310 | /**
|
||
311 | * @param isMultiple the isMultiple to set
|
||
312 | */
|
||
313 | public void setMultiple(boolean isMultiple) { |
||
314 | this.isMultiple = isMultiple;
|
||
315 | } |
||
316 | |||
317 | /**
|
||
318 | * @return the projDest
|
||
319 | */
|
||
320 | public IProjection getProjDest() {
|
||
321 | return projDest;
|
||
322 | } |
||
323 | 31459 | csanchez | |
324 | |||
325 | public IProjection getProjOrig() {
|
||
326 | return projOrig;
|
||
327 | } |
||
328 | |||
329 | |||
330 | public Rectangle2D getExtent(Rectangle2D extent) { |
||
331 | //If the project orig its different than the destination one, then must be reprojected
|
||
332 | if (getProjDest().getAbrev().compareTo(getProjOrig().getAbrev())!=0){ |
||
333 | if (coordTrans == null){ |
||
334 | if ((projOrig == null) || (projDest == null)){ |
||
335 | return null; |
||
336 | } |
||
337 | coordTrans = projOrig.getCT(projDest); |
||
338 | } |
||
339 | return coordTrans.convert(extent);
|
||
340 | } |
||
341 | return extent;
|
||
342 | } |
||
343 | 27093 | jpiera | } |