我在 google 地球中看不到多边形

I can't see the polygon in google earth

这是我用来生成 .kml 文件的代码:

def kmlForLab2():
    #XYpoints1_wgs84
    #XYpoints1_wgs84.csv
    
    #Input the file name."JoeDupes3_forearth"
    fname = input("Enter file name WITHOUT extension: ")
    data = csv.reader(open(fname + '.csv'), delimiter = ',')
    
    #Skip the 1st header row.
    #data.next()
    #Open the file to be written.
    f = open('Buffered_kml.kml', 'w')
    
    #Writing the kml file.
    f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
    f.write("<kml xmlns='http://earth.google.com/kml/2.0'>\n")
    f.write("<Document>\n")
    f.write("<!-- first buffer -->")
    f.write("<Placemark>\n")
    f.write("   <name>" + fname + '.kml' +"</name>\n")
    f.write("   <Polygon> <outerBoundaryIs> <LinearRing>\n")
    f.write("           <coordinates>\n" )
    next(data)
    for row in data:
        f.write(str((row[1])) + "," + " "+ (str(row[2]))+"\n") 
    f.write("           </coordinates>\n" )
    f.write("   </LinearRing> </outerBoundaryIs> </Polygon> \n")
    f.write("</Placemark>\n")
    f.write("</Document>\n")
    f.write("</kml>\n")
    f.close()
    print ("File Created. ")
    print ("Press ENTER to exit. ")

它会生成 .kml 文件,但不会放大新西兰境内的多边形。发生什么事了?

文档的 <coordinates> 部分旨在包含逗号分隔的元组,每个元组与下一个元组之间用空格分隔。所以像这样:

-77.05788457660967,38.87253259892824,100
-77.05465973756702,38.87291016281703,100

或:

-77.05788457660967,38.87253259892824,100 -77.05465973756702,38.87291016281703,100

您的代码在坐标元组的中间插入空格。给出这样的输入:

unknown column,lat,lon,ele
1,-77.05788457660967,38.87253259892824,100
2,-77.05465973756702,38.87291016281703,100
3,-77.05315536854791,38.87053267794386,100
4,-77.05552622493516,38.868757801256,100
5,-77.05844056290393,38.86996206506943,100
6,-77.05788457660967,38.87253259892824,100

您的代码生成以下 <coordinates> 部分:

<coordinates>
-77.05788457660967, 38.87253259892824
-77.05465973756702, 38.87291016281703
-77.05315536854791, 38.87053267794386
-77.05552622493516, 38.868757801256
-77.05844056290393, 38.86996206506943
-77.05788457660967, 38.87253259892824
</coordinates>

这显示不正确。但是如果我们修改你的代码,让它 看起来像这样:

    for row in data:
        f.write(str((row[1])) + "," + (str(row[2]))+"\n") 

或者像这样使用 join

    for row in data:
        f.write(",".join(str(x) for x in row[1:3]))
        f.write("\n")

我们得到:

<coordinates>
-77.05788457660967,38.87253259892824
-77.05465973756702,38.87291016281703
-77.05315536854791,38.87053267794386
-77.05552622493516,38.868757801256
-77.05844056290393,38.86996206506943
-77.05788457660967,38.87253259892824
</coordinates>

这在 Google 地球中正确显示。