Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_daldb / src-test / org / gvsig / fmap / dal / store / postgresql / TestReadAndWriteGeom.java @ 28678

History | View | Annotate | Download (8.28 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
* 2009 IVER T.I   {{Task}}
26
*/
27

    
28
package org.gvsig.fmap.dal.store.postgresql;
29

    
30
import java.sql.Connection;
31
import java.sql.DriverManager;
32
import java.sql.PreparedStatement;
33
import java.sql.ResultSet;
34
import java.sql.SQLException;
35
import java.sql.Statement;
36
import java.text.MessageFormat;
37

    
38
import junit.framework.TestCase;
39

    
40
import org.gvsig.fmap.geom.Geometry;
41
import org.gvsig.fmap.geom.GeometryLibrary;
42
import org.gvsig.fmap.geom.GeometryLocator;
43
import org.gvsig.fmap.geom.GeometryManager;
44
import org.gvsig.fmap.geom.impl.DefaultGeometryLibrary;
45
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB;
46
import org.gvsig.fmap.geom.operation.fromwkb.FromWKBGeometryOperationContext;
47
import org.gvsig.fmap.geom.operation.impl.DefaultGeometryOperationLibrary;
48
import org.gvsig.fmap.geom.operation.towkb.ToWKB;
49
import org.gvsig.fmap.geom.operation.towkt.ToWKT;
50
import org.gvsig.fmap.geom.primitive.impl.Point2D;
51
import org.gvsig.tools.ToolsLibrary;
52

    
53
public class TestReadAndWriteGeom extends TestCase {
54
        private static final int FEATURES_TO_INSERT = 12000;
55
        private static final String TABLE_NAME_INSERT = "testReadadnwritegeom_testcase";
56
        private static final String FIELD_NAME_INSERT = "geom";
57
        private Connection conn;
58
        private GeometryManager geoManager;
59
        private static String SQL_FOR_READ_TEST = "Select {0} from medio_ejes";
60

    
61
        public TestReadAndWriteGeom() throws Exception {
62
                ToolsLibrary toolsLib = new ToolsLibrary();
63
                GeometryLibrary geoLib = new GeometryLibrary();
64
                DefaultGeometryLibrary impGeoLib = new DefaultGeometryLibrary();
65
                DefaultGeometryOperationLibrary opGeoLib = new DefaultGeometryOperationLibrary();
66

    
67
                toolsLib.initialize();
68
                geoLib.initialize();
69
                impGeoLib.initialize();
70
                opGeoLib.initialize();
71

    
72
                toolsLib.postInitialize();
73
                geoLib.postInitialize();
74
                impGeoLib.postInitialize();
75
                opGeoLib.postInitialize();
76

    
77
                geoManager = GeometryLocator.getGeometryManager();
78

    
79
                Class klass = Class.forName(PostgreSQLLibrary.DEFAULT_JDCB_DRIVER_NAME);
80
                if (klass == null) {
81
                        throw new Exception("Driver not found: "
82
                                        + PostgreSQLLibrary.DEFAULT_JDCB_DRIVER_NAME);
83
                }
84

    
85
                // inicializamos operaciones de geometrias de las que dependemos
86
                int code = FromWKB.CODE;
87
                code = ToWKB.CODE;
88
                code = ToWKT.CODE;
89

    
90
        }
91

    
92
        public void testInsertWKB() throws Exception {
93
                PreparedStatement pst = null;
94
                try {
95
                        crearTablaTest();
96
                        Point2D geom = (Point2D) geoManager.createPoint(0, 0,
97
                                        Geometry.SUBTYPES.GEOM2D);
98
                        pst = conn.prepareStatement(
99
                                        "Insert into "
100
                                        + TABLE_NAME_INSERT.toLowerCase() +
101
                                        "  ("
102
                                        + FIELD_NAME_INSERT + ") Values (GeomFromWKB(?))");
103

    
104

    
105
                        int i;
106
                        for (i = 1; i <= FEATURES_TO_INSERT; i++) {
107
                                pst.clearParameters();
108
                                geom.setX(i);
109
                                geom.setY(i);
110
                                pst.setBytes(1,
111
                                        (byte[]) geom.invokeOperation(
112
                                                        ToWKB.CODE,
113
                                                        null)
114
                                        );
115

    
116
                                pst.executeUpdate();
117
                        }
118

    
119
                        System.out
120
                                        .println("TestReadAndWriteGeom.testInsertPostgis() Inserteds= i");
121

    
122

    
123

    
124

    
125
                } finally {
126
                        if (pst != null) {
127
                                try {pst.close();} catch (SQLException e) {e.printStackTrace();}
128
                        }
129

    
130
                }
131

    
132

    
133
        }
134

    
135

    
136
        protected void setUp() throws Exception {
137
                super.setUp();
138

    
139
                conn = DriverManager.getConnection(PostgreSQLLibrary.getJdbcUrl(
140
                                "localhost", 5432, "gis"), "postgres", "postgres");
141
        }
142

    
143
        private void crearTablaTest() throws SQLException {
144
                execute("DROP TABLE IF EXISTS " + TABLE_NAME_INSERT.toLowerCase());
145
                execute("Delete from geometry_columns  where  f_table_name = '"
146
                                + TABLE_NAME_INSERT.toLowerCase() + "'");
147
                execute("CREATE TABLE " + TABLE_NAME_INSERT.toLowerCase()
148
                                + " (id serial PRIMARY KEY)");
149
                execute("Select AddGeometryColumn('" + TABLE_NAME_INSERT.toLowerCase()
150
                                + "','"
151
                                + FIELD_NAME_INSERT + "',-1,'GEOMETRY',2)");
152
        }
153

    
154
        private void execute(String sql) throws SQLException {
155
                Statement st = null;
156
                try {
157
                        st = conn.createStatement();
158
                        st.execute(sql);
159
                } finally {
160
                        if (st != null) {
161
                                try {st.close();} catch (SQLException e) {e.printStackTrace();}
162
                        }
163
                }
164

    
165
        }
166

    
167
        public String getSQLForRead(String geoColumn) {
168
                return MessageFormat.format(SQL_FOR_READ_TEST, geoColumn);
169

    
170
        }
171

    
172
        public void testReadBinary() throws Exception {
173
                // st.execute("declare " + getTableName() + myCursorId +
174
                // "_wkb_cursor binary scroll cursor with hold for " + sqlAux);
175
                // rs = st.executeQuery("fetch forward " + FETCH_SIZE+ " in " +
176
                // getTableName() + myCursorId + "_wkb_cursor");
177

    
178
                Statement st = null;
179
                ResultSet rs = null;
180
                long count = 0;
181
                Geometry geom;
182
                Geometry nullGeom = geoManager.createNullGeometry(Geometry.SUBTYPES.GEOM2D);
183

    
184
                try {
185
                        st = conn.createStatement();
186
                        String cursorName = "myCursor___xxx";
187
                        st.execute("declare " + cursorName
188
                                        + "_wkb_cursor binary scroll cursor with hold for "
189
                                        + getSQLForRead("the_geom"));
190
                        rs = st.executeQuery("fetch forward all in " + cursorName
191
                                        + "_wkb_cursor");
192
                        byte[] buff;
193
                        FromWKBGeometryOperationContext opContext = new FromWKBGeometryOperationContext();
194
                        while (rs.next()) {
195
                                count++;
196
                                buff = rs.getBytes(1);
197
//                                if (buff != null) {
198
//                                        opContext.setData(buff);
199
//                                        geom = (Geometry) nullGeom.invokeOperation(FromWKB.CODE,
200
//                                                        opContext);
201
//                                        assertNotNull(geom);
202
//                                }
203
                        }
204

    
205
                        System.out
206
                                        .println("TestReadAndWriteGeom.testReadPostgis() Count = "
207
                                                        + count);
208

    
209
                } finally {
210
                        if (st != null) {
211
                                try {
212
                                        st.close();
213
                                } catch (SQLException e) {
214
                                        e.printStackTrace();
215
                                }
216
                        }
217
                        if (rs != null) {
218
                                try {
219
                                        rs.close();
220
                                } catch (SQLException e) {
221
                                        e.printStackTrace();
222
                                }
223
                        }
224
                }
225

    
226
        }
227

    
228
        public void testReadWKB() throws Exception {
229
                Statement st = null;
230
                ResultSet rs = null;
231
                long count = 0;
232
                Geometry geom;
233
                Geometry nullGeom = geoManager
234
                                .createNullGeometry(Geometry.SUBTYPES.GEOM2D);
235
                byte[] buff;
236
                FromWKBGeometryOperationContext opContext = new FromWKBGeometryOperationContext();
237
                try {
238
                        st = conn.createStatement();
239
                        rs = st.executeQuery(getSQLForRead("asBinary(the_geom)"));
240
                        while (rs.next()) {
241
                                count++;
242
                                buff = rs.getBytes(1);
243
//                                if (buff != null) {
244
//                                        opContext.setData(buff);
245
//                                        geom = (Geometry) nullGeom.invokeOperation(FromWKB.CODE,
246
//                                                        opContext);
247
//                                        assertNotNull(geom);
248
//                                }
249
                        }
250

    
251
                        System.out
252
                                        .println("TestReadAndWriteGeom.testReadPostgis() Count = "
253
                                                        + count);
254

    
255

    
256
                } finally {
257
                        if (st != null) {
258
                                try {
259
                                        st.close();
260
                                } catch (SQLException e) {
261
                                        e.printStackTrace();
262
                                }
263
                        }
264
                        if (rs != null) {
265
                                try {
266
                                        rs.close();
267
                                } catch (SQLException e) {
268
                                        e.printStackTrace();
269
                                }
270
                        }
271
                }
272
        }
273

    
274
//        public void testReadPostgis() throws Exception {
275
//                Statement st = null;
276
//                ResultSet rs = null;
277
//                long count = 0;
278
//                Geometry geom;
279
//                PGgeometry pgGeom;
280
//                PostGIS2Geometry converter = new PostGIS2Geometry();
281
//                try {
282
//                        st = conn.createStatement();
283
//                        rs = st.executeQuery(getSQLForRead("the_geom"));
284
//                        while (rs.next()) {
285
//                                count++;
286
//                                pgGeom = (PGgeometry) rs.getObject(1);
287
//                                if (pgGeom != null) {
288
//                                        geom = converter.getGeometry(pgGeom);
289
//                                        assertNotNull(geom);
290
//                                }
291
//                        }
292
//
293
//
294
//                        System.out
295
//                                        .println("TestReadAndWriteGeom.testReadPostgis() Count = "
296
//                                                        + count);
297
//
298
//
299
//                } finally {
300
//                        if (st != null) {
301
//                                try {
302
//                                        st.close();
303
//                                } catch (SQLException e) {
304
//                                        e.printStackTrace();
305
//                                }
306
//                        }
307
//                        if (rs != null) {
308
//                                try {
309
//                                        rs.close();
310
//                                } catch (SQLException e) {
311
//                                        e.printStackTrace();
312
//                                }
313
//                        }
314
//                }
315
//        }
316

    
317

    
318
        protected void tearDown() throws Exception {
319
                super.tearDown();
320
                conn.close();
321
        }
322

    
323
}