0
|
1 '''
|
|
2 Created on 11.09.2012
|
|
3
|
|
4 @author: dwinter
|
|
5 '''
|
|
6
|
|
7
|
|
8 from lxml import etree
|
|
9 import os
|
|
10 import codecs
|
|
11
|
|
12 class Event:
|
|
13 long=""
|
|
14 lat=""
|
|
15 oid=""
|
|
16 placename=""
|
|
17 date=""
|
|
18 species=""
|
|
19 description=""
|
|
20
|
|
21 def __str__(self):
|
|
22 ret=""
|
|
23 ret="%s %s %s"%(self.oid,self.lat,self.long)
|
|
24
|
|
25 return ret
|
|
26
|
|
27
|
|
28 def toKML(self):
|
|
29 ret="""
|
|
30 <Placemark>
|
|
31 <name>%s - %s</name>
|
|
32 <description><![CDATA[%s]]></description>
|
|
33 <TimeStamp><when>%s</when></TimeStamp>
|
|
34 <Point>
|
|
35 <coordinates>%s,%s</coordinates>
|
|
36 </Point>
|
|
37 </Placemark>
|
|
38 """%(self.oid,self.placename,self.description,self.date,self.long,self.lat)
|
|
39
|
|
40
|
|
41
|
|
42
|
|
43 return ret
|
|
44
|
|
45 class EventKMLTransformer:
|
|
46
|
|
47 def __init__(self,sqlFilename=None):
|
|
48 self.cnt=0
|
|
49 #falls ein Filename anfgegeben wird dann wird ein sql-file erzeugt
|
|
50 if sqlFilename is not None:
|
|
51 self.f=codecs.open(sqlFilename,"w","utf-8")
|
|
52 else:
|
|
53 self.f=None
|
|
54 #self.f = codecs.open("/tmp/out.sql","w","utf-8")
|
|
55
|
|
56 def createDescription(self,element):
|
|
57 xslt_root=etree.parse("../../XSLT/entryToXHTML.xsl")
|
|
58 transform = etree.XSLT(xslt_root)
|
|
59 res=transform(element)
|
|
60 return unicode(res)
|
|
61
|
|
62
|
|
63 def calcDate(self,date):
|
|
64
|
|
65 splitted=date.split("-")
|
|
66
|
|
67 if len(splitted)==3:
|
|
68 year="20"+splitted[2]
|
|
69 month=splitted[1]
|
|
70 date=splitted[0]
|
|
71
|
|
72 return "%s-%s-%s"%(year,month,date)
|
|
73
|
|
74 return "2000"
|
|
75
|
|
76
|
|
77
|
|
78 def writeToSQL(self,event,ident,cnt,f):
|
|
79 insert="""
|
|
80 INSERT INTO locations (data, "idTxt",id) VALUES ('%s', '%s',%s);"""
|
|
81
|
|
82 evString = etree.tostring(event).replace("'","")
|
|
83 f.write(insert%(evString,ident,cnt))
|
|
84
|
|
85
|
|
86
|
|
87 def readString(self,xmlString,locationXPath,cnt):
|
|
88
|
|
89 tree = etree.XML(xmlString)
|
|
90 return self.analyseEventXML(tree,locationXPath,cnt)
|
|
91
|
|
92
|
|
93 def readFile(self,filePath,locationXPath,cnt):
|
|
94 tree = etree.parse(filePath)
|
|
95 return self.analyseEventXML(tree,locationXPath,cnt)
|
|
96
|
|
97 def analyseEventXML(self,tree,locationXPath,cnt):
|
|
98 counter=0
|
|
99
|
|
100 ret=[]
|
|
101 for event in tree.xpath("//event"):
|
|
102
|
|
103 text=event.get("text")
|
|
104
|
|
105
|
|
106 #erzeuge zunaechste eine id aus dem textname und dem counter
|
|
107
|
|
108
|
|
109
|
|
110 #place_information=event.xpath(".//place_information")
|
|
111 place_information=event.xpath(locationXPath)
|
|
112
|
|
113 for place in place_information:
|
|
114 ev=Event()
|
|
115 ev.date=self.calcDate(event.get("date_filed"))
|
|
116 ev.description=self.createDescription(event)
|
|
117 ev.oid="%s.%s"%(text,counter)
|
|
118 counter+=1
|
|
119 print ev.oid
|
|
120 ev.placename=place.text
|
|
121
|
|
122 ev.lat=place.get("latitude")
|
|
123 ev.long=place.get("longitude")
|
|
124
|
|
125 ret.append(ev)
|
|
126
|
|
127 if self.f is not None:
|
|
128 self.writeToSQL(event,ev.oid,cnt,self.f)
|
|
129 cnt+=1
|
|
130 return ret,cnt
|
|
131
|
1
|
132 def readFiles(self,path,locationXPath,cnt=0):
|
0
|
133 ret=[]
|
1
|
134
|
0
|
135 for f in os.listdir(path):
|
|
136 ret2,cnt=self.readFile(path+f,locationXPath,cnt)
|
|
137 ret+=ret2
|
|
138
|
1
|
139 return ret,cnt
|
0
|
140
|
|
141
|
|
142 def toKML(self,events):
|
|
143 ret="""<kml>
|
|
144 """
|
|
145 ret+="<Document>"
|
|
146
|
|
147 for event in events:
|
|
148 ret+=event.toKML()
|
|
149
|
|
150 ret+="</Document>"
|
|
151 ret+="</kml>"
|
|
152
|
|
153 return ret
|
|
154
|
|
155 def close(self):
|
|
156 if self.f is not None:
|
|
157 self.f.close()
|
|
158
|
|
159
|
|
160 if __name__ == '__main__':
|
|
161
|
|
162 tf=EventKMLTransformer("/tmp/out.sql")
|
|
163
|
1
|
164 x,cnt = tf.readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/events/",".//research_location/place_information")
|
0
|
165 #x = readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/Results/events/")
|
|
166 evs= tf.toKML(x)
|
|
167
|
|
168 out = codecs.open("/tmp/outResearch.xml","w","utf-8")
|
|
169
|
|
170 out.write(evs)
|
|
171 out.close()
|
|
172
|
1
|
173 x,cnt = tf.readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/events/",".//applicant_locations/place_information",cnt)
|
0
|
174 #x = readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/Results/events/")
|
|
175 evs= tf.toKML(x)
|
|
176
|
|
177 out = codecs.open("/tmp/outApplicants.xml","w","utf-8")
|
|
178
|
|
179 out.write(evs)
|
|
180 out.close()
|
|
181
|
|
182 tf.close() |