-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateKML.py
62 lines (54 loc) · 1.52 KB
/
generateKML.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
"""
generates KML files for visualization.
"""
import urllib
import lxml.etree
import re
import json
import time
import lxml.etree
import os
from pykml.factory import KML_ElementMaker as KML
from pykml.factory import GX_ElementMaker as GX
from collections import defaultdict
OUTPUT_FOLDER = 'kml_files/'
def constructKML(urls, course_name):
"""
Create KML files given the urls and the course names.
"""
doc = GX.kml()
for url in urls:
# print url #see if it prints urls
name = url.split('/')[-1]
point = points_dict.get(url)
if point:
langt, longt = point.strip('POINT()').split(' ')
pm = GX.Placemark(
GX.name(name),
GX.Point(
GX.coordinates(langt + ',' + longt))
)
doc.append(pm)
xml = lxml.etree.tostring(doc, pretty_print=True, xml_declaration=True)
with open(OUTPUT_FOLDER+course_name+'.kml', 'w') as f:
f.write(xml)
def retrieveUrls(prog, data_dict):
"""
extract urls from the data repository containing urls.
"""
urls = set()
languages = data_dict[prog].keys()
for lang in languages:
for author in data_dict[prog][lang]:
#could also be something else instead of only locations
all_places = data_dict[prog][lang][author]['all_dbp_urls_all_places']
urls.update(all_places)
constructKML(urls, prog)
if __name__=='__main__':
points_dict = json.load(open('lookUpDict.json', 'r'))
data = json.load(open('scripties.json'))
if not os.path.isdir(OUTPUT_FOLDER):
os.mkdir(OUTPUT_FOLDER)
# retrieveUrls('erf', data) #exute only one
for prog in data.keys():
retrieveUrls(prog, data)