/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.map.utils; import java.util.*; import vnsim.map.object.*; /** * @author Victor-Radu */ public class ParsingUtil { public static Road parseRoad(byte[] buffer){ //parses a record in RT1 format into a road object Road r = new Road(); Point p; if (buffer[19] != ' ' && buffer[55] == 'A'){ // it is a road with non-empty name field // we care only obut roads try{ r.setId( Long.parseLong(new String(buffer, 5, 10).trim()) ); }catch (NumberFormatException e){ e.printStackTrace(); return null; } r.setName( new String(buffer, 19, 30) ); r.setType( new String(buffer, 49, 4)); r.setDirectionPrefix( (new String(buffer, 17, 2)).getBytes()); r.setDirectionSuffix( (new String(buffer, 53, 2)).getBytes()); r.setRoadinfo( (new String(buffer, 55, 3)).getBytes()); //start and end point p = parsePoint(new String(buffer, 190, 19).getBytes()); if (p!= null) r.points.add(p); else return null; p = parsePoint(new String(buffer, 209, 19).getBytes()); if (p!= null) r.points.add(p); else return null; return r; } return null; } public static Point parsePoint(byte[] buffer){ //parses a buffer into a point (longitude 10 digits, latitude 9 digits) // input example: buffer= " -74407665+40526477" (longitude, latitude) try{ String s = new String(buffer); byte[] buf = s.replace('+', ' ').getBytes(); Point p = new Point((double)( Long.parseLong(new String(buf, 0, 10).trim())) * 1e-6f, (double)( Long.parseLong(new String(buf, 10, 9).trim())) * 1e-6f); return p; }catch (NumberFormatException e){ e.printStackTrace(); return null; } } }