Statistics
| Revision:

root / branches / v10 / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / PostGisDriver.java @ 8913

History | View | Annotate | Download (24.6 KB)

1
/*
2
 * Created on 04-mar-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.fmap.drivers.jdbc.postgis;
45

    
46
import java.awt.geom.Rectangle2D;
47
import java.math.BigDecimal;
48
import java.nio.ByteBuffer;
49
import java.sql.Connection;
50
import java.sql.Date;
51
import java.sql.ResultSet;
52
import java.sql.SQLException;
53
import java.sql.Statement;
54
import java.sql.Time;
55
import java.sql.Timestamp;
56
import java.sql.Types;
57
import java.util.Hashtable;
58

    
59
import org.apache.log4j.Logger;
60
import org.postgis.PGbox2d;
61
import org.postgis.PGbox3d;
62

    
63
import com.hardcode.gdbms.engine.data.edition.DataWare;
64
import com.hardcode.gdbms.engine.values.Value;
65
import com.hardcode.gdbms.engine.values.ValueFactory;
66
import com.iver.andami.messages.NotificationManager;
67
import com.iver.cit.gvsig.fmap.DriverException;
68
import com.iver.cit.gvsig.fmap.SqlDriveExceptionType;
69
import com.iver.cit.gvsig.fmap.core.FShape;
70
import com.iver.cit.gvsig.fmap.core.ICanReproject;
71
import com.iver.cit.gvsig.fmap.core.IGeometry;
72
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
73
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver;
74
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
75
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
76
import com.iver.cit.gvsig.fmap.drivers.WKBParser2;
77
import com.iver.cit.gvsig.fmap.drivers.XTypes;
78
import com.iver.cit.gvsig.fmap.edition.EditionException;
79
import com.iver.cit.gvsig.fmap.edition.IWriteable;
80
import com.iver.cit.gvsig.fmap.edition.IWriter;
81

    
82
/**
83
 * @author FJP
84
 *
85
 * TODO To change the template for this generated type comment go to Window -
86
 * Preferences - Java - Code Generation - Code and Comments
87
 */
