root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FGeometryCollection.java @ 5878
History | View | Annotate | Download (14.4 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 |
import java.util.Comparator; |
53 |
import java.util.TreeMap; |
54 |
|
55 |
import org.cresques.cts.ICoordTrans; |
56 |
import org.geotools.data.postgis.attributeio.WKBEncoder; |
57 |
|
58 |
import com.iver.cit.gvsig.fmap.ViewPort; |
59 |
import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
60 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
61 |
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D; |
62 |
import com.vividsolutions.jts.geom.Geometry; |
63 |
import com.vividsolutions.jts.geom.GeometryCollection; |
64 |
import com.vividsolutions.jts.geom.GeometryFactory; |
65 |
|
66 |
|
67 |
/**
|
68 |
* Colecci?n de Geometr?as.
|
69 |
*
|
70 |
* @author Vicente Caballero Navarro
|
71 |
*/
|
72 |
public class FGeometryCollection implements IGeometry { |
73 |
private ArrayList geometries = new ArrayList(); |
74 |
private int type=FShape.LINE; |
75 |
/**
|
76 |
* Crea un nuevo FGeometryCollection.
|
77 |
*
|
78 |
* @param geoms vector de geometr?as.
|
79 |
*/
|
80 |
public FGeometryCollection(IGeometry[] geoms) { |
81 |
for (int i = 0; i < geoms.length; i++) { |
82 |
geometries.add(geoms[i]); |
83 |
} |
84 |
} |
85 |
|
86 |
public void addGeometry(IGeometry g){ |
87 |
geometries.add(g); |
88 |
} |
89 |
|
90 |
/**
|
91 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D,
|
92 |
* com.iver.cit.gvsig.fmap.ViewPort, FStyle2D)
|
93 |
*/
|
94 |
public void draw(Graphics2D g, ViewPort vp, FStyle2D symbol) { |
95 |
for (int i = 0; i < geometries.size(); i++) |
96 |
((IGeometry)geometries.get(i)).draw(g, vp, symbol); |
97 |
} |
98 |
|
99 |
/**
|
100 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#createLabels(int, boolean)
|
101 |
*/
|
102 |
public FLabel[] createLabels(int position, boolean duplicates) { |
103 |
return null; |
104 |
} |
105 |
|
106 |
/**
|
107 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
|
108 |
*/
|
109 |
/* public boolean intersects(Rectangle2D r, double flatness) {
|
110 |
boolean resul = false;
|
111 |
for (int i = 0; i < geometries.size(); i++)
|
112 |
{
|
113 |
resul = ((IGeometry)geometries.get(i)).intersects(r, flatness);
|
114 |
if (resul) break;
|
115 |
}
|
116 |
|
117 |
return resul;
|
118 |
}
|
119 |
*/
|
120 |
/**
|
121 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
|
122 |
*/
|
123 |
public boolean intersects(Rectangle2D r) { |
124 |
boolean resul = false; |
125 |
if (type==FShape.LINE) {
|
126 |
for (int i = 0; i < geometries.size(); i++){ |
127 |
resul = ((IGeometry)geometries.get(i)).intersects(r); |
128 |
if (resul) break; |
129 |
} |
130 |
}else if (type==FShape.POLYGON) { |
131 |
GeneralPathX gpx=new GeneralPathX();
|
132 |
Handler lastHandler=null; |
133 |
for (int i = 0; i < geometries.size(); i++) { |
134 |
IGeometry geom=(IGeometry)geometries.get(i); |
135 |
GeneralPathXIterator gpxi=(GeneralPathXIterator)geom.getPathIterator(null);
|
136 |
Handler[] handlers=geom.getHandlers(IGeometry.SELECTHANDLER); |
137 |
if (lastHandler==null || handlers[0].getPoint().distance(lastHandler.getPoint())==0) { |
138 |
gpx.append(gpxi,true);
|
139 |
}else {
|
140 |
gpx.append(gpxi,false);
|
141 |
} |
142 |
lastHandler=handlers[handlers.length-1];
|
143 |
} |
144 |
resul=ShapeFactory.createPolygon2D(gpx).intersects(r); |
145 |
} |
146 |
return resul;
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getBounds2D()
|
151 |
*/
|
152 |
public Rectangle2D getBounds2D() { |
153 |
Rectangle2D rAux = null; |
154 |
|
155 |
for (int i = 0; i < geometries.size(); i++) |
156 |
if (rAux==null){ |
157 |
rAux=((IGeometry)geometries.get(i)).getBounds2D(); |
158 |
}else{
|
159 |
rAux.add(((IGeometry)geometries.get(i)).getBounds2D()); |
160 |
} |
161 |
return rAux;
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeometryType()
|
166 |
*/
|
167 |
public int getGeometryType() { |
168 |
/*int ret = 0;
|
169 |
|
170 |
for (int i = 0; i < geometries.length; i++) {
|
171 |
ret = ret | geometries[i].getGeometryType();
|
172 |
}
|
173 |
|
174 |
return ret;
|
175 |
*/
|
176 |
return type;
|
177 |
} |
178 |
public void setGeometryType(int type) { |
179 |
this.type=type;
|
180 |
} |
181 |
/* (non-Javadoc)
|
182 |
* @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)
|
183 |
*/
|
184 |
public void draw(Graphics2D g, ViewPort vp, FSymbol symbol) { |
185 |
if (type==FShape.LINE) {
|
186 |
for (int i = 0; i < geometries.size(); i++) |
187 |
((IGeometry)geometries.get(i)).draw(g, vp, symbol); |
188 |
}else if (type==FShape.POLYGON) { |
189 |
//GeneralPathXIterator gpxi=(GeneralPathXIterator)getPathIterator(null);
|
190 |
GeneralPathX gpx=new GeneralPathX();
|
191 |
Handler lastHandler=null; |
192 |
for (int i = 0; i < geometries.size(); i++) { |
193 |
IGeometry geom=(IGeometry)geometries.get(i); |
194 |
GeneralPathXIterator gpxi=(GeneralPathXIterator)geom.getPathIterator(null);
|
195 |
Handler[] handlers=geom.getHandlers(IGeometry.SELECTHANDLER); |
196 |
if (lastHandler==null || handlers[0].getPoint().distance(lastHandler.getPoint())==0) { |
197 |
gpx.append(gpxi,true);
|
198 |
}else {
|
199 |
gpx.append(gpxi,false);
|
200 |
} |
201 |
lastHandler=handlers[handlers.length-1];
|
202 |
} |
203 |
ShapeFactory.createPolygon2D(gpx).draw(g,vp,symbol); |
204 |
} |
205 |
} |
206 |
|
207 |
/* (non-Javadoc)
|
208 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#cloneGeometry()
|
209 |
*/
|
210 |
public IGeometry cloneGeometry() {
|
211 |
IGeometry[] newGeometries = new IGeometry[geometries.size()]; |
212 |
|
213 |
for (int i = 0; i < geometries.size(); i++) |
214 |
newGeometries[i] = ((IGeometry)geometries.get(i)).cloneGeometry(); |
215 |
FGeometryCollection gc=new FGeometryCollection(newGeometries);
|
216 |
gc.type=type; |
217 |
return gc;
|
218 |
} |
219 |
|
220 |
/* (non-Javadoc)
|
221 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#toJTSGeometry()
|
222 |
*/
|
223 |
public Geometry toJTSGeometry() {
|
224 |
Geometry[] theGeoms = new Geometry[geometries.size()]; |
225 |
for (int i = 0; i < geometries.size(); i++) |
226 |
{ |
227 |
theGeoms[i] = ((IGeometry)geometries.get(i)).toJTSGeometry(); |
228 |
} |
229 |
GeometryCollection geomCol = new GeometryFactory().createGeometryCollection(theGeoms);
|
230 |
|
231 |
|
232 |
return geomCol;
|
233 |
} |
234 |
|
235 |
/* (non-Javadoc)
|
236 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#reProject(org.cresques.cts.ICoordTrans)
|
237 |
*/
|
238 |
public void reProject(ICoordTrans ct) { |
239 |
for (int i = 0; i < geometries.size(); i++) |
240 |
((IGeometry)geometries.get(i)).reProject(ct); |
241 |
} |
242 |
|
243 |
/**
|
244 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
|
245 |
*/
|
246 |
public PathIterator getPathIterator(AffineTransform at) { |
247 |
// Necesitamos convertir todo esto a una GeneralPathX, tarde o temprano.
|
248 |
// As? que lo intento aqu? por primera vez.
|
249 |
// Lo necesitamos para la edici?n, porque se est?n
|
250 |
// a?adiendo las geometr?as como FGeometryCollection
|
251 |
// para que el explode sea sencillo. No lo veo muy
|
252 |
// claro eso, pero bueno.
|
253 |
/* GeneralPathX gp = new GeneralPathX();
|
254 |
double[] coords = new double[6];
|
255 |
for (int i=0; i < geometries.size(); i++)
|
256 |
{
|
257 |
IGeometry gAux = (IGeometry) geometries.get(i);
|
258 |
GeneralPathXIterator pi = gAux.getGeneralPathXIterator();
|
259 |
// Si el primer punto y el ultimo son iguales, conectamos
|
260 |
// la geometr?a.
|
261 |
boolean bFirst = true;
|
262 |
double[] firstCoord = new double[6];
|
263 |
while (!pi.isDone())
|
264 |
{
|
265 |
int type = pi.currentSegment(coords);
|
266 |
switch (type)
|
267 |
{
|
268 |
case GeneralPathXIterator.SEG_MOVETO:
|
269 |
if ((!bFirst) || (firstCoord != coords))
|
270 |
gp.moveTo(coords[0], coords[1]);
|
271 |
break;
|
272 |
case SEG_LINETO:
|
273 |
lineTo(coords[0], coords[1]);
|
274 |
break;
|
275 |
case SEG_QUADTO:
|
276 |
quadTo(coords[0], coords[1],
|
277 |
coords[2], coords[3]);
|
278 |
break;
|
279 |
case SEG_CUBICTO:
|
280 |
// Not implemented
|
281 |
System.err.println("ERROR. TRAMO CUBICO. SIN IMPLEMENTAR TODAV?A");
|
282 |
curveTo(coords[0], coords[1],
|
283 |
coords[2], coords[3],
|
284 |
coords[4], coords[5]);
|
285 |
break;
|
286 |
case SEG_CLOSE:
|
287 |
closePath();
|
288 |
break;
|
289 |
}
|
290 |
pi.next();
|
291 |
}
|
292 |
} */
|
293 |
GeneralPathX gp = new GeneralPathX();
|
294 |
for (int i=0; i < geometries.size(); i++) |
295 |
{ |
296 |
IGeometry gAux = (IGeometry) geometries.get(i); |
297 |
gp.append(gAux.getPathIterator(null), true); |
298 |
} |
299 |
return (GeneralPathXIterator) gp.getPathIterator(null); |
300 |
} |
301 |
|
302 |
/* (non-Javadoc)
|
303 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#fastIntersects(double, double, double, double)
|
304 |
*/
|
305 |
public boolean fastIntersects(double x, double y, double w, double h) { |
306 |
boolean resul = false; |
307 |
for (int i = 0; i < geometries.size(); i++) |
308 |
{ |
309 |
resul = ((IGeometry)geometries.get(i)).fastIntersects(x,y,w,h); |
310 |
if (resul) break; |
311 |
} |
312 |
return resul;
|
313 |
} |
314 |
|
315 |
/**
|
316 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#toWKB()
|
317 |
*/
|
318 |
public byte[] toWKB() throws IOException { |
319 |
return WKBEncoder.encodeGeometry(toJTSGeometry());
|
320 |
} |
321 |
|
322 |
/* (non-Javadoc)
|
323 |
* @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)
|
324 |
*/
|
325 |
public void drawInts(Graphics2D g, ViewPort vp, FSymbol symbol) { |
326 |
if (type==FShape.LINE) {
|
327 |
for (int i = 0; i < geometries.size(); i++) |
328 |
((IGeometry)geometries.get(i)).drawInts(g, vp, symbol); |
329 |
}else if (type==FShape.POLYGON) { |
330 |
//GeneralPathXIterator gpxi=(GeneralPathXIterator)getPathIterator(null);
|
331 |
GeneralPathX gpx=new GeneralPathX();
|
332 |
Handler lastHandler=null; |
333 |
for (int i = 0; i < geometries.size(); i++) { |
334 |
IGeometry geom=(IGeometry)geometries.get(i); |
335 |
GeneralPathXIterator gpxi=(GeneralPathXIterator)geom.getPathIterator(null);
|
336 |
Handler[] handlers=geom.getHandlers(IGeometry.SELECTHANDLER); |
337 |
if (lastHandler==null || handlers[0].getPoint().distance(lastHandler.getPoint())==0) { |
338 |
gpx.append(gpxi,true);
|
339 |
}else {
|
340 |
gpx.append(gpxi,false);
|
341 |
} |
342 |
lastHandler=handlers[handlers.length-1];
|
343 |
} |
344 |
IGeometry geom=ShapeFactory.createPolygon2D(gpx); |
345 |
geom.setGeometryType(type); |
346 |
geom.drawInts(g,vp,symbol); |
347 |
} |
348 |
|
349 |
} |
350 |
|
351 |
/**
|
352 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getShapes()
|
353 |
*/
|
354 |
/*public FShape[] getShapes() {
|
355 |
ArrayList shapes=new ArrayList();
|
356 |
for (int i= 0;i<geometries.size();i++){
|
357 |
FShape[] s=((IGeometry)geometries.get(i)).getShapes();
|
358 |
for (int j=0;j<s.length;j++){
|
359 |
shapes.add(s[j]);
|
360 |
}
|
361 |
}
|
362 |
return (FShape[])shapes.toArray(new FShape[0]);
|
363 |
}*/
|
364 |
public IGeometry[] getGeometries(){ |
365 |
return (IGeometry[])geometries.toArray(new IGeometry[0]).clone(); |
366 |
} |
367 |
|
368 |
/* (non-Javadoc)
|
369 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getHandlers(int)
|
370 |
*/
|
371 |
public Handler[] getHandlers(int type) { |
372 |
ArrayList handlers=new ArrayList(); |
373 |
for (int i = 0; i < geometries.size(); i++){ |
374 |
Handler[] handAux=((IGeometry)geometries.get(i)).getHandlers(type); |
375 |
for (int j=0;j<handAux.length;j++){ |
376 |
if (!handlers.contains(handAux[j]))
|
377 |
handlers.add(handAux[j]); |
378 |
} |
379 |
} |
380 |
return (Handler[])handlers.toArray(new Handler[0]); |
381 |
} |
382 |
|
383 |
|
384 |
public void transform(AffineTransform at) { |
385 |
for (int i = 0; i < geometries.size(); i++){ |
386 |
((IGeometry)geometries.get(i)).transform(at); |
387 |
} |
388 |
} |
389 |
|
390 |
public PathIterator getPathIterator(AffineTransform at, double flatness) { |
391 |
GeneralPathX gp = new GeneralPathX();
|
392 |
for (int i=0; i < geometries.size(); i++) |
393 |
{ |
394 |
IGeometry gAux = (IGeometry) geometries.get(i); |
395 |
gp.append(gAux.getPathIterator(null), true); |
396 |
} |
397 |
return (GeneralPathXIterator) gp.getPathIterator(at, flatness);
|
398 |
} |
399 |
|
400 |
public boolean contains(double x, double y) { |
401 |
boolean bRes;
|
402 |
for (int i=0; i < geometries.size(); i++) |
403 |
{ |
404 |
IGeometry gAux = (IGeometry) geometries.get(i); |
405 |
bRes = gAux.contains(x,y); |
406 |
if (bRes) return bRes; |
407 |
} |
408 |
|
409 |
return false; |
410 |
} |
411 |
|
412 |
public boolean contains(double x, double y, double w, double h) { |
413 |
boolean bRes;
|
414 |
for (int i=0; i < geometries.size(); i++) |
415 |
{ |
416 |
IGeometry gAux = (IGeometry) geometries.get(i); |
417 |
bRes = gAux.contains(x,y, w, h); |
418 |
if (bRes) return bRes; |
419 |
} |
420 |
|
421 |
return false; |
422 |
} |
423 |
|
424 |
public boolean intersects(double x, double y, double w, double h) { |
425 |
boolean resul = false; |
426 |
if (type==FShape.LINE) {
|
427 |
for (int i=0; i < geometries.size(); i++){ |
428 |
IGeometry gAux = (IGeometry) geometries.get(i); |
429 |
resul= gAux.intersects(x,y, w, h); |
430 |
if (resul) break; |
431 |
} |
432 |
}else if (type==FShape.POLYGON) { |
433 |
GeneralPathX gpx=new GeneralPathX();
|
434 |
Handler lastHandler=null; |
435 |
for (int i = 0; i < geometries.size(); i++) { |
436 |
IGeometry geom=(IGeometry)geometries.get(i); |
437 |
GeneralPathXIterator gpxi=(GeneralPathXIterator)geom.getPathIterator(null);
|
438 |
Handler[] handlers=geom.getHandlers(IGeometry.SELECTHANDLER); |
439 |
if (lastHandler==null || handlers[0].getPoint().distance(lastHandler.getPoint())==0) { |
440 |
gpx.append(gpxi,true);
|
441 |
}else {
|
442 |
gpx.append(gpxi,false);
|
443 |
} |
444 |
lastHandler=handlers[handlers.length-1];
|
445 |
} |
446 |
IGeometry geom=ShapeFactory.createPolygon2D(gpx); |
447 |
geom.setGeometryType(FShape.POLYGON); |
448 |
resul=geom.intersects(x,y,w,h); |
449 |
} |
450 |
return resul;
|
451 |
} |
452 |
|
453 |
public Rectangle getBounds() { |
454 |
Rectangle rAux = null; |
455 |
|
456 |
for (int i = 0; i < geometries.size(); i++) |
457 |
if (rAux==null){ |
458 |
rAux=((IGeometry)geometries.get(i)).getBounds(); |
459 |
}else{
|
460 |
rAux.add(((IGeometry)geometries.get(i)).getBounds()); |
461 |
} |
462 |
return rAux;
|
463 |
} |
464 |
|
465 |
public boolean contains(Point2D p) { |
466 |
boolean bRes;
|
467 |
for (int i=0; i < geometries.size(); i++) |
468 |
{ |
469 |
IGeometry gAux = (IGeometry) geometries.get(i); |
470 |
bRes = gAux.contains(p); |
471 |
if (bRes) return bRes; |
472 |
} |
473 |
|
474 |
return false; |
475 |
} |
476 |
|
477 |
public boolean contains(Rectangle2D r) { |
478 |
boolean bRes;
|
479 |
for (int i=0; i < geometries.size(); i++) |
480 |
{ |
481 |
IGeometry gAux = (IGeometry) geometries.get(i); |
482 |
bRes = gAux.contains(r); |
483 |
if (bRes) return bRes; |
484 |
} |
485 |
|
486 |
return false; |
487 |
} |
488 |
|
489 |
public Shape getInternalShape() { |
490 |
return this; |
491 |
} |
492 |
|
493 |
public int getOriginalGeometryType() { |
494 |
return type;
|
495 |
} |
496 |
|
497 |
public boolean isClosed() { |
498 |
GeneralPathX gpx=new GeneralPathX();
|
499 |
PathIterator pi=getPathIterator(null); |
500 |
gpx.append(pi,true);
|
501 |
return gpx.isClosed();
|
502 |
} |
503 |
} |