root / tags / v1_1_Build_1004 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FGeometryCollection.java @ 12319
History | View | Annotate | Download (11.1 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.core; |
42 |
|
43 |
import java.awt.Graphics2D; |
44 |
import java.awt.Rectangle; |
45 |
import java.awt.Shape; |
46 |
import java.awt.geom.AffineTransform; |
47 |
import java.awt.geom.PathIterator; |
48 |
import java.awt.geom.Point2D; |
49 |
import java.awt.geom.Rectangle2D; |
50 |
import java.io.IOException; |
51 |
import java.util.ArrayList; |
52 |
|
53 |
import org.cresques.cts.ICoordTrans; |
54 |
import org.geotools.data.postgis.attributeio.WKBEncoder; |
55 |
|
56 |
import com.iver.cit.gvsig.fmap.ViewPort; |
57 |
import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
58 |
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D; |
59 |
import com.vividsolutions.jts.geom.Geometry; |
60 |
import com.vividsolutions.jts.geom.GeometryCollection; |
61 |
import com.vividsolutions.jts.geom.GeometryFactory; |
62 |
|
63 |
|
64 |
/**
|
65 |
* Colecci?n de Geometr?as.
|
66 |
*
|
67 |
* @author Vicente Caballero Navarro
|
68 |
*/
|
69 |
public class FGeometryCollection implements IGeometry { |
70 |
private ArrayList geometries = new ArrayList(); |
71 |
|
72 |
/**
|
73 |
* Crea un nuevo FGeometryCollection.
|
74 |
*
|
75 |
* @param geoms vector de geometr?as.
|
76 |
*/
|
77 |
public FGeometryCollection(IGeometry[] geoms) { |
78 |
for (int i = 0; i < geoms.length; i++) { |
79 |
geometries.add(geoms[i]); |
80 |
} |
81 |
} |
82 |
|
83 |
public void addGeometry(IGeometry g){ |
84 |
geometries.add(g); |
85 |
} |
86 |
|
87 |
/**
|
88 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D,
|
89 |
* com.iver.cit.gvsig.fmap.ViewPort, FStyle2D)
|
90 |
*/
|
91 |
public void draw(Graphics2D g, ViewPort vp, FStyle2D symbol) { |
92 |
for (int i = 0; i < geometries.size(); i++) |
93 |
((IGeometry)geometries.get(i)).draw(g, vp, symbol); |
94 |
} |
95 |
|
96 |
/**
|
97 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#createLabels(int, boolean)
|
98 |
*/
|
99 |
public FLabel[] createLabels(int position, boolean duplicates) { |
100 |
return null; |
101 |
} |
102 |
|
103 |
/**
|
104 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
|
105 |
*/
|
106 |
/* public boolean intersects(Rectangle2D r, double flatness) {
|
107 |
boolean resul = false;
|
108 |
for (int i = 0; i < geometries.size(); i++)
|
109 |
{
|
110 |
resul = ((IGeometry)geometries.get(i)).intersects(r, flatness);
|
111 |
if (resul) break;
|
112 |
}
|
113 |
|
114 |
return resul;
|
115 |
}
|
116 |
*/
|
117 |
/**
|
118 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
|
119 |
*/
|
120 |
public boolean intersects(Rectangle2D r) { |
121 |
boolean resul = false; |
122 |
for (int i = 0; i < geometries.size(); i++) |
123 |
{ |
124 |
resul = ((IGeometry)geometries.get(i)).intersects(r); |
125 |
if (resul) break; |
126 |
} |
127 |
|
128 |
return resul;
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getBounds2D()
|
133 |
*/
|
134 |
public Rectangle2D getBounds2D() { |
135 |
Rectangle2D rAux = null; |
136 |
|
137 |
for (int i = 0; i < geometries.size(); i++) |
138 |
if (rAux==null){ |
139 |
rAux=((IGeometry)geometries.get(i)).getBounds2D(); |
140 |
}else{
|
141 |
rAux.add(((IGeometry)geometries.get(i)).getBounds2D()); |
142 |
} |
143 |
return rAux;
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeometryType()
|
148 |
*/
|
149 |
public int getGeometryType() { |
150 |
/*int ret = 0;
|
151 |
|
152 |
for (int i = 0; i < geometries.length; i++) {
|
153 |
ret = ret | geometries[i].getGeometryType();
|
154 |
}
|
155 |
|
156 |
return ret;
|
157 |
*/
|
158 |
return FShape.LINE;
|
159 |
} |
160 |
|
161 |
/* (non-Javadoc)
|
162 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.core.v02.FSymbol)
|
163 |
*/
|
164 |
public void draw(Graphics2D g, ViewPort vp, ISymbol symbol) { |
165 |
for (int i = 0; i < geometries.size(); i++) |
166 |
((IGeometry)geometries.get(i)).draw(g, vp, symbol); |
167 |
} |
168 |
|
169 |
/* (non-Javadoc)
|
170 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#cloneGeometry()
|
171 |
*/
|
172 |
public IGeometry cloneGeometry() {
|
173 |
IGeometry[] newGeometries = new IGeometry[geometries.size()]; |
174 |
|
175 |
for (int i = 0; i < geometries.size(); i++) |
176 |
newGeometries[i] = ((IGeometry)geometries.get(i)).cloneGeometry(); |
177 |
|
178 |
return new FGeometryCollection(newGeometries); |
179 |
} |
180 |
|
181 |
/* (non-Javadoc)
|
182 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#toJTSGeometry()
|
183 |
*/
|
184 |
public Geometry toJTSGeometry() {
|
185 |
Geometry[] theGeoms = new Geometry[geometries.size()]; |
186 |
for (int i = 0; i < geometries.size(); i++) |
187 |
{ |
188 |
theGeoms[i] = ((IGeometry)geometries.get(i)).toJTSGeometry(); |
189 |
} |
190 |
GeometryCollection geomCol = new GeometryFactory().createGeometryCollection(theGeoms);
|
191 |
|
192 |
|
193 |
return geomCol;
|
194 |
} |
195 |
|
196 |
/* (non-Javadoc)
|
197 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#reProject(org.cresques.cts.ICoordTrans)
|
198 |
*/
|
199 |
public void reProject(ICoordTrans ct) { |
200 |
for (int i = 0; i < geometries.size(); i++) |
201 |
((IGeometry)geometries.get(i)).reProject(ct); |
202 |
} |
203 |
|
204 |
/**
|
205 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
|
206 |
*/
|
207 |
public PathIterator getPathIterator(AffineTransform at) { |
208 |
// Necesitamos convertir todo esto a una GeneralPathX, tarde o temprano.
|
209 |
// As? que lo intento aqu? por primera vez.
|
210 |
// Lo necesitamos para la edici?n, porque se est?n
|
211 |
// a?adiendo las geometr?as como FGeometryCollection
|
212 |
// para que el explode sea sencillo. No lo veo muy
|
213 |
// claro eso, pero bueno.
|
214 |
/* GeneralPathX gp = new GeneralPathX();
|
215 |
double[] coords = new double[6];
|
216 |
for (int i=0; i < geometries.size(); i++)
|
217 |
{
|
218 |
IGeometry gAux = (IGeometry) geometries.get(i);
|
219 |
GeneralPathXIterator pi = gAux.getGeneralPathXIterator();
|
220 |
// Si el primer punto y el ultimo son iguales, conectamos
|
221 |
// la geometr?a.
|
222 |
boolean bFirst = true;
|
223 |
double[] firstCoord = new double[6];
|
224 |
while (!pi.isDone())
|
225 |
{
|
226 |
int type = pi.currentSegment(coords);
|
227 |
switch (type)
|
228 |
{
|
229 |
case GeneralPathXIterator.SEG_MOVETO:
|
230 |
if ((!bFirst) || (firstCoord != coords))
|
231 |
gp.moveTo(coords[0], coords[1]);
|
232 |
break;
|
233 |
case SEG_LINETO:
|
234 |
lineTo(coords[0], coords[1]);
|
235 |
break;
|
236 |
case SEG_QUADTO:
|
237 |
quadTo(coords[0], coords[1],
|
238 |
coords[2], coords[3]);
|
239 |
break;
|
240 |
case SEG_CUBICTO:
|
241 |
// Not implemented
|
242 |
System.err.println("ERROR. TRAMO CUBICO. SIN IMPLEMENTAR TODAV?A");
|
243 |
curveTo(coords[0], coords[1],
|
244 |
coords[2], coords[3],
|
245 |
coords[4], coords[5]);
|
246 |
break;
|
247 |
case SEG_CLOSE:
|
248 |
closePath();
|
249 |
break;
|
250 |
}
|
251 |
pi.next();
|
252 |
}
|
253 |
} */
|
254 |
GeneralPathX gp = new GeneralPathX();
|
255 |
for (int i=0; i < geometries.size(); i++) |
256 |
{ |
257 |
IGeometry gAux = (IGeometry) geometries.get(i); |
258 |
gp.append(gAux.getPathIterator(null), true); |
259 |
} |
260 |
return (GeneralPathXIterator) gp.getPathIterator(null); |
261 |
} |
262 |
|
263 |
/* (non-Javadoc)
|
264 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#fastIntersects(double, double, double, double)
|
265 |
*/
|
266 |
public boolean fastIntersects(double x, double y, double w, double h) { |
267 |
boolean resul = false; |
268 |
for (int i = 0; i < geometries.size(); i++) |
269 |
{ |
270 |
resul = ((IGeometry)geometries.get(i)).fastIntersects(x,y,w,h); |
271 |
if (resul) break; |
272 |
} |
273 |
return resul;
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#toWKB()
|
278 |
*/
|
279 |
public byte[] toWKB() throws IOException { |
280 |
return WKBEncoder.encodeGeometry(toJTSGeometry());
|
281 |
} |
282 |
|
283 |
/* (non-Javadoc)
|
284 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#drawInts(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.core.v02.FSymbol)
|
285 |
*/
|
286 |
public void drawInts(Graphics2D g, ViewPort vp, ISymbol symbol) { |
287 |
for (int i = 0; i < geometries.size(); i++) |
288 |
((IGeometry)geometries.get(i)).drawInts(g, vp, symbol); |
289 |
|
290 |
} |
291 |
|
292 |
/**
|
293 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getShapes()
|
294 |
*/
|
295 |
/*public FShape[] getShapes() {
|
296 |
ArrayList shapes=new ArrayList();
|
297 |
for (int i= 0;i<geometries.size();i++){
|
298 |
FShape[] s=((IGeometry)geometries.get(i)).getShapes();
|
299 |
for (int j=0;j<s.length;j++){
|
300 |
shapes.add(s[j]);
|
301 |
}
|
302 |
}
|
303 |
return (FShape[])shapes.toArray(new FShape[0]);
|
304 |
}*/
|
305 |
public IGeometry[] getGeometries(){ |
306 |
return (IGeometry[])geometries.toArray(new IGeometry[0]).clone(); |
307 |
} |
308 |
|
309 |
/* (non-Javadoc)
|
310 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getHandlers(int)
|
311 |
*/
|
312 |
public Handler[] getHandlers(int type) { |
313 |
ArrayList handlers=new ArrayList(); |
314 |
for (int i = 0; i < geometries.size(); i++){ |
315 |
Handler[] handAux=((IGeometry)geometries.get(i)).getHandlers(type); |
316 |
for (int j=0;j<handAux.length;j++){ |
317 |
handlers.add(handAux[j]); |
318 |
} |
319 |
} |
320 |
return (Handler[])handlers.toArray(new Handler[0]); |
321 |
} |
322 |
|
323 |
|
324 |
public void transform(AffineTransform at) { |
325 |
for (int i = 0; i < geometries.size(); i++){ |
326 |
((IGeometry)geometries.get(i)).transform(at); |
327 |
} |
328 |
} |
329 |
|
330 |
public PathIterator getPathIterator(AffineTransform at, double flatness) { |
331 |
GeneralPathX gp = new GeneralPathX();
|
332 |
for (int i=0; i < geometries.size(); i++) |
333 |
{ |
334 |
IGeometry gAux = (IGeometry) geometries.get(i); |
335 |
gp.append(gAux.getPathIterator(null), true); |
336 |
} |
337 |
return gp.getPathIterator(at, flatness);
|
338 |
} |
339 |
|
340 |
public boolean contains(double x, double y) { |
341 |
boolean bRes;
|
342 |
for (int i=0; i < geometries.size(); i++) |
343 |
{ |
344 |
IGeometry gAux = (IGeometry) geometries.get(i); |
345 |
bRes = gAux.contains(x,y); |
346 |
if (bRes) return bRes; |
347 |
} |
348 |
|
349 |
return false; |
350 |
} |
351 |
|
352 |
public boolean contains(double x, double y, double w, double h) { |
353 |
boolean bRes;
|
354 |
for (int i=0; i < geometries.size(); i++) |
355 |
{ |
356 |
IGeometry gAux = (IGeometry) geometries.get(i); |
357 |
bRes = gAux.contains(x,y, w, h); |
358 |
if (bRes) return bRes; |
359 |
} |
360 |
|
361 |
return false; |
362 |
} |
363 |
|
364 |
public boolean intersects(double x, double y, double w, double h) { |
365 |
boolean bRes;
|
366 |
for (int i=0; i < geometries.size(); i++) |
367 |
{ |
368 |
IGeometry gAux = (IGeometry) geometries.get(i); |
369 |
bRes = gAux.intersects(x,y, w, h); |
370 |
if (bRes) return bRes; |
371 |
} |
372 |
|
373 |
return false; |
374 |
} |
375 |
|
376 |
public Rectangle getBounds() { |
377 |
Rectangle rAux = null; |
378 |
|
379 |
for (int i = 0; i < geometries.size(); i++) |
380 |
if (rAux==null){ |
381 |
rAux=((IGeometry)geometries.get(i)).getBounds(); |
382 |
}else{
|
383 |
rAux.add(((IGeometry)geometries.get(i)).getBounds()); |
384 |
} |
385 |
return rAux;
|
386 |
} |
387 |
|
388 |
public boolean contains(Point2D p) { |
389 |
boolean bRes;
|
390 |
for (int i=0; i < geometries.size(); i++) |
391 |
{ |
392 |
IGeometry gAux = (IGeometry) geometries.get(i); |
393 |
bRes = gAux.contains(p); |
394 |
if (bRes) return bRes; |
395 |
} |
396 |
|
397 |
return false; |
398 |
} |
399 |
|
400 |
public boolean contains(Rectangle2D r) { |
401 |
boolean bRes;
|
402 |
for (int i=0; i < geometries.size(); i++) |
403 |
{ |
404 |
IGeometry gAux = (IGeometry) geometries.get(i); |
405 |
bRes = gAux.contains(r); |
406 |
if (bRes) return bRes; |
407 |
} |
408 |
|
409 |
return false; |
410 |
} |
411 |
|
412 |
public Shape getInternalShape() { |
413 |
return this; |
414 |
} |
415 |
} |