File size: 1,731 Bytes
26bc902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f685be0
 
 
 
 
 
 
 
26bc902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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()