Statistics
| Revision:

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

History | View | Annotate | Download (5.81 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

    
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.Patterngeocoding;
44
import org.gvsig.geocoding.result.GeocodingResult;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
import com.iver.andami.PluginServices;
49
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
50

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
207
}