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): seeTLcdEPSGModelReferenceDecoderfor more info. - WKT references (
<filename>.prj): seeTLcdWKTModelReferenceDecoderfor more info. - Luciad encoded model references (
<filename>.ref): seeTLcdModelReferenceDecoderfor 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
.csvtfile. 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
.cpgfile. 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
InputStreamFactoryof 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 ofILcdMultiDimensionalModeland 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
ILcdDataSourceModelDescriptorwith aTLcdCSVDataSourceas 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
FieldsModifier and TypeFieldDescriptionstatic final StringTheILcdModelDescriptor.getTypeName()of all models decoded by this model decoder. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancanDecodeSource(ILcdDataSource aDataSource) Checks whether this model decoder can decode the data source(s), identified by the passedILcdDataSource.booleancanDecodeSource(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_TIMEcolumns.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.voidsetDateFormat(DateFormat aDateFormat) Configures the date format to use for parsingTLcdCSVDataSource.ColumnType.DATE_TIMEcolumns.voidsetDefaultModelReference(ILcdModelReference aDefaultModelReference) Sets the model reference to be used for models when the model reference decoder is set to null.voidsetExtensions(String[] aExtensions) Sets the file extensions to use by thecanDecodeSource(String)method to determine whether or not to accept a filevoidsetInputStreamFactory(ILcdInputStreamFactory aInputStreamFactory) Sets the input stream factory to be used.voidsetModelReferenceDecoder(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, waitMethods 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: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
Checks 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:
aSource- the data source to be verified; typically a file name or a URL.- Returns:
trueif the file extension is contained ingetExtensions()- See Also:
-
decode
Description copied from interface:ILcdModelDecoderCreates a new model from the given data source.- 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:
-
canDecodeSource
Description copied from interface:ILcdModelDecoderChecks 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
ILcdDataSourceis aTLcdDataSource. If not, this method returns false. Otherwise, it delegates the source to theILcdModelDecoder.canDecodeSource(String)method.- Specified by:
canDecodeSourcein interfaceILcdModelDecoder- Parameters:
aDataSource- theILcdModelSourceto be verified.- Returns:
trueif this decoder can likely decode the data specified byaDataSource,falseotherwise.- See Also:
-
decodeSource
Description copied from interface:ILcdModelDecoderCreates a new model from the given data source.
By default, this method:
- Throws a
NullPointerExceptionwhen anulldata source is passed. - Delegates to the
ILcdModelDecoder.decode(String)method when aTLcdDataSourceis passed. - Throws an IOException in other case.
- Specified by:
decodeSourcein interfaceILcdModelDecoder- Parameters:
aDataSource- theILcdDataSourceto be decoded.- 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 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:ILcdInputStreamFactoryCapableReturns the input stream factory that is used.- Specified by:
getInputStreamFactoryin interfaceILcdInputStreamFactoryCapable- Returns:
- the input stream factory that is used.
-
setInputStreamFactory
Description copied from interface:ILcdInputStreamFactoryCapableSets the input stream factory to be used.- Specified by:
setInputStreamFactoryin 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_TIMEcolumns. The default is aSimpleDateFormatwith 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_TIMEcolumns.- Parameters:
aDateFormat- the format to use- Since:
- 2020.1
-