Commits

Benoît Allard committed 525924d

osm.go: Order of points is now always correct

  • Participants
  • Parent commits e5ed7c0

Comments (0)

Files changed (1)

 
 
 type Nd struct {
-	Ref int `xml:"ref,attr"`
+	Ref uint `xml:"ref,attr"`
 }
 
 type Tag struct{
 }
 
 type Way struct{
-	Id int `xml:"id,attr"`
+	Id uint `xml:"id,attr"`
 	Nds []Nd `xml:"nd"`
 	Tags []Tag `xml:"tag"`
 }
 
 type Node struct{
-	Id int `xml:"id,attr"`
+	Id uint `xml:"id,attr"`
 	Lat float64 `xml:"lat,attr"`
 	Lon float64 `xml:"lon,attr"`
 }
 	Nodes []Node `xml:"node"`
 }
 
-func getWay(id int, out chan []Point){
+func getWay(id uint, out chan []Point){
 	url := fmt.Sprintf("http://www.openstreetmap.org/api/0.6/way/%d", id)
-	fmt.Println("Fetching ", url)
 	resp, err := http.Get(url)
 	if err != nil {
 		log.Fatal(err)
 		log.Fatal(err)
 	}
 
-	fmt.Println(w)
+	theWay := w.Ways[0]
 
 	nodes_c := make(chan Node)
 
-	for _, nd := range w.Ways[0].Nds{
+	for _, nd := range theWay.Nds{
 		go getNode(nd.Ref, nodes_c)
 	}
-	nodes := make([]Node, len(w.Ways[0].Nds))
+	nodes := make(map[uint]Node)
 
 	lon0 := 0.0
 	lat0 := 0.0
 
-	for i := range w.Ways[0].Nds{
-		nodes[i] = <- nodes_c
-		lon0 += nodes[i].Lon
-		lat0 += nodes[i].Lat
+	// Just run it for the right amount of times ...
+	for _ = range theWay.Nds {
+		nd := <- nodes_c
+		nodes[nd.Id] = nd
+		lon0 += nd.Lon
+		lat0 += nd.Lat
 	}
 
 	lon0 /= float64(len(nodes))
 	rx := 6371 * math.Acos(math.Pow(math.Sin(radians(lat0)),2) + math.Pow(math.Cos(radians(lat0)),2) * math.Cos(radians(lon0+.5) - radians(lon0-.5)))
 	ry := 6371 * math.Acos(math.Sin(radians(lat0 - .5)) * math.Sin(radians(lat0+.5)) + math.Cos(radians(lat0-.5)) * math.Cos(radians(lat0+.5)))
 
-	points := make([]Point, len(w.Ways[0].Nds))
-	for i, nd := range(nodes){
-		points[i] = Point{(nd.Lon - lon0)*rx*1000, -(nd.Lat - lat0)*ry*1000}
+	points := make([]Point, len(theWay.Nds))
+	for i, nd := range(theWay.Nds){
+		points[i] = Point{(nodes[nd.Ref].Lon - lon0)*rx*1000, -(nodes[nd.Ref].Lat - lat0)*ry*1000}
 	}
 	out <- points
 }
 
-func getNode(ref int, out chan Node){
+func getNode(ref uint, out chan Node){
 	url := fmt.Sprintf("http://www.openstreetmap.org/api/0.6/node/%d", ref)
-	fmt.Println("Fetching ", url)
 	resp, err := http.Get(url)
 	if err != nil {
 		log.Fatal(err)
 		log.Fatal(err)
 	}
 
-	fmt.Println(w)
-
-	out <- w.Nodes[0]
+	for node := range w.Nodes {
+		out <- node
+	}
 }
 
 type way_json struct{
 
 func main(){
 
-	ways := []int{ 117739400, 226111405, 115796878}
+	ways := []uint{ 117739400, 226111405, 115796878}
 	ways_c := make(chan []Point)
 	for _, w := range(ways){
 		go getWay(w, ways_c)
 	ws := make([]way_json, len(ways))
 	for i := range(ways){
 		ws[i] = way_json{fmt.Sprintf("osm-%d", ways[i]), <- ways_c}
-//		fmt.Println(ws[i].Points)
 	}
 	b, err := json.Marshal(ws)
 	if err != nil {