changeset 1:283badd62593

new version for embedded e4D
author dwinter
date Fri, 12 Oct 2012 08:36:57 +0200
parents 26c06d568e1d
children d8af0ff76407
files .project .pydevproject eventsDB2kml.py eventsRestService.py harvestDataFromEvents.py
diffstat 5 files changed, 304 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.project	Fri Oct 12 08:36:57 2012 +0200
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>events2kml</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.python.pydev.PyDevBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.python.pydev.pythonNature</nature>
+	</natures>
+</projectDescription>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.pydevproject	Fri Oct 12 08:36:57 2012 +0200
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<?eclipse-pydev version="1.0"?>
+
+<pydev_project>
+<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
+<path>/events2kml</path>
+</pydev_pathproperty>
+<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
+<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
+</pydev_project>
--- a/eventsDB2kml.py	Thu Sep 13 16:56:49 2012 +0200
+++ b/eventsDB2kml.py	Fri Oct 12 08:36:57 2012 +0200
@@ -3,19 +3,17 @@
 import psycopg2
 import harvestDataFromEvents
 
+CONNECTION_STRING="dbname='mmpermits' user='www' host='localhost' password='XX'"
+
 class DBQuery:
     def __init__(self):
-        self.conn=psycopg2.connect("dbname='mmpermits' user='postgres' host='localhost' password='XX'")
+        self.conn=psycopg2.connect(CONNECTION_STRING)
         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)
-        
+    def sql(self,query):
+  
         
         self.cur.execute(query)
       
@@ -24,7 +22,56 @@
         
         ret=""
         for result in results:
-            ret+=result[0]+"\n"
+            ret+="%s\n"%result[0]
+            
+        
+       
+        return ret
+    
+    def searchXPaths(self,queries,query="data",asList=False):
+        
+        qs=[]
+        for q in queries:
+            qs.append(""" '%s' = ANY (cast (xpath('%s', data) as text[]))"""%(q[1],q[0]))
+            
+        query="""select  %s from locations where"""%query
+        
+        query+=" AND ".join(qs)
+        
+        self.cur.execute(query)
+      
+        print query
+       
+        results= self.cur.fetchall()
+        if asList:
+            return results
+        
+        ret=""
+        for result in results:
+            ret+="%s\n"%result[0]
+            
+        
+       
+        return ret
+        
+            
+    def searchXPath(self,xpath,searchTerm,query="data",asList=False):
+        query="""select  %s from locations
+        where
+        '%s' = ANY (cast (xpath('%s', data) as text[])) ;
+        """%(query,searchTerm,xpath)
+        
+        
+        self.cur.execute(query)
+      
+       
+        results= self.cur.fetchall()
+        if asList:
+            return results
+        
+        ret=""
+        for result in results:
+            ret+="%s\n"%result[0]
             
         
        
--- a/eventsRestService.py	Thu Sep 13 16:56:49 2012 +0200
+++ b/eventsRestService.py	Fri Oct 12 08:36:57 2012 +0200
@@ -2,49 +2,208 @@
 import eventsDB2kml
 import harvestDataFromEvents
 import urllib
+import urlparse
+
 urls = (
     '/xpath/(.*)', 'xpath',
+    '/xpathSearch', 'xpathSearch',
+    '/sqlSearch', 'sqlSearch',
+    '/sql', 'sql',
     '/species', 'species',
     '/applicants', 'applicants',
-    '/locations','locations'
+    '/locations','locations',
+    '/restricted', 'restricted'
 )
 app = web.application(urls, globals())
 
-E4D_URL="http://localhost:8180/e4D/"
-BASE_URL="http://localhost:8080/"
+E4D_URL="http://localhost/mmpa/"
+BASE_URL="http://localhost/m/"
+
+APPLICANT_FILE_NAME="http://localhost/outApplicants.xml"
+RESEARCH_FILE_NAME="http://localhost/outResearch.xml"
 class locations:
     
     def GET(self):
         
         ret=""
         
-        url1=urllib.quote_plus("http://localhost/outApplicants.xml")
+        url1=urllib.quote_plus(APPLICANT_FILE_NAME)
         ret+="""
             
             <a href="
             %s?kml1=%s&source1=1"  
-            target="e4D">applicants</a><br/>
+            >applicants</a><br/>
             """%(E4D_URL,url1)
             
             
