Class TLcdSpatiaLiteModelDecoder
- All Implemented Interfaces:
ILcdModelDecoder
Input files
| File | Required | Entry point | Description |
|---|---|---|---|
| *.spl | x | Java properties file containing all the necessary properties to access the spatial data |
Alternatively, a Properties object can be constructed
programmatically and passed to the model decoder.
The common properties needed to establish a connection are:
driver(default =org.sqlite.JDBC): the JDBC driver that will be used to connect to the database.url: the database URL, absolute or relative to the location of the properties file.
The properties that identify the tables, columns, rows, etc. within the database are:
table: the name of the table containing the geometries. The table name may be qualified by prepending the database schema. It may be followed by one or more spaces and an alias, which can be used instead of the original name to disambiguate columns in the other properties.spatialColumn: the column containing the geometries.spatialQueryColumn: an optional column containing the geometries that should be used in spatial queries. By default, this column is equal to thespatialColumn. It should not be changed for most typical applications.featureNames.0, ...: the column names that should be retrieved as object features in the model. For read-only models, more complex SQL expressions, or columns from selection tables (see below) are accepted too.featureDisplayNames.0, ...: optional display names corresponding to the feature names.primaryFeatureIndex(default = 0): the index of the feature that serves as a primary key.primaryFeatureAutoGenerated(default =false): specifies whether the database automatically generates the primary key whenever a geometry is inserted into the spatial table. The primary feature on the java side will then be updated with this generated value.selectionTables(default =null): an optional comma-separated list of tables required to constrain the geometries of the model. These tables will be added after the main table in the FROM parts of SELECT queries. A name may be followed by one or more spaces and an alias, which can be used instead of the original name to disambiguate columns in the other properties.selectionFilter(default =null): an optional SQL select query to constrain the geometries of the model. This query will be added after the spatial query in the WHERE parts of SELECT queries.
If a schema name, table name, or spatial column name is surrounded by double quotes (") then the quotes are stripped and the remaining string is passed unmodified to SQLite. If a name is not quoted, then it is converted to lower case before being passed on to the database. See the SQLite documentation for a more detailed explanation. A schema name may be quoted separately from the actual table name.
Supported file transfer protocols
- This model decoder supports all transfer protocols that are supported by java.io.File.
Model structure
- This model decoder creates a model per spatial table.
- The models returned by this decoder are instances of
TLcdDatabaseModelorTLcdDatabaseReadOnlyModel, depending on whether this decoder creates read-only models or not. - All models returned by this model decoder implement
ILcd2DBoundsIndexedModel.
Model descriptor
- The model descriptor of a decoded model is a
TLcdSpatiaLiteModelDescriptor.
sourceName(default = the source name that is passed to the model decoder): the source name of the model descriptor.typeName(default = the display name of the model decoder): the type name of the model descriptor. The type name is also used when composing the name of the data model.displayName(default = the spatial table name): the display name of the model descriptor. The display name is also used when composing the name of the data model and the name of the data type.
Model reference
By default, the model reference of a decoded model is determined by the EPSG code or the WKT reference of the spatial table. If no model reference can be created the default model reference set on this decoder will be used (seesetDefaultModelReference(com.luciad.model.ILcdModelReference)).
Model elements
- A decoded database model contains
ILcdShapeobjects, decoded from the Well-Known Binary representation that SpatiaLite provides:FROM WKB GEOMETRYTO ILcdShapePOINTILcdShapeListwith anILcd2DEditablePointobjectLINESTRINGILcdShapeListwith anILcd2DEditablePolylineobjectPOLYGONILcdShapeListwith anILcdComplexPolygonobjectMULTIPOINTILcdShapeListwith anILcd2DEditablePolypointobjectMULTILINESTRINGILcdShapeListwithILcd2DEditablePolylineobjectsMULTIPOLYGONILcdShapeListwithILcdComplexPolygonobjectsGEOMETRYCOLLECTIONILcdShapeListwith objects from this table (also recursive) - The elements also implement the
ILcdFeaturedinterface and theILcdDataObjectinterface, providing access to their features. - Instead of static typing, sqlite uses manifest typing, i.e. the datatype is a property of the value itself, not a column property. Sqlite allows to store any datatype in any column. As a result all the element's properties are of type TLcdCoreDataTypes.OBJECT_TYPE.
Useful settings
- The decoder property
readOnlycan be set to specify whether the model will be used as a read-only model.
Sample code
Decoding SQLite data and visualizing it on a Lightspeed map:
//First create the model
ILcdModelDecoder decoder =
new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));
ILcdModel model = decoder.decode("Data/Sqlite/states.spl");
//Create a layer for the model with default styling
ILspLayer layer = TLspShapeLayerBuilder.newBuilder()
.model(model)
.build();
//Add the layer to the Lightspeed view (an ILspView)
view.addLayer(layer);
Decoding SQLite data and visualizing it on a GXY map:
//First create the model
ILcdModelDecoder decoder =
new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));
ILcdModel model = decoder.decode("Data/Sqlite/states.spl");
//Create a layer for the model with default styling
ILcdGXYLayer layer = TLcdGXYLayer.create(model);
//Wrap the layer with an async layer wrapper to ensure
//that the view remains responsive while data is being painted
layer = ILcdGXYAsynchronousLayerWrapper.create(layer);
//Add the async layer to the GXY view (an ILcdGXYView)
view.addGXYLayer(layer);
An example of the states.spl file is available in the samples.
Performance tips
Some optional properties in the properties file or object can help to tune the database access:alwaysCloseConnection(default =false): an optional boolean to specify that the database connection should be closed after each query.maxCacheSize(default = 100, or the value set on the decoder): the maximum number of objects in the cache. Using a cache will improve performance if transferring and decoding geometries and their features is relatively expensive compared to performing the original SQL queries. The current implementation of the decoder will then perform queries in two passes, first retrieving the relevant primary keys and then retrieving and decoding the actual geometries and features that are not in the cache. On the other hand, setting the cache size to 0 may be better if performing SQL queries is relatively expensive. The current implementation of the decoder will then perform queries in a single pass, retrieving and decoding the relevant geometries every time.
Thread safety
- The decoding of models is thread-safe, as long as no properties are changed during the decoding.
- The decoded models are thread-safe for read access.
Supported versions and specifications
- SQLite SpatiaLite files, up to version 3.x of SpatiaLite.
Known limitations
- Some rare EPSG and WKT model references are not supported.
- Updating database views and data sets involving additional tables is generally not possible.
- The decoder does not respect the user's schema search path when decoding some GIS-specific metadata (srid and geometry type). If you have tables/columns with identical names but under different schemas it is therefore possible that a wrong model reference might be used. To avoid this problem it is recommended that you specify the table name as a qualified name (including the schema).
- Since:
- 11.0
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new, read-write SQLite SpatiaLite model decoder.TLcdSpatiaLiteModelDecoder(boolean aReadOnly) Creates a new SQLite SpatiaLite model decoder. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanDecodeSource(String aSourceAsString) Checks whether this model decoder can decode the specified data source.Decodes a new database model based on the properties specified in the given file.decode(Properties aProperties) Decodes a new database model based on the given properties.intReturn the default maximum size of the cache, expressed in number of elements (rows|geometries).Returns the default model reference to be assigned to decoded models.Returns a short, displayable name for the format that is decoded by thisILcdModelDecoder.booleanvoidsetDefaultMaxCacheSize(int aDefaultMaxCacheSize) Sets the default maximum size of the cache.voidsetDefaultModelReference(ILcdModelReference aDefaultModelReference) Sets the default model reference to be assigned to decoded models if no model reference can be created based on information from the database.voidsetReadOnly(boolean aReadOnly) Specifies whether the decoded models should be read-only or not.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.luciad.model.ILcdModelDecoder
canDecodeSource, decodeModelMetadata, decodeModelMetadata, decodeSource, discoverDataSources
-
Constructor Details
-
TLcdSpatiaLiteModelDecoder
public TLcdSpatiaLiteModelDecoder()Creates a new, read-write SQLite SpatiaLite model decoder. -
TLcdSpatiaLiteModelDecoder
public TLcdSpatiaLiteModelDecoder(boolean aReadOnly) Creates a new SQLite SpatiaLite model decoder.- Parameters:
aReadOnly- specifies the type of models the decoder should construct.
-
-
Method Details
-
setDefaultModelReference
Sets the default model reference to be assigned to decoded models if no model reference can be created based on information from the database.- Parameters:
aDefaultModelReference- the default model reference.- See Also:
-
getDefaultModelReference
Returns the default model reference to be assigned to decoded models.- Returns:
- the default model reference.
- See Also:
-
setDefaultMaxCacheSize
public void setDefaultMaxCacheSize(int aDefaultMaxCacheSize) Sets the default maximum size of the cache. It is possible to override this value by means of themaxCacheSizeproperty in the properties file.- Parameters:
aDefaultMaxCacheSize- the default maximum number of objects in the cache.
-
getDefaultMaxCacheSize
public int getDefaultMaxCacheSize()Return the default maximum size of the cache, expressed in number of elements (rows|geometries). -
setReadOnly
public void setReadOnly(boolean aReadOnly) Specifies whether the decoded models should be read-only or not.- Parameters:
aReadOnly- specifies the type of models the decoder should construct. Iftrue, the decoder will createTLcdDatabaseReadOnlyModelobjects. Iffalse, the decoder will createTLcdDatabaseModelobjects.
-
getReadOnly
public boolean getReadOnly()- See Also:
-
getDisplayName
Description copied from interface:ILcdModelDecoderReturns a short, displayable name for the format that is decoded by thisILcdModelDecoder.- Specified by:
getDisplayNamein interfaceILcdModelDecoder- Returns:
- the displayable name of this
ILcdModelDecoder.
-
canDecodeSource
Description copied from interface:ILcdModelDecoderChecks whether this model decoder can decode the specified data source. It is acceptable for this method to returntruefor a source name whiledecodethrows an exception for that same source name.For performance reasons, we strongly recommend that this will only be a simple test. For example: check the file extension of a file, but not that the file exists or contains expected content.
- Specified by:
canDecodeSourcein interfaceILcdModelDecoder- Parameters:
aSourceAsString- the data source to be verified; typically a file name or a URL.- Returns:
trueif this decoder can likely decode the data specified by the source name,falseotherwise.- See Also:
-
decode
Decodes a new database model based on the properties specified in the given file.- Specified by:
decodein interfaceILcdModelDecoder- Parameters:
aSourceName- the data source to be decoded; typically a file name or a URL.- Returns:
- A model containing the decoded data. While
nullis allowed, implementors are advised to throw an error instead. - Throws:
IOException- for any exceptions caused by IO problems or invalid data. Since decoding invalid data almost always results in RunTimeExceptions (NullPointerException, IndexOutOfBoundsException, IllegalArgumentException, ...) on unexpected places, implementations are advised to catch RuntimeExceptions in their decode() method, and wrap them into an IOException, as illustrated in the code snippet below.public ILcdModel decode( String aSourceName ) throws IOException { try (InputStream input = fInputStreamFactory.createInputStream(aSourceName)) { // Perform decoding ... } catch (RuntimeException e) { throw new IOException(e); } }- See Also:
-
decode
Decodes a new database model based on the given properties.- Throws:
IOException
-