88
public class PostGisDriver extends DefaultDBDriver implements ICanReproject, IWriteable {
89
        private static Logger logger = Logger.getLogger(PostGisDriver.class
90
                        .getName());
91

    
92
        private static int FETCH_SIZE = 5000;
93

    
94
        private PostGISWriter writer = new PostGISWriter();
95

    
96
        private WKBParser2 parser = new WKBParser2();
97

    
98
        private int fetch_min = -1;
99

    
100
        private int fetch_max = -1;
101

    
102
        private String sqlOrig;
103

    
104
        /**
105
         * Used by setAbsolutePosition
106
         */
107
        private String sqlTotal;
108

    
109
        private String strEPSG = null;
110

    
111
        private String originalEPSG = null;
112

    
113
        private Rectangle2D fullExtent = null;
114

    
115
        private String strAux;
116

    
117
        private String completeWhere;
118

    
119
        private String provCursorName = null;
120

    
121
        int numProvCursors = 0;
122
        
123
        boolean bShapeTypeRevised = false;
124

    
125
        static {
126
                try {
127
                        Class.forName("org.postgresql.Driver");
128
                } catch (ClassNotFoundException e) {
129
                        throw new RuntimeException(e);
130
                }
131
        }
132

    
133
        /**
134
         *
135
         */
136
        public PostGisDriver() {
137

    
138
        }
139

    
140
        /*
141
         * (non-Javadoc)
142
         *
143
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
144
         */
145
        public DriverAttributes getDriverAttributes() {
146
                return null;
147
        }
148

    
149
        /*
150
         * (non-Javadoc)
151
         *
152
         * @see com.hardcode.driverManager.Driver#getName()
153
         */
154
        public String getName() {
155
                return "PostGIS JDBC Driver";
156
        }
157

    
158
        /**
159
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
160
         */
161
        public IGeometry getShape(int index) {
162
                IGeometry geom = null;
163
                boolean resul;
164
                try {
165
                        setAbsolutePosition(index);
166
                        // strAux = rs.getString(1);
167
                        // geom = parser.read(strAux);
168
                        if (rs != null) {
169
                                byte[] data = rs.getBytes(1);
170
                                if (data == null) // null geometry. 
171
                                        return null;
172
                                geom = parser.parse(data);
173
                        }
174
                } catch (SQLException e) {
175
                        e.printStackTrace();
176
                }
177

    
178
                return geom;
179
        }
180

    
181
        /**
182
         * First, the geometry field. After, the rest of fields
183
         *
184
         * @return
185
         */
186
        /*
187
         * public String getTotalFields() { String strAux = "AsBinary(" +
188
         * getLyrDef().getFieldGeometry() + ")"; String[] fieldNames =
189
         * getLyrDef().getFieldNames(); for (int i=0; i< fieldNames.length; i++) {
190
         * strAux = strAux + ", " + fieldNames[i]; } return strAux; }
191
         */
192

    
193
        /**
194
         * Antes de llamar a esta funci?n hay que haber fijado el workingArea si se
195
         * quiere usar.
196
         *
197
         * @param conn
198
         */
199
        public void setData(Connection conn, DBLayerDefinition lyrDef) {
200
                this.conn = conn;
201
                // TODO: Deber?amos poder quitar Conneciton de la llamada y meterlo
202
                // en lyrDef desde el principio.
203

    
204
                lyrDef.setConnection(conn);
205
                setLyrDef(lyrDef);
206

    
207
                getTableEPSG_and_shapeType();
208
                
209
                getLyrDef().setSRID_EPSG(originalEPSG);
210

    
211
                try {
212
                        conn.setAutoCommit(false);
213
                        sqlOrig = "SELECT " + getTotalFields() + " FROM "
214
                                        + getLyrDef().getTableName() + " ";
215
                                        // + getLyrDef().getWhereClause();
216
                        if (canReproject(strEPSG)) {
217
                                completeWhere = getCompoundWhere(sqlOrig, workingArea, strEPSG);
218
                        } else {
219
                                completeWhere = getCompoundWhere(sqlOrig, workingArea,
220
                                                originalEPSG);
221
                        }
222
                        // completeWhere = getLyrDef().getWhereClause() + completeWhere;
223

    
224
                        String sqlAux = sqlOrig + completeWhere + " ORDER BY "
225
                        + getLyrDef().getFieldID();
226

    
227
                        sqlTotal = sqlAux;
228
                        logger.info("Cadena SQL:" + sqlAux);
229
                        st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
230
                                        ResultSet.CONCUR_READ_ONLY);
231
                        // st.setFetchSize(FETCH_SIZE);
232
                        st.execute("declare wkb_cursor binary cursor for " + sqlAux);
233
                        rs = st.executeQuery("fetch forward " + FETCH_SIZE
234
                                        + " in wkb_cursor");
235
                        // st.execute("begin");
236
                        // bCursorActivo = true;
237
                        // rs = st.executeQuery(sqlOrig);
238
                        fetch_min = 0;
239
                        fetch_max = FETCH_SIZE - 1;
240
                        metaData = rs.getMetaData();
241
                        doRelateID_FID();
242
                        
243
                        writer.setCreateTable(false);
244
                        writer.setWriteAll(false);
245
                        writer.initialize(lyrDef);
246

    
247

    
248
                } catch (SQLException e) {
249
                        NotificationManager.addError(
250
                                        "Error al conectar a la base de datos.", e);
251
                } catch (EditionException e) {
252
                        e.printStackTrace();
253
                        NotificationManager.addError(
254
                                        "Error inicializando PosGIS Writer.", e);
255
                        
256
                }
257
        }
258

    
259
        /**
260
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
261
         */
