root/trunk/autotest/ogr/ogr_sde.py

Revision 14246, 10.2 kB (checked in by hobu, 3 months ago)

test inserting features into a layer with UNKNOWN srs

  • Property svn:keywords set to Id Rev Date Author
Line 
1 #!/usr/bin/env python
2 ###############################################################################
3 # $Id$
4 #
5 # Project:  GDAL/OGR Test Suite
6 # Purpose:  Test OGR ArcSDE driver.
7 # Author:   Howard Butler <hobu.inc@gmail.com>
8 #
9 ###############################################################################
10 # Copyright (c) 2008, Howard Butler <hobu.inc@gmail.com>
11 #
12 #  Permission is hereby granted, free of charge, to any person obtaining a
13 #  copy of this software and associated documentation files (the "Software"),
14 #  to deal in the Software without restriction, including without limitation
15 #  the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 #  and/or sell copies of the Software, and to permit persons to whom the
17 #  Software is furnished to do so, subject to the following conditions:
18 #
19 #  The above copyright notice and this permission notice shall be included
20 #  in all copies or substantial portions of the Software.
21 #
22 #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 #  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 #  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 #  DEALINGS IN THE SOFTWARE.
29 ###############################################################################
30
31 import os
32 import sys
33 import string
34
35 sys.path.append( '../pymod' )
36
37 import gdaltest
38 import ogrtest
39
40 try:
41     from osgeo import ogr
42     from osgeo import osr
43     from osgeo import gdal
44 except ImportError:
45     import ogr
46     import osr
47     import gdal
48
49 ###############################################################################
50 # Open ArcSDE datasource.
51
52 sde_server = '172.16.1.193'
53 sde_port = '5151'
54 sde_db = 'sde'
55 sde_user = 'sde'
56 sde_password = 'sde'
57
58 gdaltest.sde_dr = None
59 try:
60     gdaltest.sde_dr = ogr.GetDriverByName( 'SDE' )
61 except:
62     pass
63 def ogr_sde_1():
64     "Test basic opening of a database"
65
66
67     if gdaltest.sde_dr is None:
68         return 'skip'
69        
70     base = 'SDE:%s,%s,%s,%s,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password)
71     ds = ogr.Open(base)
72     ds.Destroy()
73
74     ds = ogr.Open(base, update=1)
75     ds.Destroy()   
76
77     return 'success'
78
79 def ogr_sde_2():
80     "Test creation of a layer"
81     if gdaltest.sde_dr is None:
82         return 'skip'
83     base = 'SDE:%s,%s,%s,%s,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password)
84
85     shp_ds = ogr.Open( 'data/poly.shp' )
86     gdaltest.shp_ds = shp_ds
87     shp_lyr = shp_ds.GetLayer(0)
88    
89     ds = ogr.Open(base, update=1)
90     lyr = ds.CreateLayer( 'SDE.TPOLY' ,geom_type=ogr.wkbPolygon, srs=shp_lyr.GetSpatialRef(),options = [ 'OVERWRITE=YES' ] )
91 #    lyr = ds.CreateLayer( 'SDE.TPOLY' ,geom_type=ogr.wkbPolygon)
92
93     ogrtest.quick_create_layer_def( lyr,
94                                     [ ('AREA', ogr.OFTReal),
95                                       ('EAS_ID', ogr.OFTInteger),
96                                       ('PRFEDEA', ogr.OFTString),
97                                       ('WHEN', ogr.OFTDateTime) ] )
98    
99     #######################################################
100     # Copy in poly.shp
101
102     dst_feat = ogr.Feature( feature_def = lyr.GetLayerDefn() )
103
104
105    
106     feat = shp_lyr.GetNextFeature()
107     gdaltest.poly_feat = []
108    
109     while feat is not None:
110
111         gdaltest.poly_feat.append( feat )
112
113         dst_feat.SetFrom( feat )
114         lyr.CreateFeature( dst_feat )
115
116         feat = shp_lyr.GetNextFeature()
117
118     dst_feat.Destroy()
119     return 'success'
120    
121
122 def ogr_sde_3():
123     "Test basic version locking"
124     if gdaltest.sde_dr is None:
125         return 'skip'
126        
127     base = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT' % (sde_server, sde_port, sde_db, sde_user, sde_password)
128     ds = ogr.Open(base, update=1)
129
130     ds2 = ogr.Open(base, update=1)
131     if ds2 is not None:
132         gdaltest.post_reason('A locked version was able to be opened')
133         return 'fail'
134        
135     ds.Destroy()
136
137     return 'success'
138
139
140 def ogr_sde_4():
141     "Test basic version creation"
142
143
144     if gdaltest.sde_dr is None:
145         return 'skip'
146     version_name = 'TESTING'
147     gdal.SetConfigOption( 'SDE_VERSIONOVERWRITE', 'TRUE' )
148
149     base = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password, version_name)
150     ds = ogr.Open(base, update=1)
151     ds.Destroy()
152    
153     gdal.SetConfigOption( 'SDE_VERSIONOVERWRITE', 'FALSE' )
154
155     base = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password, version_name)
156     ds = ogr.Open(base, update=1)
157     ds.Destroy()
158
159
160     return 'success'
161
162 def ogr_sde_5():
163     "Test versioned editing"
164
165     if gdaltest.sde_dr is None:
166         return 'skip'
167     version_name = 'TESTING'
168     gdal.SetConfigOption( 'SDE_VERSIONOVERWRITE', 'TRUE' )
169
170     base = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password, version_name)
171     ds = ogr.Open(base, update=1)
172
173     l1 = ds.GetLayerByName('SDE.TPOLY')
174
175     f1 = l1.GetFeature(1)
176     f1.SetField("PRFEDEA",'SDE.TESTING')
177     l1.SetFeature(f1)
178    
179     ds.Destroy()
180     del ds
181    
182     default = 'DEFAULT'
183     gdal.SetConfigOption( 'SDE_VERSIONOVERWRITE', 'FALSE' )
184
185     default = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password, default)
186 #    print default
187     ds2 = ogr.Open(default, update=1)
188
189     l2 = ds2.GetLayerByName('SDE.TPOLY')
190
191     f2 = l2.GetFeature(1)
192    
193     f2.SetField("PRFEDEA",'SDE.DEFAULT')
194     f2.SetField("WHEN", 2008, 03, 19, 16, 15, 00, 0)
195
196     l2.SetFeature(f2)
197     ds2.Destroy()
198     del ds2
199    
200     ds3 = ogr.Open(base)
201     l3 = ds3.GetLayerByName('SDE.TPOLY')
202     f3 = l3.GetFeature(1)
203     if f3.GetField("PRFEDEA") != "SDE.TESTING":
204         gdaltest.post_reason('versioned editing failed for child version SDE.TESTING')
205         return 'fail'
206
207
208     ds3.Destroy()
209     del ds3
210
211     ds4 = ogr.Open(default)
212     l4 = ds4.GetLayerByName('SDE.TPOLY')
213     f4 = l4.GetFeature(1)
214     if f4.GetField("PRFEDEA") != "SDE.DEFAULT":
215         gdaltest.post_reason('versioned editing failed for parent version SDE.DEFAULT')
216         return 'fail'
217
218
219     idx = f4.GetFieldIndex('WHEN')
220     df = f4.GetField(idx)
221     if df != '2008/03/19 16:15:00':
222         gdaltest.post_reason("datetime handling did not work -- expected '2008/03/19 16:15:00' got '%s' "% df)
223     ds4.Destroy()
224     del ds4
225     return 'success'
226
227 def ogr_sde_6():
228     "Extent fetching"
229
230     if gdaltest.sde_dr is None:
231         return 'skip'
232
233     base = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT' % (sde_server, sde_port, sde_db, sde_user, sde_password)
234     ds = ogr.Open(base, update=1)
235
236     l1 = ds.GetLayerByName('SDE.TPOLY')
237     extent = l1.GetExtent(force=0)
238     if extent != (0.0, 2147483645.0, 0.0, 2147483645.0):
239         gdaltest.post_reason("unforced extent did not equal expected value")
240        
241
242     extent = l1.GetExtent(force=1)
243     if extent !=     (478316.0, 481645.0, 4762881.0, 4765611.0):
244         gdaltest.post_reason("forced extent did not equal expected value")
245     return 'success'
246
247 def ogr_sde_7():
248     "Bad layer test"
249
250     if gdaltest.sde_dr is None:
251         return 'skip'
252
253     base = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT' % (sde_server, sde_port, sde_db, sde_user, sde_password)
254     ds = ogr.Open(base, update=1)
255
256     l1 = ds.GetLayerByName('SDE.TPOLY2')
257     if l1:
258         gdaltest.post_reason("we got a layer when we shouldn't have")
259
260     ds.Destroy()
261
262     default = 'DEFAULT'
263     gdal.SetConfigOption( 'SDE_VERSIONOVERWRITE', 'FALSE' )
264
265     default = 'SDE:%s,%s,%s,%s,%s,SDE.TPOLY,SDE.DEFAULT,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password, default)
266     ds = ogr.Open(default, update=1)
267
268     l1 = ds.GetLayerByName('SDE.TPOLY2')
269     if l1:
270         gdaltest.post_reason("we got a layer when we shouldn't have")
271     ds.Destroy()
272
273     default = 'DEFAULT'
274     gdal.SetConfigOption( 'SDE_VERSIONOVERWRITE', 'FALSE' )
275
276     default = 'SDE:%s,%s,%s,%s,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password)
277     ds = ogr.Open(default)
278
279     l1 = ds.GetLayerByName('SDE.TPOLY2')
280     if l1:
281         gdaltest.post_reason("we got a layer when we shouldn't have")
282     ds.Destroy()
283
284
285     return 'success'
286
287 def ogr_sde_8():
288     "Test spatial references"
289     if gdaltest.sde_dr is None:
290         return 'skip'
291     base = 'SDE:%s,%s,%s,%s,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password)
292
293     shp_ds = ogr.Open( 'data/poly.shp' )
294     gdaltest.shp_ds = shp_ds
295     shp_lyr = shp_ds.GetLayer(0)
296    
297     ref = osr.SpatialReference()
298     ref.ImportFromWkt('LOCAL_CS["IMAGE"]')
299    
300     ds = ogr.Open(base, update=1)
301     lyr = ds.CreateLayer( 'SDE.TPOLY' ,geom_type=ogr.wkbPolygon, srs=ref,options = [ 'OVERWRITE=YES' ] )
302     ref.ImportFromEPSG(4326)
303     lyr = ds.CreateLayer( 'SDE.TPOLY' ,geom_type=ogr.wkbPolygon, srs=ref,options = [ 'OVERWRITE=YES' ] )
304     ogrtest.quick_create_layer_def( lyr,
305                                     [ ('AREA', ogr.OFTReal),
306                                       ('EAS_ID', ogr.OFTInteger),
307                                       ('PRFEDEA', ogr.OFTString),
308                                       ('WHEN', ogr.OFTDateTime) ] )
309    
310     #######################################################
311     # Copy in poly.shp
312
313     dst_feat = ogr.Feature( feature_def = lyr.GetLayerDefn() )
314
315
316    
317     feat = shp_lyr.GetNextFeature()
318     gdaltest.poly_feat = []
319    
320     while feat is not None:
321
322         gdaltest.poly_feat.append( feat )
323
324         dst_feat.SetFrom( feat )
325         lyr.CreateFeature( dst_feat )
326
327         feat = shp_lyr.GetNextFeature()
328
329     dst_feat.Destroy()
330     return 'success'
331    
332 def ogr_sde_cleanup():
333     if gdaltest.sde_dr is None:
334         return 'skip'
335     base = 'SDE:%s,%s,%s,%s,%s' % (sde_server, sde_port, sde_db, sde_user, sde_password)
336     ds = ogr.Open(base, update=1)
337     ds.DeleteLayer('%s.%s'%(sde_user.upper(),'TPOLY'))
338     ds.Destroy()   
339
340
341     return 'success'
342
343 gdaltest_list = [
344     ogr_sde_1,
345     ogr_sde_2,
346     ogr_sde_3,
347     ogr_sde_4,
348     ogr_sde_5,
349     ogr_sde_6,
350     ogr_sde_7,
351     ogr_sde_8,
352    
353     ogr_sde_cleanup
354 ]
355
356 if __name__ == '__main__':
357
358     gdaltest.setup_run( 'ogr_sde' )
359
360     gdaltest.run_tests( gdaltest_list )
361
362     gdaltest.summarize()
363
Note: See TracBrowser for help on using the browser.