Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.geocoding.extension / src / org / gvsig / geocoding / tasks / GeocodingTask.java @ 32526

History | View | Annotate | Download (6.1 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
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
 * as published by the Free Software Foundation; either version 2
9
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 Prodevelop S.L. main development
26
 */
27

    
28
package org.gvsig.geocoding.tasks;
29

    
30
import java.util.List;
31
import java.util.Set;
32
import java.util.TreeSet;
33

    
34
import javax.swing.JTable;
35

    
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.fmap.geom.primitive.Point;
38
import org.gvsig.geocoding.Geocoder;
39
import org.gvsig.geocoding.GeocodingLocator;
40
import org.gvsig.geocoding.address.Address;
41
import org.gvsig.geocoding.extension.GeocodingController;
42
import org.gvsig.geocoding.gui.TableResultsModel;
43
import org.gvsig.geocoding.impl.DataGeocoderImpl;
44
import org.gvsig.geocoding.impl.LuceneGeocoderImpl;
45
import org.gvsig.geocoding.pattern.GeocodingPattern;
46
import org.gvsig.geocoding.result.GeocodingResult;
47
import org.gvsig.geocoding.utils.GeocodingUtils;
48
import org.gvsig.utils.swing.threads.AbstractMonitorableTask;
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

    
52
/**
53
 * Geocoding task in background
54
 * 
55
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
56
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
57
 */
58
public class GeocodingTask extends AbstractMonitorableTask {
59

    
60
        private static final Logger log = LoggerFactory
61
                        .getLogger(GeocodingTask.class);
62
        private GeocodingController control;
63
        private long finalStep;
64
        private Geocoder geoco = null;
65

    
66
        /**
67
         * Constructor task
68
         * 
69
         * @param control
70
         */
71
        public GeocodingTask(GeocodingController control) {
72

    
73
                this.control = control;
74
                // configure task
75
                setInitialStep(0);
76
                setStatusMessage(PluginServices.getText(this, "geocodingrun"));
77
                setDeterminatedProcess(true);
78
                finalStep = control.getGeocodingProcessCount();
79
                setFinalStep((int) finalStep);
80
                //
81
                GeocodingLocator loc = GeocodingLocator.getInstance();
82
                geoco = loc.getGeocoder();
83
        }
84

    
85
        /**
86
         * run process
87
         */
88
        public void run() throws Exception {
89

    
90
                String logtask = "";
91
                Address address = null;
92

    
93
                // Remove old results of the model list
94
                control.getGmodel().clearResults();
95

    
96
                // SIMPLE GEOCODING
97
                if (finalStep == 1) {
98
                        logtask = PluginServices.getText(this, "addressgeocoded");
99

    
100
                        if (!isCanceled()) {
101
                                setCurrentStep(1);
102
                                address = getSimpleAddress();
103
                                Set<GeocodingResult> result = geocoding(control.getPattern(),
104
                                                address);
105
                                control.getGmodel().addResult(result);
106
                                setNote(1 + " " + logtask);
107
                        } else {
108
                                return;
109
                        }
110
                }
111

    
112
                // TABLE GEOCODING (MASSIVE)
113
                else {
114
                        logtask = PluginServices.getText(this, "addressesgeocoded");
115
                        for (int i = 1; i < finalStep + 1; i++) {
116
                                if (!isCanceled()) {
117
                                        setCurrentStep(i);
118
                                        address = getTableAddress(i - 1);
119
                                        Set<GeocodingResult> result = geocoding(control
120
                                                        .getPattern(), address);
121
                                        control.getGmodel().addResult(result);
122
                                        setNote(i + " " + logtask);
123
                                } else {
124
                                        return;
125
                                }
126
                        }
127
                }
128

    
129
                List<Set<GeocodingResult>> results = control.getGmodel()
130
                                .getAllResults();
131
                // get first result to show in the table
132
                Set<GeocodingResult> result = results.get(0);
133
                // save in the model number of result showed
134
                control.getGmodel().setNumResultShowed(0);
135
                // build array with results selected and save in the model
136
                Integer[] expElems = control
137
                                .createInitialResultsExportElements(results);
138
                control.getGmodel().setExportElements(expElems);
139

    
140
                if (result.size() > 0) {
141
                        int max = control.getPattern().getSettings().getMaxResultsNumber();
142

    
143
                        JTable jTableResults = control.getJTableResults();
144
                        TableResultsModel model = new TableResultsModel(control, address);
145
                        model.setResultSet(result, max, control.getPattern().getSettings()
146
                                        .getMinScore());
147

    
148
                        jTableResults.setModel(model);
149
                        jTableResults.validate();
150
                        jTableResults.repaint();
151

    
152
                        // Show results position in the gvSIG view
153
                        //FIXME
154
                        control.showResultsPositionsOnView(result);
155

    
156
                        
157
                        if(model.getRowCount() > 0){
158
                                // select first result
159
                                jTableResults.setRowSelectionInterval(0, 0);
160
                                
161
                                // zoom position to first result in the view
162
                                Point pto = model.getGeometry(0, 2, 3);
163
                                control.zoomToPoint(pto.getX(), pto.getY());
164

    
165
                                if (results.size() > 1) {
166
                                        control.getGPanel().activeTableGUIFeatures(true);
167
                                        control.getGPanel().setLabRow(control.ROW + 1);
168
                                        control.getGmodel().setNumResultShowed(0);
169
                                }
170
                        }
171

    
172
                
173
                }
174
        }
175

    
176
        /**
177
         * get simple address
178
         * 
179
         * @return
180
         */
181
        private Address getSimpleAddress() {
182
                return control.getGPanel().getAddressPanel().getSimpleAddress();
183
        }
184

    
185
        /**
186
         * get table address from row
187
         * 
188
         * @param row
189
         * @return
190
         */
191
        private Address getTableAddress(int row) {
192
                return control.getGPanel().getAddressPanel().getTableAddress(row);
193
        }
194

    
195
        /**
196
         * geocoding process
197
         * 
198
         * @param pat
199
         * @param address
200
         * @return set with sorted results
201
         */
202
        private Set<GeocodingResult> geocoding(GeocodingPattern pat, Address address) {
203

    
204
                Set<GeocodingResult> results = new TreeSet<GeocodingResult>();
205
                try {
206
                        geoco.setPattern(pat);
207
                        geoco.setStore(GeocodingUtils
208
                                        .searchLayerInCurrentView(pat));
209
                        results = geoco.geocode(address);
210
                } catch (Exception e) {
211
                        log.error("ERROR in geocoding process", e);
212
                }
213
                return results;
214
        }
215

    
216
}