262
        public Rectangle2D getFullExtent() {
263
                if (fullExtent == null) {
264
                        try {
265
                                Statement s = conn.createStatement();
266
                                ResultSet r = s.executeQuery("SELECT extent("
267
                                                + getLyrDef().getFieldGeometry()
268
                                                + ") AS FullExtent FROM " + getLyrDef().getTableName()
269
                                                + " " + getCompleteWhere());
270
                                r.next();
271
                                String strAux = r.getString(1);
272
                                System.out.println("fullExtent = " + strAux);
273
                                if (strAux == null) 
274
                                {
275
                                        logger.debug("La capa " + getLyrDef().getName() + " no tiene FULLEXTENT");
276
                                        return null;
277
                                }
278
                                if (strAux.startsWith("BOX3D")) {
279
                                        PGbox3d regeom = new PGbox3d(strAux);
280
                                        double x = regeom.getLLB().x;
281
                                        double y = regeom.getLLB().y;
282
                                        double w = regeom.getURT().x - x;
283
                                        double h = regeom.getURT().y - y;
284
                                        fullExtent = new Rectangle2D.Double(x, y, w, h);
285
                                } else {
286
                                        PGbox2d regeom = new PGbox2d(strAux);
287
                                        double x = regeom.getLLB().x;
288
                                        double y = regeom.getLLB().y;
289
                                        double w = regeom.getURT().x - x;
290
                                        double h = regeom.getURT().y - y;
291
                                        fullExtent = new Rectangle2D.Double(x, y, w, h);
292
                                }
293
                        } catch (SQLException e) {
294
                                System.err.println(e.getMessage());
295
                        }
296

    
297
                }
298
                return fullExtent;
299
        }
300

    
301
        /*
302
         * (non-Javadoc)
303
         *
304
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
305
         */
306
        public IFeatureIterator getFeatureIterator(String sql)
307
                        throws com.iver.cit.gvsig.fmap.DriverException {
308
                PostGisFeatureIterator geomIterator = null;
309
                try {
310
                        // st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
311
                        // ResultSet.CONCUR_READ_ONLY);
312

    
313

    
314
                        if (provCursorName != null) {
315
                                /* st.execute("BEGIN");
316
                                st.execute("CLOSE " + provCursorName);
317
                                bCursorActivo = false;
318
                                st.execute("COMMIT"); */
319
                                numProvCursors++;
320
                        }
321
                        // st.execute("BEGIN");
322
                        provCursorName = "wkb_cursor_prov_" + System.currentTimeMillis() + "" + numProvCursors;
323

    
324
                        // st.execute("BEGIN");
325
                        bCursorActivo = true;
326
                        // ResultSet rs = st.executeQuery(sql);
327
                        geomIterator = new PostGisFeatureIterator(conn, provCursorName, sql);
328
                        geomIterator.setLyrDef(getLyrDef());
329
                } catch (SQLException e) {
330
                        e.printStackTrace();
331
                        e.printStackTrace();
332
                        SqlDriveExceptionType type = new SqlDriveExceptionType();
333
            type.setDriverName("PostGIS Driver");
334
            type.setSql(sql);
335
            type.setLayerName(getTableName());
336
            type.setSchema(null);
337
            throw new com.iver.cit.gvsig.fmap.DriverException(e, type);
338
//                        throw new DriverException(e);
339
                        // return null;
340
                }
341

    
342
                return geomIterator;
343
        }
344

    
345
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG)
346
                        throws DriverException {
347
                if (workingArea != null)
348
                        r = r.createIntersection(workingArea);
349

    
350
                String sqlAux;
351
                if (canReproject(strEPSG)) {
352
                        sqlAux = sqlOrig + getCompoundWhere(sqlOrig, r, strEPSG);
353
                } else {
354
                        sqlAux = sqlOrig + getCompoundWhere(sqlOrig, r, originalEPSG);
355
                }
356

    
357
                System.out.println("SqlAux getFeatureIterator = " + sqlAux);
358

    
359
                return getFeatureIterator(sqlAux);
360
        }
361

    
362
        /**
363
         * Le pasas el rect?ngulo que quieres pedir. La primera vez es el
364
         * workingArea, y las siguientes una interseccion de este rectangulo con el
365
         * workingArea
366
         *
367
         * @param r
368
         * @param strEPSG
369
         * @return
370
         */
