forked from Ceciliawangwang/CycleTrajectory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
155 lines (107 loc) · 3.77 KB
/
utils.py
File metadata and controls
155 lines (107 loc) · 3.77 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import pandas as pd
import geopandas as gpd
import gpxpy
from shapely.geometry import Point
import os
"""This file includes the gpx preparation functions
"""
def get_speed_values(p):
"""This is a function extract speed values from .gpx files
Args:
p (_type_): _description_
Returns:
v1 (_type_): value of speed_2d
v2 (_type_): value of speed_3d
"""
v1 = None
v2 = None
for e in p.extensions:
if e.tag == 'speed_2d':
for echild in list(e):
if echild.tag == 'value':
# print(echild.text)
v1 = echild.text
elif e.tag == 'speed_3d':
for echild in list(e):
if echild.tag == 'value':
v2 = echild.text
return v1, v2
## ================================ remove to GPXHandler ====================================
# def gpx_to_df(gpx):
# """_summary_
# Args:
# gpx (_type_): _description_
# Returns:
# _type_: _description_
# """
# points = []
# for segment in gpx.tracks[0].segments:
# for p in segment.points:
# speed_2d, speed_3d = get_speed_values(p) # function: get_speed_values()
# points.append({
# 'time': p.time,
# 'latitude': p.latitude,
# 'longitude': p.longitude,
# 'elevation': p.elevation,
# 'speed_2d': speed_2d,
# 'speed_3d': speed_3d
# })
# df = pd.DataFrame.from_records(points)
# return df
## ================================= remove to GPXHandler ====================================
def df_to_gdf(df):
""" This is a function ... (could be extended then)
Args:
df (DataFrame): _description_
Returns:
_type_: _description_
"""
geometry = [Point(lon, lat) for lon, lat in zip(df['longitude'], df['latitude'])]
gdf = gpd.GeoDataFrame(df, geometry = geometry, crs= 'EPSG:4326')
return gdf
def df_to_gpx(data):
"""Convert a DataFrame to a GPX object.
Args:
data (DataFrame): The DataFrame to be converted.
Returns:
gpx (GPX): The GPX object created from the DataFrame.
"""
gpx = gpxpy.gpx.GPX()
# Create a new GPX track
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
# Create a new GPX track segment
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
# Add track points
for index, row in data.iterrows():
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(row['latitude'],
row['longitude'],
elevation=row['elevation'],
time=row['time'],
speed=row['speed_2d']))
return gpx
def save_gpx(gpx, root_path, filename):
"""This is a function to save a GPX object to a file.
Args:
gpx (GPX): The GPX object to be saved.
root_path (str): The root directory where the GPX file will be saved.
filename (str): The name of the file to save the GPX data.
"""
path = os.path.join(root_path, filename)
with open(path, "w") as f:
f.write(gpx.to_xml())
def reprojection(gdf, c):
"""This is a function for reprojection
Args:
gdf (_type_): geodataframe to be projected
c (str): objective projection (as a number string, e.g., "27700")
Returns:
gdf_projected (_type_): projected geodataframe
"""
# Construct the full CRS string
ns = f'EPSG:{c}'
gdf_projected = gdf.to_crs(ns)
return gdf_projected
# def test():
# df = fdsfw