-        url2=urllib.quote_plus("http://localhost/outResearch.xml")
+        url2=urllib.quote_plus(RESEARCH_FILE_NAME)
         ret+="""
             
             <a href="
             %s?kml1=%s&source1=1"  
-            target="e4D">research</a><br/>
+            >research</a><br/>
             """%(E4D_URL,url2)
             
         ret+= """
         <a href="
            %s?kml1=%s&source1=1&kml2=%s&source2=1"  
-            target="e4D">both</a><br/>
+            >both</a><br/>
             """%(E4D_URL,url1,url2)
             
         return ret
         
+class restricted:
+    def GET(self):      
+        x=web.input()
+        pathes=x.get("path")
+        print pathes
+        select=x.get("select")
+        newRestriction=x.get("newRestriction")
+        
+        if select is None:
+            select="data"
+        
+        
+        
+        #path kann meherer abfragen enthalten durch | getrennt
+      
+        splittedPath = pathes.split("|")
+        print splittedPath
+        queriesArray=[]
+        for xlinkPath in splittedPath:
+            url = urllib.unquote_plus(xlinkPath);
         
        
+            qs=urlparse.urlparse(url).query
+      
+            queries=urlparse.parse_qsl(qs)
+            q=""
+            for query in queries:
+                if query[0]=='q':
+                    q=query[1]
+        
+        
+        
+        
+        
+            url= urlparse.urlparse(url).path
+            
+            pathSplitted=url.split("xpath/")
+            if len(pathSplitted)>1:
+                path=pathSplitted[1]
+                queriesArray.append((path,q))
+        print queriesArray
+        
+        
+        
+        db = eventsDB2kml.DBQuery()
+       
+        if newRestriction is None: # keine weiteren Einschaenkungen dann erzeugen das kml file
+            res= "<events>"+ db.searchXPaths(queriesArray)+"</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
+             
+             
+        #sonst suche nach restriction suche muss eine liste von werten ergeben die sich mit der REstriction versteht, dh. restriction =wert wird dann in die Links eingebaut
+        # e.g. man such nach name d.h
+        # select=%20distinct%20cast%28xpath%28%27//applicant/name/text%28%29%27,%20data%29%20as%20text[]%29%20AS%20name
+        # dann muss die Restriction darauf passen i.a. wird die Restriction dem xpath im Suchstring entsprechen,
+        # newRestriction=http%3A%2F%2Flocalhost%2Fm%2Fxpath%2F%2F%2Fapplicant%2Fnam%2Ftext%28%29%
+        
+        newRestriction=urllib.unquote_plus(newRestriction)
+       
+        vals = db.searchXPaths(queriesArray,select,True)
+        
+        
+        ret=""
+        
+        
+        #berechen den neuer link = alter path ergaenzt um neue restriction
+        for val in vals:
+            val=val[0] # result ist immer ein tuple
+            # mehr als ein Treffer pro eintrag
+            for v in val:
+                newQuery=""
+                splittedPath.append(newRestriction+"?q="+v)
+                splittedPathQuoted=[urllib.quote_plus(x) for x in splittedPath]
+                
+                pathNew="|".join(splittedPathQuoted)
+                urlNew=urllib.quote_plus("""%srestricted?path=%s"""%(BASE_URL,pathNew))
+                ret+="""<a href="%s?kml1=%s&source1=1">%s</a><br/>"""%(E4D_URL,urlNew,v)
+        return ret
+        
+class sqlSearch:
+    def GET(self):      
+        x=web.input()
+        sql=x.get("sql")
+        p=x.get("xpathPlace")
+        output=x.get("output")
+        
+       
+            
+        url="%ssql?q=%s&p=%s"%(BASE_URL,urllib.quote_plus(sql),p)
+        
+        if output=="sql":
+            raise web.seeother(url)
+        
+        url=urllib.quote_plus(url)
+        
+        
+        
+        url="""%s?kml1=%s&source1=1"""%(E4D_URL,url)
+        raise web.seeother(url)
+    
+    
+class xpathSearch:
+    def GET(self):      
+        x=web.input()
+        path=x.get("path")
+        value=x.get("value")
+        
+        url="%sxpath/%s?q=%s"%(BASE_URL,path,value)
+        
+        url=urllib.quote_plus(url)
+        
+        
+        
+        url="""%s?kml1=%s&source1=1"""%(E4D_URL,url)
+        raise web.seeother(url)
+  
+class sql:
+    def GET(self):
+        x = web.input()
+        q= x.get("q")
+        p= x.get("p")
+        db = eventsDB2kml.DBQuery()
+        res= "<events>"+db.sql(q)+"</events>"
+    
+    
+       
+        hv=harvestDataFromEvents.EventKMLTransformer()
+    
+    
+        #x,cnt=hv.readString(res, ".//research_location/place_information", 0)
+        x,cnt=hv.readString(res, p, 0)
+    
+        
+    
+        s = hv.toKML(x)
+        
+        return s
+      
+      
 class xpath:
  
     def GET(self, path):
