Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / strategies / SelectedEnvelopeVisitor.java @ 39511

History | View | Annotate | Download (6.17 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 org.gvsig.fmap.mapcontext.rendering.strategies;
42

    
43
import org.cresques.cts.ICoordTrans;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

    
47
import org.gvsig.fmap.dal.DataSet;
48
import org.gvsig.fmap.dal.feature.Feature;
49
import org.gvsig.fmap.dal.feature.FeatureSelection;
50
import org.gvsig.fmap.geom.primitive.Envelope;
51
import org.gvsig.fmap.mapcontext.layers.FLayer;
52
import org.gvsig.fmap.mapcontext.layers.operations.LayersVisitor;
53
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
54
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
55
import org.gvsig.tools.dispose.DisposableIterator;
56
import org.gvsig.tools.exception.BaseException;
57
import org.gvsig.tools.visitor.NotSupportedOperationException;
58

    
59
/**
60
 * This visitor accumulates the envelope of the
61
 * selected geometries
62
 */
63
public class SelectedEnvelopeVisitor implements LayersVisitor {
64
    
65
    private static final Logger logger =
66
        LoggerFactory.getLogger(SelectedEnvelopeVisitor.class);
67
    
68
        private Envelope rectangle = null;
69
        public Envelope getSelectioEnvelope() {
70
            return rectangle;
71
        }
72

    
73
        public String getProcessDescription() {
74
                return "Defining rectangle to zoom from selected geometries";
75
        }
76

    
77
        public void visit(Object obj) throws BaseException {
78
                if (obj instanceof FLayer) {
79
                        this.visit((FLayer) obj);
80
                } else {
81
                    throw new NotSupportedOperationException(this, obj);
82
                }
83
        }
84

    
85
        public void visit(FLayer layer) throws BaseException {
86
            
87
                if (!(layer instanceof SingleLayer)) {
88
                        return;
89
                } else if (!layer.isActive()) {
90
                        return;
91
        } else if (!(layer instanceof FLyrVect)) {
92
            return;
93
        }
94
                
95
                FLyrVect lyr_vect = (FLyrVect) layer;
96
                BaseException be = null;
97
                
98
                DataSet selection = ((SingleLayer) layer).getDataStore()
99
                                .getSelection();
100
                
101
                if (selection != null && (selection instanceof FeatureSelection)) {
102
                    
103
                    long tot_count = lyr_vect.getFeatureStore().getFeatureCount();
104
                    FeatureSelection fsel = (FeatureSelection) selection;
105
                    long sel_count = fsel.getSize();
106
                    if (tot_count == sel_count) {
107
                        try {
108
                            /*
109
                             * If all selected, use layer envelope
110
                             */
111
                            rectangle = accum(rectangle, lyr_vect.getFullEnvelope(), null);
112
                        } catch (CloneNotSupportedException ex) {
113
                    /*
114
                     * This should not happen because
115
                     * envelopes support clone 
116
                     */
117
                            logger.debug("Error while adding envelope: " + ex.getMessage(), ex);
118
                        }
119
                    } else {
120
                        
121
                        /*
122
                         * We'll use this reprojection if not null
123
                         * to convert feature envelopes
124
                         */
125
                        ICoordTrans ct = lyr_vect.getCoordTrans();
126
                        
127
                        DisposableIterator iter = fsel.fastIterator();
128
                        Feature feat = null;
129
                        while (iter.hasNext()) {
130
                            feat = (Feature) iter.next();
131
                            try {
132
                            rectangle = accum(
133
                                rectangle, feat.getDefaultEnvelope(), ct);
134
                            } catch (CloneNotSupportedException ex) {
135
                            /*
136
                             * This should not happen because
137
                             * envelopes support clone 
138
                             */
139
                            logger.debug("Error while adding envelope: " + ex.getMessage(), ex);
140
                            break;
141
                            }
142
                        }
143
                        iter.dispose();
144
                    }
145
                }
146
        }
147

    
148
        /**
149
         * Adds add_env envelope to ini_env after reprojecting add_env
150
         * if cotr not null
151
         * 
152
         * @param ini_env
153
         * @param add_env
154
         * @param cotr
155
         * @return
156
         */
157
    private Envelope accum(
158
        Envelope ini_env,
159
        Envelope add_env,
160
        ICoordTrans cotr) throws CloneNotSupportedException {
161
        
162
        if (add_env == null) {
163
            /*
164
             * Nothing to add
165
             */
166
            return ini_env;
167
        } else {
168
            
169
            if (cotr == null) {
170
                
171
                /*
172
                 * No reprojection needed
173
                 */
174
                if (ini_env == null) {
175
                    return add_env;
176
                } else {
177
                    Envelope resp = (Envelope) ini_env.clone(); 
178
                    resp.add(add_env);
179
                    return resp;
180
                }
181
                
182
            } else {
183
                
184
                /*
185
                 * Reproject before adding
186
                 */
187
                Envelope add_rep = (Envelope) add_env.convert(cotr).clone();
188
                if (ini_env == null) {
189
                    /*
190
                     * Initial was null
191
                     */
192
                    return add_rep;
193
                } else {
194
                    Envelope resp = (Envelope) ini_env.clone(); 
195
                    resp.add(add_rep);
196
                    return resp;
197
                }
198
            }
199
        }
200
    }
201

    
202

    
203
}