371
        private String getCompoundWhere(String sql, Rectangle2D r, String strEPSG) {
372
                if (r == null)
373
                        return getWhereClause();
374

    
375
                double xMin = r.getMinX();
376
                double yMin = r.getMinY();
377
                double xMax = r.getMaxX();
378
                double yMax = r.getMaxY();
379
                String wktBox = "GeometryFromText('LINESTRING(" + xMin + " " + yMin
380
                                + ", " + xMax + " " + yMin + ", " + xMax + " " + yMax + ", "
381
                                + xMin + " " + yMax + ")', " + strEPSG + ")";
382
                String sqlAux;
383
                if (getWhereClause().indexOf("WHERE") != -1)
384
                        sqlAux = getWhereClause() + " AND " + getLyrDef().getFieldGeometry() + " && " + wktBox;
385
                else
386
                        sqlAux = "WHERE " + getLyrDef().getFieldGeometry() + " && "
387
                                        + wktBox;
388
                return sqlAux;
389
        }
390

    
391
        /**
392
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getConnectionStringBeginning()
393
         */
394
        public String getConnectionStringBeginning() {
395
                return "jdbc:postgresql:";
396
        }
397

    
398
        /*
399
         * (non-Javadoc)
400
         *
401
         * @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#getFieldValue(long,
402
         *      int)
403
         */
404
        public Value getFieldValue(long rowIndex, int idField)
405
                        throws com.hardcode.gdbms.engine.data.driver.DriverException {
406
                boolean resul;
407
                // EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
408
                // FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
409
                // LO HAGA
410
                // System.out.println("getShape " + index);
411
                int index = (int) (rowIndex);
412
                try {
413
                        setAbsolutePosition(index);
414
                        int fieldId = idField + 2;
415
                        byte[] byteBuf = rs.getBytes(fieldId);
416
                        if (byteBuf == null)
417
                                return ValueFactory.createNullValue();
418
                        else {
419
                                ByteBuffer buf = ByteBuffer.wrap(byteBuf);
420
                                if (metaData.getColumnType(fieldId) == Types.VARCHAR)
421
                                        return ValueFactory.createValue(rs.getString(fieldId));
422
                                if (metaData.getColumnType(fieldId) == Types.FLOAT)
423
                                        return ValueFactory.createValue(buf.getFloat());
424
                                if (metaData.getColumnType(fieldId) == Types.DOUBLE)
425
                                        return ValueFactory.createValue(buf.getDouble());
426
                                if (metaData.getColumnType(fieldId) == Types.INTEGER)
427
                                        return ValueFactory.createValue(buf.getInt());
428
                                if (metaData.getColumnType(fieldId) == Types.BIGINT)
429
                                        return ValueFactory.createValue(buf.getLong());
430
                                if (metaData.getColumnType(fieldId) == Types.BIT)
431
                                        // TODO
432
                                        return ValueFactory.createValue(rs.getBoolean(fieldId));
433
                                if (metaData.getColumnType(fieldId) == Types.DATE)
434
                                {
435
                        long daysAfter2000 = buf.getInt() + 1;
436
                        long msecs = daysAfter2000*24*60*60*1000;
437
                        long real_msecs_date1 = (long) (XTypes.NUM_msSecs2000 + msecs);
438
                        Date realDate1 = new Date(real_msecs_date1);
439
                                        return ValueFactory.createValue(realDate1);
440
                                }
441
                                if (metaData.getColumnType(fieldId) == Types.TIME)
442
                                {
443
                                        // TODO:
444
                                        // throw new RuntimeException("TIME type not implemented yet");
445
                                        return ValueFactory.createValue("NOT IMPLEMENTED YET");
446
                                }                                
447
                                if (metaData.getColumnType(fieldId) == Types.TIMESTAMP)                                
448
                                {
449
                                        double segsReferredTo2000 = buf.getDouble();
450
                                        long real_msecs = (long) (XTypes.NUM_msSecs2000 + segsReferredTo2000*1000);
451
                                        Timestamp valTimeStamp = new Timestamp(real_msecs);
452
                                        return ValueFactory.createValue(valTimeStamp);
453
                                }
454

    
455
                                if (metaData.getColumnType(fieldId) == Types.NUMERIC) {
456
                                        // System.out.println(metaData.getColumnName(fieldId) + " "
457
                                        // + metaData.getColumnClassName(fieldId));
458
                                        short ndigits = buf.getShort();
459
                                        short weight = buf.getShort();
460
                                        short sign = buf.getShort();
461
                                        short dscale = buf.getShort();
462
                                        String strAux;
463
                                        if (sign == 0)
464
                                                strAux = "+";
465
                                        else
466
                                                strAux = "-";
467

    
468
                                        for (int iDigit = 0; iDigit < ndigits; iDigit++) {
469
                                                short digit = buf.getShort();
470
                                                strAux = strAux + digit;
471
                                                if (iDigit == weight)
472
                                                        strAux = strAux + ".";
473

    
474
                                        }
475
                                        strAux = strAux + "0";
476
                                        BigDecimal dec;
477
                                        dec = new BigDecimal(strAux);
478
                                        // System.out.println(ndigits + "_" + weight + "_" + dscale
479
                                        // + "_" + strAux);
480
                                        // System.out.println(strAux + " Big= " + dec);
481
                                        return ValueFactory.createValue(dec.doubleValue());
482
                                }
483

    
484
                        }
485
                } catch (SQLException e) {
486
                        throw new com.hardcode.gdbms.engine.data.driver.DriverException(e
487
                                        .getMessage());
488
                }
489
                return ValueFactory.createNullValue();
490

    
491
        }