@@ -76,11 +235,31 @@
     
      def GET(self):
         db = eventsDB2kml.DBQuery()
+        
+           
+        x = web.input()
+        where= x.get("where")
+      
+        whereClause=""
+
+#Beispiel:
+#select distinct
+# 
+#cast (xpath('//name/text()',data) as text[]) AS name
+#
+#from locations
+#        where
+#        'Astrophytum asterius'  = ANY (cast (xpath('//species/text()', data) as text[])) ;
+        
+        if where!="" and where is not None:
+            whereClause="where %s"%where
+            
         q="""
           select 
             cast(xpath('//applicant/name/text()', data) as text[]) AS name 
             from locations
-            """
+            %s
+            """%whereClause
             
         res=db.query(q)
         #hole alle species
@@ -97,15 +276,15 @@
         
         spList.sort()
         for x in spList:
-            print x
-            ret+="""<a href="/xpath///applicant/name/text()?q=%s">%s</a>"""%(x,x)
+          
+            ret+="""<a href="%sxpath///applicant/name/text()?q=%s">%s</a>"""%(BASE_URL,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</a><br/>
             """%(E4D_URL,url)
         
         return ret
@@ -113,12 +292,32 @@
     
     def GET(self):
         db = eventsDB2kml.DBQuery()
+        
+        x = web.input()
+        where= x.get("where")
+        
+        whereClause=""
+        
+# Beispiel:       
+#select 
+# 
+#cast (xpath('//species/text()',data) as text[]) AS name
+#
+#from locations
+#        where
+#        'Anna George'  = ANY (cast (xpath('//name/text()', data) as text[])) ;
+#        
+        if where!="" and where is not None:
+            whereClause="where %s"%where
+            
         q="""
-          select 
+          select distinct
             cast(xpath('//species/text()', data) as text[]) AS name 
             from locations
-            """
-            
+            %s
+            """%whereClause
+        
+      
         res=db.query(q)
         #hole alle species
         spec=set()
@@ -134,15 +333,15 @@
         
         spList.sort()
         for x in spList:
-            print x
-            ret+="""<a href="/xpath///species/text()?q=%s">%s</a>"""%(x,x)
+            
+            ret+="""<a href="%sxpath///species/text()?q=%s">%s</a>"""%(BASE_URL,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</a><br/>
             """%(E4D_URL,url)
         
         return ret
--- a/harvestDataFromEvents.py	Thu Sep 13 16:56:49 2012 +0200
+++ b/harvestDataFromEvents.py	Fri Oct 12 08:36:57 2012 +0200
@@ -129,14 +129,14 @@
                 cnt+=1
         return ret,cnt
     
-    def readFiles(self,path,locationXPath):
+    def readFiles(self,path,locationXPath,cnt=0):
         ret=[]
-        cnt=0
+       
         for f in os.listdir(path):
             ret2,cnt=self.readFile(path+f,locationXPath,cnt)
             ret+=ret2   
         
-        return ret
+        return ret,cnt
 
 
     def toKML(self,events):
@@ -161,7 +161,7 @@
     
     tf=EventKMLTransformer("/tmp/out.sql")
     
-    x = tf.readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/events/",".//research_location/place_information")
+    x,cnt = 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)
     
@@ -170,7 +170,7 @@
     out.write(evs)
     out.close()
     
-    x = tf.readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/events/",".//applicant_locations/place_information")
+    x,cnt = tf.readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/events/",".//applicant_locations/place_information",cnt)
     #x = readFiles("/Users/dwinter/Documents/Projekte/mmpa-permit-etienne/Results/events/")
     evs= tf.toKML(x)