forked from geoinsights/ChinaAQIData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2json.py
66 lines (61 loc) · 1.85 KB
/
xml2json.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
# coding=utf-8
import json
import geojson
import time
import xml.dom.minidom
def data_from_xml_json(xmlfile):
fp = open(xmlfile)
data = fp.read()
# this is for the air quality data, to split some charicters
data = data.replace("a:", "")
data = data.replace("b:", "")
data = data.replace("c:", "")
data = data.replace("&mdash", "-")
return xmlparse(data)
def xmlparse(xmlstr):
'''
parse air quality xml data to dict list
'''
dom = xml.dom.minidom.parseString(xmlstr)
root = dom.documentElement
stats = dom.getElementsByTagName("AQIDataPublishLive")
airdata = []
for stat in stats:
# print len(stat.childNodes)
r = {}
for node in stat.childNodes:
if (node.nodeName == "#text" or
node.nodeName == "OpenAccessGenerated"):
continue
inx = node.nodeName
inx = inx.lower()
for n in node.childNodes:
# print n.data
r[inx] = n.data
airdata.append(r)
return airdata
# return json.dumps(airdata, ensure_ascii=False)
if __name__ == "__main__":
data = data_from_xml_json("data.xml")
featList = []
for recd in data:
try:
lon = float(recd["longitude"])
lat = float(recd["latitude"])
except:
continue
geom = geojson.Point((lon, lat))
del recd["longitude"]
del recd["latitude"]
properties = recd
feat = geojson.Feature(geometry=geom, properties=properties)
featList.append(feat)
fc = geojson.FeatureCollection(featList)
fname = time.strftime("%Y-%m-%d-%H", time.localtime())
fstr = geojson.dumps(fc, sort_keys=True)
fp = open("archives/%s.json" % fname, "w")
fp.write(fstr)
fp.close()
fp = open("airnow.json", "w")
fp.write(fstr)
fp.close()