492

    
493
        public void open() throws com.iver.cit.gvsig.fmap.DriverException {
494
                /*
495
                 * try { st = conn.createStatement(); st.setFetchSize(2000); if
496
                 * (bCursorActivo) close(); st.execute("declare wkb_cursor binary cursor
497
                 * for " + sqlOrig); rs = st.executeQuery("fetch forward all in
498
                 * wkb_cursor"); // st.execute("BEGIN"); bCursorActivo = true; } catch
499
                 * (SQLException e) { e.printStackTrace(); throw new
500
                 * com.iver.cit.gvsig.fmap.DriverException(e); }
501
                 */
502

    
503
        }
504

    
505
        private void setAbsolutePosition(int index) throws SQLException {
506
                // TODO: USAR LIMIT Y ORDER BY, Y HACERLO TAMBI?N PARA
507
                // MYSQL
508

    
509
                // EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
510
                // FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
511
                // LO HAGA
512
                // System.out.println("getShape " + index + " fetchMin=" + fetch_min + "
513
                // fetchMax=" + fetch_max);
514
                if (index < fetch_min) {
515
                        // rs.close();
516
                        try {
517
                                st.execute("CLOSE wkb_cursor");        
518
                        }
519
                        catch (SQLException e)
520
                        {
521
                                e.printStackTrace();
522
                                rs.close();
523
                                conn.rollback();
524
                        }
525
                        
526
                        st.execute("declare wkb_cursor binary cursor for " + sqlTotal);
527
                        rs = st.executeQuery("fetch forward " + FETCH_SIZE
528
                                        + " in wkb_cursor");
529

    
530
                        // rs.beforeFirst();
531

    
532
                        // rs = st.executeQuery(sqlOrig);
533
                        fetch_min = 0;
534
                        fetch_max = FETCH_SIZE - 1;
535
                }
536
                if (index > fetch_max && index != 0) {
537
                        try {
538
                                rs = st.executeQuery("fetch forward " + FETCH_SIZE
539
                                                + " in wkb_cursor");
540
                        }
541
                        catch (SQLException e)
542
                        {
543
                                // el cursor no est? definido porque hemos modificado los campos.
544
                                st.execute("declare wkb_cursor binary cursor for " + sqlTotal);
545
                                // reentramos una vez que hemos creado de nuevo el cursor para que 
546
                                // se reposicione correctamente.
547
                                setAbsolutePosition(index);
548
                        }
549
                        // rs.next();
550
                        /*
551
                         * rs.afterLast(); // forzamos una carga rs.next();
552
                         */
553
                        fetch_min = fetch_max + 1;
554
                        fetch_max = fetch_min + FETCH_SIZE - 1;
555
                        // System.out.println("fetchSize = " + rs.getFetchSize() + " " +
556
                        // fetch_min + "-" + fetch_max);
557
                }
558
                if (rs != null)
559
                        rs.absolute(index - fetch_min + 1);
560

    
561
                // rs.absolute(index+1);
562

    
563
        }
