Statistics
| Revision:

gvsig-gazetteer / org.gvsig.gazetteer / trunk / org.gvsig.gazetteer / org.gvsig.gazetteer.extension / src / main / java / org / gvsig / gazetteer / loaders / FeatureLoader.java @ 103

History | View | Annotate | Download (8.89 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.gazetteer.loaders;
42

    
43

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

    
49
import org.cresques.cts.ICoordTrans;
50
import org.cresques.cts.IProjection;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53
import org.gvsig.andami.PluginServices;
54
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
55
import org.gvsig.fmap.crs.CRSFactory;
56
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
57
import org.gvsig.fmap.geom.GeometryLocator;
58
import org.gvsig.fmap.geom.GeometryManager;
59
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
60
import org.gvsig.fmap.geom.exception.CreateGeometryException;
61
import org.gvsig.fmap.geom.primitive.Envelope;
62
import org.gvsig.fmap.geom.primitive.Point;
63
import org.gvsig.fmap.mapcontext.MapContext;
64
import org.gvsig.fmap.mapcontext.MapContextLocator;
65
import org.gvsig.fmap.mapcontext.MapContextManager;
66
import org.gvsig.fmap.mapcontext.ViewPort;
67
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
68
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
69
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
70
import org.gvsig.gazetteer.DeleteSearchesExtension;
71
import org.gvsig.gazetteer.querys.Feature;
72
import org.gvsig.gazetteer.querys.GazetteerQuery;
73
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
74

    
75

    
76

    
77

    
78
/**
79
 * This class is used to load a new feature like a layer in gvSIG
80
 *
81
 * @author Jorge Piera Llodra (piera_jor@gva.es)
82
 */
83
public class FeatureLoader {
84
        private static final Logger LOG =
85
        LoggerFactory.getLogger(FeatureLoader.class);
86

    
87
    private static GeometryManager geometryManager = GeometryLocator.getGeometryManager();
88
        private static MapContextManager mapContextManager = MapContextLocator.getMapContextManager();
89

    
90
        /**
91
         * Coordinates Transformer
92
         */
93
        private ICoordTrans coordTrans;
94

    
95
        /**
96
         * @param projection
97
         * Server projection
98
         */
99
        public FeatureLoader(String sProjection){
100
                AbstractViewPanel activeView =
101
                        (AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
102

    
103

    
104
                IProjection projection = CRSFactory.getCRS(sProjection);
105
                if (projection == null){
106
                        projection = activeView.getMapControl().getViewPort().getProjection();
107
                }
108

    
109
                coordTrans=  projection.getCT(activeView.getMapControl().getViewPort().getProjection());
110
        }
111
        /**
112
         * It makes a zoom in gvSIG
113
         * @param
114
         * feature
115
         * @param
116
         * query
117
         * Query that contains advanced options to search and
118
         * to show the results
119
         * @return
120
         * true or false if fail
121
         */
122
        public boolean load(Feature feature,GazetteerQuery query) {
123
                addAndDrawLabel(feature,
124
                                query.getOptions().getAspect().isKeepOld(),
125
                                query.getOptions().getAspect().isPaintCurrent());
126

    
127
                if (query.getOptions().getAspect().isGoTo()){
128
                        focusCenter(feature);
129
                }
130

    
131
                return true;
132
        }
133

    
134
        /**
135
         * This method focus the toponim in the center of the view
136
         * @param feature
137
         * Feature that contains the coordinates
138
         */
139
        private void focusCenter(Feature feature){
140
                AbstractViewPanel activeView =
141
                        (AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
142

    
143
                IProjection projection = activeView.getProjection();
144
                ViewPort viewPort = activeView.getMapControl().getViewPort();
145
                MapContext mapContext = activeView.getMapControl().getMapContext();
146
                Point point = getReprojectedPoint((feature.getCoordinates()));
147
//        Point point = feature.getCoordinates();
148

    
149
                if (viewPort.getAdjustedEnvelope() != null){
150
                        Toolkit kit = Toolkit.getDefaultToolkit();
151
                        double dpi = kit.getScreenResolution();
152

    
153
                        Envelope envelope = viewPort.getAdjustedEnvelope();
154
                        Rectangle2D rectangle = new Rectangle2D.Double(envelope.getLowerCorner().getX(),
155
                                        envelope.getLowerCorner().getY(),
156
                                        envelope.getUpperCorner().getX() - envelope.getLowerCorner().getX(),
157
                                        envelope.getUpperCorner().getY() - envelope.getLowerCorner().getY());
158

    
159
                        Rectangle2D extent = projection.getExtent(rectangle,
160
                                new Double(25000).doubleValue(),
161
                                        new Double(viewPort.getImageWidth()).doubleValue(),
162
                                        new Double(viewPort.getImageHeight()).doubleValue(),
163
                                        viewPort.getMapUnits(),
164
                                        viewPort.getDistanceUnits(),
165
                                        dpi);
166
                        if (extent != null){
167
                                try {
168
                                        envelope = geometryManager.createEnvelope(SUBTYPES.GEOM2D);
169
                                        Point loweCorner = geometryManager.createPoint(point.getX() - extent.getWidth()/2,
170
                                                        point.getY() - extent.getHeight()/2,
171
                                                        SUBTYPES.GEOM2D);
172
                                        Point upperCorner = geometryManager.createPoint(point.getX() + extent.getWidth()/2,
173
                                                        point.getY() + extent.getHeight()/2,
174
                                                        SUBTYPES.GEOM2D);
175
                                        envelope.setLowerCorner(loweCorner);
176
                                        envelope.setUpperCorner(upperCorner);
177
                                        mapContext.zoomToEnvelope(envelope);
178
                                } catch (CreateEnvelopeException e) {
179
                                    LOG.error("Error creating the envelope", e);
180
                                } catch (CreateGeometryException e) {
181
                                    LOG.error("Error creating the envelope", e);
182
                                }
183
                        }
184
                }
185
        }
186

    
187
        /**
188
         * It adds a new Label to the current view
189
         * @param feature
190
         * To obtain the coordinates and the toponim name
191
         * @param isRemoveOldClicked
192
         * To remove or keep the old searches
193
         */
194
        private void addAndDrawLabel(Feature feature,boolean isRemoveOldClicked,boolean isMarkedPlaceClicked){
195
                AbstractViewPanel activeView =
196
                        (AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
197
                MapContext mc = activeView.getMapControl().getMapContext();
198
                GraphicLayer lyr = mc.getGraphicsLayer();
199

    
200
                if (isRemoveOldClicked){
201
                        lyr.clearAllGraphics();
202
                }
203

    
204
        if (isMarkedPlaceClicked) {
205
            int pointSymbol = lyr.addSymbol(getSymbol());
206
            int textSymbol = lyr.addSymbol(getTextSymbol(feature.getName()));
207

    
208
            Point point = feature.getCoordinates();
209
            lyr.setCoordTrans(getCoordTrans());
210
            lyr.addGraphic("gazetteer", point, pointSymbol, null);
211
            lyr.addGraphic("gazetteer", point, textSymbol, feature.getName());
212
            DeleteSearchesExtension.setVisible();
213
            PluginServices.getMainFrame().enableControls();
214
        }
215

    
216
                mc.invalidate();
217
        }
218

    
219
        /**
220
         * Creates a FSymbol
221
         * @return
222
         * FSymbol
223
         */
224
        private ISymbol getTextSymbol(String text){
225
                ITextSymbol theSymbol =
226
                                (ITextSymbol) mapContextManager.getSymbolManager()
227
                                                .createSymbol(ITextSymbol.SYMBOL_NAME);
228
                theSymbol.setColor(Color.RED);
229
                theSymbol.setTextColor(Color.BLACK);
230
                theSymbol.setAutoresizeEnabled(false);
231
                theSymbol.setFontSize(16);
232
                theSymbol.setText(text);
233
                return theSymbol;
234
        }
235

    
236
        /**
237
         * Creates a FSymbol
238
         * @return
239
         * FSymbol
240
         */
241
        private ISymbol getSymbol(){
242
                IMarkerSymbol theSymbol =
243
                                (IMarkerSymbol) mapContextManager.getSymbolManager()
244
                                                .createSymbol(IMarkerSymbol.CIRCLE_STYLE);
245
                theSymbol.setColor(Color.RED);
246
                theSymbol.setSize(4);
247
                return theSymbol;
248
        }
249

    
250
//        /**
251
//         * Reprojects the new point
252
//         * @param ptOrig
253
//         * Origin point
254
//         * @return
255
//         * FPoint2D
256
//         */
257
//        private Point2D getReprojectedPoint(Point2D ptOrigin){
258
//                Point2D ptDest = null;
259
//                ICoordTrans ct = getCoordTrans();
260
//                if(ct != null) {
261
//                    return ct.convert(ptOrigin, ptDest);
262
//                }
263
//                return ptOrigin;
264
//        }
265

    
266
           /**
267
     * Reprojects the new point
268
     * @param ptOrig
269
     * Origin point
270
     * @return
271
     * Point
272
     */
273
    private Point getReprojectedPoint(Point ptOrigin){
274
        Point ptDest = ((Point)ptOrigin.cloneGeometry());
275
        ICoordTrans ct = getCoordTrans();
276
        if(ct != null) {
277
            ptDest.reProject(ct);
278
        }
279
        return ptDest;
280
    }
281

    
282
        /**
283
         * @return the coordTrans
284
         */
285
        public ICoordTrans getCoordTrans() {
286
                return coordTrans;
287
        }
288
}