forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogr_db2.py
executable file
·273 lines (181 loc) · 6.94 KB
/
ogr_db2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env pytest
###############################################################################
#
# Project: GDAL/OGR Test Suite
# Purpose: Test DB2 vector driver
#
# Author: David Adler <[email protected]>
#
###############################################################################
# Copyright (c) 2015, David Adler <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
###############################################################################
# Before this test is run with a real database connection,
# set DB2_TEST_SERVER to point to the server and table to be used, like:
# DB2_TEST_SERVER=Database=SAMP105;DSN=SAMP105A;tables=TEST.ZIPPOINT
# or
# DB2_TEST_SERVER=Database=SAMP105;Driver={IBM DB2 CLIDRIVER};Hostname=<>;Port=<>;PROTOCOL=TCPIP;UID=<>;PWD=<>;tables=TEST.ZIPPOINT
#
# Also before running, the db2 setup script must be run to create the
# needed SRS and test tables
# In a DB2 command window, connect to a database and issue a command like
# db2 -tvf ogr\data\db2\db2_setup.sql
#
# These tests currently only run on Windows
import os
import ogrtest
from osgeo import ogr
import pytest
###############################################################################
# Test if driver is available
def test_ogr_db2_check_driver():
ogrtest.db2_drv = None
ogrtest.db2_drv = ogr.GetDriverByName('DB2ODBC')
if ogrtest.db2_drv is None:
pytest.skip()
###############################################################################
# Test if environment variable for DB2 connection is set and we can connect
def test_ogr_db2_init():
if ogrtest.db2_drv is None:
pytest.skip()
if 'DB2_TEST_SERVER' in os.environ:
ogrtest.db2_test_server = "DB2ODBC:" + os.environ['DB2_TEST_SERVER']
else:
ogrtest.db2_drv = None
pytest.skip('Environment variable DB2_TEST_SERVER not found')
###############################################################################
# Test GetFeatureCount()
def test_ogr_db2_GetFeatureCount():
if ogrtest.db2_drv is None:
pytest.skip()
ds = ogr.Open(ogrtest.db2_test_server)
assert ds is not None
lyr = ds.GetLayer(0)
assert lyr is not None
count = lyr.GetFeatureCount()
assert count == 5, 'did not get expected feature count'
###############################################################################
# Test GetSpatialRef()
def test_ogr_db2_GetSpatialRef():
if ogrtest.db2_drv is None:
pytest.skip()
ds = ogr.Open(ogrtest.db2_test_server)
assert ds is not None
lyr = ds.GetLayer(0)
assert lyr is not None
sr = lyr.GetSpatialRef()
assert sr is not None, 'did not get expected srs'
txt = sr.ExportToWkt()
assert txt.find('GEOGCS[\"GCS_WGS_1984') != -1, 'did not get expected srs'
###############################################################################
# Test GetExtent()
def test_ogr_db2_GetExtent():
if ogrtest.db2_drv is None:
pytest.skip()
ds = ogr.Open(ogrtest.db2_test_server)
assert ds is not None
lyr = ds.GetLayer(0)
assert lyr is not None
extent = lyr.GetExtent()
assert extent is not None, 'did not get extent'
assert extent == (-122.030745, -121.95672, 37.278665, 37.440885), \
'did not get expected extent'
###############################################################################
# Test GetFeature()
def test_ogr_db2_GetFeature():
if ogrtest.db2_drv is None:
pytest.skip()
ds = ogr.Open(ogrtest.db2_test_server)
assert ds is not None
lyr = ds.GetLayer(0)
assert lyr is not None
feat = lyr.GetFeature(5)
assert feat is not None, 'did not get a feature'
if feat.GetField('ZIP') != '95008':
feat.DumpReadable()
pytest.fail('did not get expected feature')
###############################################################################
# Test SetSpatialFilter()
def test_ogr_db2_SetSpatialFilter():
if ogrtest.db2_drv is None:
pytest.skip()
ds = ogr.Open(ogrtest.db2_test_server)
assert ds is not None
lyr = ds.GetLayer(0)
assert lyr is not None
# set a query envelope so we only get one feature
lyr.SetSpatialFilterRect(-122.02, 37.42, -122.01, 37.43)
count = lyr.GetFeatureCount()
assert count == 1, 'did not get expected feature count (1)'
feat = lyr.GetNextFeature()
assert feat is not None, 'did not get a feature'
if feat.GetField('ZIP') != '94089':
feat.DumpReadable()
pytest.fail('did not get expected feature')
# start over with a larger envelope to get 3 out of 5 of the points
lyr.ResetReading()
lyr.SetSpatialFilterRect(-122.04, 37.30, -121.80, 37.43)
count = lyr.GetFeatureCount()
assert count == 3, 'did not get expected feature count (3)'
# iterate through the features to make sure we get the same count
count = 0
feat = lyr.GetNextFeature()
while feat is not None:
count = count + 1
feat = lyr.GetNextFeature()
assert count == 3, 'did not get expected feature count (3)'
#
# test what capabilities the DB2 driver provides
#
def test_ogr_db2_capabilities():
if ogrtest.db2_drv is None:
pytest.skip()
ds = ogr.Open(ogrtest.db2_test_server)
assert ds is not None
layer = ds.GetLayer()
capabilities = [
ogr.OLCRandomRead,
ogr.OLCSequentialWrite,
ogr.OLCRandomWrite,
ogr.OLCFastSpatialFilter,
ogr.OLCFastFeatureCount,
ogr.OLCFastGetExtent,
ogr.OLCCreateField,
ogr.OLCDeleteField,
ogr.OLCReorderFields,
ogr.OLCAlterFieldDefn,
ogr.OLCTransactions,
ogr.OLCDeleteFeature,
ogr.OLCFastSetNextByIndex,
ogr.OLCStringsAsUTF8,
ogr.OLCIgnoreFields
]
print("Layer Capabilities:")
for cap in capabilities:
print(" %s = %s" % (cap, layer.TestCapability(cap)))
def ogr_db2_listdrivers():
cnt = ogr.GetDriverCount()
formatsList = [] # Empty List
for i in range(cnt):
driver = ogr.GetDriver(i)
driverName = driver.GetName()
# print driverName
if driverName not in formatsList:
formatsList.append(driverName)
formatsList.sort() # Sorting the messy list of ogr drivers
for i in formatsList:
print(i)