Skip to content

Commit e47b8ee

Browse files
committed
add gis
1 parent baf4150 commit e47b8ee

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

modules/gis/distance/distance.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/asmarques/geodist"
6+
geo "github.com/kellydunn/golang-geo"
7+
"gitlab.com/psns/geocalc"
8+
)
9+
10+
type Dis struct {
11+
Start geocalc.Point
12+
End geocalc.Point
13+
Distance float64
14+
}
15+
16+
// Distance calculation
17+
func Distance() {
18+
for _, dis := range []Dis{
19+
{
20+
//41.53786, 121.5665 40.99264, 123.1513 145.661公里 / 90.509英里
21+
Start: geocalc.Point{Lat: 41.53786, Lon: 121.5665},
22+
End: geocalc.Point{Lat: 40.99264, Lon: 123.1513},
23+
Distance: 145661,
24+
},
25+
{
26+
Start: geocalc.Point{Lat: 30.551589, Lon: 114.202234},
27+
End: geocalc.Point{Lat: 30.557181, Lon: 114.503553},
28+
Distance: 28841,
29+
},
30+
} {
31+
d1, _ := geodist.VincentyDistance(geodist.Point{Lat: dis.Start.Lat, Long: dis.Start.Lon}, geodist.Point{Lat: dis.End.Lat, Long: dis.End.Lon})
32+
d2 := geodist.HaversineDistance(geodist.Point{Lat: dis.Start.Lat, Long: dis.Start.Lon}, geodist.Point{Lat: dis.End.Lat, Long: dis.End.Lon})
33+
d3 := D2(dis.Start.Lat, dis.Start.Lon, dis.End.Lat, dis.End.Lon)
34+
d4 := geocalc.Distance(dis.Start, dis.End)
35+
d5 := geo.NewPoint(dis.Start.Lat, dis.Start.Lon).GreatCircleDistance(geo.NewPoint(dis.End.Lat, dis.End.Lon))
36+
fmt.Printf("d1:%.4f,d2:%.4f,d3:%.4f,d4:%.4f,d5:%.4f\n", d1, d2, d3/1000, d4/1000, d5)
37+
}
38+
}
39+
40+
func main() {
41+
Distance()
42+
}

modules/gis/distance/f.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "math"
4+
5+
func D2(lat1, lon1, lat2, lon2 float64) float64 {
6+
const r = 6378137 // Earth radius in METERS https://en.wikipedia.org/wiki/Earth_radius
7+
8+
// Distance between lat and longs
9+
dLat := (lat2 - lat1) * math.Pi / 180.0
10+
dLon := (lon2 - lon1) * math.Pi / 180.0
11+
12+
// convert to radians
13+
lat1 = lat1 * math.Pi / 180.0
14+
lat2 = lat2 * math.Pi / 180.0
15+
16+
// apply formulae
17+
a := (math.Pow(math.Sin(dLat/2), 2)) + math.Pow(math.Sin(dLon/2), 2)*math.Cos(lat1)*math.Cos(lat2)
18+
c := 2 * math.Asin(math.Sqrt(a))
19+
20+
return r * c
21+
}

modules/gis/go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module psns.geocalc
2+
3+
go 1.17
4+
5+
require (
6+
github.com/asmarques/geodist v1.0.1
7+
github.com/kellydunn/golang-geo v0.7.0
8+
gitlab.com/psns/geocalc v1.3.1
9+
)
10+
11+
require (
12+
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
13+
github.com/kylelemons/go-gypsy v1.0.0 // indirect
14+
github.com/lib/pq v1.10.3 // indirect
15+
github.com/ziutek/mymysql v1.5.4 // indirect
16+
)

0 commit comments

Comments
 (0)