简体   繁体   中英

Python convert x,y data to latitude & longitude data

This code is a function which then runs it through a for loop with an if statement (not included for brevity but basically it lets me separate some factory output data by machine and location). The function is designed to convert x and y data i am given in meters from a known lat and long position and convert that to a new lat and long. I do this using Pythagoras and then a formula i found on SO.

It works without error but the math fails as it adds more or less a degree of longitude to the output. It should produce data very similar to the reference location as it is never more than about 50 meters away from it.

here is a section of the JSON from which the data is taken for interest

"id": "b4994c877c9c",
"name": "forklift_0001",  <---forklift data used in IF statement
"areaId": "Tracking001",
"areaName": "hall_1",
"color": "#FF0000",
"coordinateSystemId": "CoordSys001",
"coordinateSystemName": null,
"covarianceMatrix": [
    0.47,
    0.06,
    0.06,
    0.61
],
"position": [
    33.86,    <---position data converted from known lat/long, X then Y.
    33.07,
    2.15
   ],
    "positionAccuracy": 0.36,
    "positionTS": 1489363199493,
    "smoothedPosition": [
        33.96,
        33.13,
        2.15

and here is the code

import json
import pprint
import time
import math

file_list = ['13_01.json']

output_nr = 1

def positionToLatLon( position ):
    posx = position[0]
    posy = position[1]
    R = 6371 #Radius of the Earth
    brng = 1.57 #Bearing is 90 degrees converted to radians.
    d = math.sqrt((posx*posx) + (posy*posy)) #Distance in km from the lat/long  #Pythagoras formula
    lat1 = math.radians(40.477719)#reference lat point converted to radians
    lon1 = math.radians(16.941589)#reference long point converted to radians
    lat2 = math.asin(math.sin(lat1)*math.cos(d/R) + math.cos(lat1)*math.sin(d/R)*math.cos(brng))
    lon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),
    math.cos(d/R)-math.sin(lat1)*math.sin(lat2))
    lat2 = math.degrees(lat2)
    lon2 = math.degrees(lon2)
    result = []
    result.append(lat2)
    result.append(lon2)
    return result

So it runs without any errors but the output is incorrect, it adds more or less a degree of longitude and so moves the whole result about 60m east making the analysis no good and I cannot see why.

I've looked for a different formula but no luck and my maths isn't good enough to see if I am using an incorrect trig function or something.

All help appreciated.

Change brng = 1.57 to brng = (math.pi/2). This is not a suitable estimate, and is likely the source of some if not all of your error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM