forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogr_couchdb.py
executable file
·139 lines (106 loc) · 4.55 KB
/
ogr_couchdb.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
#!/usr/bin/env pytest
# -*- coding: utf-8 -*-
###############################################################################
# $Id$
#
# Project: GDAL/OGR Test Suite
# Purpose: CouchDB driver testing.
# Author: Even Rouault <even dot rouault at spatialys.com>
#
###############################################################################
# Copyright (c) 2011-2014, Even Rouault <even dot rouault at spatialys.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
import os
import uuid
import gdaltest
import ogrtest
from osgeo import gdal
from osgeo import ogr
import pytest
###############################################################################
# Test if driver is available
def test_ogr_couchdb_init():
ogrtest.couchdb_drv = None
ogrtest.couchdb_drv = ogr.GetDriverByName('CouchDB')
if ogrtest.couchdb_drv is None:
pytest.skip()
if 'COUCHDB_TEST_SERVER' in os.environ:
ogrtest.couchdb_test_server = os.environ['COUCHDB_TEST_SERVER']
else:
ogrtest.couchdb_test_server = 'http://127.0.0.1:5984'
ogrtest.couchdb_temp_layer_name = 'layer_' + str(uuid.uuid1()).replace('-', '_')
if gdaltest.gdalurlopen(ogrtest.couchdb_test_server) is None:
ogrtest.couchdb_drv = None
pytest.skip('cannot open %s' % ogrtest.couchdb_test_server)
###############################################################################
# Basic test
def test_ogr_couchdb_1():
if ogrtest.couchdb_drv is None:
pytest.skip()
gdal.VectorTranslate('CouchDB:' + ogrtest.couchdb_test_server, 'data/poly.shp',
format='CouchDB',
layerName=ogrtest.couchdb_temp_layer_name,
layerCreationOptions=['UPDATE_PERMISSIONS=ALL'])
ds = ogr.Open('couchdb:%s' % ogrtest.couchdb_test_server, update=1)
assert ds is not None
lyr = ds.GetLayerByName(ogrtest.couchdb_temp_layer_name)
f = lyr.GetNextFeature()
if f['AREA'] != 215229.266 or f['EAS_ID'] != '168' or f.GetGeometryRef() is None:
f.DumpReadable()
pytest.fail()
ds.ExecuteSQL('DELLAYER:' + ogrtest.couchdb_temp_layer_name)
###############################################################################
# Test null / unset
def test_ogr_couchdb_2():
if ogrtest.couchdb_drv is None:
pytest.skip()
ds = ogr.Open('couchdb:%s' % ogrtest.couchdb_test_server, update=1)
assert ds is not None
lyr = ds.CreateLayer(ogrtest.couchdb_temp_layer_name, geom_type=ogr.wkbNone, options=['UPDATE_PERMISSIONS=ALL'])
lyr.CreateField(ogr.FieldDefn('str_field', ogr.OFTString))
f = ogr.Feature(lyr.GetLayerDefn())
f['str_field'] = 'foo'
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFieldNull('str_field')
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
lyr.CreateFeature(f)
f = lyr.GetNextFeature()
if f['str_field'] != 'foo':
f.DumpReadable()
pytest.fail()
f = lyr.GetNextFeature()
if f['str_field'] is not None:
f.DumpReadable()
pytest.fail()
f = lyr.GetNextFeature()
if f.IsFieldSet('str_field'):
f.DumpReadable()
pytest.fail()
###############################################################################
# Cleanup
def test_ogr_couchdb_cleanup():
if ogrtest.couchdb_drv is None:
pytest.skip()
ds = ogr.Open('couchdb:%s' % ogrtest.couchdb_test_server, update=1)
assert ds is not None
ds.ExecuteSQL('DELLAYER:' + ogrtest.couchdb_temp_layer_name)