Statistics
| Revision:

svn-gvsig-desktop / branches / v05 / extensions / extCatalogYNomenclator / src / es / gva / cit / gvsig / gazetteer / loaders / FeatureLoader.java @ 4488

History | View | Annotate | Download (5.96 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 es.gva.cit.gvsig.gazetteer.loaders;
42

    
43

    
44
import java.awt.Color;
45
import java.awt.geom.Point2D;
46
import java.awt.geom.Rectangle2D;
47

    
48
import org.cresques.cts.IDatum;
49
import org.cresques.cts.IProjection;
50
import org.cresques.cts.ProjectionPool;
51
import org.cresques.cts.gt2.CoordSys;
52
import org.cresques.cts.gt2.CoordTrans;
53

    
54
import com.iver.andami.PluginServices;
55
import com.iver.cit.gvsig.fmap.core.FPoint2D;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
58
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
59
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
60
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
61
import com.iver.cit.gvsig.fmap.rendering.FGraphicLabel;
62

    
63
import es.gva.cit.gazetteer.querys.Feature;
64
import es.gva.cit.gazetteer.querys.Query;
65
import es.gva.cit.gvsig.gazetteer.DeleteSearchesModule;
66

    
67

    
68
/**
69
 * This class is used to load a new feature like a layer in gvSIG
70
 * 
71
 * @author Jorge Piera Llodra (piera_jor@gva.es)
72
 */
73
public class FeatureLoader {     
74
        /**
75
         * Coordinates Transformer
76
         */
77
        private CoordTrans coordTrans;        
78
        
79
        /**
80
         * @param projection
81
         * Server projection
82
         */
83
        public FeatureLoader(String sProjection){
84
                com.iver.cit.gvsig.gui.View activeView = 
85
                        (com.iver.cit.gvsig.gui.View) PluginServices.getMDIManager().getActiveView();
86
                
87
                
88
                IProjection projection = ProjectionPool.get(sProjection);
89
                if (projection == null){
90
                        projection = activeView.getMapControl().getViewPort().getProjection();
91
                }
92
                
93
                coordTrans=  new CoordTrans((CoordSys) projection,
94
                                (CoordSys) activeView.getMapControl().getViewPort().getProjection());
95
        }
96
        /**
97
         * It makes a zoom in gvSIG
98
         * @param
99
         * feature
100
         * @param
101
         * query
102
         * Query that contains advanced options to search and
103
         * to show the results
104
         * @return
105
         * true or false if fail
106
         */
107
        public boolean load(Feature feature,Query query) {
108
                addAndDrawLabel(feature,
109
                                query.getOptions().getAspect().isKeepOld(),
110
                                query.getOptions().getAspect().isPaintCurrent());
111

    
112
                if (query.getOptions().getAspect().isGoTo()){
113
                        focusCenter(feature);
114
                }        
115

    
116
                return true;
117
        }    
118

    
119
        /**
120
         * This method focus the toponim in the center of the view 
121
         * @param feature
122
         * Feature that contains the coordinates
123
         */
124
        private void focusCenter(Feature feature){
125
                com.iver.cit.gvsig.gui.View activeView = 
126
                        (com.iver.cit.gvsig.gui.View) PluginServices.getMDIManager().getActiveView();
127

    
128
                IDatum datum = activeView.getProjection().getDatum();
129
                
130
                Point2D point = getReprojectedPoint(feature.getCoordinates());
131
                Rectangle2D extent;
132
                                
133
                if (datum.toString().indexOf("European_Datum_1950") > 0){
134
                        extent = new Rectangle2D.Double(point.getX() - 5000,
135
                                        point.getY() - 5000,
136
                                        10000,
137
                                        10000);
138
                }else{//WGS_1984
139
                        extent = new Rectangle2D.Double(point.getX() - 0.05,
140
                                        point.getY() - 0.05,
141
                                        0.1,
142
                                        0.1);
143
                }                  
144
                
145
                activeView.getMapControl().getViewPort().setExtent(extent);
146
        } 
147

    
148
        /**
149
         * It adds a new Label to the current view
150
         * @param feature
151
         * To obtain the coordinates and the toponim name
152
         * @param isRemoveOldClicked
153
         * To remove or keep the old searches
154
         */
155
        private void addAndDrawLabel(Feature feature,boolean isRemoveOldClicked,boolean isMarkedPlaceClicked){
156
                com.iver.cit.gvsig.gui.View activeView = 
157
                        (com.iver.cit.gvsig.gui.View) PluginServices.getMDIManager().getActiveView();
158

    
159
                GraphicLayer lyr = activeView.getMapControl().getMapContext().getGraphicsLayer();
160
                
161
                if (isRemoveOldClicked){
162
                        lyr.clearAllGraphics();
163
                }        
164
                
165
                if (isMarkedPlaceClicked){
166
                        int idSymbol = lyr.addSymbol(getSymbol());                        
167
                                        
168
                        IGeometry geom = ShapeFactory.createPoint2D(new FPoint2D(getReprojectedPoint(feature.getCoordinates())));
169

    
170
                        FGraphicLabel theLabel = new FGraphicLabel(geom, idSymbol, feature.getName());
171
                        lyr.addGraphic(theLabel);
172

    
173
                        DeleteSearchesModule.setVisible();
174
                        PluginServices.getMainFrame().enableControls();
175
                }                        
176
                
177
                activeView.getMapControl().drawGraphics();
178

    
179
                //This line could look stupid, but is necessary because grawGraphics does't
180
                //remove the old Graphics searched
181
                activeView.getMapControl().getViewPort().setExtent(activeView.getMapControl().getViewPort().getExtent());
182
        }
183
        
184
        /**
185
         * Creates a FSymbol
186
         * @return
187
         * FSymbol
188
         */
189
        private FSymbol getSymbol(){
190
                FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_TEXT); 
191
                theSymbol.setColor(Color.RED);
192
                theSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_CIRCLE);
193
                theSymbol.setFontColor(Color.BLACK);
194
                theSymbol.setSizeInPixels(true);
195
                theSymbol.setSize(10);
196
                return theSymbol;
197
        }
198
        
199
        /**
200
         * Reprojects the new point
201
         * @param ptOrig
202
         * Origin point
203
         * @return
204
         * FPoint2D
205
         */
206
        private Point2D getReprojectedPoint(Point2D ptOrigin){
207
                Point2D ptDest = null;
208
                return getCoordTrans().convert(ptOrigin, ptDest);
209
        }
210
        /**
211
         * @return the coordTrans
212
         */
213
        public CoordTrans getCoordTrans() {
214
                return coordTrans;
215
        }
216
        
217
        
218

    
219
}