1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.map.utils; |
---|
7 | |
---|
8 | |
---|
9 | import java.util.*; |
---|
10 | |
---|
11 | import vnsim.map.object.*; |
---|
12 | |
---|
13 | |
---|
14 | /** |
---|
15 | * @author Victor-Radu |
---|
16 | */ |
---|
17 | |
---|
18 | public class ParsingUtil { |
---|
19 | |
---|
20 | public static Road parseRoad(byte[] buffer){ |
---|
21 | //parses a record in RT1 format into a road object |
---|
22 | Road r = new Road(); |
---|
23 | Point p; |
---|
24 | |
---|
25 | if (buffer[19] != ' ' && buffer[55] == 'A'){ |
---|
26 | // it is a road with non-empty name field |
---|
27 | // we care only obut roads |
---|
28 | try{ |
---|
29 | r.setId( Long.parseLong(new String(buffer, 5, 10).trim()) ); |
---|
30 | }catch (NumberFormatException e){ |
---|
31 | e.printStackTrace(); |
---|
32 | return null; |
---|
33 | } |
---|
34 | r.setName( new String(buffer, 19, 30) ); |
---|
35 | r.setType( new String(buffer, 49, 4)); |
---|
36 | r.setDirectionPrefix( (new String(buffer, 17, 2)).getBytes()); |
---|
37 | r.setDirectionSuffix( (new String(buffer, 53, 2)).getBytes()); |
---|
38 | r.setRoadinfo( (new String(buffer, 55, 3)).getBytes()); |
---|
39 | //start and end point |
---|
40 | p = parsePoint(new String(buffer, 190, 19).getBytes()); |
---|
41 | if (p!= null) |
---|
42 | r.points.add(p); |
---|
43 | else return null; |
---|
44 | p = parsePoint(new String(buffer, 209, 19).getBytes()); |
---|
45 | if (p!= null) |
---|
46 | r.points.add(p); |
---|
47 | else return null; |
---|
48 | return r; |
---|
49 | } |
---|
50 | return null; |
---|
51 | } |
---|
52 | |
---|
53 | public static Point parsePoint(byte[] buffer){ |
---|
54 | //parses a buffer into a point (longitude 10 digits, latitude 9 digits) |
---|
55 | // input example: buffer= " -74407665+40526477" (longitude, latitude) |
---|
56 | try{ |
---|
57 | String s = new String(buffer); |
---|
58 | byte[] buf = s.replace('+', ' ').getBytes(); |
---|
59 | Point p = new Point((double)( Long.parseLong(new String(buf, 0, 10).trim())) * 1e-6f, |
---|
60 | (double)( Long.parseLong(new String(buf, 10, 9).trim())) * 1e-6f); |
---|
61 | return p; |
---|
62 | }catch (NumberFormatException e){ |
---|
63 | e.printStackTrace(); |
---|
64 | return null; |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|