The majority of formats require the same two steps for visualization on a GXY view. To visualize CSV data:
-
Decode the data into an
ILcdModel
using anILcdModelDecoder
. -
Create an
ILcdGXYLayer
for theILcdModel
and add it to theILcdGXYView
.
//First create the model
ILcdModelDecoder decoder =
new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));
ILcdModel model = decoder.decode(sourceName);
//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);
This results in a CSV layer with default styling. See Visualizing Vector Data for more information about visualizing and styling vector data.
This code snippet uses the The model decoder class that is actually responsible for decoding CSV data is the
|
Handling non-standard CSV files
The above code relies on the model decoder to be able to correctly parse the CSV file. Sometimes the decoder does not have enough information to automatically determine the structure of the file.
In that case, you can pass extra information to the decoder in the form of a TLcdCSVDataSource
.
The following example uses the TLcdCSVDataSource
to specify both the source CSV file and the separator used in that file.
//Create the TLcdCSVDataSource
//This is a container object with all the information needed
//by the model decoder to parse the CSV file
TLcdCSVDataSource dataSource =
TLcdCSVDataSource.newBuilder()
.source(aCSVSourceFile) //specify the path
.separator(",") //specify extra information for the decoder
.build();
ILcdModelDecoder decoder =
new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));
//Pass the TLcdCSVDataSource to the model decoder
ILcdModel model = decoder.decodeSource(dataSource);
There are a number of other settings that can be configured on the TLcdCSVDataSource
allowing to deal with non-standard CSV files.
Check the javadoc of that class for more information on the available settings.