The following code example shows how the OpenLR decoder can be used. The code uses the "OpenLR Access Layer for SQLite" and the binary physical format. The example application can be run with the following binary location reference: locRef.bin. The example location consists of four lines and can be decoded with the TomTom test map.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import openlr.LocationReference;
import openlr.OpenLRProcessingException;
import openlr.PhysicalFormatException;
import openlr.binary.ByteArray;
import openlr.binary.impl.LocationReferenceBinaryImpl;
import openlr.decoder.OpenLRDecoder;
import openlr.decoder.OpenLRDecoderParameter;
import openlr.location.Location;
import openlr.map.Line;
import openlr.map.MapDatabase;
import openlr.map.loader.MapLoadParameter;
import openlr.map.sqlite.loader.DBFileNameParameter;
import openlr.map.sqlite.loader.SQLiteMapLoader;
import openlr.properties.OpenLRPropertiesReader;
import openlr.properties.OpenLRPropertyException;
import org.apache.commons.configuration.Configuration;
import org.apache.log4j.PropertyConfigurator;
public class DecoderExample {
private static final String PATH_TO_DB = ".../tt_utrecht_2008_04.db3";
private static final InputStream DECODER_PROPERTIES = DecoderExample.class
.getClassLoader().getResourceAsStream(
"OpenLR-Decoder-Properties.xml");
private static final String PATH_TO_LOG_PROPERTIES = "...";
// first argument shall be the path to a file holding the binary
// location reference
public static void main(String[] args) {
// setup logging
PropertyConfigurator.configure(PATH_TO_LOG_PROPERTIES);
// instantiate map database
MapDatabase mdb = null;
SQLiteMapLoader mapLoader = new SQLiteMapLoader();
List<MapLoadParameter> params = new ArrayList<MapLoadParameter>();
DBFileNameParameter dbFile = new DBFileNameParameter();
dbFile.setValue(PATH_TO_DB);
params.add(dbFile);
try {
mdb = mapLoader.load(params);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
// check binary file
File binFile = new File(args[0]);
FileInputStream fib = null;
try {
fib = new FileInputStream(binFile);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
System.exit(2);
}
// setup location reference
LocationReference locRef = null;
try {
ByteArray ba = new ByteArray(new BufferedInputStream(fib));
locRef = new LocationReferenceBinaryImpl("LocationID", ba);
} catch (IOException e) {
e.printStackTrace();
System.out.println("io error");
System.exit(2);
} catch (PhysicalFormatException e) {
e.printStackTrace();
System.out.println("invalid binary file");
System.exit(2);
}
// load decoder properties
Configuration prop = null;
try {
prop = OpenLRPropertiesReader.loadPropertiesFromStream(
DECODER_PROPERTIES, true);
} catch (OpenLRPropertyException e1) {
e1.printStackTrace();
}
// prepare decoder parameter with map database and properties
OpenLRDecoderParameter decParam = new OpenLRDecoderParameter.Builder()
.with(mdb).with(prop).buildParameter();
// start decoder
Location decodedLoc = null;
try {
OpenLRDecoder decoder = new OpenLRDecoder();
decodedLoc = decoder.decode(decParam, locRef);
} catch (OpenLRProcessingException e) {
e.printStackTrace();
System.exit(5);
}
// check validity of decoding result
if (decodedLoc.isValid()) {
// decoded location is valid
List<? extends Line> decodedPath = decodedLoc.getLocationLines();
System.out.println("Location is valid -- number of lines: "
+ decodedPath.size());
} else {
// decoded location is not valid
System.out.println("Location is NOT valid -- return code: "
+ decodedLoc.getReturnCode());
}
}
}
The following pom.xml can be used to run the code example
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>openlr.examples</groupId>
<artifactId>decoderExample</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>decoderExample</name>
<dependencies>
<dependency>
<groupId>openlr</groupId>
<artifactId>decoder</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>openlr</groupId>
<artifactId>binary</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>openlr-mapLoader</groupId>
<artifactId>tt-sqlite</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>openlr</id>
<url>http://www.openlr.org/nexus/content/repositories/releases</url>
</repository>
</repositories>
</project>