Last active
August 29, 2015 14:14
-
-
Save pylemon/8f106bed0289ab725b27 to your computer and use it in GitHub Desktop.
compare the distance calculate between django geo and go.geo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/paulmach/go.geo" | |
) | |
func main() { | |
// django.geo: 1143 m (result from django geo) | |
// go.geo: 1142.81 m (Mercator.Project + DistanceFrom) | |
// go.geo: 977.26 m (GeoDistanceFrom) | |
lat1, lng1 := 31.223440, 121.445300 // Jin'an Temple | |
lat2, lng2 := 31.226290, 121.435590 // Leader Building | |
p1, p2 := geo.NewPoint(lng1, lat1), geo.NewPoint(lng2, lat2) | |
// calculates GeoDistance using math, result is inaccurate. | |
fmt.Println(p1, p2) | |
fmt.Printf("%f\n", p2.GeoDistanceFrom(p1)) | |
// point value has converted to mercator projection | |
geo.Mercator.Project(p1) | |
geo.Mercator.Project(p2) | |
// should calculate euclidian distance | |
fmt.Println(p1, p2) | |
fmt.Printf("%f\n", p2.DistanceFrom(p1))] | |
// raw sql | |
// SELECT ST_Distance(ST_Transform(ST_GeomFromText('POINT(121.445300 31.223440)', 4326), 3857), ST_Transform(ST_GeomFromText('POINT(121.435590 31.226290)', 4326), 3857)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment