0

I am trying to create a kml file in python (using simplekml) that has both points and lines that connect these points. I also want to make the points into squares instead of the default yellow pushpin.

Now i have successfully created kml files that have either points OR lines. But i want to combine the two together into one file. I am reading the files from a csv file. So i thought just putting the two codes together would give me a line and a point, but it did not. I just see points. What am i missing here?

inputfile = csv.reader(open(file, 'r'))
kml = simplekml.Kml()
ls = kml.newlinestring(name='A LineString')
ls.coords = np.array([[float(row[2]),float(row[1])] for row in inputfile ])
ls.extrude = 1
ls.altitudemode = simplekml.AltitudeMode.relativetoground
for row in inputfile:
    kml.newpoint(name=row[0], coords=[(row[2], row[1])])
    pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_square.png'
kml.save("Points_and_Line.kml") 
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
ColleenB
  • 135
  • 5
  • 19

1 Answers1

1

The file is at the end after line 4 is executed so second for loop iterates over an empty list when trying to create the points.

Read in the coordinates for each row from the file as you create points and add the coordinates to a list which can be used to create the line.

inputfile = csv.reader(open("points.dat", 'r')) 
points = []
for row in inputfile:
    coord = (row[2], row[1]) # lon, lat order
    pnt = kml.newpoint(name=row[0], coords=[coord])
    points.append(coord)    
    pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_square.png'

ls = kml.newlinestring(name='A LineString')
ls.coords = np.array(points)
ls.altitudemode = simplekml.AltitudeMode.relativetoground
ls.extrude = 1

kml.save("Points_and_Line.kml")
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75