Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeocoding / src / org / gvsig / geocoding / extension / GeocodingTask.java @ 31249

History | View | Annotate | Download (5.95 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.extension;
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.gui.TableResultsModel;
42
import org.gvsig.geocoding.impl.DataGeocoderImpl;
43
import org.gvsig.geocoding.pattern.GeocodingPattern;
44
import org.gvsig.geocoding.result.GeocodingResult;
45
import org.gvsig.geocoding.utils.GeocodingUtils;
46
import org.gvsig.utils.swing.threads.AbstractMonitorableTask;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

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

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

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

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

    
83
        /**
84
         * run process
85
         */
86
        public void run() throws Exception {
87

    
88
                String logtask = "";
89
                Address address = null;
90

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

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

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

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

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

    
138
                if (result.size() > 0) {
139
                        int max = control.getPattern().getSettings().getResultsNumber();
140

    
141
                        JTable jTableResults = control.getJTableResults();
142
                        TableResultsModel model = new TableResultsModel(control, address);
143
                        model.setResultSet(result, max, control.getPattern().getSettings()
144
                                        .getScore());
145

    
146
                        jTableResults.setModel(model);
147
                        jTableResults.validate();
148
                        jTableResults.repaint();
149

    
150
                        // Show results position in the gvSIG view
151
                        control.showResultsPositionsOnView(result);
152

    
153
                        // select first result
154
                        jTableResults.setRowSelectionInterval(0, 0);
155

    
156
                        // zoom position to first result in the view
157
                        Point pto = model.getGeometry(0, 2, 3);
158
                        control.zoomToPoint(pto.getX(), pto.getY());
159

    
160
                        if (results.size() > 1) {
161
                                control.getGPanel().activeTableGUIFeatures(true);
162
                                control.getGPanel().setLabRow(control.ROW + 1);
163
                                control.getGmodel().setNumResultShowed(0);
164
                        }
165
                }
166
        }
167

    
168
        /**
169
         * get simple address
170
         * 
171
         * @return
172
         */
173
        private Address getSimpleAddress() {
174
                return control.getGPanel().getAddressPanel().getSimpleAddress();
175
        }
176

    
177
        /**
178
         * get table address from row
179
         * 
180
         * @param row
181
         * @return
182
         */
183
        private Address getTableAddress(int row) {
184
                return control.getGPanel().getAddressPanel().getTableAddress(row);
185
        }
186

    
187
        /**
188
         * geocoding process
189
         * 
190
         * @param pat
191
         * @param address
192
         * @return set with sorted results
193
         */
194
        private Set<GeocodingResult> geocoding(GeocodingPattern pat, Address address) {
195

    
196
                Set<GeocodingResult> results = new TreeSet<GeocodingResult>();
197
                try {
198
                        ((DataGeocoderImpl) geoco).setPattern(pat);
199
                        ((DataGeocoderImpl) geoco).setStore(GeocodingUtils
200
                                        .searchLayerInCurrentView(pat));
201
                        results = geoco.geocode(address);
202
                } catch (Exception e) {
203
                        log.error("ERROR in geocoding process", e);
204
                }
205
                return results;
206
        }
207

    
208
}