564

    
565
        /**
566
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String)
567
         */
568
        public String getGeometryField(String fieldName) {
569
                return "ASBINARY(" + fieldName + ", 'XDR')";
570
        }
571

    
572
        /**
573
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
574
         */
575
        public int[] getPrimaryKeys()
576
                        throws com.hardcode.gdbms.engine.data.driver.DriverException {
577
                // TODO Auto-generated method stub
578
                return null;
579
        }
580

    
581
        /**
582
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getDefaultPort()
583
         */
584
        public int getDefaultPort() {
585
                return 5432;
586
        }
587

    
588
        /**
589
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
590
         */
591
        public void write(DataWare arg0)
592
                        throws com.hardcode.gdbms.engine.data.driver.DriverException {
593
                // TODO Auto-generated method stub
594

    
595
        }
596

    
597
        /*
598
         * (non-Javadoc)
599
         *
600
         * @see com.iver.cit.gvsig.fmap.core.ICanReproject#getSourceProjection()
601
         */
602
        public String getSourceProjection() {
603
                if (originalEPSG == null)
604
                        getTableEPSG_and_shapeType();
605
                return originalEPSG;
606
        }
607

    
608
        /**
609
         * Las tablas con geometr?as est?n en la tabla GEOMETRY_COLUMNS y de
610
         * ah? sacamos en qu? proyecci?n est?n.
611
         * El problema es que si el usuario hace una vista de esa
612
         * tabla, no estar? dada de alta aqu? y entonces gvSIG
613
         * no se entera en qu? proyecci?n est? trabajando (y le
614
         * ponemos un -1 como mal menor). El -1 implica que luego
615
         * no podremos reproyectar al vuelo desde la base de datos.
616
         */
617
        private void getTableEPSG_and_shapeType() {
618
                try {
619
                        Statement stAux = conn.createStatement();
620

    
621
                        String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '"
622
                                        + getTableName() + "' AND F_GEOMETRY_COLUMN = '" + getLyrDef().getFieldGeometry() + "'";
623
                        ResultSet rs = stAux.executeQuery(sql);
624
                        rs.next();
625
                        originalEPSG = "" + rs.getInt("SRID");
626
                        String geometryType = rs.getString("TYPE");
627
                        int shapeType = FShape.MULTI;
628
                        if (geometryType.compareToIgnoreCase("POINT") == 0)
629
                                shapeType = FShape.POINT;
630
                        if (geometryType.compareToIgnoreCase("LINESTRING") == 0)
631
                                shapeType = FShape.LINE;
632
                        if (geometryType.compareToIgnoreCase("POLYGON") == 0)
633
                                shapeType = FShape.POLYGON;
634
                        if (geometryType.compareToIgnoreCase("MULTIPOINT") == 0)
635
                                shapeType = FShape.POINT;
636
                        if (geometryType.compareToIgnoreCase("MULTILINESTRING") == 0)
637
                                shapeType = FShape.LINE;
638
                        if (geometryType.compareToIgnoreCase("MULTIPOLYGON") == 0)
639
                                shapeType = FShape.POLYGON;                        
640
                        
641
                        getLyrDef().setShapeType(shapeType);
642
                        rs.close();
643
                } catch (SQLException e) {
644
                        // TODO Auto-generated catch block
645
                        originalEPSG = "-1";
646
                        logger.error(e);
647
                        e.printStackTrace();
648
                }
649

    
650
        }
651

    
652
        /*
653
         * (non-Javadoc)
654
         *
655
         * @see com.iver.cit.gvsig.fmap.core.ICanReproject#getDestProjection()
656
         */
