@LcdService(service=ILcdModelDecoder.class, priority=20000) public final class TLcdCSVModelDecoder extends Object implements ILcdModelDecoder, ILcdInputStreamFactoryCapable
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.
ILcdModelReferenceDecoder
. The default reference decoder set on this
model decoder is based on all model reference decoders annotated with the LcdService
annotation, and can handle
<filename>.epsg
): see TLcdEPSGModelReferenceDecoder
for more info.<filename>.prj
): see TLcdWKTModelReferenceDecoder
for more info.<filename>.ref
): see TLcdModelReferenceDecoder
for more info.decode(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:
.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:
.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.
InputStreamFactory
of this decoder.
ILcd2DBoundsIndexedModel
.TLcdCSVDataSource.ColumnType.DATE_TIME
, the
resulting model is an instance of ILcdMultiDimensionalModel
and contains one dimension for each DateTime column.ILcdDataModelDescriptor
.ILcdDataSourceModelDescriptor
with a TLcdCSVDataSource
as data source.ILcdDataObject
.
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
.
ILcdModelDecoder decoder = new TLcdCSVModelDecoder();
ILcdModel model = decoder.decode("world.csv");
TLcdCSVDataSource
Modifier and Type | Field and Description |
---|---|
static String |
TYPE_NAME
The
ILcdModelDescriptor.getTypeName() of all models decoded by this model decoder. |
Constructor and Description |
---|
TLcdCSVModelDecoder() |
Modifier and Type | Method and Description |
---|---|
boolean |
canDecodeSource(ILcdDataSource aDataSource)
Checks whether this model decoder can decode the data source(s), identified by the passed
ILcdDataSource . |
boolean |
canDecodeSource(String aSource)
Checks whether this model decoder can decode the specified data source.
|
ILcdModel |
decode(String aSourceName)
Creates a new model from the given data source.
|
ILcdModel |
decodeSource(ILcdDataSource aDataSource)
Creates a new model from the given data source.
|
DateFormat |
getDateFormat()
Returns the date format used for parsing
TLcdCSVDataSource.ColumnType.DATE_TIME columns. |
ILcdModelReference |
getDefaultModelReference()
Returns the model reference to be used for models when the model reference decoder is set to null.
|
String |
getDisplayName()
Returns a short, displayable name for the format that is decoded by this
ILcdModelDecoder . |
String[] |
getExtensions()
Returns the current file extensions which are used by the
canDecodeSource(String) method to determine
whether or not to accept a file |
ILcdInputStreamFactory |
getInputStreamFactory()
Returns the input stream factory that is used.
|
ILcdModelReferenceDecoder |
getModelReferenceDecoder()
Returns the decoder used to produce model references.
|
void |
setDateFormat(DateFormat aDateFormat)
Configures the date format to use for parsing
TLcdCSVDataSource.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 the
canDecodeSource(String) method to determine whether or not to accept a file |
void |
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.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
decodeModelMetadata, decodeModelMetadata, discoverDataSources
public static final String TYPE_NAME
ILcdModelDescriptor.getTypeName()
of all models decoded by this model decoder.public String getDisplayName()
ILcdModelDecoder
ILcdModelDecoder
.getDisplayName
in interface ILcdModelDecoder
ILcdModelDecoder
.public boolean canDecodeSource(String aSource)
true
for a source name while decode
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.
canDecodeSource
in interface ILcdModelDecoder
aSource
- the data source to be verified; typically a file name or a URL.true
if the file extension is contained in getExtensions()
ILcdModelDecoder.decode(String)
,
ILcdModelDecoder.decodeModelMetadata(String)
public ILcdModel decode(String aSourceName) throws IOException
ILcdModelDecoder
decode
in interface ILcdModelDecoder
aSourceName
- the data source to be decoded; typically a file name or a URL.null
is allowed, implementors are advised to throw an error instead.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);
}
}
InterruptedIOException
- When the thread on which this method is called is interrupted: it is recommended to stop the decoding
and throw an InterruptedIOException
.
This same exception can also be used if the decoder shows UI to the user, and the user cancels the decoding
through the UI.ILcdModelDecoder.canDecodeSource(String)
public boolean canDecodeSource(ILcdDataSource aDataSource)
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 a TLcdDataSource
.
The default implementation of this method will check if the given ILcdDataSource
is a
TLcdDataSource
. If not, this method returns false. Otherwise, it delegates
the source to the ILcdModelDecoder.canDecodeSource(String)
method.
canDecodeSource
in interface ILcdModelDecoder
aDataSource
- the ILcdModelSource
to be verified.true
if this decoder can likely decode the data specified by aDataSource
, false
otherwise.ILcdModelDecoder.decodeSource(ILcdDataSource)
,
ILcdModelDecoder.decodeModelMetadata(ILcdDataSource)
public ILcdModel decodeSource(ILcdDataSource aDataSource) throws IOException
ILcdModelDecoder
Creates a new model from the given data source.
By default, this method:
NullPointerException
when a null
data source is passed.ILcdModelDecoder.decode(String)
method when a TLcdDataSource
is passed.decodeSource
in interface ILcdModelDecoder
aDataSource
- the ILcdDataSource
to be decoded.null
is allowed, implementors are advised to throw an error instead.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);
}
}
ILcdModelDecoder.canDecodeSource(ILcdDataSource)
public void setDefaultModelReference(ILcdModelReference aDefaultModelReference)
aDefaultModelReference
- the model reference to be used for models when the model reference
decoder is set to null.getDefaultModelReference()
public ILcdModelReference getDefaultModelReference()
setDefaultModelReference(com.luciad.model.ILcdModelReference)
public ILcdModelReferenceDecoder getModelReferenceDecoder()
setModelReferenceDecoder(com.luciad.model.ILcdModelReferenceDecoder)
,
setDefaultModelReference(com.luciad.model.ILcdModelReference)
public void setModelReferenceDecoder(ILcdModelReferenceDecoder aModelReferenceDecoder)
aModelReferenceDecoder
- the decoder to use to produce model references for models created with this decoder.getModelReferenceDecoder()
,
setDefaultModelReference(com.luciad.model.ILcdModelReference)
public ILcdInputStreamFactory getInputStreamFactory()
ILcdInputStreamFactoryCapable
getInputStreamFactory
in interface ILcdInputStreamFactoryCapable
public void setInputStreamFactory(ILcdInputStreamFactory aInputStreamFactory)
ILcdInputStreamFactoryCapable
setInputStreamFactory
in interface ILcdInputStreamFactoryCapable
aInputStreamFactory
- the input stream factory to be used.public String[] getExtensions()
canDecodeSource(String)
method to determine
whether or not to accept a filecanDecodeSource(String)
method to determine
whether or not to accept a filepublic void setExtensions(String[] aExtensions)
canDecodeSource(String)
method to determine whether or not to accept a fileaExtensions
- The extensions to acceptpublic DateFormat getDateFormat()
TLcdCSVDataSource.ColumnType.DATE_TIME
columns.
The default is a SimpleDateFormat
with pattern "yyyy-MM-dd HH:mm:ssXXX"
.public void setDateFormat(DateFormat aDateFormat)
TLcdCSVDataSource.ColumnType.DATE_TIME
columns.aDateFormat
- the format to use