"Adapter does not support geometry" 声明几何字段时出现异常

"Adapter does not support geometry" exception when declaring geometry field

在我的应用程序中,尝试创建 "geometry()" 类型的字段时抛出 "Adapter does not support geometry" 异常。对于我的测试应用程序,我使用的是 sqlite 数据库(生产环境将使用 postgres):

db = DAL('sqlite://storage.sqlite', pool_size = 1, fake_migrate_all= False)

有问题的数据库 table 在模块内的 class 中声明,并包含几个字段,其中一些包含位置数据:

from gluon.dal import Field, geoPoint, geoLine, geoPolygon

class Info(Base_Model):

    def __init__(...):

        try: 
            db.define_table('t_info', 
                ...
                Field('f_geolocation', type='geometry()', 
                    label =  current.T('Geolocation')),
                Field('f_city', type='string', 
                    label = current.T('City')),
                ...
        except Exception as e:
            ...

编辑:

根据 Anthony 的建议,我已将 DAL 构造函数调用修改为以下内容:

db = DAL('spatialite://storage.sqlite', pool_size = 1)

它产生以下错误消息:

Traceback (most recent call last):
  File "C:\...\web2py\gluon\restricted.py", line 227, in restricted
    exec ccode in environment
  File "C:/My_Stuff/Programs/web2py/applications/Proj/models/db.py", line 38, in <module>
    db = DAL('spatialite://storage.sqlite', pool_size = 1) 
  File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 171, in __call__
    obj = super(MetaDAL, cls).__call__(*args, **kwargs)
  File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 457, in __init__
    raise RuntimeError("Failure to connect, tried %d times:\n%s" % (attempts, tb))
RuntimeError: Failure to connect, tried 5 times:
Traceback (most recent call last):
  File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 435, in __init__
    self._adapter = ADAPTERS[self._dbname](**kwargs)
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 53, in __call__
    obj = super(AdapterMeta, cls).__call__(*args, **kwargs)
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\sqlite.py", line 169, in __init__
    if do_connect: self.reconnect()
  File "C:\...\web2py\gluon\packages\dal\pydal\connection.py", line 129, in reconnect
    self.after_connection_hook()
  File "C:\...\web2py\gluon\packages\dal\pydal\connection.py", line 81, in after_connection_hook
    self.after_connection()
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\sqlite.py", line 177, in after_connection
    self.execute(r'SELECT load_extension("%s");' % libspatialite)
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 1326, in execute
    return self.log_execute(*a, **b)
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 1320, in log_execute
    ret = self.cursor.execute(command, *a[1:], **b)
OperationalError: The specified module could not be found.

如果你想在 SQLite 中使用几何字段,你必须使用 spatialite 适配器,它利用了 SQLite 的 SpatialLite 扩展:

db = DAL('spatialite://storage.sqlite', pool_size = 1)

注意,您必须安装 spatialite 才能工作。