Mercurial > hg > events2kml
changeset 0:26c06d568e1d
first
author | dwinter |
---|---|
date | Thu, 13 Sep 2012 16:56:49 +0200 |
parents | |
children | 283badd62593 |
files | eventsDB2kml.py eventsRestService.py harvestDataFromEvents.py |
diffstat | 3 files changed, 389 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/eventsDB2kml.py Thu Sep 13 16:56:49 2012 +0200 @@ -0,0 +1,56 @@ +#diese klasse fragt die postgres db ab und erzeugt ein kml file aus den abfragen + +import psycopg2 +import harvestDataFromEvents + +class DBQuery: + def __init__(self): + self.conn=psycopg2.connect("dbname='mmpermits' user='postgres' host='localhost' password='XX'") + self.cur=self.conn.cursor() + + + + def searchXPath(self,xpath,searchTerm): + query="""select data from locations + where + '%s' = ANY (cast (xpath('%s', data) as text[])) ; + """%(searchTerm,xpath) + + + self.cur.execute(query) + + + results= self.cur.fetchall() + + ret="" + for result in results: + ret+=result[0]+"\n" + + + + return ret + + def query(self,q): + self.cur.execute(q) + results= self.cur.fetchall() + + return results + + +if __name__ == '__main__': + db = DBQuery() + res= "<events>"+db.searchXPath("//name/text()", "Brian L. Cypher")+"</events>" + + + + hv=harvestDataFromEvents.EventKMLTransformer() + + + x,cnt=hv.readString(res, ".//research_location/place_information", 0) + + hv.close() + + s = hv.toKML(x) + + print s +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/eventsRestService.py Thu Sep 13 16:56:49 2012 +0200 @@ -0,0 +1,151 @@ +import web +import eventsDB2kml +import harvestDataFromEvents +import urllib +urls = ( + '/xpath/(.*)', 'xpath', + '/species', 'species', + '/applicants', 'applicants', + '/locations','locations' +) +app = web.application(urls, globals()) + +E4D_URL="http://localhost:8180/e4D/" +BASE_URL="http://localhost:8080/" +class locations: + + def GET(self): + + ret="" + + url1=urllib.quote_plus("http://localhost/outApplicants.xml") + ret+=""" + + <a href=" + %s?kml1=%s&source1=1" + target="e4D">applicants</a><br/> + """%(E4D_URL,url1) + + + url2=urllib.quote_plus("http://localhost/outResearch.xml") + ret+=""" + + <a href=" + %s?kml1=%s&source1=1" + target="e4D">research</a><br/> + """%(E4D_URL,url2) + + ret+= """ + <a href=" + %s?kml1=%s&source1=1&kml2=%s&source2=1" + target="e4D">both</a><br/> + """%(E4D_URL,url1,url2) + + return ret + + + +class xpath: + + def GET(self, path): + + if not path: + return "" + + x = web.input() + q= x.get("q") + + db = eventsDB2kml.DBQuery() + res= "<events>"+db.searchXPath(path, q)+"</events>" + + + + hv=harvestDataFromEvents.EventKMLTransformer() + + + #x,cnt=hv.readString(res, ".//research_location/place_information", 0) + x,cnt=hv.readString(res, ".//place_information", 0) + + + + s = hv.toKML(x) + + return s + +class applicants: + + def GET(self): + db = eventsDB2kml.DBQuery() + q=""" + select + cast(xpath('//applicant/name/text()', data) as text[]) AS name + from locations + """ + + res=db.query(q) + #hole alle species + spec=set() + + for r in res: + + l=r[0] + for x in l: + spec.add(x) + + ret="" + spList=[x for x in spec] + + spList.sort() + for x in spList: + print x + ret+="""<a href="/xpath///applicant/name/text()?q=%s">%s</a>"""%(x,x) + + url=urllib.quote_plus("%sxpath///applicant/name/text()?q=%s"%(BASE_URL,x)) + ret+=""" + + <a href=" + %s?kml1=%s&source1=1" + target="e4D">e4D</a><br/> + """%(E4D_URL,url) + + return ret +class species: + + def GET(self): + db = eventsDB2kml.DBQuery() + q=""" + select + cast(xpath('//species/text()', data) as text[]) AS name + from locations + """ + + res=db.query(q) + #hole alle species + spec=set() + + for r in res: + + l=r[0] + for x in l: + spec.add(x) + + ret="" + spList=[x for x in spec] + + spList.sort() + for x in spList: + print x + ret+="""<a href="/xpath///species/text()?q=%s">%s</a>"""%(x,x) + + url=urllib.quote_plus("%sxpath///species/text()?q=%s"%(BASE_URL,x)) + ret+=""" + + <a href=" + %s?kml1=%s&source1=1" + target="e4D">e4D</a><br/> + """%(E4D_URL,url) + + return ret + +if __name__ == "__main__": + app.run() \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/harvestDataFromEvents.py Thu Sep 13 16:56:49 2012 +0200 @@ -0,0 +1,182 @@ +''' +Created on 11.09.2012 + +@author: dwinter +''' + + +from lxml import etree +import os +import codecs + +class Event: + long="" + lat="" + oid="" + placename="" + date="" + species="" + description="" + + def __str__(self): + ret="" + ret="%s %s %s"%(self.oid,self.lat,self.long) + + return ret + + + def toKML(self): + ret=""" + <Placemark> + <name>%s - %s</name> + <description><![CDATA[%s]]></description> + <TimeStamp><when>%s</when></TimeStamp> + <Point> + <coordinates>%s,%s</coordinates> + </Point> + </Placemark> + """%(self.oid,self.placename,self.description,self.date,self.long,self.lat) + + + + + return ret + +class EventKMLTransformer: + + def __init__(self,sqlFilename=None): + self.cnt=0 + #falls ein Filename anfgegeben wird dann wird ein sql-file erzeugt + if sqlFilename is not None: + self.f=codecs.open(sqlFilename,"w","utf-8") + else: + self.f=None + #self.f = codecs.open("/tmp/out.sql","w","utf-8") + + def createDescription(self,element): + xslt_root=etree.parse("../../XSLT/entryToXHTML.xsl") + transform = etree.XSLT(xslt_root) + res=transform(element) + return unicode(res) + + + def calcDate(self,date): + + splitted=date.split("-") + + if len(splitted)==3: + year="20"+splitted[2] + month=splitted[1] + date=splitted[0] + + return "%s-%s-%s"%(year,month,date) + + return "2000" + + + + def writeToSQL(self,event,ident,cnt,f): + insert=""" + INSERT INTO locations (data, "idTxt",id) VALUES ('%s', '%s',%s);""" + + evString = etree.tostring(event).replace("'","") + f.write(insert%(evString,ident,cnt)) + + + + def readString(self,xmlString,locationXPath,cnt): + + tree = etree.XML(xmlString) + return self.analyseEventXML(tree,locationXPath,cnt) + + + def readFile(self,filePath,locationXPath,cnt): + tree = etree.parse(filePath) + return self.analyseEventXML(tree,locationXPath,cnt) + + def analyseEventXML(self,tree,locationXPath,cnt): + counter=0 + + ret=[] + for event in tree.xpath("//event"): + + text=event.get("text") + + + #erzeuge zunaechste eine id aus dem textname und dem counter + + + + #place_information=event.xpath(".//place_information") + place_information=event.xpath(locationXPath) + + for place in place_information: + ev=Event() + ev.date=self.calcDate(event.get("date_filed")) + ev.description=self.createDescription(event) + ev.oid="%s.%s"%(text,counter) + counter+=1 + print ev.oid + ev.placename=place.text + + ev.lat=place.get("latitude") + ev.long=place.get("longitude") + + ret.append(ev) + + if self.f is not None: + self.writeToSQL(event,ev.oid,cnt,self.f) + cnt+=1 + return ret,cnt + + def readFiles(self,path,locationXPath): + ret=[] + cnt=0 + for f in os.listdir(path): + ret2,cnt=self.readFile(path+f,locationXPath,cnt) + ret+=ret2 + + return ret + + + def toKML(self,events): + ret="""<kml> + """ + ret+="<Document>" + + for event in events: + ret+=event.toKML() + + ret+="</Document>" + ret+="</kml>" + + return ret + + def close(self): + if self.f is not None: + self.f.close() + + +if __name__ == '__main__': + + tf=EventKMLTransformer("/tmp/out.sql") + + x = tf.readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/events/",".//research_location/place_information") + #x = readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/Results/events/") + evs= tf.toKML(x) + + out = codecs.open("/tmp/outResearch.xml","w","utf-8") + + out.write(evs) + out.close() + + x = tf.readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/events/",".//applicant_locations/place_information") + #x = readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/Results/events/") + evs= tf.toKML(x) + + out = codecs.open("/tmp/outApplicants.xml","w","utf-8") + + out.write(evs) + out.close() + + tf.close() \ No newline at end of file