/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: Damian Waradzyn
*/
package com.mapmidlet.routing;
import java.io.*;
import java.util.*;
import org.kxml.kdom.*;
import org.kxml.parser.XmlParser;
import com.mapmidlet.misc.IOTool;
import com.mapmidlet.options.Options;
import com.mapmidlet.projection.WorldCoordinate;
import com.mapmidlet.routing.Route.RouteDirection;
public class CloudmadeRouter extends Router {
public static Hashtable ROUTING_OPTIONS;
public static Vector OPTION_NAMES;
private WorldCoordinate lastC1, lastC2;
private Route lastRoute;
static {
ROUTING_OPTIONS = new Hashtable();
ROUTING_OPTIONS.put("Car (fastest)", "car.gpx");
ROUTING_OPTIONS.put("Car (shortest)", "car/shortest.gpx");
ROUTING_OPTIONS.put("Bicycle", "bicycle.gpx");
ROUTING_OPTIONS.put("Foot", "foot.gpx");
Enumeration keys = ROUTING_OPTIONS.keys();
OPTION_NAMES = new Vector();
while (keys.hasMoreElements()) {
OPTION_NAMES.addElement(keys.nextElement());
}
}
public String getName() {
return "Cloudmade router";
}
public Route getRoute(WorldCoordinate c1, WorldCoordinate c2) {
if (lastC1 != null && lastC1.latitude == c1.latitude && lastC1.longitude == c1.longitude
&& lastC2.latitude == c2.latitude && lastC2.longitude == c2.longitude) {
return lastRoute;
}
lastC1 = new WorldCoordinate(c1);
lastC2 = new WorldCoordinate(c2);
try {
String routeGpx = IOTool.download("http://routes.cloudmade.com/e4b1777b4b5154d69dbfc4678216183a/api/0.3/"
+ c1.latitude + "," + c1.longitude + "," + c2.latitude + "," + c2.longitude + "/"
+ ROUTING_OPTIONS.get(Options.getInstance().routeType) + "?units=km&lang=en");
if (Options.getInstance().debugMode) {
System.out.println(routeGpx);
}
XmlParser parser = new XmlParser(new InputStreamReader(new ByteArrayInputStream(routeGpx.getBytes())));
Document doc = new Document();
doc.parse(parser);
parser = null;
Element root = doc.getRootElement();
Route result = new Route();
result.waypoints = new Vector();
result.routeDirections = new Vector();
for (int i = 0; i < root.getChildCount(); i++) {
Object child = root.getChild(i);
if (child instanceof Element) {
Element elem = (Element) child;
String name = elem.getName();
if (name.equals("extensions")) {
for (int j = 0; j < elem.getChildCount(); j++) {
Object child2 = elem.getChild(i);
if (child2 instanceof Element) {
Element elem2 = (Element) child2;
if (elem2.getName().equals("distance")) {
result.distance = Long.parseLong(elem2.getText());
}
}
}
} else if (name.equals("wpt")) {
WorldCoordinate c = new WorldCoordinate();
c.latitude = Double.parseDouble(elem.getAttribute("lat").getValue());
c.longitude = Double.parseDouble(elem.getAttribute("lon").getValue());
result.waypoints.addElement(c);
} else if (name.equals("rte")) {
for (int j = 0; j < elem.getChildCount(); j++) {
Object child2 = elem.getChild(j);
if (child2 instanceof Element) {
Element elem2 = (Element) child2;
if (elem2.getName().equals("rtept")) {
RouteDirection d = new RouteDirection();
for (int k = 0; k < elem2.getChildCount(); k++) {
Object child3 = elem2.getChild(k);
if (child3 instanceof Element) {
Element elem3 = (Element) child3;
if (elem3.getName().equals("desc")) {
d.text = elem3.getText();
} else if (elem3.getName().equals("extensions")) {
for (int l = 0; l < elem3.getChildCount(); l++) {
Object child4 = elem3.getChild(l);
if (child4 instanceof Element) {
Element elem4 = (Element) child4;
if (elem4.getName().equals("turn")) {
d.turn = elem4.getText();
} else if (elem4.getName().equals("distance-text")) {
d.distanceText = elem4.getText();
} else if (elem4.getName().equals("offset")) {
d.offset = Integer.parseInt(elem4.getText());
}
}
}
}
}
}
result.routeDirections.addElement(d);
}
}
}
}
}
}
lastRoute = result;
if (Options.getInstance().debugMode) {
result.print();
}
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Vector getRouteTypes() {
return OPTION_NAMES;
}
}