Right, I made a hash of asking this question when I was fried at the back end of yesterday so I'm doing it properly now.
The block below converts some data into lat/long info and then there's an if statement where I can grab certain parts of a large JSON file which there's a section of below.
What I want to do it take the forklift data, convert it's position (which is x and y meters from a known lat/long into an actual lat/long using Pythagoras) and then use the if statement to only print certain parts of the JSON (name, time, converted position). The JSON consists of people & machine info and I want to separate them.
"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
],
"zones": [
{
"id": "Zone001",
"name": "Halli1"
The problem I have seems to be that the first print statement is a float and to be able to pass the result into the if statement it needs to be a string.
Here's the code I have
for f in file_list:
print('Input file: ' + f) # Replace with desired operations
with open(f, 'r') as f:
distros = json.load(f)
output_file = 'forklift_0001_parse' + str(output_nr) + '.csv' #output file name may be changed
with open(output_file, 'w') as text_file:
for distro in distros:
position = distro['position']
R = 6378.1 #Radius of the Earth
brng = 1.57 #Bearing is 90 degrees converted to radians.
d = math.sqrt((position[0]*position[0] + position[1]*position[1]) + 0.00303) #Pythagoras formula to work distance from ref lat/long point
lat1 = math.radians(60.477719)#Reference lat point
lon1 = math.radians(26.941589)#Reference long point
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)
print((lat2, lon2), file=text_file)
if distro['name'].startswith(('Trukki_0001')): #choose desired parameters to be parsed
print (str(distro['name']))
print(distro['name'] + ',' + str(distro['positionTS']) + ',' + str(distro['position']) + ',' + str(distro['lat2']), file=text_file)
#replace desired roots between square brackets
print('Output written to file: ' + output_file)
output_nr = output_nr + 1
The error this gives is
Traceback (most recent call last):
File "position_parse_3.py", line 45, in <module>
print(distro['name'] + ',' + str(distro['positionTS']) + ',' +
str(distro['position']) + ',' + str(distro['lat2']), file=text_file)
KeyError: 'lat2'
I am completely stuck and have tried a few ways to solve it. I've tried to convert them from floats earlier in the script that first prints statement but that did not work (can't convert them implicitly) and I am also concerned I'll lose the accuracy of the result.
If someone could help me pass the result of the lat/long calculation into the IF statement, i would be eternally grateful.