Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package geocoding;
- import graph.ALGOBuslineEdge;
- import java.io.*;
- import graph.ALGOEdge;
- import graph.ALGOGraph;
- import graph.ALGOPlacemarkNode;
- import graph.AStarGraph;
- import graph.BreadthFirstGraph;
- import graph.DepthFirstGraph;
- import graph.SearchableGraph;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Hashtable;
- import gpstest.example.com.R;
- import android.app.Activity;
- import android.widget.Toast;
- import com.keithpower.gekmlib.Placemark;
- import com.keithpower.gekmlib.Point;
- public class MapReader extends Activity {
- private double minLat = Double.MAX_VALUE;
- private double maxLat = Double.MIN_VALUE;
- private double minLng = Double.MAX_VALUE;
- private double maxLng = Double.MIN_VALUE;
- public String motorwayName;
- public MapReader() {
- }
- public ALGOGraph readMapFromFile(String filename) {
- //*** set search-algorithm here:
- SearchableGraph map = new AStarGraph();
- // SearchableGraph map = new BreadthFirstGraph();
- // SearchableGraph map = new DepthFirstGraph();
- System.out.println("yxc Mapreader readMapFromFile");
- int count = 0;
- try {
- System.out.println("yxc try");
- Hashtable<Integer,ALGOPlacemarkNode> stops = new Hashtable<Integer,ALGOPlacemarkNode>();
- String line;
- boolean readName = false;
- int lastStopNum = -1, stopNum = 0;
- // BufferedReader f = new BufferedReader(new FileReader(filename));
- BufferedReader f = new BufferedReader (new InputStreamReader(getResources().openRawResource(R.raw.autobahnnetz_gps))) ;
- while ((line = f.readLine()) != null) {
- String data [] = line.split(":");
- try {
- //stopNum = Integer.parseInt(line);
- stopNum = Integer.parseInt(data[0]);
- readName = false;
- }
- catch (NumberFormatException e) {
- readName = true;
- }
- if (line.startsWith("von")) {
- int startNum = Integer.parseInt(line.substring(4, line.indexOf("nach")-1));
- int endNum = Integer.parseInt(line.substring(line.indexOf("nach")+5));
- ALGOPlacemarkNode pv = stops.get(new Integer(startNum));
- ALGOPlacemarkNode pn = stops.get(new Integer(endNum));
- if (pv == null) System.out.println("Start unbekannt: " + startNum);
- else if (pn == null) System.out.println("Ende unbekannt: " + endNum);
- else {
- double costs = DistanceCalculator.getDistance(pv.getValue().getPoint().getNumericalCoordinates(), pn.getValue().getPoint().getNumericalCoordinates());
- map.addEdge(new ALGOBuslineEdge(pv.getNodeID(), pn.getNodeID(), costs));
- map.addEdge(new ALGOBuslineEdge(pn.getNodeID(), pv.getNodeID(), costs));
- System.out.println("von " + startNum + " nach " + endNum);
- }
- }
- else if (line.contains("keine GPS")) {
- System.out.println(data[1] + " hat keine GPS-Koordinaten");
- continue;
- }
- else {
- if (stopNum < lastStopNum) {
- System.out.println("neue Autobahn");
- stops.clear();
- lastStopNum = -1;
- }
- if ((data.length == 3) && !data[2].equals(" ")) {
- String coords [] = data[2].split(",");
- String name;
- double lat, lng;
- ALGOPlacemarkNode node;
- int pos;
- name = data[1];
- pos = name.indexOf("(");
- if (pos > 0) name = name.substring(0, pos);
- if (name.equals("motorway junction")) {
- name = motorwayName + "-" + data[0];
- }
- if ((node = map.findNode(name)) == null) {
- Point p = new Point();
- Placemark pm = new Placemark();
- lng = Double.parseDouble(coords[0]);
- lat = Double.parseDouble(coords[1]);
- if (lat < minLat) minLat = lat;
- if (lat > maxLat) maxLat = lat;
- if (lng < minLng) minLng = lng;
- if (lng > maxLng) maxLng = lng;
- p.setNumericalCoordinates(new double[]{lat, lng, 0.0});
- pm.addPoint(p);
- pm.setName(name);
- node = new ALGOPlacemarkNode(count ++, pm);
- map.addNode(node);
- System.out.println(data[0]+ ":" +name+ ":" + lat + "," + lng + ",0.0");
- }
- else System.out.println("schon da: " + name);
- stops.put(new Integer(stopNum), node);
- }
- else if (data[0].equals("MOTORWAY")) motorwayName = data[1];
- }
- lastStopNum = stopNum;
- }
- }
- catch (IOException e) {
- System.out.println(e);
- System.out.println("catch");
- }
- System.out.println("yxc MapReader fettich");
- return map;
- }
- public double getMinLat() {
- return minLat;
- }
- public double getMaxLat() {
- return maxLat;
- }
- public double getMinLng() {
- return minLng;
- }
- public double getMaxLng() {
- return maxLng;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement