-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcsv2kml.py
More file actions
123 lines (114 loc) · 4.04 KB
/
csv2kml.py
File metadata and controls
123 lines (114 loc) · 4.04 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
#
# This program is called csv2kml.py. It reads a CSV file
# and converts it to KMZ format using simple formattin.
# 2014 Jan 11 Frank Monaldo Modified to start a new LineString
# if there is a time gap of grater than
# 120 seconds.
#
# Import modules
#
import sys
import os
import re
import math
# parse input information
input_file = sys.argv[1]
output_file = sys.argv[2]
#
# Constants
#
radius_earth = 6371.00 # kilometers
max_distance = 2.0 # kilometers
#
# Read csvfile
#
f = open(input_file, 'r')
s = f.readlines()
n= len(s)
#
# Open output file
#
out = open(output_file, "w")
#
# Write prefix
#
out.write('<?xml version="1.0" encoding="UTF-8"?> \n')
out.write('<kml xmlns="http://earth.google.com/kml/2.1"> \n')
out.write('<Folder> \n')
red = 'FF1400FF'
blue = 'FFFF7800'
yellow = 'FF78FFF0'
green = 'FF00FF14'
pink = 'FFB478F0'
color = [ red, blue, yellow, green, pink]
i = 0
ci = 0
old_time = 0
long0 = 0.00
lat0 = 90
print ' Seconds Kilometers'
for line in s:
check= line.find('index')
#
# In the word 'index is in the line, we are at a header line
# check will be les than zero and we do not process
#
if check <0:
x = line.split()
t = int(x[1]) # Convert from string to integer
longitude = x[3]
latitude = x[4]
altitude = x[5]
long1 = float(longitude)
lat1 = float(latitude)
phi0 = math.radians(lat0)
phi = math.radians(lat1)
lam0 = math.radians(long0)
lam = math.radians(long1)
dphi = phi - phi0
dlam = lam - lam0
a = math.sin(dphi/2.0)**2 + math.cos(phi0) * math.cos(phi) * math.sin(dlam/2)**2
c = 2.0 * math.atan2( math.sqrt(a), math.sqrt(1.0-a) )
d = radius_earth * c
#
# If there is at least one position written to the kml file
# and if there is greater than 120 second in time
# start a new line string
#
if i == 0 or (t - old_time) >60 or (d > max_distance):
print 'New seqment time/dist delta = ' + '{0:12d}'.format((t - old_time)) + ' ' + '{:f}'.format(d)
#
# close out old line i > 0
#
if i > 0 :
out.write(' </coordinates> \n')
out.write(' </LineString> \n')
out.write(' </Placemark> \n')
out.write(' \n')
out.write(' <Placemark> \n')
out.write(' <Style> \n')
out.write(' <LineStyle> \n')
cci = ci % 5
colorline = ' <color>' + color[cci] +'</color> \n'
ci = ci + 1
out.write(colorline)
#print colorline
#out.write(' <color>AA3030FF</color> \n')
out.write(' <width>6</width> \n')
out.write(' </LineStyle> \n')
out.write(' </Style> \n')
out.write(' <LineString> \n')
out.write(' <coordinates> \n')
outline = ' '+ longitude + ','+latitude +','+ altitude+ ' \n'
out.write(outline)
i = i + 1
long0 = float(longitude)
lat0 = float(latitude)
old_time = t
out.write(' </coordinates> \n')
out.write(' </LineString> \n')
out.write(' </Placemark> \n')
out.write('</Folder> \n')
out.write('</kml> \n')
out.close()