Hi Michael,
I never used spatialite. But from looking at your example code, you seem to
create a “simple sqlite” connection, whereas somehow this extension needs to be
loaded. From what a quick google search revealed, there seems not to exist some
special connection string, that does the magic to load this extension.
Looking here:
https://datasette.readthedocs.io/en/stable/spatialite.html#spatial-indexing-latitude-longitude-columns
it looks like you’re missing
conn.enable_load_extension(True)
conn.load_extension('/usr/local/lib/mod_spatialite.dylib')
which I would guess belongs into your `get_tm_session` function. Try debugging
into that and see if it works on `dbsession` or some property on it.
Hope that helps, Andi
From: <pylons-discuss@googlegroups.com> on behalf of Michael Lane
<michael.lane.f...@gmail.com>
Reply-To: <pylons-discuss@googlegroups.com>
Date: Tuesday, 9. July 2019 at 23:01
To: pylons-discuss <pylons-discuss@googlegroups.com>
Subject: [pylons-discuss] How to integrate spatialite in pyramid?
I would like to use spatialite in my project but I don't find the right way to
do that. Does anybody have already done that and can give me an example?
my environment:
Windows 10:
Python version: 3.6.8
Local database: sqlite (for spatialite)
Remote database: postgresql
I find a lot of exemples where spatialite is set in the module parameter for
the engine in create_engine using lib like libsqlite3, mod_spatialite,
pysqlite3 in my case I use spatialiteinstall with pip (package define in
setup.py)
My problem is then I don't have the control of create_engine, in the engine
comming from settings in ini so I think that I have to pass the setting by the
ini file but I did'nt get it. I didn't find any good example of code using same
combination environment as mine (windows, pyramid, spatialite).
There are the settings of ini file:
pyramid.reload_templates = true
pyramid.reload_assets = true
pyramid.debug_authorization = true
pyramid.debug_notfound = false
pyramid.debug_routematch = true
pyramid.default_locale_name = en
pyramid.includes =
pyramid_debugtoolbar
pyramid_tm
sqlalchemy.url = sqlite:///%(here)s/risc.sqlite
Code where the engine is create:
from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import configure_mappers
import zope.sqlalchemy
# run configure_mappers after defining all of the models to ensure
# all relationships can be setup
configure_mappers()
def get_engine(settings, prefix='sqlalchemy.'):
return engine_from_config(settings, prefix)
def get_session_factory(engine):
factory = sessionmaker()
factory.configure(bind=engine)
return factory
def get_tm_session(session_factory, transaction_manager):
dbsession = session_factory()
zope.sqlalchemy.register(dbsession, transaction_manager=transaction_manager)
return dbsession
def includeme(config):
settings = config.get_settings()
settings['tm.manager_hook'] = 'pyramid_tm.explicit_manager'
# use pyramid_tm to hook the transaction lifecycle to the request
config.include('pyramid_tm')
# use pyramid_retry to retry a request when transient exceptions occur
config.include('pyramid_retry')
session_factory = get_session_factory(get_engine(settings))
config.registry['dbsession_factory'] = session_factory
# make request.dbsession available for use in Pyramid
config.add_request_method(
# r.tm is the transaction manager used by pyramid_tm
lambda r: get_tm_session(session_factory, r.tm),
'dbsession',
reify=True
)
And the settings module of setup.py
requires = [
'bcrypt',
'docutils',
'plaster_pastedeploy',
'pyramid',
'pyramid_debugtoolbar',
'pyramid_retry',
'pyramid_tm',
'sqlalchemy',
'transaction',
'zope.sqlalchemy',
'waitress',
'sphinx',
'flake8',
'pyLint',
'pg8000',
'geoalchemy2',
'spatialite'
]
I'm pretty sure than I miss a little something, but I'm not able to find it my
way. I little hint will be appreciated on this one.
Thanks
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/pylons-discuss/15188080-e8eb-4e72-bf32-14dadf302909%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/pylons-discuss/80C70D79-1985-414F-99B6-7278E1C0DE3F%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.