-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElevation2Plot.py
73 lines (66 loc) · 2.27 KB
/
Elevation2Plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import requests
import sys
import json
import pandas as pd
from geopy import distance
from zipfile import ZipFile
from bs4 import BeautifulSoup as bs
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import make_interp_spline
distances=[0,]
elevations=[]
coorListFull =[]
def parse_kmz():
"""Extract and read coordinates from KML file"""
try:
file = sys.argv[1]
except IndexError:
raise SystemExit(f"====\nUsage: {sys.argv[0]} <.kmz file name>\n====")
with ZipFile(file, 'r') as kmz:
with kmz.open('doc.kml', 'r') as kml:
print("File successfully loaded...")
bs_data = bs(kml, 'lxml')
print("Listing coordinates...")
tagData = bs_data.find('coordinates').text.strip().split(' ')
print("Requesting elevation data...\nThis might take a moment...")
get_elevation(tagData)
print("Calculating distances...")
get_distance(coorListFull)
def get_elevation(coorList):
"""
Perform GET request to the server [https://api.open-elevation.com]
More info: https://open-elevation.com
"""
for item in coorList:
query = item.split(',')[1] + ',' + item.split(',')[0]
coorListFull.append(query)
uri = f'https://api.open-elevation.com/api/v1/lookup?locations={query}'
#print(f'Trying ID >> {query}')
response = requests.get(uri)
data = response.json()
if data['results'] != None:
elevations.append(data['results'][0]['elevation'])
def get_distance(coorList):
"""Calculate the distance between coordinates"""
a = 1
while a < len(coorList):
distances.append(distance.distance(coorList[0], coorList[a]).m)
a += 1
plot(distances, elevations)
def plot(dist, elev):
"""Generate the plot"""
X_Y_Spline = make_interp_spline(dist, elev)
X_ = np.linspace(min(distances), max(distances), 30)
Y_ = X_Y_Spline(X_)
f = plt.figure()
f.set_figheight(1)
plt.plot(X_, Y_) #Smoother plot [rather than >> plt.plot(distances, elevations)]
plt.title("Elevation Data")
plt.xlabel("Distance (m)")
plt.ylabel("Elevation (m)")
print("Plot successfully generated...")
plt.show()
print('Done')
if __name__ == '__main__':
parse_kmz()