657
        public String getDestProjection() {
658
                return strEPSG;
659
        }
660

    
661
        /*
662
         * (non-Javadoc)
663
         *
664
         * @see com.iver.cit.gvsig.fmap.core.ICanReproject#setDestProjection(java.lang.String)
665
         */
666
        public void setDestProjection(String toEPSG) {
667
                this.strEPSG = toEPSG;
668
        }
669

    
670
        /*
671
         * (non-Javadoc)
672
         *
673
         * @see com.iver.cit.gvsig.fmap.core.ICanReproject#canReproject(java.lang.String)
674
         */
675
        public boolean canReproject(String toEPSGdestinyProjection) {
676
                // TODO POR AHORA, REPROYECTA SIEMPRE gvSIG.
677
                return false;
678
        }
679

    
680
        /*
681
         * (non-Javadoc)
682
         *
683
         * @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#doRelateID_FID()
684
         */
685
        protected void doRelateID_FID() {
686
                hashRelate = new Hashtable();
687
                try {
688
                        String strSQL = "SELECT " + getLyrDef().getFieldID() + " FROM "
689
                                        + getLyrDef().getTableName() + " ";
690
                                        // + getLyrDef().getWhereClause();
691
                        if (canReproject(strEPSG)) {
692
                                strSQL = strSQL
693
                                                + getCompoundWhere(strSQL, workingArea, strEPSG);
694
                        } else {
695
                                strSQL = strSQL
696
                                                + getCompoundWhere(strSQL, workingArea, originalEPSG);
697
                        }
698
                        strSQL = strSQL + " ORDER BY " + getLyrDef().getFieldID();
699
                        Statement s = getConnection().createStatement(
700
                                        ResultSet.TYPE_SCROLL_INSENSITIVE,
701
                                        ResultSet.CONCUR_READ_ONLY);
702
                        int fetchSize = 5000;
703
                        ResultSet r = s.executeQuery(strSQL);
704
                        int id = 0;
705
                        String gid;
706
                        while (r.next()) {
707
                                gid = r.getString(1);
708
                                Value aux = ValueFactory.createValue(gid);
709
                                hashRelate.put(aux, new Integer(id));
710
                                // System.out.println("ASOCIANDO CLAVE " + aux + " CON VALOR " + id);
711
                                id++;
712
                                // System.out.println("Row " + id + ":" + strAux);
713
                        }
714
                        s.close();
715
                        numReg = id;
716

    
717
                        /*
718
                         * for (int index = 0; index < getShapeCount(); index++) { Value aux =
719
                         * getFieldValue(index, idFID_FieldName-2); hashRelate.put(aux, new
720
                         * Integer(index)); // System.out.println("Row " + index + " clave=" +
721
                         * aux); }
722
                         */
723
                        /*
724
                         * int index = 0;
725
                         *
726
                         * while (rs.next()) { Value aux = getFieldValue(index,
727
                         * idFID_FieldName-2); hashRelate.put(aux, new Integer(index));
728
                         * index++; System.out.println("Row " + index + " clave=" + aux); }
729
                         * numReg = index;
730
                         */
731
                        // rs.beforeFirst();
732
                        /*
733
                         * } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) { //
734
                         * TODO Auto-generated catch block e.printStackTrace();
735
                         */
736
                } catch (SQLException e) {
737
                        // TODO Auto-generated catch block
738
                        e.printStackTrace();
739
                }
740
        }
741

    
742
        public String getSqlTotal() {
743
                return sqlTotal;
744
        }
745

    
746
        /**
747
         * @return Returns the completeWhere.
748
         */
749
        public String getCompleteWhere() {
750
                return completeWhere;
751
        }
752

    
753
        /*
754
         * (non-Javadoc)
755
         *
756
         * @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#close()
757
         */
