0

我正在阅读 .gpx 文件的正文,需要更改数据的格式,以便可以将其读取为 .kml

.kml 具有latlong.gpx 的交换顺序,因此我需要找到一种方法来连续获取两个子字符串之间的值并在以不同的顺序写入它们之前临时存储它。.kml 也与之分开<time><coords>但这几乎是相同类型的任务。

我查看了许多资源,包括:

我试过了

lat = re.search('<trkpt lat="(.*)" lon="', x)
lon = re.search('" lon="(.*)">', x)

这显然不适用于原始文件中的多个值。我的代码可能不是很pythonic(还)。编码:

def convert(fileName):
    f = open(fileName, "r")
    x = f.read()                
    x = re.sub(r'<trkpt lat="', '           <gx:coord>', x)
    x = re.sub(r'" lon="', ' ', x)
    x = re.sub(r'"><ele>', ' ', x)
    x = re.sub(r'</ele>', '</gx:coord>\n', x)
    x = re.sub(r'<speed>.*?</speed>', '', x)
    return x

让我接近所需的格式。但是我不知道如何连续传递多个值,稍微交换它们并逐步重写

我是 python 新手....请发送帮助。谢谢!

编辑

以下是每种文件类型的示例(为清楚起见,我已删除每个文件的标题文本)

.gpx 看起来像这样,并且有时间和坐标并发。如您所见,每个数据点都存在于<trkpt和之间</trkpt>(.gpx 也有速度,有时还有其他需要清理的东西):

<trkseg> <trkpt lat="-33.8598" lon="151.17912"><ele>7.8</ele><speed>0.9013878</speed><time>2012-09-25T07:38:42Z</time></trkpt><trkpt lat="-33.859936" lon="151.17906"><ele>20.8</ele><speed>2.25</speed><time>2012-09-25T07:38:43Z</time></trkpt><trkpt lat="-33.859818" lon="151.17934"><ele>-3.4</ele><speed>1.5</speed><time>2012-09-25T07:38:45Z</time></trkpt> <trkpt lat="-33.859947" lon="151.17914"><ele>16.2</ele><speed>1.5</speed><time>2012-09-25T07:38:49Z</time></trkpt><trkpt lat="-33.860016" lon="151.1792"><ele>18.0</ele><speed>1.75</speed><time>2012-09-25T07:38:52Z</time></trkpt><trkpt lat="-33.86008" lon="151.17923"><ele>18.4</ele><speed>1.5811388</speed><time>2012-09-25T07:38:57Z</time></trkpt><trkpt lat="-33.86013" lon="151.17932"><ele>18.1</ele><speed>1.75</speed><time>2012-09-25T07:39:03Z</time></trkpt>

好的....这是与<when>坐标分开的等效 .kml <gx:coords>。当然,每个人的数量总是相同的。您可以看到海拔(<ele>在 .gpx 中)是位置数据之后的坐标中未标记的数字。

`

        <when>2012-09-25T07:38:42Z</when>
        <when>2012-09-25T07:38:43Z</when>
        <when>2012-09-25T07:38:45Z</when>
        <when>2012-09-25T07:38:49Z</when>
        <when>2012-09-25T07:38:52Z</when>
        <when>2012-09-25T07:38:57Z</when>
        <when>2012-09-25T07:39:03Z</when>
        <gx:coord>151.17912 -33.8598 7.8</gx:coord>
        <gx:coord>151.17906 -33.859936 20.8</gx:coord>
        <gx:coord>151.17934 -33.859818 -3.4</gx:coord>
        <gx:coord>151.17914 -33.859947 16.2</gx:coord>
        <gx:coord>151.1792 -33.860016 18</gx:coord>
        <gx:coord>151.17923 -33.86008 18.4</gx:coord>
        <gx:coord>151.17932 -33.86013 18.1</gx:coord> 

`

4

1 回答 1

1

这是有效的,但速度很慢。对于 477k 的小 .gpx,它正在编写 207k 的 .kml,需要 198 秒才能完成。我的直觉是,它是stringIO.stringIO(x)如此缓慢。任何如何加快速度的想法都会很棒。

以下是我所做的关键片段:

f = open(fileName, "r")
x = f.read()
x = re.sub(r'\n', '', x, re.S) #remove any newline returns
name = re.search('<name>(.*)</name>', x, re.S)
print "Attachment name (as recorded from GPS device): " + name.group(1)

x = re.sub(r'<(.*)<trkseg>', '', x, re.S)  #strip header
x = x.replace("</trkseg></trk></gpx>",""); #strip footer
x = x.replace("<trkpt","\n<trkpt"); #make the file in lines
x = re.sub(r'<speed>(.*?)</speed>', '', x, re.S) #strip speed
x = re.sub(r'<extensions>(.*?)</extensions>', '', x, re.S) # strip out extensions

然后

#.kml header goes here
kmlTrack = """<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.ope......etc etc

然后

buf = StringIO.StringIO(x)
for line in buf:
            if line is not None:
                    timm = re.search('time>(.*?)</time', line, re.S)
                    if timm is not None:
                            kmlTrack += ("          <when>"+ timm.group(1)+"</when>\n")
                            checkSumA =+ 1
buf = StringIO.StringIO(x)
for line in buf:
            if line is not None:
                    lat = re.search('lat="(.*?)" lo', line, re.S)
                    lon = re.search('lon="(.*?)"><ele>', line, re.S)
                    ele = re.search('<ele>(.*?)</ele>', line, re.S)
                    if lat is not None:
                            kmlTrack += ("          <gx:coord>"+ lon.group(1) + " " + lat.group(1) + " " + ele.group(1) + "</gx:coord>\n")
                            checkSumB =+ 1
if checkSumA == checkSumB:
            #put a footer on
            kmlTrack += """     </gx:Track></Placemark></Document></kml>"""
else:
            print ("checksum error")
            return None

with open("Realbush2.kml", "a") as myfile:
            myfile.write(kmlTrack)
return ("succsesful .kml file-write completed in :" + str(c.seconds) + " seconds.")

再一次,这是可行的,但速度很慢。如果有人可以看到如何加快速度,请告诉我!谢谢

于 2013-04-27T05:02:08.810 回答