-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkml.py
209 lines (124 loc) · 4.81 KB
/
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
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
# -*- coding: utf-8 -*-
"""
###################################################
Single placemark
@author: lkiener
###################################################
"""
import yattag
from yattag import Doc
import json
f = open("hanoi.kml", "w")
doc, tag, text = Doc().tagtext()
doc.asis('<?xml version="1.0" encoding="UTF-8"?>')
with tag('kml', ("xmlns","http://www.opengis.net/kml/2.2")):
with tag('Document'):
with tag('Placemark'):
with tag('name'):
text('Hanoi')
with tag('Point'):
with tag('coordinates'):
text('105.84,21.033,0')
print(doc.getvalue())
file=doc.getvalue()
f.write(file)
f.close()
"""
###################################################
several placemark
@author: lkiener
###################################################
"""
f = open("villes_afriquedusud.kml", "w")
doc, tag, text = Doc().tagtext()
doc.asis('<?xml version="1.0" encoding="UTF-8"?>')
with tag('kml', ("xmlns","http://www.opengis.net/kml/2.2")):
with tag('Document'):
with tag('Placemark'):
with tag('name'):
text('Johannesbourg')
with tag('Point'):
with tag('coordinates'):
text('28.0343,-26.204,0')
with tag('Placemark'):
with tag('name'):
text('Le Cap')
with tag('Point'):
with tag('coordinates'):
text('18.4197,-33.9248,0')
print(doc.getvalue())
file=doc.getvalue()
f.write(file)
f.close()
"""
###################################################
conversion geojson kml
@author: lkiener
###################################################
"""
f1 = open("villes_vietnam_exo_gen_from_geojson.kml", "w")
doc, tag, text = Doc().tagtext()
source= open("villes_vietnam_exo.geojson",'r+')
# First we load existing data into a dict.
file_data = json.load(source)
print("\n"+str(file_data))
print("\n"+str(type(file_data)))
doc.asis('<?xml version="1.0" encoding="UTF-8"?>')
with tag('kml', ("xmlns","http://www.opengis.net/kml/2.2")):
with tag('Document'):
for feature in file_data['features']:
with tag('Placemark'):
with tag('name'):
text(str(feature['properties']['name']))
with tag(str(feature['geometry']['type'])):
with tag('coordinates'):
text(str(feature['geometry']['coordinates'])[1:len(str(feature['geometry']['coordinates']))-1]+",0")
print(doc.getvalue())
file=doc.getvalue()
f1.write(file)
source.close()
f1.close()
#for i in range(file_data.count('Point')):
# print(i)
"""
###################################################
Sérialisation en KML de données stockées dans geojson
@author: lkiener
###################################################
"""
f1 = open("trip.kml", "w")
doc, tag, text = Doc().tagtext()
source= open("trip.geojson",'r+')
# First we load existing data into a dict.
file_data = json.load(source)
print("\n\n")
text_coord=""
nb_coord=0
doc.asis('<?xml version="1.0" encoding="UTF-8"?>')
with tag('kml', ("xmlns","http://www.opengis.net/kml/2.2")):
with tag('Document'):
for feature in file_data['features']:
with tag('Placemark'):
with tag('name'):
text(str(feature['properties']['Name']))
with tag(str(feature['geometry']['type'])):
with tag('coordinates'):
for coord in feature['geometry']['coordinates']:
nb_coord+=1
if type(coord) is list:
for sub_coord in coord:
text_coord+=str(sub_coord)+","
text(text_coord[:-1]+"\n")
text_coord=""
#text_line_coord=text_line_coord+str(sub_coord)+"\n"
if type(coord) is float:
text_coord+=str(coord)+","
if(nb_coord==3):
text(text_coord[:-1])
text_coord=""
nb_coord=0
print(doc.getvalue())
file=doc.getvalue()
f1.write(file)
source.close()
f1.close()