svn-gvsig-desktop / tags / v1_1_Build_1013 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FGeometryCollection.java @ 13521
History | View | Annotate | Download (11.1 KB)
1 | 1100 | fjp | /* 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 | 296 | fernando | package com.iver.cit.gvsig.fmap.core; |
42 | |||
43 | 3885 | caballero | import java.awt.Graphics2D; |
44 | 4879 | fjp | import java.awt.Rectangle; |
45 | 5008 | fjp | import java.awt.Shape; |
46 | 3904 | fjp | import java.awt.geom.AffineTransform; |
47 | 4799 | fjp | import java.awt.geom.PathIterator; |
48 | 4879 | fjp | import java.awt.geom.Point2D; |
49 | 3885 | caballero | 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 | 296 | fernando | import com.iver.cit.gvsig.fmap.ViewPort; |
57 | 757 | fjp | import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
58 | 324 | fernando | import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D; |
59 | 703 | fjp | import com.vividsolutions.jts.geom.Geometry; |
60 | 3207 | fjp | import com.vividsolutions.jts.geom.GeometryCollection; |
61 | import com.vividsolutions.jts.geom.GeometryFactory; |
||
62 | 296 | fernando | |
63 | 1005 | vcaballero | |
64 | /**
|
||
65 | * Colecci?n de Geometr?as.
|
||
66 | *
|
||
67 | * @author Vicente Caballero Navarro
|
||
68 | */
|
||
69 | 305 | fjp | public class FGeometryCollection implements IGeometry { |
70 | 3783 | caballero | private ArrayList geometries = new ArrayList(); |
71 | 5880 | fjp | |
72 | 296 | fernando | /**
|
73 | 1005 | vcaballero | * Crea un nuevo FGeometryCollection.
|
74 | *
|
||
75 | * @param geoms vector de geometr?as.
|
||
76 | 296 | fernando | */
|
77 | 1005 | vcaballero | public FGeometryCollection(IGeometry[] geoms) { |
78 | 3783 | caballero | for (int i = 0; i < geoms.length; i++) { |
79 | geometries.add(geoms[i]); |
||
80 | } |
||
81 | 1005 | vcaballero | } |
82 | |||
83 | 3783 | caballero | public void addGeometry(IGeometry g){ |
84 | geometries.add(g); |
||
85 | } |
||
86 | |||
87 | 1005 | vcaballero | /**
|
88 | * @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D,
|
||
89 | * com.iver.cit.gvsig.fmap.ViewPort, FStyle2D)
|
||
90 | */
|
||
91 | 324 | fernando | public void draw(Graphics2D g, ViewPort vp, FStyle2D symbol) { |
92 | 3783 | caballero | for (int i = 0; i < geometries.size(); i++) |
93 | ((IGeometry)geometries.get(i)).draw(g, vp, symbol); |
||
94 | 296 | fernando | } |
95 | |||
96 | /**
|
||
97 | 305 | fjp | * @see com.iver.cit.gvsig.fmap.core.IGeometry#createLabels(int, boolean)
|
98 | 296 | fernando | */
|
99 | 757 | fjp | public FLabel[] createLabels(int position, boolean duplicates) { |
100 | 296 | fernando | return null; |
101 | } |
||
102 | |||
103 | /**
|
||
104 | 305 | fjp | * @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
|
105 | 296 | fernando | */
|
106 | 3783 | caballero | /* 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 | 296 | fernando | public boolean intersects(Rectangle2D r) { |
121 | 1298 | fjp | boolean resul = false; |
122 | 5880 | fjp | for (int i = 0; i < geometries.size(); i++) |
123 | { |
||
124 | resul = ((IGeometry)geometries.get(i)).intersects(r); |
||
125 | if (resul) break; |
||
126 | 1298 | fjp | } |
127 | 5880 | fjp | |
128 | 1298 | fjp | return resul;
|
129 | 296 | fernando | } |
130 | |||
131 | /**
|
||
132 | 305 | fjp | * @see com.iver.cit.gvsig.fmap.core.IGeometry#getBounds2D()
|
133 | 296 | fernando | */
|
134 | public Rectangle2D getBounds2D() { |
||
135 | 310 | fjp | Rectangle2D rAux = null; |
136 | 1005 | vcaballero | |
137 | 3783 | caballero | 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 | 310 | fjp | return rAux;
|
144 | 296 | fernando | } |
145 | 324 | fernando | |
146 | /**
|
||
147 | * @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeometryType()
|
||
148 | */
|
||
149 | public int getGeometryType() { |
||
150 | 3783 | caballero | /*int ret = 0;
|
151 | 1005 | vcaballero | |
152 | 324 | fernando | for (int i = 0; i < geometries.length; i++) {
|
153 | 3783 | caballero | ret = ret | geometries[i].getGeometryType();
|
154 | 324 | fernando | }
|
155 | 1005 | vcaballero | |
156 | 324 | fernando | return ret;
|
157 | 3783 | caballero | */
|
158 | 5880 | fjp | return FShape.LINE;
|
159 | 324 | fernando | } |
160 | 5880 | fjp | |
161 | 458 | fjp | /* (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 | 8765 | jjdelcerro | public void draw(Graphics2D g, ViewPort vp, ISymbol symbol) { |
165 | 5880 | fjp | for (int i = 0; i < geometries.size(); i++) |
166 | ((IGeometry)geometries.get(i)).draw(g, vp, symbol); |
||
167 | 458 | fjp | } |
168 | 703 | fjp | |
169 | /* (non-Javadoc)
|
||
170 | * @see com.iver.cit.gvsig.fmap.core.IGeometry#cloneGeometry()
|
||
171 | */
|
||
172 | public IGeometry cloneGeometry() {
|
||
173 | 3783 | caballero | IGeometry[] newGeometries = new IGeometry[geometries.size()]; |
174 | 1005 | vcaballero | |
175 | 3783 | caballero | for (int i = 0; i < geometries.size(); i++) |
176 | newGeometries[i] = ((IGeometry)geometries.get(i)).cloneGeometry(); |
||
177 | 5880 | fjp | |
178 | return new FGeometryCollection(newGeometries); |
||
179 | 703 | fjp | } |
180 | |||
181 | /* (non-Javadoc)
|
||
182 | * @see com.iver.cit.gvsig.fmap.core.IGeometry#toJTSGeometry()
|
||
183 | */
|
||
184 | public Geometry toJTSGeometry() {
|
||
185 | 3783 | caballero | Geometry[] theGeoms = new Geometry[geometries.size()]; |
186 | for (int i = 0; i < geometries.size(); i++) |
||
187 | 3207 | fjp | { |
188 | 3783 | caballero | theGeoms[i] = ((IGeometry)geometries.get(i)).toJTSGeometry(); |
189 | 3207 | fjp | } |
190 | GeometryCollection geomCol = new GeometryFactory().createGeometryCollection(theGeoms);
|
||
191 | 5880 | fjp | |
192 | |||
193 | 3207 | fjp | return geomCol;
|
194 | 703 | fjp | } |
195 | 885 | fjp | |
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 | 3783 | caballero | for (int i = 0; i < geometries.size(); i++) |
201 | ((IGeometry)geometries.get(i)).reProject(ct); |
||
202 | 885 | fjp | } |
203 | 1132 | vcaballero | |
204 | /**
|
||
205 | 4879 | fjp | * @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
|
206 | 1132 | vcaballero | */
|
207 | 4879 | fjp | public PathIterator getPathIterator(AffineTransform at) { |
208 | 3935 | fjp | // 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 | 4711 | caballero | {
|
226 | 3935 | fjp | int type = pi.currentSegment(coords);
|
227 | switch (type)
|
||
228 | 4711 | caballero | {
|
229 | 3935 | fjp | case GeneralPathXIterator.SEG_MOVETO:
|
230 | if ((!bFirst) || (firstCoord != coords))
|
||
231 | gp.moveTo(coords[0], coords[1]);
|
||
232 | break;
|
||
233 | 4711 | caballero | case SEG_LINETO:
|
234 | 3935 | fjp | 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 | 4711 | caballero | }
|
253 | 3935 | fjp | } */
|
254 | GeneralPathX gp = new GeneralPathX();
|
||
255 | for (int i=0; i < geometries.size(); i++) |
||
256 | { |
||
257 | IGeometry gAux = (IGeometry) geometries.get(i); |
||
258 | 4879 | fjp | gp.append(gAux.getPathIterator(null), true); |
259 | 4711 | caballero | } |
260 | 3935 | fjp | return (GeneralPathXIterator) gp.getPathIterator(null); |
261 | 1132 | vcaballero | } |
262 | 1298 | fjp | |
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 | 3783 | caballero | for (int i = 0; i < geometries.size(); i++) |
269 | 1298 | fjp | { |
270 | 3783 | caballero | resul = ((IGeometry)geometries.get(i)).fastIntersects(x,y,w,h); |
271 | 1298 | fjp | if (resul) break; |
272 | 3783 | caballero | } |
273 | 1298 | fjp | return resul;
|
274 | } |
||
275 | 2183 | fernando | |
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 | 2859 | fjp | |
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 | 8765 | jjdelcerro | public void drawInts(Graphics2D g, ViewPort vp, ISymbol symbol) { |
287 | 5880 | fjp | for (int i = 0; i < geometries.size(); i++) |
288 | ((IGeometry)geometries.get(i)).drawInts(g, vp, symbol); |
||
289 | 3783 | caballero | |
290 | 2859 | fjp | } |
291 | 3783 | caballero | |
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 | 3833 | caballero | |
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 | 5880 | fjp | handlers.add(handAux[j]); |
318 | 3833 | caballero | } |
319 | } |
||
320 | return (Handler[])handlers.toArray(new Handler[0]); |
||
321 | } |
||
322 | 3885 | caballero | |
323 | 3904 | fjp | |
324 | public void transform(AffineTransform at) { |
||
325 | 3885 | caballero | for (int i = 0; i < geometries.size(); i++){ |
326 | 3904 | fjp | ((IGeometry)geometries.get(i)).transform(at); |
327 | 4711 | caballero | } |
328 | 3885 | caballero | } |
329 | 4799 | fjp | |
330 | 4879 | fjp | public PathIterator getPathIterator(AffineTransform at, double flatness) { |
331 | 4799 | fjp | GeneralPathX gp = new GeneralPathX();
|
332 | for (int i=0; i < geometries.size(); i++) |
||
333 | { |
||
334 | IGeometry gAux = (IGeometry) geometries.get(i); |
||
335 | 4879 | fjp | gp.append(gAux.getPathIterator(null), true); |
336 | 4799 | fjp | } |
337 | 5883 | caballero | return gp.getPathIterator(at, flatness);
|
338 | 4799 | fjp | } |
339 | 4879 | fjp | |
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 | 5880 | fjp | 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 | 4879 | fjp | } |
372 | 5880 | fjp | |
373 | return false; |
||
374 | 4879 | fjp | } |
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 | 5008 | fjp | |
412 | public Shape getInternalShape() { |
||
413 | return this; |
||
414 | } |
||
415 | 296 | fernando | } |