-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_kml
executable file
·268 lines (191 loc) · 6.66 KB
/
process_kml
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#! /usr/bin/env python
"""
Process an KML file downloaded from Google Maps Engine Lite and process it to
use on Maverick by:
- Download images and place then in a 'photo' directory
"""
from __future__ import absolute_import, print_function
import argparse
import codecs
import os
import shutil
import urllib.parse
import urllib.request
import zipfile
from lxml import etree, objectify
from pykml import parser as kml_parser
# Prevent adding a namespace to every new element
KML = objectify.ElementMaker(
annotate=False,
namespace=None,
nsmap=None,
)
TEXT_ELEMENTS = [
'description', 'text', 'linkDescription', 'displayName', 'Data/*',
]
def get_xml_with_cdata(obj, cdata_elements):
"""Render CDATA tags in the given document."""
# Convert Objectify document to lxml.etree (is there a better way?)
root = etree.fromstring(etree.tostring(etree.ElementTree(obj)))
# Create an xpath expression to search for all desired cdata elements
xpath = '|'.join([
'//kml:' + tag
for tag in cdata_elements
])
results = root.xpath(
xpath,
namespaces={'kml': 'http://www.opengis.net/kml/2.2'},
)
for element in results:
element.text = etree.CDATA(element.text)
return root
def get_parsed_file(filename):
"""
Return a parsed file
"""
return kml_parser.parse(open(filename, 'r')).getroot()
def check_for_dir(path):
"""
Check if the given path exists, otherwise, create it
"""
if not os.path.exists(path):
os.makedirs(path)
return path
def download_pic(url, img_name, dest_dirs):
"""
Download an image from the given url and store it on the specified dir
:param url: URL to the image to download.
:param img_name: Name to use to store the file.
:param dest_dirs: List of directories to store the image.
"""
if ' ' in url:
url = url.split(' ')[0]
path = urllib.parse.urlparse(url).path
extension = os.path.splitext(path)[1]
# TODO: Infer filetype.
if '?' in url:
# Lets assume it's 'jpg'
extension = 'jpg'
filename = '{0}.{1}'.format(img_name, extension)
first_dir = dest_dirs[0]
filepath = os.path.join(first_dir, filename)
# If the file already exists, don't download it again
if not os.path.isfile(filepath):
print(u'Downloading: {0}'.format(url))
try:
urllib.request.urlretrieve(url, filepath)
except urllib.error.URLError:
print('Image not found.')
if os.path.isfile(filepath):
# If there are more directories, copy the image to them.
for other_dir in dest_dirs[1:]:
shutil.copy2(filepath, os.path.join(other_dir, filename))
return filename
def parse_args():
"""Parse arguments from command line."""
parser = argparse.ArgumentParser(
description='Download images referenced in a KML file',
)
parser.add_argument(
'--maverick',
action='store_true',
help='Save pictures in a `photos` directory.')
parser.add_argument(
'input_files',
nargs='+',
default='',
help='Path to files to process',
)
args = parser.parse_args()
return args
def init_maverick(filepath, filename):
"""Initialize the directory for outputting Maverick files."""
path = os.path.join(os.path.dirname(filepath), '%s_maverick' % filename)
return check_for_dir(os.path.join(path, 'photos'))
def init_kmz(filepath, filename):
"""Initialize the directory for outputting the KMZ content."""
return os.path.join(os.path.dirname(filepath), '%s_kmz' % filename)
def process_placemark(placemark, dirs):
"""Download images defined in the placemark."""
name = placemark.name.text
print(name)
try:
extended_data = placemark.ExtendedData
except AttributeError:
return
media_elements = extended_data.findall(
'.//*[@name="gx_media_links"]'
)
for media in media_elements:
url = media.value.text
print(url)
picname = download_pic(url, name, dirs)
# Add new data.
img = '<img src="photos/{filename}">'.format(
filename=picname,
)
extended_data.append(
KML.Data(KML.value(img), name='pictures'))
extended_data.append(
KML.Data(KML.value(placemark.description.text), name='notes'))
placemark.description = KML.description('{img}{desc}'.format(
img=img,
desc=placemark.description,
))
def zipdir(filename, path):
"""Zip a directory."""
with zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(path):
for file in files:
zipf.write(os.path.join(root, file))
def kmz_sequence_name(filename):
"""Determine a sequenced filename to store the new KMZ."""
seq = 0
pattern = '%s%d.kmz'
new_filename = pattern % (filename, seq)
while os.path.exists(new_filename):
seq += 1
new_filename = pattern % (filename, seq)
return new_filename
def main():
"""
Main application
"""
args = parse_args()
for filepath in args.input_files:
filepath = os.path.abspath(filepath)
filename, extension = os.path.splitext(filepath)
if extension.lower() not in ('.kml', '.kmz'):
print('Invalid file type. Skipping.')
continue
kmz_dir = init_kmz(filepath, filename)
kmz_photos_dir = check_for_dir(os.path.join(kmz_dir, 'photos'))
dirs = [kmz_photos_dir]
if args.maverick:
dirs.append(init_maverick(filepath, filename))
if extension.lower() == '.kml':
# Copy KML file.
shutil.copy2(filepath, os.path.join(kmz_dir, 'doc.kml'))
else:
# Extract files from KMZ.
kmz_instance = zipfile.ZipFile(filepath)
kmz_instance.extractall(kmz_dir)
kmz_filepath = os.path.join(kmz_dir, 'doc.kml')
doc = get_parsed_file(kmz_filepath)
print('Downloading pictures for {0}'.format(filepath))
try:
placemarks = doc.Document.Folder.Placemark
except AttributeError:
placemarks = doc.Document.Placemark
for placemark in placemarks:
process_placemark(placemark, dirs)
# Save KMZ file.
with codecs.open(kmz_filepath, 'w', 'utf-8') as kmz_file:
kmlobj_with_cdata = get_xml_with_cdata(doc, TEXT_ELEMENTS)
kmz_file.write(etree.tostring(
kmlobj_with_cdata, pretty_print=True,
).decode('utf-8'))
zipdir(kmz_sequence_name(filename), kmz_dir)
if __name__ == "__main__":
main()
print('Process finished.')