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