- Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparse-geojson.go
90 lines (81 loc) · 2.48 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
89
90
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
// Property Geojson Property
typePropertystruct {
IDstring`json:"id"`
Typestring`json:"type"`
Highwaystring`json:"highway"`
Accessstring`json:"access"`
Litstring`json:"lit"`
Sidewalkstring`json:"sidewalk"`
}
// Coordinate pair of lng, lat
typeCoordinate [2]float64
// Geometry Geojson Geometry
typeGeometrystruct {
Typestring`json:"type"`
Coordinates []Coordinate`json:"coordinates"`
}
// Feature Geojson Feature
typeFeaturestruct {
Typestring`json:"type"`
IDstring`json:"id"`
PropertiesProperty`json:"properties"`
GeometryGeometry`json:"geometry"`
}
// GeoJSON data structure
typeGeoJSONstruct {
Typestring`json:"type"`
Features []Feature`json:"features"`
}
funccreateGraph(geojsonGeoJSON) Graph {
vargraphGraph
graph.Init()
fori:=0; i<len(geojson.Features); i++ {
varfeature=geojson.Features[i]
varprev*Node
forj:=0; j<len(feature.Geometry.Coordinates); j++ {
varcoords=feature.Geometry.Coordinates[j]
node:=Node{coords[0], coords[1]}
ifj!=0 {
graph.AddEdge(node, prev)
graph.AddEdge(*prev, &node)
}
prev=&node
}
}
fmt.Println("geojson Graph created with", len(graph.edges), "nodes and", graph.numEdges, "edges")
returngraph
}
funcloadGeoJSON(filenamestring) Graph {
// GeoJSON for Greater London
// from http://download.geofabrik.de/europe/great-britain/england/greater-london.html
// geoJsonDownloadLink := "https://ucb7e1be7e59700bb615fc052d06.dl.dropboxusercontent.com/cd/0/get/ApeoomlSroMi4LLrd88j2O1YyfZcz-fnOcR-BMu7Ca3F-aclMpnyLmlzJPZtgze6QSfiGh_SZAcCl-TzGSrcNR14iFsaOBl-vs7CsUzWnL6UbsaH7V_CR-apDThjG8fUH78/file?dl=1DownloadLink"
// resp, err := http.Get(geoJsonDownloadLink)
// if err != nil {
// // handle error
// }
// defer resp.Body.Close()
// jsonFile := resp.Body
// GeoJSON for central london around highbury islington
// jsonFile, err := os.Open("./data/central.geojson")
// jsonFile, err := os.Open("./data/greater-london-latest.geojson")
// 2.73GB
jsonFile, err:=os.Open(filename)
iferr!=nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened geojson")
byteValue, _:=ioutil.ReadAll(jsonFile)
fmt.Println("Successfully ReadAll geojson")
vargeojsonGeoJSON
json.Unmarshal(byteValue, &geojson)
fmt.Println("Successfully Unmarshalled geojson with N features", len(geojson.Features))
deferjsonFile.Close()
returncreateGraph(geojson)
}