Posts Tagged ‘TomTom One’

Reading .OV2 TomTom POI Files using Java

December 31st, 2009

I’m posting today some source code about the famous TomTom One .OV2 file formats. These files are the one used by TomTom to store Point of Interest information within their navigation systems. However such format is proprietary, the data specs can be found in the TomTom website (see this PDF at section 2.4). I wrote down a simple procedure that gathers record by record only the simple POI records. Actually the OV2 format is a little bit more complicated than a simple list of POIs and it can be subjected to changes and version incompatibilities. I tested it with POIs taken from my TomTom ONE XL and ti worked great, however if the OV2 files contains proprietary or special record types (not specified in TomTom data sheets) these procedures will simply stop since it is not possible for this format to distinguish between a corrupted file or non standard encoding.

I hope you’ll find it interesting.

package readers;

import java.io.FileInputStream;
import java.io.IOException;

public class OV2RecordReader {

	public static String[] readOV2Record(FileInputStream inputStream){
		String[] record = null;
		int b = -1;
		try{
			if ((b = inputStream.read())> -1) {
				// if it is a simple POI record
				if (b == 2) {
					record = new String[3];
		    	    long total = readLong(inputStream);

		    	    double longitude = (double) readLong(inputStream) / 100000.0;
		    	    double latitude = (double) readLong(inputStream) / 100000.0;

		    	    byte[] r = new byte[(int) total - 13];
		    	    inputStream.read(r);

		    	    record[0] = new String(r);
		    	    record[0] = record[0].substring(0,record[0].length()-1);
		    	    record[1] = Double.toString(latitude);
		    	    record[2] = Double.toString(longitude);
		    	}
				//if it is a deleted record
				else if(b == 0){
					byte[] r = new byte[9];
		    	    inputStream.read(r);
				}
				//if it is a skipper record
				else if(b == 1){
					byte[] r = new byte[20];
		    	    inputStream.read(r);
				}
				else{
					throw new IOException("wrong record type");
				}
			}
			else{
				return null;
			}
		}
		catch(IOException e){
			e.printStackTrace();
		}
		return record;
	}

	private static long readLong(FileInputStream is){
		long res = 0;
		try{
			res = is.read();
			res += is.read() <<8;
			res += is.read() <<16;
			res += is.read() <<24;
		}
		catch(IOException e){
			e.printStackTrace();
		}
		return res;
	}
}