SQLiteStore#
- class SQLiteStore(connection=':memory:', compression='zlib', compression_level=9, auto_commit=True)[source]#
SQLite backed annotation store.
Uses and rtree index for fast spatial queries.
Version History: 1.0.0:
Initial version.
- 1.0.1 (07/10/2022):
Added optional “area” column and queries sorted/filtered by area.
Methods
Add a column to store the area of the geometry.
append_many
bquery
Remove all annotations from the store.
close
commit
Get the list of options that sqlite3 was compiled with.
Create an SQLite expression index based on the provided predicate.
Deserialize a geometry from a string or bytes.
Drop an index from the store.
dump
dumps
features
Returns a list of the names of all indexes in the store.
iquery
items
keys
open
Optimize the database with VACUUM and ANALYZE.
patch_many
Query the store for annotation properties.
query
Remove the area column from the store.
remove_many
Serialise a geometry to WKB with optional compression.
to_dataframe
values
- Parameters:
- static compile_options()[source]#
Get the list of options that sqlite3 was compiled with.
Example
>>> for opt in SQLiteRTreeStore.compile_options(): >>> print(opt) COMPILER=gcc-7.5.0 ENABLE_COLUMN_METADATA ENABLE_DBSTAT_VTAB ENABLE_FTS3 ENABLE_FTS3_PARENTHESIS ENABLE_FTS3_TOKENIZER ENABLE_FTS4 ENABLE_FTS5 ENABLE_JSON1 ENABLE_LOAD_EXTENSION ENABLE_PREUPDATE_HOOK ENABLE_RTREE ENABLE_SESSION ENABLE_STMTVTAB ENABLE_UNLOCK_NOTIFY ENABLE_UPDATE_DELETE_LIMIT HAVE_ISNAN LIKE_DOESNT_MATCH_BLOBS MAX_SCHEMA_RETRY=25 MAX_VARIABLE_NUMBER=250000 OMIT_LOOKASIDE SECURE_DELETE SOUNDEX TEMP_STORE=1 THREADSAFE=1
- create_index(name, where, analyze=True)[source]#
Create an SQLite expression index based on the provided predicate.
Note that an expression index will only be used if the query expression (in the WHERE clause) exactly matches the expression used when creating the index (excluding minor inconsequential changes such as whitespace).
SQLite expression indexes require SQLite version 3.9.0 or higher.
- drop_index(name)[source]#
Drop an index from the store.
- Parameters:
name (str) – The name of the index to drop.
- Return type:
None
- indexes()[source]#
Returns a list of the names of all indexes in the store.
- Returns:
The list of index names.
- Return type:
List[str]
- optimize(vacuum=True, limit=1000)[source]#
Optimize the database with VACUUM and ANALYZE.
- Parameters:
vacuum (bool) – Whether to run VACUUM.
limit (int) – The approximate maximum number of rows to examine when running ANALYZE. If zero or negative, not limit will be used. For more information see https://www.sqlite.org/pragma.html#pragma_analysis_limit.
- Return type:
None
- pquery(select, geometry=None, where=None, geometry_predicate='intersects', unique=True, squeeze=True)[source]#
Query the store for annotation properties.
Acts similarly to AnnotationStore.query but returns only the value defined by select.
- Parameters:
select (str or bytes or Callable) – A statement defining the value to look up from the annotation properties. If select = “*”, all properties are returned for each annotation (unique must be False).
geometry (Geometry or Iterable) – Geometry to use when querying. This can be a bounds (iterable of length 4) or a Shapely geometry (e.g. Polygon). If a geometry is provided, the bounds of the geometry will be used for the query. Full geometry intersection is not used for the query method.
where (str or bytes or Callable) – A statement which should evaluate to a boolean value. Only annotations for which this predicate is true will be returned. Defaults to None (assume always true). This may be a string, callable, or pickled function as bytes. Callables are called to filter each result returned the from annotation store backend in python before being returned to the user. A pickle object is, where possible, hooked into the backend as a user defined function to filter results during the backend query. Strings are expected to be in a domain specific language and are converted to SQL on a best-effort basis. For supported operators of the DSL see
tiatoolbox.annotation.dsl
. E.g. a simple python expression props[“class”] == 42 will be converted to a valid SQLite predicate when using SQLiteStore and inserted into the SQL query. This should be faster than filtering in python after or during the query. It is important to note that untrusted user input should never be accepted to this argument as arbitrary code can be run via pickle or the parsing of the string statement.geometry_predicate (str) – A string defining which binary geometry predicate to use when comparing the query geometry and a geometry in the store. Only annotations for which this binary predicate is true will be returned. Defaults to “intersects”. For more information see the shapely documentation on binary predicates.
unique (bool) – If True, only unique values for each selected property will be returned as a list of sets. If False, all values will be returned as a dictionary mapping keys values. Defaults to True.
squeeze (bool) – If True, when querying for a single value with unique=True, the result will be a single set instead of a list of sets.
- Return type:
Examples
>>> from tiatoolbox.annotation.storage import AnnotationStore >>> from shapely.geometry import Point >>> store = AnnotationStore() >>> annotation = Annotation( ... geometry=Point(0, 0), ... properties={"class": 42}, ... ) >>> store.add(annotation, "foo") >>> store.pquery("*", unique=False) ... {'foo': {'class': 42}}
>>> from tiatoolbox.annotation.storage import AnnotationStore >>> from shapely.geometry import Point >>> store = AnnotationStore() >>> annotation = Annotation( ... geometry=Point(0, 0), ... properties={"class": 42}, ... ) >>> store.add(annotation, "foo") >>> store.pquery("props['class']") ... {42} >>> annotation = Annotation(Point(1, 1), {"class": 123}) >>> store.add(annotation, "foo") >>> store.pquery("props['class']") ... {42, 123}