-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsv_to_kml.py
80 lines (72 loc) · 2.96 KB
/
csv_to_kml.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
"""
Convert CSV file with GNSS positions into KML files:
Expects CSV files from clean_csv.py
@author: Jonas Beuchert
"""
import glob
import numpy as np
# Path to files goes here
files = glob.glob("*_reduced.csv")
for file in files:
print(f"Process {file}.")
lats, lons = np.loadtxt(file,
delimiter=',',
skiprows=1,
usecols=[1, 2],
unpack=True,
dtype="str")
with open(f"{file[:-4]}.kml", "w") as file_pointer:
file_pointer.write(f"""<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="{file[:-4]}_KML" id="{file[:-4]}_KML">
<SimpleField name="descriptio" type="string"></SimpleField>
<SimpleField name="timestamp" type="string"></SimpleField>
<SimpleField name="begin" type="string"></SimpleField>
<SimpleField name="end" type="string"></SimpleField>
<SimpleField name="altitudeMo" type="string"></SimpleField>
<SimpleField name="tessellate" type="float"></SimpleField>
<SimpleField name="extrude" type="float"></SimpleField>
<SimpleField name="visibility" type="float"></SimpleField>
<SimpleField name="drawOrder" type="float"></SimpleField>
<SimpleField name="icon" type="string"></SimpleField>
<SimpleField name="snippet" type="string"></SimpleField>
</Schema>
<Folder>
<name>{file[:-4]}_KML</name>
<Placemark>
<name>SnapperGPS data</name>
<Style>
<LineStyle>
<color>ff0000ff</color>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<ExtendedData>
<SchemaData schemaUrl="#{file[:-4]}_KML">
<SimpleData name="descriptio">&nbsp;</SimpleData>
<SimpleData name="altitudeMo">clampToGround</SimpleData>
<SimpleData name="tessellate">1</SimpleData>
<SimpleData name="extrude">0</SimpleData>
<SimpleData name="visibility">-1</SimpleData>
</SchemaData>
</ExtendedData>
<MultiGeometry>
<LineString>
<coordinates>
""")
for lat, lon in zip(lats, lons):
file_pointer.write(f""" {lon},{lat}
""")
file_pointer.write(""" </coordinates>
</LineString>
</MultiGeometry>
</Placemark>
</Folder>
</Document>
</kml>
""")
print()