-
Notifications
You must be signed in to change notification settings - Fork 6
/
kmlparser.py
128 lines (111 loc) · 4.21 KB
/
kmlparser.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
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
122
123
124
125
126
127
128
#!/usr/bin/env python
# encoding: utf-8
"""
kmlparser.py
Created by John Wesonga on 2011-11-03.
"""
import os
import csv
import getpass
import logging
from optparse import OptionParser
from bs4 import BeautifulSoup
import gdata.docs.service
import gdata.docs.data
class KmlParser(object):
"""
KmlParser
"""
def __init__(self, kmlfile, csvfile):
self.kmlfile = kmlfile
self.csvfile = csvfile
self.outputfile = ''
self.outputdata = []
def ParseKml(self):
"""
parse_kml
"""
try:
handler = open(self.kmlfile).read()
soup = BeautifulSoup(handler, "html.parser")
for message in soup.findAll('placemark'):
locationdata = {}
coordinates = message.find('coordinates')
locationdata['geometry'] = '<LineString> %s </LineString>' % (coordinates)
names = message.findAll('name')
for name in names:
text = name.find(text = True)
locationdata['name'] = text.encode('utf-8')
self.outputdata.append(locationdata)
except IOError as (errno, strerror):
logging.error("I/O error(%d): %s" %(errno, strerror))
def WriteCsv(self):
"""
write_csv
"""
self.outputfile = os.getcwd() + '/' + self.csvfile
try:
out = open(self.outputfile,'w')
print 'Writing output to file ', self.outputfile
try:
fieldnames = sorted(self.outputdata[0].keys())
writer = csv.DictWriter(out, dialect = 'excel',
fieldnames = fieldnames,
extrasaction='ignore', quoting=csv.QUOTE_NONNUMERIC)
headers = dict((n, n) for n in fieldnames)
writer.writerow(headers)
for row in self.outputdata:
writer.writerow(row)
print 'Output file ', self.outputfile, ' written'
finally:
out.close()
except IOError as (errno, strerror):
logging.error("I/O error(%d): %s" % (errno, strerror))
return self.outputfile
def Upload(self, output_file):
"""
upload
"""
upload_prompt = raw_input('Would you like to upload your csv file to Google Docs? (y/n) ')
if upload_prompt == 'y':
email = raw_input('Please enter your gmail address: ')
password = getpass.getpass('Please enter your gmail password: ')
client = gdata.docs.service.DocsService()
client.ClientLogin(email, password, 'kmltocsv')
uploaded_filename = self.csvfile[0:-4]
mediasource = gdata.data.MediaSource(file_path = output_file,
content_type = gdata.docs.service.SUPPORTED_FILETYPES['TXT'])
entry = client.Upload(mediasource, uploaded_filename)
if entry:
logging.info("Upload successful")
logging.info("Document now accessible at:", entry.GetAlternateLink().href)
else:
print 'Upload error'
else:
logging.info("Please access your CSV file at %s" % self.outputfile)
def main():
"""
Main method
"""
parser = OptionParser()
parser.add_option("-f", "--file", dest = "kmlfile",
help = "KML file to be parsed",
metavar = "FILE")
parser.add_option("-d", "--output", dest = "csvfile",
help = "CSV output file",
metavar = "FILE")
(options, args) = parser.parse_args()
if not options.kmlfile:
print "please type python " \
"kmlparser.py --file=[kmlfilename] --output=[csvfilename]"
elif not options.csvfile:
print "please type python " \
"kmlparser.py --file=[kmlfilename] --output=[csvfilename]"
else:
kmlparser = KmlParser(kmlfile=options.kmlfile,
csvfile=options.csvfile)
kmlparser.ParseKml()
upload_file = kmlparser.WriteCsv()
kmlparser.Upload(upload_file)
if __name__ == "__main__":
main()