Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. package geocoding;
  2.  
  3. import graph.ALGOBuslineEdge;
  4.  
  5. import java.io.*;
  6. import graph.ALGOEdge;
  7. import graph.ALGOGraph;
  8. import graph.ALGOPlacemarkNode;
  9. import graph.AStarGraph;
  10. import graph.BreadthFirstGraph;
  11. import graph.DepthFirstGraph;
  12. import graph.SearchableGraph;
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.FileReader;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18.  
  19. import java.util.Hashtable;
  20.  
  21. import gpstest.example.com.R;
  22.  
  23. import android.app.Activity;
  24.  
  25.  
  26.  
  27.  
  28. import android.widget.Toast;
  29.  
  30. import com.keithpower.gekmlib.Placemark;
  31. import com.keithpower.gekmlib.Point;
  32.  
  33. public class MapReader extends Activity {
  34.     private double minLat = Double.MAX_VALUE;
  35.     private double maxLat = Double.MIN_VALUE;
  36.     private double minLng = Double.MAX_VALUE;
  37.     private double maxLng = Double.MIN_VALUE;
  38.     public String motorwayName;
  39.    
  40.    
  41.     public MapReader() {
  42.        
  43.     }
  44.    
  45.     public ALGOGraph readMapFromFile(String filename) {
  46.        
  47.         //*** set search-algorithm here:
  48.         SearchableGraph map = new AStarGraph();
  49.         // SearchableGraph map = new BreadthFirstGraph();
  50.         // SearchableGraph map = new DepthFirstGraph();
  51.        
  52.         System.out.println("yxc Mapreader readMapFromFile");
  53.  
  54.        
  55.  
  56.              
  57.    
  58.  
  59.         int count = 0;
  60.        
  61.         try {
  62.             System.out.println("yxc try");
  63.             Hashtable<Integer,ALGOPlacemarkNode> stops = new Hashtable<Integer,ALGOPlacemarkNode>();
  64.             String line;
  65.             boolean readName = false;
  66.             int lastStopNum = -1, stopNum = 0;
  67.            
  68.        
  69.  
  70. //          BufferedReader f = new BufferedReader(new FileReader(filename));
  71.             BufferedReader f = new BufferedReader (new InputStreamReader(getResources().openRawResource(R.raw.autobahnnetz_gps))) ;
  72.            
  73.             while ((line = f.readLine()) != null) {
  74.                 String data [] = line.split(":");
  75.                 try {
  76.                     //stopNum = Integer.parseInt(line);
  77.                     stopNum = Integer.parseInt(data[0]);
  78.                     readName = false;
  79.                 }
  80.                 catch (NumberFormatException e) {
  81.                     readName = true;
  82.                 }
  83.                
  84.                 if (line.startsWith("von")) {
  85.                     int startNum = Integer.parseInt(line.substring(4, line.indexOf("nach")-1));
  86.                     int endNum = Integer.parseInt(line.substring(line.indexOf("nach")+5));
  87.                     ALGOPlacemarkNode pv = stops.get(new Integer(startNum));
  88.                     ALGOPlacemarkNode pn = stops.get(new Integer(endNum));
  89.                    
  90.                     if (pv == null) System.out.println("Start unbekannt: " + startNum);
  91.                     else if (pn == null) System.out.println("Ende unbekannt: " + endNum);
  92.                     else {
  93.                        
  94.                        
  95.                         double costs = DistanceCalculator.getDistance(pv.getValue().getPoint().getNumericalCoordinates(), pn.getValue().getPoint().getNumericalCoordinates());
  96.                         map.addEdge(new ALGOBuslineEdge(pv.getNodeID(), pn.getNodeID(), costs));
  97.                         map.addEdge(new ALGOBuslineEdge(pn.getNodeID(), pv.getNodeID(), costs));
  98.                         System.out.println("von " + startNum + " nach " + endNum);
  99.                     }
  100.                 }
  101.                 else if (line.contains("keine GPS")) {
  102.                     System.out.println(data[1] + " hat keine GPS-Koordinaten");
  103.                     continue;
  104.                 }
  105.                 else {
  106.                     if (stopNum < lastStopNum) {
  107.                         System.out.println("neue Autobahn");
  108.                         stops.clear();
  109.                         lastStopNum = -1;
  110.                     }
  111.                    
  112.                     if ((data.length == 3) && !data[2].equals(" ")) {
  113.                         String coords [] = data[2].split(",");
  114.                         String name;
  115.                         double lat, lng;
  116.                         ALGOPlacemarkNode node;
  117.                         int pos;
  118.                        
  119.                         name = data[1];
  120.                         pos = name.indexOf("(");
  121.                         if (pos > 0) name = name.substring(0, pos);
  122.                        
  123.                         if (name.equals("motorway junction")) {
  124.                             name = motorwayName + "-" + data[0];
  125.                         }
  126.                        
  127.                         if ((node = map.findNode(name)) == null) {
  128.                             Point p = new Point();
  129.                             Placemark pm = new Placemark();
  130.  
  131.  
  132.                             lng = Double.parseDouble(coords[0]);
  133.                             lat = Double.parseDouble(coords[1]);
  134.  
  135.                             if (lat < minLat) minLat = lat;
  136.                             if (lat > maxLat) maxLat = lat;
  137.  
  138.                             if (lng < minLng) minLng = lng;
  139.                             if (lng > maxLng) maxLng = lng;
  140.  
  141.                             p.setNumericalCoordinates(new double[]{lat, lng, 0.0});
  142.                             pm.addPoint(p);
  143.                             pm.setName(name);
  144.  
  145.                             node = new ALGOPlacemarkNode(count ++, pm);
  146.  
  147.                             map.addNode(node);
  148.                             System.out.println(data[0]+ ":" +name+ ":" + lat + "," + lng + ",0.0");
  149.                            
  150.                        
  151.                         }
  152.                         else System.out.println("schon da: " + name);
  153.                        
  154.                         stops.put(new Integer(stopNum), node);
  155.                     }
  156.                     else if (data[0].equals("MOTORWAY")) motorwayName = data[1];
  157.                 }      
  158.  
  159.                 lastStopNum = stopNum;
  160.             }
  161.         }
  162.         catch (IOException e) {
  163.             System.out.println(e);
  164.             System.out.println("catch");
  165.         }
  166.         System.out.println("yxc MapReader fettich");
  167.         return map;
  168.     }
  169.  
  170.     public double getMinLat() {
  171.         return minLat;
  172.     }
  173.    
  174.     public double getMaxLat() {
  175.         return maxLat;
  176.     }
  177.    
  178.     public double getMinLng() {
  179.         return minLng;
  180.     }
  181.    
  182.     public double getMaxLng() {
  183.         return maxLng;
  184.  
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement