Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_daldb / src-test / org / gvsig / fmap / dal / store / postgresql / TestReadAndWriteGeom.java @ 30580

History | View | Annotate | Download (8.22 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.GeometryLocator;
42
import org.gvsig.fmap.geom.GeometryManager;
43
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB;
44
import org.gvsig.fmap.geom.operation.fromwkb.FromWKBGeometryOperationContext;
45
import org.gvsig.fmap.geom.operation.towkb.ToWKB;
46
import org.gvsig.fmap.geom.operation.towkt.ToWKT;
47
import org.gvsig.fmap.geom.primitive.impl.Point2D;
48
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
49

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

    
58
        public TestReadAndWriteGeom() throws Exception {
59
                new DefaultLibrariesInitializer().fullInitialize();
60
                
61
//                ToolsLibrary toolsLib = new ToolsLibrary();
62
//                GeometryLibrary geoLib = new GeometryLibrary();
63
//                DefaultGeometryLibrary impGeoLib = new DefaultGeometryLibrary();
64
//                DefaultGeometryOperationLibrary opGeoLib = new DefaultGeometryOperationLibrary();
65
//
66
//                toolsLib.initialize();
67
//                geoLib.initialize();
68
//                impGeoLib.initialize();
69
//                opGeoLib.initialize();
70
//
71
//                toolsLib.postInitialize();
72
//                geoLib.postInitialize();
73
//                impGeoLib.postInitialize();
74
//                opGeoLib.postInitialize();
75

    
76
                geoManager = GeometryLocator.getGeometryManager();
77

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

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

    
89
        }
90

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

    
103

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

    
115
                                pst.executeUpdate();
116
                        }
117

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

    
121

    
122

    
123

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

    
129
                }
130

    
131

    
132
        }
133

    
134

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

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

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

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

    
164
        }
165

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

    
169
        }
170

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

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

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

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

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

    
225
        }
226

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

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

    
254

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

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

    
316

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

    
322
}