美文网首页
pyproj distance

pyproj distance

作者: hehehehe | 来源:发表于2021-07-20 16:59 被阅读0次

https://pyproj4.github.io/pyproj/stable/examples.html

from pyproj import CRS
from pyproj import Transformer
import math
import geopy
from geopy import distance

wgs84 = CRS.from_epsg(4326)
geocent = CRS.from_proj4("+proj=geocent +datum=WGS84 +units=m +no_defs")

transformer1 = Transformer.from_crs(wgs84, geocent, always_xy=True)
transformer2 = Transformer.from_crs(geocent, wgs84, always_xy=True)

DEFAULT_RADIUS = 6371008.8


def toRadians(angleInDegrees):
    return (angleInDegrees * math.pi) / 180;


def getDistance(c1, c2):
    radius = DEFAULT_RADIUS;
    lat1 = toRadians(c1[1]);
    lat2 = toRadians(c2[1]);
    deltaLatBy2 = (lat2 - lat1) / 2
    deltaLonBy2 = toRadians(c2[0] - c1[0]) / 2
    a = math.sin(deltaLatBy2) * math.sin(deltaLatBy2) + math.sin(deltaLonBy2) * math.sin(deltaLonBy2) * math.cos(
        lat1) * math.cos(lat2)

    return 2 * radius * math.atan2(math.sqrt(a), math.sqrt(1 - a))


def sqrt_distance(c1, c2):
    sqrt = (c1[0] - c2[0]) * (c1[0] - c2[0]) + (c1[1] - c2[1]) * (c1[1] - c2[1]) + (c1[2] - c2[2]) * (c1[2] - c2[2])
    return math.sqrt(sqrt)


if __name__ == '__main__':
    c1 = transformer1.transform(116, 39, 10)
    c2 = transformer1.transform(115, 38, 10)
    # geocent sqrt distance
    print(sqrt_distance(c1, c2))
    print(distance.geodesic((39, 116, 10), (38, 115, 10)).meters)
    print(distance.distance((39, 116, 10), (38, 115, 10)).meters)
    print(distance.great_circle((39, 116, 10), (38, 115, 10)).meters)
    # openlayers
    print(getDistance((116, 39, 10), (115, 38, 10)))
    # 141175.39195258985
    # 141178.05958707447
    # 141178.05958707447
    # 141196.93600623446
    # 141196.93157375156

package main

import (
    "fmt"
    "github.com/jftuga/geodist"
)

func main() {
    var newYork = geodist.Coord{Lat: 39, Lon: 116}
    var sanDiego = geodist.Coord{Lat: 38, Lon: 115}
    miles, km, _ := geodist.VincentyDistance(newYork, sanDiego)
    fmt.Println(miles, km)
    miles, km = geodist.HaversineDistance(newYork, sanDiego)
    fmt.Println( miles, km)
}
87.72395206279786 141.17805958565472
87.83333205887088 141.35408968051433


相关文章

网友评论

      本文标题:pyproj distance

      本文链接:https://www.haomeiwen.com/subject/hmrrmltx.html