Revision 344

View differences:

org.gvsig.gpe.exportto/trunk/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.generic/src/main/java/org/gvsig/gpe/exportto/generic/GeometryToGPEWriter.java
1
package org.gvsig.gpe.exportto.generic;
2

  
3
import java.awt.geom.PathIterator;
4
import java.awt.geom.Rectangle2D;
5

  
6

  
7
/**
8
 * 
9
 * Utility class to write gvSIG geometries in a GPE writer.
10
 * 
11
 * 
12
 * @author Jorge Piera LLodr� (jorge.piera@iver.es)
13
 * @author jldominguez (I have removed the reprojection methods/fields)
14
 */
15
public class GeometryToGPEWriter {
16
    
17
	private IGPEWriterHandler writer = null;
18
	//To know if the geometry is multiple
19
	private boolean isMultiple = false;
20
	//To reproject geometries
21
	/*
22
	private IProjection projOrig = null;
23
	private IProjection projDest = null;
24
	private ICoordTrans coordTrans = null;
25
	*/
26
	private String crs = null;
27

  
28
	public GeometryToGPEWriter(IGPEWriterHandler writer) {
29
		this.writer = writer;
30
	}
31

  
32
	
33
	/**
34
	 * It writes a geometry
35
	 * @param geom
36
	 * The geometry to write
37
	 * @param crs
38
	 * The coordinates reference system
39
	 */
40
	public void writeGeometry(IGeometry geom){
41
		crs = null;
42
		if (projDest != null){
43
			crs = projDest.getAbrev();
44
		}
45
		if (geom instanceof FMultiPoint2D){
46
			FMultiPoint2D multi = (FMultiPoint2D)geom;
47
			for (int i=0 ; i<multi.getNumPoints() ; i++){
48
				reproject(multi.getPoint(i));
49
			}
50
			writeMultiPoint(multi, crs);
51
			return;
52
		}
53
		FShape shp = (FShape)geom.getInternalShape();
54
		reproject(shp);
55
		int type = shp.getShapeType() % FShape.Z % FShape.M;
56
		
57
		if (type == FShape.POINT){
58
			writePoint((FPoint2D)shp, crs);
59
		}else if (type==FShape.LINE){
60
			writeLine((FPolyline2D)shp, crs);
61
		}else if (type==FShape.POLYGON){
62
			writePolygon((FPolygon2D)shp, crs);
63
		}
64
	}
65

  
66
	/**
67
	 * Reproject a geometry
68
	 * @param shp
69
	 */
70
	private void reproject(FShape shp){
71
		ICoordTrans coordTrans = getCoordTrans();
72
		if (coordTrans != null){
73
			try{
74
				shp.reProject(coordTrans);
75
			}catch(Exception e){
76
				//The server is the responsible to reproject
77
				if (projOrig != null){
78
					crs = projOrig.getAbrev();
79
				}
80
			}
81
		}
82
	}
83

  
84
	/**
85
	 * Writes a point in 2D
86
	 * @param point
87
	 * The point to write
88
	 * @param crs
89
	 * The coordinates reference system
90
	 */
91
	private void writePoint(FPoint2D point, String crs){
92
		writer.startPoint(null, new CoordinatesSequencePoint(point), crs);
93
		writer.endPoint();
94
	}
95

  
96
	/**
97
	 * Writes a multipoint in 2D
98
	 * @param point
99
	 * The point to write
100
	 * @param crs
101
	 * The coordinates reference system
102
	 */
103
	private void writeMultiPoint(FMultiPoint2D multi, String crs){
104
		writer.startMultiPoint(null, crs);
105
		for (int i=0 ; i<multi.getNumPoints() ; i++){
106
			FPoint2D point = multi.getPoint(i);
107
			writePoint(point, crs);
108
		}
109
		writer.endMultiPoint();
110
	}
111

  
112
	/**
113
	 * Writes a line in 2D
114
	 * @param line
115
	 * The line to write
116
	 * @param crs
117
	 * The coordinates reference system
118
	 * @param geometries
119
	 * The parsed geometries
120
	 */
121
	private void writeLine(FPolyline2D line, String crs){
122
		boolean isMultipleGeometry = false;
123
		if (isMultiple){
124
			writer.startMultiLineString(null, crs);
125
		}else{
126
			isMultipleGeometry = isMultiple(line.getPathIterator(null));
127
			if (isMultipleGeometry){
128
				writer.startMultiLineString(null, crs);
129
			}
130
		}
131
		CoordinatesSequenceGeneralPath sequence = new CoordinatesSequenceGeneralPath(line.getPathIterator(null));
132
		writer.startLineString(null, sequence, crs);
133
		writer.endLineString();	
134
		if (isMultiple || isMultipleGeometry){
135
			while (sequence.hasMoreGeometries()){
136
				sequence.initialize();
137
				writer.startLineString(null, sequence, crs);
138
				writer.endLineString();	
139
			}
140
			writer.endMultiLineString();
141
		}
142
	}
143

  
144
	/**
145
	 * Writes a polygon in 2D
146
	 * @param polygon
147
	 * The polygon to write
148
	 * @param crs
149
	 * The coordinates reference system
150
	 * @param geometries
151
	 * The parsed geometries
152
	 */
153
	private void writePolygon(FPolygon2D polygon, String crs){
154
		boolean isMultipleGeometry = false;
155
		if (isMultiple){
156
			writer.startMultiPolygon(null, crs);
157
		}else{
158
			isMultipleGeometry = isMultiple(polygon.getPathIterator(null));
159
			if (isMultipleGeometry){
160
				writer.startMultiPolygon(null, crs);
161
			}
162
		}
163
		CoordinatesSequenceGeneralPath sequence = new CoordinatesSequenceGeneralPath(polygon.getPathIterator(null));
164
		writer.startPolygon(null, sequence ,crs);
165
		writer.endPolygon();	
166
		if (isMultiple || isMultipleGeometry){
167
			while (sequence.hasMoreGeometries()){
168
				sequence.initialize();
169
				writer.startPolygon(null, sequence ,crs);
170
				writer.endPolygon();
171
			}
172
			writer.endMultiPolygon();
173
		}
174
	}
175
	
176
	/**
177
	 * Return if the geometry is multiple	
178
	 * @param path
179
	 * @return
180
	 */
181
	public boolean isMultiple(PathIterator path){
182
		double[] coords = new double[2];
183
		int type = 0;
184
		int numGeometries = 0;
185
		while (!path.isDone()){
186
			type = path.currentSegment(coords);
187
			 switch (type) {
188
			 	case PathIterator.SEG_MOVETO:
189
			 		numGeometries++;
190
			 		if (numGeometries == 2){
191
			 			return true;
192
			 		}
193
			 		break;
194
			 	case PathIterator.SEG_CLOSE:
195
			 		return false;			 		
196
			 	default:
197
			 		break;
198
			 }
199
			 path.next();
200
		}
201
		return false;
202
	}
203

  
204
	/**
205
	 * @param projOrig the projOrig to set
206
	 */
207
	public void setProjOrig(IProjection projOrig) {
208
		this.projOrig = projOrig;
209
	}
210

  
211
	/**
212
	 * @param projDest the projDest to set
213
	 */
214
	public void setProjDest(IProjection projDest) {
215
		this.projDest = projDest;
216
	}
217

  
218
	/**
219
	 * @return the coordTrans
220
	 */
221
	private ICoordTrans getCoordTrans() {
222
		if (coordTrans == null){
223
			if ((projOrig == null) || (projDest == null)){
224
				return null;
225
			}
226
			coordTrans = projOrig.getCT(projDest);
227
		}
228
		return coordTrans;
229
	}
230

  
231
	/**
232
	 * @param writer the writer to set
233
	 */
234
	public void setWriter(GPEWriterHandler writer) {
235
		this.writer = writer;
236
	}
237

  
238

  
239

  
240
	/**
241
	 * @param geometry the geometry to set
242
	 */
243
	public void setGeometry(XMLElement geometry) {
244
		if (geometry != null){
245
			if (geometry.getEntityType().getName().toLowerCase().indexOf("multi") > 0){
246
				isMultiple = true;				
247
			}else{
248
				isMultiple = false;	
249
			}
250
		}
251
	}
252

  
253
	/**
254
	 * @return the isMultiple
255
	 */
256
	public boolean isMultiple() {
257
		return isMultiple;
258
	}
259

  
260
	/**
261
	 * @param isMultiple the isMultiple to set
262
	 */
263
	public void setMultiple(boolean isMultiple) {
264
		this.isMultiple = isMultiple;
265
	}
266

  
267
	/**
268
	 * @return the projDest
269
	 */
270
	public IProjection getProjDest() {
271
		return projDest;
272
	}
273

  
274

  
275
	public IProjection getProjOrig() {
276
		return projOrig;
277
	}
278

  
279

  
280
	public Rectangle2D getExtent(Rectangle2D fullExtent) {
281
		if (getProjDest().getAbrev().compareTo(getProjOrig().getAbrev())!=0){
282
			coordTrans = getCoordTrans();
283
			if (coordTrans != null){
284
				try{
285
					return coordTrans.convert(fullExtent);
286
				}catch(Exception e){
287
					//The server is the responsible to reproject
288
					if (projOrig != null){
289
						crs = projOrig.getAbrev();
290
					}
291
				}
292
			}
293
		}	
294
		return fullExtent;
295
	}
296
}

Also available in: Unified diff