The following code example shows how the OpenLR encoder can be used for line locations. The code uses the "OpenLR Access Layer for SQLite" and the binary physical format.
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import openlr.LocationReference;
import openlr.OpenLRProcessingException;
import openlr.binary.ByteArray;
import openlr.encoder.LocationReferenceHolder;
import openlr.encoder.OpenLREncoder;
import openlr.encoder.OpenLREncoderParameter;
import openlr.location.Location;
import openlr.location.LocationFactory;
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 EncoderExample {
/* needs to be adjusted */
private static final String PATH_TO_DB = ".../tt_utrecht_2008_04.db3";
/* needs to be adjusted */
private static final String PATH_TO_LOG_PROPERTIES = "...";
private static final InputStream ENCODER_PROPERTIES = EncoderExample.class
.getClassLoader().getResourceAsStream(
"OpenLR-Encoder-Properties.xml");
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);
}
// load location path with n lines
List<Line> path = new ArrayList<Line>();
path.add(mdb.getLine(15280002805007L));
path.add(mdb.getLine(15280002805010L));
path.add(mdb.getLine(15280002805011L));
path.add(mdb.getLine(15280002805003L));
// instantiate Location object without offsets
Location location = LocationFactory.createLineLocation("Location-1",
path);
// prepare encoding result object
LocationReferenceHolder locRef = null;
// prepare encoder parameter with map database and properties
Configuration conf = null;
try {
conf = OpenLRPropertiesReader.loadPropertiesFromStream(
ENCODER_PROPERTIES, true);
} catch (OpenLRPropertyException e1) {
e1.printStackTrace();
System.exit(2);
}
OpenLREncoderParameter encParams = new OpenLREncoderParameter.Builder()
.with(mdb).with(conf).buildParameter();
try {
// encode the location
OpenLREncoder encoder = new OpenLREncoder();
locRef = encoder.encodeLocation(encParams, location);
} catch (OpenLRProcessingException e) {
// encoding runtime exception
e.printStackTrace();
System.exit(3);
}
// check validity of the location reference
if (!locRef.isValid()) {
// location reference is not valid, print out error code
System.out
.println("Location reference is NOT valid -- error code: "
+ locRef.getReturnCode());
} else {
// location reference is valid
// e.g. get binary format
LocationReference lr = locRef.getLocationReference("binary");
ByteArray ba = (ByteArray) lr.getLocationReferenceData();
System.out.println("Location reference is valid -- size [bytes]: "
+ ba.size());
}
}
}
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>encoderExample</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>encoderExample</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>openlr</groupId>
<artifactId>encoder</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>