Statistics
| Revision:

root / tmp / trunk / servidor / WPS_Callejero_Server / Callejero / src / es / logex / callejero / core / Callejero.java @ 26631

History | View | Annotate | Download (7.88 KB)

1 26631 kike
/**
2
 *
3
 */
4
package es.logex.callejero.core;
5
6
import java.awt.geom.Rectangle2D;
7
import java.sql.Connection;
8
import java.sql.ResultSet;
9
import java.sql.SQLException;
10
import java.sql.Statement;
11
import java.util.ArrayList;
12
import java.util.List;
13
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.postgis.PGbox2d;
17
18
import es.logex.callejero.modelo.BoundingBox;
19
20
/* logEx. Lógica Extrema s.l.*
21
* Copyright (C) 2007 Lógica Extrema and Generalitat Valenciana.
22
*
23
* This program is free software; you can redistribute it and/or
24
* modify it under the terms of the GNU General Public License
25
* as published by the Free Software Foundation; either version 2
26
* of the License, or (at your option) any later version.
27
*
28
* This program is distributed in the hope that it will be useful,
29
* but WITHOUT ANY WARRANTY; without even the implied warranty of
30
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31
* GNU General Public License for more details.
32
*
33
* You should have received a copy of the GNU General Public License
34
* along with this program; if not, write to the Free Software
35
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
36
*
37
* For more information, contact:
38
*
39
*  Generalitat Valenciana
40
*   Conselleria d'Infraestructures i Transport
41
*   Av. Blasco Ibañez, 50
42
*   46010 VALENCIA
43
*   SPAIN
44
*
45
*      +34 963862235
46
*   gvsig@gva.es
47
*      www.gvsig.gva.es
48
*
49
*    or
50
*
51
*   Lógica Extrema s.l.
52
*   Avda. Cortes Valencianas
53
*   46015 Valencia
54
*   Spain
55
*
56
*   logic@logex.es
57
*/
58
59
/**
60
 * @author kike
61
 *
62
 */
