Class TLcdCSVModelDecoder
- All Implemented Interfaces:
ILcdInputStreamFactoryCapable
,ILcdModelDecoder
Input files
File | Required | Entry point | Description |
---|---|---|---|
*.csv, *.tsv | x | x | File containing the data as character-separated values.
The accepted extensions can be configured using the setExtensions(String[]) method. |
*.csvt | File specifying the data type for each column. See the GDAL documentation and the GeoCSV specification for more information about this file. In addition to the specification it is possible to specify a 'GeoJson' (case-insensitive) data type or a 'coordinates' (case-insensitive)data type | ||
*.cpg | A plain text file which specifies the codepage number and which is used to identify the character encoding of the .csv file.
The file can contain a code page number or the name of a char set. |
When the *.csvt
file is present, it will be used to determine which columns contain the geometry information when using the decode(String)
method.
If such a file is not present, the decoder will try to auto-detect those columns based on the name of the column.
Even when a *.csvt
file is present, it does not allow to specify all information (like for example the amount of header rows to skip).
You can specify all this information by creating a TLcdCSVDataSource
, and passing that to the decodeSource(ILcdDataSource)
method.
Model reference
The model reference is obtained from anILcdModelReferenceDecoder
. The default reference decoder set on this
model decoder is based on all model reference decoders annotated with the LcdService
annotation, and can handle
- EPSG references (
<filename>.epsg
): seeTLcdEPSGModelReferenceDecoder
for more info. - WKT references (
<filename>.prj
): seeTLcdWKTModelReferenceDecoder
for more info. - Luciad encoded model references (
<filename>.ref
): seeTLcdModelReferenceDecoder
for more info.
Supported files
When using thedecode(String)
method, the source file can only be decoded when certain settings can be auto-detected,
or match the default settings.
The following list details what settings can be auto-detected, and which defaults are assumed:
- First row of the csv file contains the column names, rest of the file is the data. If the first line contains a valid WKT or GeoJson string, the first line will be considered as data.
- Separator: an attempt to auto-detect the separator is done by scanning the first line of the file. If the separator cannot be determined, a "," is assumed.
-
The column types will be determined by parsing the corresponding
.csvt
file. When no such file is available, the geometry columns will be detected by scanning the first lines of the source file. If the first line contains the column names, the geometry columns are determined based on the column names:- WKT geometries: if a column name starting with "WKT" is found (case-insensitive), it is assumed to contain WKT encoded geometries.
- GeoJson geometries: if a column name starting with "geojson" is found (case-insensitive), it is assumed to contain GeoJson encoded geometries.
- Point geometries: the following column names are recognized:
- XY coordinates: a column named "coordinates"
- X coordinate: a column named "x", or a column name containing any of the following: longitude, long, lon, easting, east (case insensitive)
- Y coordinate: a column named "y", or a column name containing any of the following: latitude, lat, northing, north (case insensitive)
- Z coordinate: a column named "z", or a column name containing any of the following: altitude, height, elevation (case insensitive)
- If there is a column named "id" (case insensitive), it is assumed to be a unique identifier.
- The encoding is determined by parsing the corresponding optional
.cpg
file. When no such file is present, the CSV file is assumed to be encoded as UTF-8.
decodeSource(ILcdDataSource)
method with
a TLcdCSVDataSource
which specifies the settings for your csv file.
Supported file transfer protocols
- This model decoder supports all transfer protocols that are supported by
the
InputStreamFactory
of this decoder.
Model structure
- This model decoder creates a model per CSV file.
- All models returned by this model decoder implement
ILcd2DBoundsIndexedModel
. - If the input CSV file contains at least one column of type
TLcdCSVDataSource.ColumnType.DATE_TIME
, the resulting model is an instance ofILcdMultiDimensionalModel
and contains one dimension for each DateTime column.
Model descriptor
- All models returned by this model decoder have a
ILcdDataModelDescriptor
. - All models returned by this model decoder have a
ILcdDataSourceModelDescriptor
with aTLcdCSVDataSource
as data source. - The type name of the model descriptor is "CSV".
Model elements
The model elements implementILcdDataObject
.
The geometries can be retrieved using the ALcdShape.fromDomainObject(Object)
method.
All other property fields of the data object will have a type as specified in the *.csvt
file.
If the file is not present, all fields will be considered Strings.
If one of the columns represents an ID, you can use the TLcdCSVDataSource.Builder.id(int)
method to
specify this column.
This property corresponding to this column will be annotated with a TLcdPrimaryKeyAnnotation
.
Sample code
ILcdModelDecoder decoder = new TLcdCSVModelDecoder();
ILcdModel model = decoder.decode("world.csv");
Thread safety
- The decoding of models is not thread-safe.
- The decoded models are thread-safe for read access.
- Since:
- 2018.0
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
TheILcdModelDescriptor.getTypeName()
of all models decoded by this model decoder. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
canDecodeSource
(ILcdDataSource aDataSource) Checks whether this model decoder can decode the data source(s), identified by the passedILcdDataSource
.boolean
canDecodeSource
(String aSource) Checks whether this model decoder can decode the specified data source.Creates a new model from the given data source.decodeSource
(ILcdDataSource aDataSource) Creates a new model from the given data source.Returns the date format used for parsingTLcdCSVDataSource.ColumnType.DATE_TIME
columns.Returns the model reference to be used for models when the model reference decoder is set to null.Returns a short, displayable name for the format that is decoded by thisILcdModelDecoder
.String[]
Returns the current file extensions which are used by thecanDecodeSource(String)
method to determine whether or not to accept a fileReturns the input stream factory that is used.Returns the decoder used to produce model references.void
setDateFormat
(DateFormat aDateFormat) Configures the date format to use for parsingTLcdCSVDataSource.ColumnType.DATE_TIME
columns.void
setDefaultModelReference
(ILcdModelReference aDefaultModelReference) Sets the model reference to be used for models when the model reference decoder is set to null.void
setExtensions
(String[] aExtensions) Sets the file extensions to use by thecanDecodeSource(String)
method to determine whether or not to accept a filevoid
setInputStreamFactory
(ILcdInputStreamFactory aInputStreamFactory) Sets the input stream factory to be used.void
setModelReferenceDecoder
(ILcdModelReferenceDecoder aModelReferenceDecoder) Sets the decoder to use to produce model references for models created with this decoder.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.luciad.model.ILcdModelDecoder
decodeModelMetadata, decodeModelMetadata, discoverDataSources
-
Field Details
-
TYPE_NAME
TheILcdModelDescriptor.getTypeName()
of all models decoded by this model decoder.- See Also:
-
-
Constructor Details
-
TLcdCSVModelDecoder
public TLcdCSVModelDecoder()
-
-
Method Details
-
getDisplayName
Description copied from interface:ILcdModelDecoder
Returns a short, displayable name for the format that is decoded by thisILcdModelDecoder
.- Specified by:
getDisplayName
in interfaceILcdModelDecoder
- Returns:
- the displayable name of this
ILcdModelDecoder
.
-
canDecodeSource
Checks whether this model decoder can decode the specified data source. It is acceptable for this method to returntrue
for a source name whiledecode
throws 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:
canDecodeSource
in interfaceILcdModelDecoder
- Parameters:
aSource
- the data source to be verified; typically a file name or a URL.- Returns:
true
if the file extension is contained ingetExtensions()
- See Also:
-
decode
Description copied from interface:ILcdModelDecoder
Creates a new model from the given data source.- Specified by:
decode
in interfaceILcdModelDecoder
- Parameters:
aSourceName
- the data source to be decoded; typically a file name or a URL.- Returns:
- A model containing the decoded data. While
null
is 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:
-
canDecodeSource
Description copied from interface:ILcdModelDecoder
Checks whether this model decoder can decode the data source(s), identified by the passed
ILcdDataSource
.For performance reasons, we strongly recommend that this will only be a simple test. For example: check the instance class of
aDataSource
, or check the file extension if it is aTLcdDataSource
.The default implementation of this method will check if the given
ILcdDataSource
is aTLcdDataSource
. If not, this method returns false. Otherwise, it delegates the source to theILcdModelDecoder.canDecodeSource(String)
method.- Specified by:
canDecodeSource
in interfaceILcdModelDecoder
- Parameters:
aDataSource
- theILcdModelSource
to be verified.- Returns:
true
if this decoder can likely decode the data specified byaDataSource
,false
otherwise.- See Also:
-
decodeSource
Description copied from interface:ILcdModelDecoder
Creates a new model from the given data source.
By default, this method:
- Throws a
NullPointerException
when anull
data source is passed. - Delegates to the
ILcdModelDecoder.decode(String)
method when aTLcdDataSource
is passed. - Throws an IOException in other case.
- Specified by:
decodeSource
in interfaceILcdModelDecoder
- Parameters:
aDataSource
- theILcdDataSource
to be decoded.- Returns:
- a model containing the decoded data. While
null
is 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 decodeSource(ILcdDataSource aDataSource) throws IOException { try { // Perform decoding ... } catch (RuntimeException e) { throw new IOException(e); } }
- See Also:
- Throws a
-
setDefaultModelReference
Sets the model reference to be used for models when the model reference decoder is set to null.- Parameters:
aDefaultModelReference
- the model reference to be used for models when the model reference decoder is set to null.- See Also:
-
getDefaultModelReference
Returns the model reference to be used for models when the model reference decoder is set to null.- Returns:
- the model reference to be used for models when the model reference decoder is set to null.
- See Also:
-
getModelReferenceDecoder
Returns the decoder used to produce model references.- Returns:
- the decoder to produce model references.
- See Also:
-
setModelReferenceDecoder
Sets the decoder to use to produce model references for models created with this decoder.- Parameters:
aModelReferenceDecoder
- the decoder to use to produce model references for models created with this decoder.- See Also:
-
getInputStreamFactory
Description copied from interface:ILcdInputStreamFactoryCapable
Returns the input stream factory that is used.- Specified by:
getInputStreamFactory
in interfaceILcdInputStreamFactoryCapable
- Returns:
- the input stream factory that is used.
-
setInputStreamFactory
Description copied from interface:ILcdInputStreamFactoryCapable
Sets the input stream factory to be used.- Specified by:
setInputStreamFactory
in interfaceILcdInputStreamFactoryCapable
- Parameters:
aInputStreamFactory
- the input stream factory to be used.
-
getExtensions
Returns the current file extensions which are used by thecanDecodeSource(String)
method to determine whether or not to accept a file- Returns:
- the current file extensions which are used by the
canDecodeSource(String)
method to determine whether or not to accept a file
-
setExtensions
Sets the file extensions to use by thecanDecodeSource(String)
method to determine whether or not to accept a file- Parameters:
aExtensions
- The extensions to accept
-
getDateFormat
Returns the date format used for parsingTLcdCSVDataSource.ColumnType.DATE_TIME
columns. The default is aSimpleDateFormat
with pattern"yyyy-MM-dd HH:mm:ssXXX"
.- Returns:
- the used format
- Since:
- 2020.1
-
setDateFormat
Configures the date format to use for parsingTLcdCSVDataSource.ColumnType.DATE_TIME
columns.- Parameters:
aDateFormat
- the format to use- Since:
- 2020.1
-