svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / rendering / strategies / SelectedEnvelopeVisitor.java @ 40559
History | View | Annotate | Download (5.9 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.fmap.mapcontext.rendering.strategies; |
||
25 | |||
26 | import org.cresques.cts.ICoordTrans; |
||
27 | import org.slf4j.Logger; |
||
28 | import org.slf4j.LoggerFactory; |
||
29 | |||
30 | import org.gvsig.fmap.dal.DataSet; |
||
31 | import org.gvsig.fmap.dal.feature.Feature; |
||
32 | import org.gvsig.fmap.dal.feature.FeatureSelection; |
||
33 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
34 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
35 | import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor; |
||
36 | import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer; |
||
37 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
38 | import org.gvsig.tools.dispose.DisposableIterator; |
||
39 | import org.gvsig.tools.exception.BaseException; |
||
40 | import org.gvsig.tools.visitor.NotSupportedOperationException; |
||
41 | |||
42 | /**
|
||
43 | * This visitor accumulates the envelope of the
|
||
44 | * selected geometries
|
||
45 | */
|
||
46 | public class SelectedEnvelopeVisitor implements LayersVisitor { |
||
47 | |||
48 | private static final Logger logger = |
||
49 | LoggerFactory.getLogger(SelectedEnvelopeVisitor.class); |
||
50 | |||
51 | private Envelope rectangle = null; |
||
52 | public Envelope getSelectioEnvelope() {
|
||
53 | return rectangle;
|
||
54 | } |
||
55 | |||
56 | public String getProcessDescription() { |
||
57 | return "Defining rectangle to zoom from selected geometries"; |
||
58 | } |
||
59 | |||
60 | public void visit(Object obj) throws BaseException { |
||
61 | if (obj instanceof FLayer) { |
||
62 | this.visit((FLayer) obj);
|
||
63 | } else {
|
||
64 | throw new NotSupportedOperationException(this, obj); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | public void visit(FLayer layer) throws BaseException { |
||
69 | |||
70 | if (!(layer instanceof SingleLayer)) { |
||
71 | return;
|
||
72 | } else if (!layer.isActive()) { |
||
73 | return;
|
||
74 | } else if (!(layer instanceof FLyrVect)) { |
||
75 | return;
|
||
76 | } |
||
77 | |||
78 | FLyrVect lyr_vect = (FLyrVect) layer; |
||
79 | BaseException be = null;
|
||
80 | |||
81 | DataSet selection = ((SingleLayer) layer).getDataStore() |
||
82 | .getSelection(); |
||
83 | |||
84 | if (selection != null && (selection instanceof FeatureSelection)) { |
||
85 | |||
86 | long tot_count = lyr_vect.getFeatureStore().getFeatureCount();
|
||
87 | FeatureSelection fsel = (FeatureSelection) selection; |
||
88 | long sel_count = fsel.getSize();
|
||
89 | if (tot_count == sel_count) {
|
||
90 | try {
|
||
91 | /*
|
||
92 | * If all selected, use layer envelope
|
||
93 | */
|
||
94 | rectangle = accum(rectangle, lyr_vect.getFullEnvelope(), null);
|
||
95 | } catch (CloneNotSupportedException ex) { |
||
96 | /*
|
||
97 | * This should not happen because
|
||
98 | * envelopes support clone
|
||
99 | */
|
||
100 | logger.debug("Error while adding envelope: " + ex.getMessage(), ex);
|
||
101 | } |
||
102 | } else {
|
||
103 | |||
104 | /*
|
||
105 | * We'll use this reprojection if not null
|
||
106 | * to convert feature envelopes
|
||
107 | */
|
||
108 | ICoordTrans ct = lyr_vect.getCoordTrans(); |
||
109 | |||
110 | DisposableIterator iter = fsel.fastIterator(); |
||
111 | Feature feat = null;
|
||
112 | while (iter.hasNext()) {
|
||
113 | feat = (Feature) iter.next(); |
||
114 | try {
|
||
115 | rectangle = accum( |
||
116 | rectangle, feat.getDefaultEnvelope(), ct); |
||
117 | } catch (CloneNotSupportedException ex) { |
||
118 | /*
|
||
119 | * This should not happen because
|
||
120 | * envelopes support clone
|
||
121 | */
|
||
122 | logger.debug("Error while adding envelope: " + ex.getMessage(), ex);
|
||
123 | break;
|
||
124 | } |
||
125 | } |
||
126 | iter.dispose(); |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /**
|
||
132 | * Adds add_env envelope to ini_env after reprojecting add_env
|
||
133 | * if cotr not null
|
||
134 | *
|
||
135 | * @param ini_env
|
||
136 | * @param add_env
|
||
137 | * @param cotr
|
||
138 | * @return
|
||
139 | */
|
||
140 | private Envelope accum(
|
||
141 | Envelope ini_env, |
||
142 | Envelope add_env, |
||
143 | ICoordTrans cotr) throws CloneNotSupportedException { |
||
144 | |||
145 | if (add_env == null) { |
||
146 | /*
|
||
147 | * Nothing to add
|
||
148 | */
|
||
149 | return ini_env;
|
||
150 | } else {
|
||
151 | |||
152 | if (cotr == null) { |
||
153 | |||
154 | /*
|
||
155 | * No reprojection needed
|
||
156 | */
|
||
157 | if (ini_env == null) { |
||
158 | return add_env;
|
||
159 | } else {
|
||
160 | Envelope resp = (Envelope) ini_env.clone(); |
||
161 | resp.add(add_env); |
||
162 | return resp;
|
||
163 | } |
||
164 | |||
165 | } else {
|
||
166 | |||
167 | /*
|
||
168 | * Reproject before adding
|
||
169 | */
|
||
170 | Envelope add_rep = (Envelope) add_env.convert(cotr).clone(); |
||
171 | if (ini_env == null) { |
||
172 | /*
|
||
173 | * Initial was null
|
||
174 | */
|
||
175 | return add_rep;
|
||
176 | } else {
|
||
177 | Envelope resp = (Envelope) ini_env.clone(); |
||
178 | resp.add(add_rep); |
||
179 | return resp;
|
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | |||
186 | } |