63
public   class Callejero implements ICallejero {
64
65
        Log log = LogFactory.getLog(this.getClass());
66
67
        private static List<String> municipios = new ArrayList<String>();
68
        public Connection conn = null;
69
70
        public Callejero(Connection conn) {
71
                this.conn = conn;
72
        }
73
74
        private ResultSet EjecutaSql(String query) {
75
                ResultSet r= null;
76
77
                try {
78
                        if(log.isDebugEnabled())
79
                        {
80
                                log.debug("Executing SQL:" + query);
81
                        }
82
                                Statement s = conn.createStatement();
83
                                r = s.executeQuery(query);
84
85
                }catch (Exception e){
86
                        log.error(e.getMessage());
87
                }
88
89
                        return r;
90
        }
91
92
        /***
93
         * Devuelve una lista de candidatos a partir de un municipio, calle, número
94
         *
95
         */
96
        public List<String> getCandidatos(String municipio, String calle, String numero) {
97
                String candMunicipio = null;
98
                if (municipio == null)
99
                        candMunicipio = "";
100
                else
101
                        candMunicipio = municipio;
102
103
                String candCalle = null;
104
                if (calle == null)
105
                        candCalle  = "";
106
                else
107
                        candCalle = calle;
108
109
                String candNumero = null;
110
                if (numero == null)
111
                        candNumero  = "";
112
                else
113
                        candNumero = numero;
114
115
                String query = null;
116
                ResultSet r = null;
117
                List<String> lista = new ArrayList<String>();
118
119
                log.debug("Decidir query en función de parámetros ");
120
121
                if ( !candMunicipio.equals("")) { //hay municipio
122
                        if (candCalle.equals("")) { //municipio sin calle
123
                                /*query = "select municipio  as texto_candidato,LogexLevenshtein('" + candMunicipio.toUpperCase() +
124
                                "',municipio) , extent from candidatos_extent where calle isnull order by LogexLevenshtein('" +
125
                                candMunicipio.toUpperCase() + "',municipio) ASC LIMIT 10";*/
126
                                query = "select municipio  as texto_candidato,Levenshtein('" + candMunicipio.toUpperCase() +
127
                                "',municipio) , extent from candidatos_extent where calle isnull order by Levenshtein('" +
128
                                candMunicipio.toUpperCase() + "',municipio) ASC LIMIT 10";
129
                        } else { //municipio con calle
130
                                query = "select municipio || ' - ' ||calle  as texto_candidato,LogexLevenshtein('" + candCalle.toUpperCase() +
131
                                "',calle), Levenshtein('" + candMunicipio.toUpperCase() +"',municipio), extent " +
132
                                " from candidatos_extent order by LogexLevenshtein('" +        candCalle.toUpperCase() + "',calle)  + " +
133
                                " Levenshtein('" +        candMunicipio.toUpperCase() + "',municipio) ASC LIMIT 10";
134
                        }
135
                }        else { //solo calle
136
                        if (!candCalle.equals("")) {
137
                                query = "select municipio || ' - ' ||calle as texto_candidato,LogexLevenshtein('" + candCalle.toUpperCase() +
138
                                "',calle) , extent from candidatos_extent order by LogexLevenshtein('" +
139
                                candCalle.toUpperCase() + "',calle) ASC LIMIT 10";
140
                        }
141
142
                }
143
144
                if (query == null)
145
                        return lista;
146
147
                log.debug("Ejecutar query "  + query);
148
                r = this.EjecutaSql(query);
149
                String text;
150
                BoundingBox extent = new BoundingBox();
151
152
                try {
153
                        while (r.next()) {
154
                                PGbox2d pgBox = new PGbox2d(r.getString("extent"));
155
156
                                double[] lowers = {pgBox.getLLB().x, pgBox.getLLB().y};
157
                                double[] uppers = {pgBox.getURT().x, pgBox.getURT().y};
158
                                extent.setLowersBoundingBox(lowers);
159
                                extent.setUppersBoundingBox(uppers);
160
161
                                lista.add(r.getString("texto_candidato") + "#$" + extent.toString());
162
                        }
163
                } catch (SQLException e) {
164
                        log.error(e.getMessage(),e);
165
166
                } finally{
167
                        if(r!=null)
168
                        {
169
                                try {
170
                                        r.close();
171
                                } catch (SQLException e) {
172
                                        log.error(e.getMessage(),e);
173
                                }
174
                        }
175
                }
176
                return lista;
177
178
        }
179
180
181
        /**
182
         * Devuelve una lista con los nombres de todos los municipios de la base de datos
183
         *
184
         * @return una lista de municipios
185
         */
186
          public List<String> getMunicipios() {
187
                  ResultSet r = null;
188
                  try{
189
                           if (this.municipios.isEmpty() ) {
190
                                   log.debug("Municipios vacío. Se llena por primera vez");
191
192
                                   r = this.EjecutaSql("select * from municipios");
193
                                   while (r.next()) {
194
                                                municipios.add(r.getString("ine_denomina"));
195
                                      }
196
197
                                    log.debug("Añadidos " + r.getRow() + "municipios a Callejero.municipios");
198
                           }
199
200
201
                }catch (Exception e) {
202
                        log.error(e.getMessage(),e);
203
                }
204
                finally{
205
                        if(r!=null)
206
                        {
207
                                try {
208
                                        r.close();
209
                                } catch (SQLException e) {
210
                                        // TODO Auto-generated catch block
211
                                        e.printStackTrace();
212
                                }
213
                        }
214
                }
215
                return municipios;
216
          }
217
218
          /**
219
           * Dado el nombre de un municipio devuelve los 10 mejores
220
           * candidatos. Si se encuentra un municipio con el mismo nombre
221
           * sólo se devuelve el municipio encontrado.
222
           */
223
          public List<String> getMunicipiosLV(String municipio){
224
                  ResultSet r = null;
225
                  List<String> retMunis = new ArrayList<String>();
226
                  try{
227
228
229
                                   r = this.EjecutaSql("select distinct ine_denomina, levenshtein(upper('"+municipio+
230
                                                   "'),upper(BUSQUEDA)) from municipios order by levenshtein(upper('"+municipio+
231
                                                   "'),upper(BUSQUEDA)) asc LIMIT 10");
232
                                   while (r.next()) {
233
                                                   retMunis.add(r.getString("ine_denomina"));
234
                                   }
235
236
                }catch (Exception e) {
237
                        log.error(e.getMessage(),e);
238
                }
239
                finally{
240
                        if(r!=null)
241
                        {
242
                                try {
243
                                        r.close();
244
                                } catch (SQLException e) {
245
                                        // TODO Auto-generated catch block
246
                                        e.printStackTrace();
247
                                }
248
                        }
249
                }
250
                return retMunis;
251
//                  LevenshteinDistance lv = new LevenshteinDistance();;
252
//                  List<String> lista = new ArrayList<String>();
253
//
254
//                   return lv.getTopX(municipio, getMunicipios(),10);
255
256
          }
257
258
          /**
259
           * Devuelve el mapa con sus layers, cada layer tendrá su gmldocument
260
           */
261
          public IMap getMapFromExtent(BoundingBox box) {
262
                  IMap map = null;
263
264
                  return map;
265
          }
266
267
          /**
268
           * Devuelve el extent máximo de la capa especificada. Quizás debería estar en un package de utils...
269
           * y devolver los máximos...
270
           * @return
271
           * @throws Exception
272
           */
273
          public Rectangle2D getMaxExtent(String layerName) throws Exception{
274
                  ResultSet r = null;
275
                Statement s = null;
276
                PGbox2d pgBox = null;
277
278
                try {
279
                        s = conn.createStatement();
280
                        r = s.executeQuery("SELECT EXTENT(the_GEOM) as bbox FROM " + layerName);
281
282
                   if (r.next() ) {
283
                         pgBox = (PGbox2d) r.getObject("bbox");
284
                   }
285
286
                   Rectangle2D rec = new Rectangle2D.Double(pgBox.getLLB().x,pgBox.getLLB().y,
287
                                                           pgBox.getURT().x-pgBox.getLLB().x, pgBox.getURT().y-pgBox.getLLB().y) ;
288
                   return rec;
289
290
                } catch (SQLException e) {
291
                        // TODO Auto-generated catch block
292
                        e.printStackTrace();
293
                        throw e;
294
                }
295
          }
296
297
          public ArrayList<String> getLayerList() {
298
             ArrayList<String> list = new ArrayList<String>();
299
300
             return list;
301
           }
302
 }