将 pandas DataFrame 或经度和纬度列表转换为 python 中的 gpx 文件
convert pandas DataFrame or list of longitude and latitudes to a gpx file in python
我在 pandas DataFrame 中有经度和纬度值,但我想将其转换为 gpx 文件,以便稍后在 google 地图中可视化。我怎样才能用 python 做到这一点?我查看了 gpxpy 库,但示例显示了如何加载或读取 gpx 文件,而我想做相反的事情。我想读取一个 csv 文件并将其转换为 gpx 文件或直接从 pandas Dataframe.
转换
请参考:https://pypi.org/project/gpxpy/
我猜你需要这个包来完成你的任务。然后你可以使用他们提供的例子:
import gpxpy
import gpxpy.gpx
with open("outfile.gpx", "w") as f:
f.write( gpx.to_xml())
将 pandas df
转换为 xml
的描述如下:
How do convert a pandas/dataframe to XML?
这个怎么样:(改编自 https://pypi.org/project/gpxpy/ 的创建文件部分)
# import pandas as pd
# import numpy as np
# df = pd.DataFrame(90*np.random.random((5, 2)))
# print(df)
import gpxpy
import gpxpy.gpx
gpx = gpxpy.gpx.GPX()
# Create first track in our GPX:
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
# Create first segment in our GPX track:
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
# Create points:
for idx in df.index:
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(df.loc[idx, 0], df.loc[idx, 1]))
# print(gpx.to_xml())
with open('output.gpx', 'w') as f:
f.write(gpx.to_xml())
以上代码示例数据:
数据框:
# 0 1
# 0 76.297096 86.421851
# 1 26.041973 5.265947
# 2 24.292204 37.074964
# 3 2.033896 19.136688
# 4 13.527561 25.136911
XML数据:
# <?xml version="1.0" encoding="UTF-8"?>
# <gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="gpx.py -- https://github.com/tkrajina/gpxpy">
# <trk>
# <trkseg>
# <trkpt lat="76.29709588485436" lon="86.42185081600744">
# </trkpt>
# <trkpt lat="26.041973450515652" lon="5.2659474962495985">
# </trkpt>
# <trkpt lat="24.292204051893012" lon="37.07496383039659">
# </trkpt>
# <trkpt lat="2.033895516776183" lon="19.1366881465948"> </trkpt>
# <trkpt lat="13.527560800715804" lon="25.136910635806306">
# </trkpt>
# </trkseg>
# </trk>
# </gpx>
我在 pandas DataFrame 中有经度和纬度值,但我想将其转换为 gpx 文件,以便稍后在 google 地图中可视化。我怎样才能用 python 做到这一点?我查看了 gpxpy 库,但示例显示了如何加载或读取 gpx 文件,而我想做相反的事情。我想读取一个 csv 文件并将其转换为 gpx 文件或直接从 pandas Dataframe.
转换请参考:https://pypi.org/project/gpxpy/
我猜你需要这个包来完成你的任务。然后你可以使用他们提供的例子:
import gpxpy
import gpxpy.gpx
with open("outfile.gpx", "w") as f:
f.write( gpx.to_xml())
将 pandas df
转换为 xml
的描述如下:
How do convert a pandas/dataframe to XML?
这个怎么样:(改编自 https://pypi.org/project/gpxpy/ 的创建文件部分)
# import pandas as pd
# import numpy as np
# df = pd.DataFrame(90*np.random.random((5, 2)))
# print(df)
import gpxpy
import gpxpy.gpx
gpx = gpxpy.gpx.GPX()
# Create first track in our GPX:
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
# Create first segment in our GPX track:
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
# Create points:
for idx in df.index:
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(df.loc[idx, 0], df.loc[idx, 1]))
# print(gpx.to_xml())
with open('output.gpx', 'w') as f:
f.write(gpx.to_xml())
以上代码示例数据:
数据框:
# 0 1
# 0 76.297096 86.421851
# 1 26.041973 5.265947
# 2 24.292204 37.074964
# 3 2.033896 19.136688
# 4 13.527561 25.136911
XML数据:
# <?xml version="1.0" encoding="UTF-8"?>
# <gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="gpx.py -- https://github.com/tkrajina/gpxpy">
# <trk>
# <trkseg>
# <trkpt lat="76.29709588485436" lon="86.42185081600744">
# </trkpt>
# <trkpt lat="26.041973450515652" lon="5.2659474962495985">
# </trkpt>
# <trkpt lat="24.292204051893012" lon="37.07496383039659">
# </trkpt>
# <trkpt lat="2.033895516776183" lon="19.1366881465948"> </trkpt>
# <trkpt lat="13.527560800715804" lon="25.136910635806306">
# </trkpt>
# </trkseg>
# </trk>
# </gpx>