import json from xml.etree.ElementTree import Element, SubElement, tostring import xml.dom.minidom as minidom import gradio as gr def convert_json_to_kml(json_file_path): with open(json_file_path.name, 'r') as f: data = json.load(f) kml = Element('kml', xmlns="http://www.opengis.net/kml/2.2") document = SubElement(kml, 'Document') for i, track in enumerate(data): placemark = SubElement(document, 'Placemark') name = SubElement(placemark, 'name') name.text = f"Track {i + 1}" # Style for pink line, width 2 style = SubElement(placemark, 'Style') linestyle = SubElement(style, 'LineStyle') color = SubElement(linestyle, 'color') color.text = 'ff00caff' # pink (AABBGGRR) width = SubElement(linestyle, 'width') width.text = '2' linestring = SubElement(placemark, 'LineString') tessellate = SubElement(linestring, 'tessellate') tessellate.text = '1' coordinates = SubElement(linestring, 'coordinates') coord_text = "\n".join( [f"{point['longitude']},{point['latitude']},0" for point in track] ) coordinates.text = coord_text kml_str = minidom.parseString(tostring(kml)).toprettyxml(indent=" ") kml_path = "/tmp/converted_track.kml" with open(kml_path, "w") as f: f.write(kml_str) return kml_path iface = gr.Interface( fn=convert_json_to_kml, inputs=gr.File(label="Upload JSON File"), outputs=gr.File(label="Download KML File"), title="JSON to KML Converter", description="Upload a JSON file with latitude/longitude tracks to generate a KML file." ) if __name__ == "__main__": iface.launch()