root / branches / v2_0_0_prep / extensions / extGeocoding / src / org / gvsig / geocoding / extension / GeocoController.java @ 27564
History | View | Annotate | Download (15.3 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 developer
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.geocoding.extension; |
29 |
|
30 |
import java.io.BufferedReader; |
31 |
import java.io.File; |
32 |
import java.io.FileReader; |
33 |
import java.io.IOException; |
34 |
import java.util.ArrayList; |
35 |
import java.util.List; |
36 |
import java.util.Locale; |
37 |
import java.util.Set; |
38 |
|
39 |
import javax.swing.DefaultComboBoxModel; |
40 |
import javax.swing.JFileChooser; |
41 |
import javax.swing.JOptionPane; |
42 |
import javax.swing.JTable; |
43 |
|
44 |
import org.gvsig.fmap.dal.DALLocator; |
45 |
import org.gvsig.fmap.dal.DataManager; |
46 |
import org.gvsig.fmap.dal.DataParameters; |
47 |
import org.gvsig.fmap.dal.DataServerExplorer; |
48 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
49 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
50 |
import org.gvsig.fmap.dal.exception.DataException; |
51 |
import org.gvsig.fmap.dal.exception.InitializeException; |
52 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
53 |
import org.gvsig.fmap.dal.exception.ReadException; |
54 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
55 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
56 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
57 |
import org.gvsig.geocoding.Literal; |
58 |
import org.gvsig.geocoding.gui.GeocodingModel; |
59 |
import org.gvsig.geocoding.gui.GeocodingPanel; |
60 |
import org.gvsig.geocoding.gui.IGeocodingModel; |
61 |
import org.gvsig.geocoding.gui.address.AbstractAddressPanel; |
62 |
import org.gvsig.geocoding.gui.address.AddressComposedPanel; |
63 |
import org.gvsig.geocoding.gui.address.AddressRangePanel; |
64 |
import org.gvsig.geocoding.gui.address.AddressSimpleCentroidPanel; |
65 |
import org.gvsig.geocoding.gui.newpattern.NewGeocoPatternPanel; |
66 |
import org.gvsig.geocoding.pattern.Patterngeocoding; |
67 |
import org.gvsig.geocoding.result.GeocodingResult; |
68 |
import org.gvsig.geocoding.styles.AbstractGeocodingStyle; |
69 |
import org.gvsig.geocoding.styles.AbstractRange; |
70 |
import org.gvsig.geocoding.styles.Composed; |
71 |
import org.gvsig.geocoding.styles.SimpleCentroid; |
72 |
import org.gvsig.geocoding.utils.GeocoUtils; |
73 |
import org.gvsig.geocoding.utils.PatternLoader; |
74 |
import org.gvsig.project.document.table.FeatureTableDocument; |
75 |
import org.slf4j.Logger; |
76 |
import org.slf4j.LoggerFactory; |
77 |
|
78 |
import com.iver.andami.PluginServices; |
79 |
import com.iver.utiles.GenericFileFilter; |
80 |
import com.iver.utiles.XMLEntity; |
81 |
|
82 |
/**
|
83 |
* Controller of the extension and all yours panels
|
84 |
*
|
85 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
86 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
87 |
*/
|
88 |
public class GeocoController { |
89 |
|
90 |
private static final Logger log = LoggerFactory |
91 |
.getLogger(GeocoController.class); |
92 |
|
93 |
private PluginServices ps = null; |
94 |
|
95 |
public static final String GEOCODINGELEMENTS = "GeocodingElements"; |
96 |
|
97 |
public static final String ROW = PluginServices.getText(null, "row") |
98 |
+ " : ";
|
99 |
|
100 |
private IGeocodingModel gmodel = null; |
101 |
private GeocodingPanel gpanel = null; |
102 |
private DataManager manager = null; |
103 |
|
104 |
/**
|
105 |
* Controller Constructor
|
106 |
*
|
107 |
*/
|
108 |
public GeocoController() {
|
109 |
|
110 |
manager = DALLocator.getDataManager(); |
111 |
|
112 |
// Load the model
|
113 |
this.gmodel = new GeocodingModel(); |
114 |
|
115 |
// Load the view and put it visible
|
116 |
this.gpanel = new GeocodingPanel(this); |
117 |
PluginServices.getMDIManager().addWindow(gpanel); |
118 |
this.gpanel.setVisible(true); |
119 |
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Run the geocoding process
|
124 |
*/
|
125 |
public void geocoding() { |
126 |
PluginServices.cancelableBackgroundExecution(new GeocodingTask(this)); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* This method loads a Pattern from XML file
|
131 |
*
|
132 |
* @return true if the load is succesfully
|
133 |
*/
|
134 |
public boolean loadPatternXML() { |
135 |
boolean apto = true; |
136 |
File thefile = null; |
137 |
JFileChooser jfc = null; |
138 |
/* Show the FileChooser to select a pattern file */
|
139 |
try {
|
140 |
jfc = new JFileChooser(); |
141 |
jfc.setDialogTitle(PluginServices.getText(this,
|
142 |
"load_geoco_pattern"));
|
143 |
String[] extensions = { "xml" }; |
144 |
jfc.setCurrentDirectory(new File("")); |
145 |
jfc.addChoosableFileFilter(new GenericFileFilter(extensions,
|
146 |
PluginServices.getText(this, "pattern_geoco_file"))); |
147 |
int returnval = jfc
|
148 |
.showOpenDialog((java.awt.Component) PluginServices |
149 |
.getMainFrame()); |
150 |
if (returnval == JFileChooser.APPROVE_OPTION) { |
151 |
thefile = jfc.getSelectedFile(); |
152 |
log.debug("file opened: " + thefile);
|
153 |
} else {
|
154 |
return false; |
155 |
} |
156 |
} catch (Exception e) { |
157 |
String mes = PluginServices.getText(this, "geocoerrorloadingfile"); |
158 |
String tit = PluginServices.getText(this, "geocoding"); |
159 |
JOptionPane.showMessageDialog(null, mes, tit, |
160 |
JOptionPane.ERROR_MESSAGE);
|
161 |
log.error("Error loading pattern file", e);
|
162 |
} |
163 |
/* Parse the xml file */
|
164 |
PatternLoader load = new PatternLoader(thefile);
|
165 |
load.run(); |
166 |
if (apto) {
|
167 |
apto = load.isApto(); |
168 |
this.gmodel.setPatternFile(load.getFile());
|
169 |
this.gmodel.setPattern(load.getPattern());
|
170 |
} |
171 |
return apto;
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* This method returns the selected layer by id
|
176 |
*
|
177 |
* @param capa
|
178 |
*/
|
179 |
public void getSelectedLayer(int poscapa) { |
180 |
|
181 |
List<FLyrVect> vectCapas = GeocoUtils.getListgvSIGVectLayers();
|
182 |
if (vectCapas.size() > 0) { |
183 |
this.getGmodel().setLyr(vectCapas.get(poscapa));
|
184 |
} |
185 |
} |
186 |
|
187 |
/**
|
188 |
* This method gets the list with the address components that the user has
|
189 |
* defined in the Geocoding Preferences
|
190 |
*
|
191 |
* @return
|
192 |
*/
|
193 |
public List<String> getListAddressComponents() { |
194 |
|
195 |
List<String> components = new ArrayList<String>(); |
196 |
PluginServices ps = PluginServices.getPluginServices(this);
|
197 |
XMLEntity xml = ps.getPersistentXML(); |
198 |
|
199 |
String tag = GEOCODINGELEMENTS + "_" + getLanguage(); |
200 |
if (xml.contains(tag)) {
|
201 |
|
202 |
String nam = String.valueOf(xml.getStringProperty(tag)); |
203 |
File persistenceFile = new File(nam); |
204 |
String str = ""; |
205 |
|
206 |
try {
|
207 |
BufferedReader br = new BufferedReader(new FileReader( |
208 |
persistenceFile)); |
209 |
while ((str = br.readLine()) != null) { |
210 |
components.add(str); |
211 |
} |
212 |
} catch (IOException e) { |
213 |
log.error("Reading the geocoding elements file", e);
|
214 |
} |
215 |
} |
216 |
return components;
|
217 |
} |
218 |
|
219 |
/**
|
220 |
* This method gets the application language
|
221 |
*
|
222 |
* @return
|
223 |
*/
|
224 |
private String getLanguage() { |
225 |
|
226 |
ArrayList<Locale> myLocs = org.gvsig.i18n.Messages |
227 |
.getPreferredLocales(); |
228 |
Locale myLoc = myLocs.size() == 0 ? Locale.ENGLISH : myLocs.get(0); |
229 |
|
230 |
/*
|
231 |
* TODO review and test with differente language codes, included Spanish
|
232 |
* languages
|
233 |
*/
|
234 |
String lang = myLoc.getLanguage().toLowerCase();
|
235 |
|
236 |
return lang;
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* This method saves the Geocoding Pattern
|
241 |
*
|
242 |
* @return
|
243 |
*/
|
244 |
public boolean savePattern() { |
245 |
|
246 |
boolean savedOK = false; |
247 |
|
248 |
JFileChooser jfc = new JFileChooser(); |
249 |
jfc.setDialogTitle(PluginServices.getText(this, "save_geoco_pattern")); |
250 |
String[] extensions = { "xml" }; |
251 |
jfc.addChoosableFileFilter(new GenericFileFilter(extensions,
|
252 |
PluginServices.getText(this, "pattern_geoco_file"))); |
253 |
int returnval = jfc.showSaveDialog((java.awt.Component) PluginServices
|
254 |
.getMainFrame()); |
255 |
|
256 |
if (returnval == JFileChooser.APPROVE_OPTION) { |
257 |
|
258 |
File file = jfc.getSelectedFile();
|
259 |
log.debug("file created: " + file);
|
260 |
if (file != null) { |
261 |
file = GeocoUtils.addXMLSuffix(file); |
262 |
Patterngeocoding pat = this.getGmodel().getPattern();
|
263 |
pat.setPatternName(file.getName()); |
264 |
try {
|
265 |
pat.saveToXML(file); |
266 |
savedOK = true;
|
267 |
log.debug("pattern saved: " + file);
|
268 |
this.gmodel.setPatternFile(file);
|
269 |
} catch (Exception e) { |
270 |
log.error("Serializing the pattern", e);
|
271 |
String mes = PluginServices.getText(this, |
272 |
"geocorrorsavingfile");
|
273 |
String tit = PluginServices.getText(this, "geocoding"); |
274 |
JOptionPane.showMessageDialog(null, mes, tit, |
275 |
JOptionPane.ERROR_MESSAGE);
|
276 |
} |
277 |
} |
278 |
} |
279 |
return savedOK;
|
280 |
} |
281 |
|
282 |
/**
|
283 |
* This method launches the New Pattern Panel
|
284 |
*
|
285 |
* @param pat
|
286 |
*/
|
287 |
public void launchNewPatternPanel(Patterngeocoding pat) { |
288 |
|
289 |
List<FLyrVect> lyrs = GeocoUtils.getListgvSIGVectLayers();
|
290 |
if (lyrs.size() > 0) { |
291 |
/* Launch the new pattern panel */
|
292 |
NewGeocoPatternPanel ppanel = new NewGeocoPatternPanel(this, pat); |
293 |
PluginServices.getMDIManager().addWindow(ppanel); |
294 |
ppanel.setVisible(true);
|
295 |
} else {
|
296 |
// show warning message
|
297 |
String title = PluginServices.getText(null, "geocoding"); |
298 |
|
299 |
String message = PluginServices.getText(null, "nolayers"); |
300 |
JOptionPane.showMessageDialog(null, message, title, |
301 |
JOptionPane.WARNING_MESSAGE);
|
302 |
} |
303 |
} |
304 |
|
305 |
/**
|
306 |
* This method updates the GeocodingPanel after save
|
307 |
*/
|
308 |
public void updateGeocoGUI() { |
309 |
|
310 |
gpanel.setJTextPatternPath(this.getGmodel().getPatternFile()
|
311 |
.getAbsolutePath()); |
312 |
gpanel.setParamMaxresults(this.getPattern().getSettings()
|
313 |
.getResultsNumber()); |
314 |
gpanel.setParamScore(this.getPattern().getSettings().getScore());
|
315 |
|
316 |
} |
317 |
|
318 |
/**
|
319 |
* Get the pattern from the model
|
320 |
*
|
321 |
* @return pattern
|
322 |
*/
|
323 |
public Patterngeocoding getPattern() {
|
324 |
return this.getGmodel().getPattern(); |
325 |
} |
326 |
|
327 |
/**
|
328 |
* get feature store of the layer
|
329 |
*
|
330 |
* @return
|
331 |
*/
|
332 |
public FeatureStore getLayerStore() {
|
333 |
FeatureStore store = null;
|
334 |
try {
|
335 |
store = gmodel.getLyr().getFeatureStore(); |
336 |
} catch (ReadException e) {
|
337 |
log.error("Obteniendo el Recorset de la capa", e);
|
338 |
} |
339 |
return store;
|
340 |
} |
341 |
|
342 |
/**
|
343 |
* @return the gmodel
|
344 |
*/
|
345 |
public IGeocodingModel getGmodel() {
|
346 |
return gmodel;
|
347 |
} |
348 |
|
349 |
/**
|
350 |
* @param gmodel
|
351 |
* the gmodel to set
|
352 |
*/
|
353 |
public void setGmodel(IGeocodingModel gmodel) { |
354 |
this.gmodel = gmodel;
|
355 |
} |
356 |
|
357 |
/**
|
358 |
* @return the gpanel
|
359 |
*/
|
360 |
public GeocodingPanel getGpanel() {
|
361 |
return gpanel;
|
362 |
} |
363 |
|
364 |
/**
|
365 |
* @param gpanel
|
366 |
* the gpanel to set
|
367 |
*/
|
368 |
public void setGpanel(GeocodingPanel gpanel) { |
369 |
this.gpanel = gpanel;
|
370 |
} |
371 |
|
372 |
/**
|
373 |
* Get a list gvSIG Project tables
|
374 |
*
|
375 |
* @return
|
376 |
*/
|
377 |
public DefaultComboBoxModel getModelgvSIGTables() { |
378 |
|
379 |
DefaultComboBoxModel comboModel = null; |
380 |
|
381 |
List<FeatureTableDocument> tables = GeocoUtils.getListgvSIGTables();
|
382 |
|
383 |
if (tables.size() > 0) { |
384 |
comboModel = new DefaultComboBoxModel(); |
385 |
for (int i = 0; i < tables.size(); i++) { |
386 |
FeatureTableDocument table = tables.get(i); |
387 |
comboModel.addElement(table); |
388 |
} |
389 |
|
390 |
} |
391 |
return comboModel;
|
392 |
} |
393 |
|
394 |
/**
|
395 |
* get the address panel from pattern style
|
396 |
*
|
397 |
* @return
|
398 |
*/
|
399 |
public AbstractAddressPanel getAddressPanelFromPattern() {
|
400 |
Patterngeocoding pat = this.getPattern();
|
401 |
AbstractGeocodingStyle style = pat.getSource().getStyle(); |
402 |
Literal lit = style.getRelationsLiteral(); |
403 |
AbstractAddressPanel aPanel = null;
|
404 |
if (style instanceof SimpleCentroid) { |
405 |
aPanel = new AddressSimpleCentroidPanel(this, lit); |
406 |
} |
407 |
if (style instanceof AbstractRange) { |
408 |
aPanel = new AddressRangePanel(this, lit); |
409 |
} |
410 |
if (style instanceof Composed) { |
411 |
aPanel = new AddressComposedPanel(this, lit); |
412 |
} |
413 |
return aPanel;
|
414 |
} |
415 |
|
416 |
// /**
|
417 |
// * This method loads the initial pattern in the model
|
418 |
// *
|
419 |
// * @return
|
420 |
// * @throws DataException
|
421 |
// */
|
422 |
// public Patterngeocoding loadInitialPattern() throws DataException {
|
423 |
//
|
424 |
// Patterngeocoding pat = new Patterngeocoding();
|
425 |
// // name
|
426 |
// pat.setPatternName("default pattern");
|
427 |
// // source
|
428 |
// GeocodingSource source = new GeocodingSource();
|
429 |
//
|
430 |
// File shp = new File("./data/streets.shp");
|
431 |
// SHPStoreParameters params = (SHPStoreParameters) manager
|
432 |
// .createStoreParameters(SHPStoreProvider.NAME);
|
433 |
// params.setFile(shp);
|
434 |
// FeatureStore store = (FeatureStore) manager.createStore(params);
|
435 |
// source.setLayerSource(store);
|
436 |
// FeatureType fType = store.getDefaultFeatureType();
|
437 |
// AbstractGeocodingStyle style = new SimpleCentroid();
|
438 |
// Literal mainLiteral = new Literal();
|
439 |
//
|
440 |
//
|
441 |
// pat.setSource(source);
|
442 |
// // settings
|
443 |
// pat.setSettings(new Settings());
|
444 |
//
|
445 |
// return pat;
|
446 |
// }
|
447 |
|
448 |
/**
|
449 |
* get loops of geocoding process
|
450 |
*
|
451 |
* @return
|
452 |
*/
|
453 |
public long getGeocodingProcessCount() { |
454 |
if (this.getGmodel().isSimple()) { |
455 |
return 1; |
456 |
} else {
|
457 |
long n = 0; |
458 |
try {
|
459 |
n = this.getGmodel().getSelectedTableStore().getFeatureSet()
|
460 |
.getSize(); |
461 |
return n;
|
462 |
} catch (Exception e) { |
463 |
return 0; |
464 |
} |
465 |
} |
466 |
} |
467 |
|
468 |
/**
|
469 |
* get JTable Results
|
470 |
*
|
471 |
* @return
|
472 |
*/
|
473 |
public JTable getJTableResults() { |
474 |
return this.gpanel.getJTableResults(); |
475 |
} |
476 |
|
477 |
/**
|
478 |
* Create initial array with list of selected results elements to export
|
479 |
*
|
480 |
* @param results
|
481 |
* @return
|
482 |
*/
|
483 |
public Integer[] createInitialResultsExportElements( |
484 |
List<Set<GeocodingResult>> results) { |
485 |
int cant = results.size();
|
486 |
Integer[] expElems = new Integer[cant]; |
487 |
int i = 0; |
488 |
for (Set<GeocodingResult> res : results) { |
489 |
if (res.size() > 0) { |
490 |
expElems[i] = 0;
|
491 |
} else {
|
492 |
expElems[i] = -1;
|
493 |
} |
494 |
i++; |
495 |
} |
496 |
return expElems;
|
497 |
} |
498 |
|
499 |
/**
|
500 |
*
|
501 |
*/
|
502 |
public void createDBFTableResults() { |
503 |
|
504 |
File thefile = null; |
505 |
JFileChooser jfc = new JFileChooser(); |
506 |
jfc.setDialogTitle(PluginServices.getText(this, "tableresults")); |
507 |
String[] extensions = { "dbf" }; |
508 |
jfc.setCurrentDirectory(new File("")); |
509 |
jfc.addChoosableFileFilter(new GenericFileFilter(extensions,
|
510 |
PluginServices.getText(this, "dbf_file"))); |
511 |
int returnval = jfc.showSaveDialog((java.awt.Component) PluginServices
|
512 |
.getMainFrame()); |
513 |
if (returnval == JFileChooser.APPROVE_OPTION) { |
514 |
thefile = jfc.getSelectedFile(); |
515 |
log.debug("file opened: " + thefile);
|
516 |
} |
517 |
if (thefile != null) { |
518 |
thefile = GeocoUtils.addDBFSuffix(thefile); |
519 |
// build store
|
520 |
try {
|
521 |
File parentfile = thefile.getParentFile();
|
522 |
String name = thefile.getName();
|
523 |
|
524 |
DataServerExplorerParameters eparams = manager |
525 |
.createServerExplorerParameters("FilesystemExplorer");
|
526 |
eparams |
527 |
.setDynValue("initialpath", parentfile
|
528 |
.getAbsolutePath()); |
529 |
|
530 |
DataServerExplorer serverExplorer = manager |
531 |
.createServerExplorer(eparams); |
532 |
|
533 |
DataParameters sparams = serverExplorer.getAddParameters("dbf");
|
534 |
|
535 |
sparams.setDynValue("fileName", name);
|
536 |
EditableFeatureType featureType = (EditableFeatureType) sparams |
537 |
.getDynValue("defaultFeatureType");
|
538 |
|
539 |
serverExplorer.add((NewDataStoreParameters) sparams, true);
|
540 |
|
541 |
} catch (InitializeException e) {
|
542 |
// TODO Auto-generated catch block
|
543 |
e.printStackTrace(); |
544 |
} catch (ProviderNotRegisteredException e) {
|
545 |
// TODO Auto-generated catch block
|
546 |
e.printStackTrace(); |
547 |
} catch (DataException e) {
|
548 |
// TODO Auto-generated catch block
|
549 |
e.printStackTrace(); |
550 |
} |
551 |
// fill store
|
552 |
} |
553 |
|
554 |
} |
555 |
|
556 |
} |