-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse-geojson.go
88 lines (77 loc) · 2.62 KB
/
parse-geojson.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Property struct {
ID string `json:"id"`
Type string `json:"type"`
Highway string `json:"highway"`
Access string `json:"access"`
Lit string `json:"lit"`
Sidewalk string `json:"sidewalk"`
}
type Coordinate = [2]float64
type Geometry struct {
Type string `json:"type"`
Coordinates []Coordinate `json:"coordinates"`
}
type Feature struct {
Type string `json:"type"`
ID string `json:"id"`
Properties Property `json:"properties"`
Geometry Geometry `json:"geometry"`
}
type GeoJson struct {
Type string `json:"type"`
Features []Feature `json:"features"`
}
var graph Graph
func loadGeoJSON() {
// GeoJSON for central london around highbury islington
jsonFile, err := os.Open("./data/central.geojson")
// GeoJSON for Greater London
// from http://download.geofabrik.de/europe/great-britain/england/greater-london.html
// jsonFile, err := os.Open("./data/greater-london-latest.geojson")
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened geojson")
byteValue, _ := ioutil.ReadAll(jsonFile)
var geojson GeoJson
json.Unmarshal(byteValue, &geojson)
for i := 0; i < len(geojson.Features); i++ {
isLineString := geojson.Features[i].Geometry.Type == "LineString"
isHighway := geojson.Features[i].Properties.Highway != ""
hasSidewalk := geojson.Features[i].Properties.Sidewalk != "" || geojson.Features[i].Properties.Sidewalk != "none"
isPath := geojson.Features[i].Properties.Highway == "path"
isValidPath := geojson.Features[i].Properties.Highway == "path" && (geojson.Features[i].Properties.Access == "no" || geojson.Features[i].Properties.Access == "private")
isNotPathOrIsValidPath := !isPath || isValidPath
isLit := geojson.Features[i].Properties.Lit != "" || geojson.Features[i].Properties.Lit == "yes"
if isHighway && isLineString && hasSidewalk && isNotPathOrIsValidPath && isLit {
var feature = geojson.Features[i]
var prev *Node
for j := 0; j < len(feature.Geometry.Coordinates); j++ {
var coords = feature.Geometry.Coordinates[j]
node := CreateNode(coords)
graph.AddNode(&node)
if j != 0 {
graph.AddEdge(&node, prev)
graph.AddEdge(prev, &node)
}
prev = &node
}
}
}
defer jsonFile.Close()
fmt.Println("geojson Graph created with", len(graph.nodes), "nodes")
}
func calculatePath(startCoords Coordinate, endCoords Coordinate) []Node {
nodeStart := graph.FindNode(startCoords)
nodeEnd := graph.FindNode(endCoords)
pathFound := graph.FindPath(graph.nodes[nodeStart], graph.nodes[nodeEnd])
fmt.Println(pathFound)
return pathFound
}