758
        public void close() {
759
                super.close();
760
                /*
761
                 * if (bCursorActivo) { try { // st =
762
                 * conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
763
                 * ResultSet.CONCUR_READ_ONLY); st.execute("CLOSE wkb_cursor_prov"); //
764
                 * st.close(); } catch (SQLException e) { // TODO Auto-generated catch
765
                 * block e.printStackTrace(); } bCursorActivo = false; }
766
                 */
767

    
768
        }
769

    
770
        /*
771
         * (non-Javadoc)
772
         *
773
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getFeatureIterator(java.awt.geom.Rectangle2D,
774
         *      java.lang.String, java.lang.String[])
775
         */
776
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG,
777
                        String[] alphaNumericFieldsNeeded) throws DriverException {
778
                String sqlAux = null;
779
                try {
780
                        if (workingArea != null)
781
                                r = r.createIntersection(workingArea);
782
                        // if (getLyrDef()==null){
783
                        // load();
784
                        // throw new DriverException("Fallo de la conexi?n");
785
                        // }
786
                        String strAux = getGeometryField(getLyrDef().getFieldGeometry());
787

    
788
                        boolean found = false;
789
                        if (alphaNumericFieldsNeeded != null) {
790
                                for (int i = 0; i < alphaNumericFieldsNeeded.length; i++) {
791
                                        strAux = strAux + ", " + alphaNumericFieldsNeeded[i];
792
                                        if (alphaNumericFieldsNeeded[i].equals(getLyrDef()
793
                                                        .getFieldID()))
794
                                                found = true;
795
                                }
796
                        }
797
                        // Nos aseguramos de pedir siempre el campo ID
798
                        if (found == false)
799
                                strAux = strAux + ", " + getLyrDef().getFieldID();
800

    
801
                        String sqlProv = "SELECT " + strAux + " FROM "
802
                                        + getLyrDef().getTableName() + " ";
803
                                        // + getLyrDef().getWhereClause();
804

    
805
                        
806
                        if (canReproject(strEPSG)) {
807
                                sqlAux = sqlProv + getCompoundWhere(sqlProv, r, strEPSG);
808
                        } else {
809
                                sqlAux = sqlProv + getCompoundWhere(sqlProv, r, originalEPSG);
810
                        }
811

    
812
                        System.out.println("SqlAux getFeatureIterator = " + sqlAux);
813

    
814
                        return getFeatureIterator(sqlAux);
815
                } catch (Exception e) {
816
                        e.printStackTrace();
817
                        SqlDriveExceptionType type = new SqlDriveExceptionType();
818
            type.setDriverName("PostGIS Driver");
819
            type.setSql(sqlAux);
820
            type.setLayerName(getTableName());
821
            type.setSchema(null);
822
            throw new com.iver.cit.gvsig.fmap.DriverException(e, type);
823
                        
824
//                        throw new DriverException(e);
825
                }
826
        }
827

    
828
        /* public void preProcess() throws EditionException {
829
                writer.preProcess();
830
        }
831

832
        public void process(IRowEdited row) throws EditionException {
833
                writer.process(row);
834
        }
835

836
        public void postProcess() throws EditionException {
837
                writer.postProcess();
838
        }
839

840
        public String getCapability(String capability) {
841
                return writer.getCapability(capability);
842
        }
843

844
        public void setCapabilities(Properties capabilities) {
845
                writer.setCapabilities(capabilities);
846
        }
847

848
        public boolean canWriteAttribute(int sqlType) {
849
                return writer.canWriteAttribute(sqlType);
850
        }
851

852
        public boolean canWriteGeometry(int gvSIGgeometryType) {
853
                return writer.canWriteGeometry(gvSIGgeometryType);
854
        }
855

856
        public void initialize(ITableDefinition layerDef) throws EditionException {
857
                writer.setCreateTable(false);
858
                writer.setWriteAll(false);
859
                // Obtenemos el DBLayerDefinition a partir del driver
860

861
                DBLayerDefinition dbLyrDef = getLyrDef();
862

863

864
                writer.initialize(dbLyrDef);
865
        }
866
*/
867
        public boolean isWritable() {
868
                return true;
869
        } 
870

    
871
        public IWriter getWriter() {
872
                return writer;
873
        }
874

    
875

    
876
}