-1

I will try to explain the problem as best as possible.

The code below is used to create a Kml file for Google Earth from a csv file (points recorded by a drone).
Each line of the "csv" file includes the time, longitude, latitude and altitude of a point.

00:06,9.166118371854222,42.48375034483273,2.58 00:07,9.166119054009813,42.48374920008085,4.354 00:07,9.16612011321665,42.48374788423119,5.601 00:08,9.166121409787642,42.483747507145594,5.765 00:08,9.166121049499995,42.48374617693683,5.973

The code creates 2 folders: one for the flight track, the other for the list of all points.
My code is:

'''
the command line :
python test_kml.py
'''

import csv
import simplekml

kml = simplekml.Kml()

# read csv file
inputfile = csv.reader(open("csv_file.csv", 'r'))

# list where to store coords
points = []

# create folder for points
fol_points = kml.newfolder(name="The points")

# create points
for row in inputfile:
    #lon_value = float(row[1]) # get lon
    #lat_value = float(row[2]) # get lat
    #alt_value = float(row[3]) # get alt
    #name_value = row[0]       # get name
    coord = (float(row[1]), float(row[2]), float(row[3])) # Tuple with lon, lat, alt order
    pnt = fol_points.newpoint(name=row[0], coords=[coord]) # create a new point whith coord value
    points.append(coord) # add coord to points list
    pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png' # the icon shape
    pnt.altitudemode = simplekml.AltitudeMode.relativetoground # set altitude option to show points at their own elevations

# create folder for linestring
fol_line = kml.newfolder(name="The line")

#create line
ls = fol_line.newlinestring(name='the LineString') # create a new line
ls.coords = points # set points values of new linestring with points values
ls.style.linestyle.width = 3 #set line width
ls.altitudemode = simplekml.AltitudeMode.relativetoground # set altitude option to show points at their own elevations
ls.extrude = 0 # option to avoid vertical line between ground and points

#try to put radiobutton on folder
fol_line.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder
fol_points.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder

#save kml file
kml.save("csv_file.kml")

The 2 problematic lines are:

fol_line.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder fol_points.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder

which create radio buttons for folders but also for items inside folders, as shown in the image below.
Google Earth capture with radio buttons

By deleting the 2 lines, we get the following result:
Google Earth capture without radio buttons

With Google Earth, it is possible to change this so that the radio buttons are only on folders and not on items inside folders, as shown in the image below.
Google Earth final result

I did not find in the documentation of the Python simplekml package, the option(s) which would make it possible to obtain the result obtained by Google Earth.

Could someone help me to achieve this last result using simplekml or with another Python package?

Thanks

Toto1515
  • 11
  • 2
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Apr 24 '23 at 15:11

1 Answers1

0

I just find a solution...

I remove the 2 lines

fol_line.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder fol_points.style.liststyle.listitemtype = simplekml.ListItemType.radiofolder

and, in order to add some lines to the KML file, at the end of the code, I wrote

# open csv file read only
fr = open("csv_file.kml", 'r').read()

#search the index of the line after "<Document ...
index_document = fr.find('<Document') # search for '<Document
index_suite = fr.find('\n',index_document) #search the end of line
index_deb=index_suite + 1 #get index of next line

# open csv file for write
fw = open("csv_file.kml",'w') 

#write the strings befor index
fw.write(fr[0:index_deb]) 

#write new strings
fw.write("\t\t<open>1</open>\n")
fw.write("\t\t<Style>\n")
fw.write("\t\t<ListStyle>\n")
fw.write("\t\t\t<listItemType>radioFolder</listItemType>\n")
fw.write("\t\t\t</ListStyle>\n")
fw.write("\t\t</Style>\n")

#write the strings after index
fw.write(fr[index_deb:])

That's the solution I found and it works fine for me...

Toto1515
  • 11
  • 2