database module
sqlalchemy interface to PySDB database
PySDB database is a simple sqlite3-based relational database to store structural data from the field. You can use apsg to manipulate the data or you can use the GUI application pysdb. There is also the QGIS plugin readsdb to plot data on a map or use map-based select to plot stereonets.
The following snippet demonstrate how to create database programmatically
>>> # Create database
>>> from apsg.database import SDBSession
>>> db = SDBSession('database.sdb', create=True)
>>> # Create unit
>>> unit = db.unit(name='DMU', description='Deamonic Magmatic Unit')
>>> # Create site
>>> site = db.site(unit=unit, name='LX001', x_coord=25934.36, y_coord=564122.5, description='diorite dyke')
>>> # Create structural types
>>> S2 = db.structype(structure='S2', description='Solid-state foliation', planar=1)
>>> L2 = db.structype(structure='L2', description='Solid-state lineation', planar=0)
>>> # Add measurement
>>> fol = db.add_structdata(site=site, structype=S2, azimuth=150, inclination=36)
>>> # Close database
>>> db.close()
You can tag individual data
>>> db = SDBSession('database.sdb')
>>> site = db.site(name='LX001')
>>> struct = db.structype(structure='S2')
>>> tag_plot = db.tag(name='plot')
>>> tag_ap = db.tag(name='AP')
>>> fol = db.add_structdata(site=site, structype=struct, azimuth=324, inclination=78, tags=[tag_plot, tag_ap])
>>> db.close()
or you can attach linear and planar features (e.g. fault data)
>>> db = SDBSession('database.sdb')
>>> unit = db.unit(name='DMU')
>>> site = db.site(name='LX001')
>>> S = db.structype(structure='S')
>>> L = db.structype(structure='L')
>>> fol = db.add_structdata(site=site, structype=S, azimuth=220, inclination=28)
>>> lin = db.add_structdata(site=site, structype=L, azimuth=212, inclination=26)
>>> pair = db.attach(fol, lin)
>>> db.close()
You can open existing database and select existing site and type of structure
>>> db = SDBSession('database.sdb')
>>> site = db.site(name='LX003')
>>> S2 = db.structype(structure='S2')
>>> L2 = db.structype(structure='L2')
and insert Foliation
, Lineation
or Pair
directly
>>> f = fol(196, 39)
>>> l = lin(210, 37)
>>> db.add_fol(f, site=site, structype=S2)
>>> db.add_lin(l, site=site, structype=L2)
>>> p = Pair(258, 42, 220, 30)
>>> db.add_pair(p, S2, L2, site=site)
>>> db.close()
To retrieve data as FeatureSet
you can use getset
method:
>>> db = SDBSession('database.sdb')
>>> S2 = db.structype(structure='S2')
>>> g = db.getset(structype=S2)
or directly
>>> g = db.getset('S2')
Classes:
|
SqlAlchemy interface to PySDB database |
|
|
|
|
|
|
|
|
|
|
|
|
|
sqlite3 based read-only interface to PySDB database |
- class apsg.database.SDBSession(sdb_file, **kwargs)
Bases:
object
SqlAlchemy interface to PySDB database
- Parameters
sdbfile (str) – filename of PySDB database
- Keyword Arguments
create (bool) – if True existing sdbfile will be deleted and new database will be created
autocommit (bool) – if True, each operation is autocommitted
Example
>>> db = SDBSession('database.sdb', create=True)
- close()
Close session
- commit()
commit session
- rollback()
rollback session
- meta(name, **kwargs)
Insert, update or retrieve (when kwargs empty) Meta
- Parameters
name (str) – meta name
- Keyword Arguments
value (str) – meta value
- Returns
Meta
- site(name, **kwargs)
Insert, update or retrieve (when kwargs empty) Site
- Parameters
name (str) – site name
- Keyword Arguments
x_coord (float) – x coord or longitude
y_coord (float) – y coord or latitude
description (str) – site description
unit (Unit) – unit instance (mus be provided)
- Returns
Site
- unit(name, **kwargs)
Insert, update or retrieve (when kwargs empty) Unit
- Parameters
name (str) – unit name
- Keyword Arguments
description (str) – unit description
- Returns
Unit
- tag(name, **kwargs)
Insert, update or retrieve (when kwargs empty) Tag
- Parameters
name (str) – tag name
- Keyword Arguments
description (str) – tag description
- Returns
Tag
- structype(structure, **kwargs)
Insert, update or retrieve (when kwargs empty) Structype
- Parameters
structure (str) – label for structure
- Keyword Arguments
description (str) – structype description
planar (int) – 1 for planar 0 for linear
structcode (int) – structcode (optional)
groupcode (int) – groupcode (optional)
- Returns
Structype
- add_structdata(site, structype, azimuth, inclination, **kwargs)
Add structdata to site
- add_fol(site, structype, fol, **kwargs)
Add Foliation to site
- add_lin(site, structype, lin, **kwargs)
Add Lineation to site
- attach(fol, lin)
Add Lineation to site
- add_pair(pair, foltype, lintype, **kwargs)
Add attached foliation and lineation to database
- sites(**kwargs)
Retrieve Site or list of Sites based on criteria in kwargs
Keyword arguments are passed to sqlalchemy filter_by method
- units(**kwargs)
Retrieve Unit or list of Units based on criteria in kwargs
Keyword arguments are passed to sqlalchemy filter_by method
- structypes(**kwargs)
Retrieve Structype or list of Structypes based on criteria in kwargs
Keyword arguments are passed to sqlalchemy filter_by method
- tags(**kwargs)
Retrieve Tag or list of Tags based on criteria in kwargs
Keyword arguments are passed to sqlalchemy filter_by method
- class apsg.database.Meta(*args: Any, **kwargs: Any)
Bases:
declarative_base
- class apsg.database.Site(*args: Any, **kwargs: Any)
Bases:
declarative_base
- class apsg.database.Structdata(*args: Any, **kwargs: Any)
Bases:
declarative_base
- class apsg.database.Structype(*args: Any, **kwargs: Any)
Bases:
declarative_base
- class apsg.database.Tag(*args: Any, **kwargs: Any)
Bases:
declarative_base
- class apsg.database.Unit(*args: Any, **kwargs: Any)
Bases:
declarative_base
- class apsg.database.SDB(sdb_file)
Bases:
object
sqlite3 based read-only interface to PySDB database
- Parameters
sdbfile (str) – filename of PySDB database
Example
>>> db = SDB('database.sdb')
- structures(**kwargs)
Return list of structures in database.
For kwargs see getset method
- sites(**kwargs)
Return list of sites in database.
For kwargs see getset method.
- units(**kwargs)
Return list of units in database.
For kwargs see getset method.
- tags(**kwargs)
Return list of tags in database.
For kwargs see getset method.
- getset(structs, **kwargs)
Method to retrieve data from SDB database to
FeatureSet
.- Parameters
structs (str) – structure or list of structures to retrieve
- Keyword Arguments
sites (str) – name or list of names of sites to retrieve from
units (str) – name or list of names of units to retrieve from
tags (str) – tag or list of tags to retrieve
labels (bool) – if True return also list of sites. Default False