I've created a perl script to create multiple line files from points. It iterates through, creates everything as expected. When you hover over a line, I get a balloon with all of the attribute information that I'm pushing. Only issue, I don't want 300 separate kml files. So, I modified the script to write to a kml "folder". All of the lines display as expected. Looks just like the individual files, but I can open the one file and see everything. EXCEPT, now the attribute information does not show up when I hover over a line/click on a line/even when I go to properties. It's the same exact setup that is in the individual file…Is it impossible to show several balloons in one file, even if they are separate "documents"?
[GIS] KML Balloon Not Showing Up In
google earthkmlkmzperlxml
Related Solutions
There is a QGIS Plugin available which "looks at the KML description field to see if there are two column tables with entries, "tag=value" pairs, or "tag: value" pairs that could be expanded into table fields."
KML Tools: https://plugins.qgis.org/plugins/kmltools/
You can use the fastkml python lib for this. It can be found at https://fastkml.readthedocs.io/en/latest/ with all instructions to the usage.
Here is a code example for using this library to generate a kml structure.
from pathlib import Path
from fastkml import kml
from shapely.geometry import shape
def read_kml(kml_text):
k = kml.KML()
# Reading the kml from a text string
k.from_string(kml_text)
k_doc = list(k.features())[0]
# Getting the first document child that must be a folder or a placemark
# but you can iterate over it to get multiple folders or placemarks
doc_child = list(k_doc.features())[0]
lst_geoms = list()
if isinstance(doc_child, kml.Folder):
for feature in doc_child.features():
lst_geoms.append(feature.geometry)
elif isinstance(doc_child, kml.Placemark):
lst_geoms.append(doc_child.geometry)
# Returning the geometries found list
return lst_geoms
def generate_kml():
# Final file IO
k = kml.KML()
ns = '{http://www.opengis.net/kml/2.2}'
# Creating document structure
document = kml.Document(
ns, "SOME_IDENTIFICATION",
"SOME_NAME", "SOME_DESCRIPTION"
)
k.append(document)
# Creating folder structure
# Note that a kml can have multiple folders
folder = kml.Folder(
ns, "SOME_IDENTIFICATION",
"SOME_NAME", "SOME_DESCRIPTION"
)
document.append(folder)
# Creating placemark structure
# Note that a kml can have multiple placemarks
placemark = kml.Placemark(
ns, "SOME_IDENTIFICATION",
"SOME_NAME", "SOME_DESCRIPTION"
)
# Passing the geometry to the placemark
# It can be a shapely geom (Polygon, MultiPolygon, Polyline, Point, etc)
placemark.geometry = shape("SOME_GEOJSON_GEOMETRY_HERE")
# Adding the placemark to the previous created folder
folder.append(placemark)
final_kml_text = k.to_string(prettyprint=True)
output = Path(__file__).parent / "test.kml"
output.write_text(final_kml_text.encode("UTF-8"))
if __name__ == '__main__':
# U can either generate a kml from a existing data
generate_kml()
# or read another kml to get the features and properties
# to generate your own
geometries = read_kml("SOME_KML_STRINGIFIED_TEXT")
Best Answer
I think you must to adapt your code perl script to generate a functional code like below:
This way, you''l have all your lines inside a single kml, using a unique baloon style, and the result can be seen below: