annotate RestDbGisApi.py @ 43:562717546168

refactoring... RestDbGisApi and RestDbJsonStore inherit from RestDbInterface
author casties
date Thu, 02 Sep 2010 13:17:23 +0200
parents
children c6c47034d2a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
562717546168 refactoring...
casties
parents:
diff changeset
1 '''
562717546168 refactoring...
casties
parents:
diff changeset
2 Created on 2.9.2010
562717546168 refactoring...
casties
parents:
diff changeset
3
562717546168 refactoring...
casties
parents:
diff changeset
4 @author: casties, fknauft
562717546168 refactoring...
casties
parents:
diff changeset
5 '''
562717546168 refactoring...
casties
parents:
diff changeset
6
562717546168 refactoring...
casties
parents:
diff changeset
7 from OFS.Folder import Folder
562717546168 refactoring...
casties
parents:
diff changeset
8 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
562717546168 refactoring...
casties
parents:
diff changeset
9 from Products.ZSQLExtend import ZSQLExtend
562717546168 refactoring...
casties
parents:
diff changeset
10 import logging
562717546168 refactoring...
casties
parents:
diff changeset
11 import re
562717546168 refactoring...
casties
parents:
diff changeset
12 import json
562717546168 refactoring...
casties
parents:
diff changeset
13 import time
562717546168 refactoring...
casties
parents:
diff changeset
14
562717546168 refactoring...
casties
parents:
diff changeset
15 from RestDbInterface import *
562717546168 refactoring...
casties
parents:
diff changeset
16
562717546168 refactoring...
casties
parents:
diff changeset
17
562717546168 refactoring...
casties
parents:
diff changeset
18 gisToSqlTypeMap = {
562717546168 refactoring...
casties
parents:
diff changeset
19 "text": "text",
562717546168 refactoring...
casties
parents:
diff changeset
20 "number": "numeric",
562717546168 refactoring...
casties
parents:
diff changeset
21 "id": "text",
562717546168 refactoring...
casties
parents:
diff changeset
22 "gis_id": "text",
562717546168 refactoring...
casties
parents:
diff changeset
23 "coord_lat": "numeric",
562717546168 refactoring...
casties
parents:
diff changeset
24 "coord_lon": "numeric"
562717546168 refactoring...
casties
parents:
diff changeset
25 }
562717546168 refactoring...
casties
parents:
diff changeset
26
562717546168 refactoring...
casties
parents:
diff changeset
27 class RestDbGisApi(RestDbInterface):
562717546168 refactoring...
casties
parents:
diff changeset
28 """Object for RESTful GIS database queries
562717546168 refactoring...
casties
parents:
diff changeset
29 path schema: /db/{schema}/{table}/
562717546168 refactoring...
casties
parents:
diff changeset
30 omitting table gives a list of schemas
562717546168 refactoring...
casties
parents:
diff changeset
31 omitting table and schema gives a list of schemas
562717546168 refactoring...
casties
parents:
diff changeset
32 """
562717546168 refactoring...
casties
parents:
diff changeset
33
562717546168 refactoring...
casties
parents:
diff changeset
34 meta_type="RESTgis"
562717546168 refactoring...
casties
parents:
diff changeset
35
562717546168 refactoring...
casties
parents:
diff changeset
36 # data templates
562717546168 refactoring...
casties
parents:
diff changeset
37 GIS_schema_table = PageTemplateFile('zpt/GIS_schema_table', globals())
562717546168 refactoring...
casties
parents:
diff changeset
38
562717546168 refactoring...
casties
parents:
diff changeset
39 def checkTableMetaPermission(self,action,schema,table,user=None):
562717546168 refactoring...
casties
parents:
diff changeset
40 """returns if the requested action on the table is allowed"""
562717546168 refactoring...
casties
parents:
diff changeset
41 logging.debug("checktablemetapermissions action=%s schema=%s table=%s user=%s"%(action,schema,table,user))
562717546168 refactoring...
casties
parents:
diff changeset
42 if user is None:
562717546168 refactoring...
casties
parents:
diff changeset
43 user = self.REQUEST.get('AUTHENTICATED_USER',None)
562717546168 refactoring...
casties
parents:
diff changeset
44 logging.debug("user=%s"%user)
562717546168 refactoring...
casties
parents:
diff changeset
45 # TODO: what now?
562717546168 refactoring...
casties
parents:
diff changeset
46 return True
562717546168 refactoring...
casties
parents:
diff changeset
47
562717546168 refactoring...
casties
parents:
diff changeset
48 def setTableMetaTypes(self,schema,table,fields):
562717546168 refactoring...
casties
parents:
diff changeset
49 """sets the GIS meta information for table"""
562717546168 refactoring...
casties
parents:
diff changeset
50 logging.debug("settablemetatypes schema=%s, table=%s, fields=%s"%(schema,table,fields))
562717546168 refactoring...
casties
parents:
diff changeset
51 gisIdField = None
562717546168 refactoring...
casties
parents:
diff changeset
52 latField = None
562717546168 refactoring...
casties
parents:
diff changeset
53 lonField = None
562717546168 refactoring...
casties
parents:
diff changeset
54 for f in fields:
562717546168 refactoring...
casties
parents:
diff changeset
55 t = f['type']
562717546168 refactoring...
casties
parents:
diff changeset
56 if t == 'gis_id':
562717546168 refactoring...
casties
parents:
diff changeset
57 gisIdField = f['name']
562717546168 refactoring...
casties
parents:
diff changeset
58 elif t == 'coord_lat':
562717546168 refactoring...
casties
parents:
diff changeset
59 latField = f['name']
562717546168 refactoring...
casties
parents:
diff changeset
60 elif t == 'coord_lon':
562717546168 refactoring...
casties
parents:
diff changeset
61 lonField = f['name']
562717546168 refactoring...
casties
parents:
diff changeset
62
562717546168 refactoring...
casties
parents:
diff changeset
63 res = self.executeSQL("select * from public.metadata where tablename=%s", (table,))
562717546168 refactoring...
casties
parents:
diff changeset
64 if len(res['rows']) > 0:
562717546168 refactoring...
casties
parents:
diff changeset
65 # meta record exists
562717546168 refactoring...
casties
parents:
diff changeset
66 if gisIdField is not None:
562717546168 refactoring...
casties
parents:
diff changeset
67 self.executeSQL('update public.metadata set "attribute with gis_id" = %s where tablename = %s', (gisIdField,table), hasResult=False)
562717546168 refactoring...
casties
parents:
diff changeset
68
562717546168 refactoring...
casties
parents:
diff changeset
69 else:
562717546168 refactoring...
casties
parents:
diff changeset
70 # new meta record
562717546168 refactoring...
casties
parents:
diff changeset
71 if gisIdField is not None:
562717546168 refactoring...
casties
parents:
diff changeset
72 self.executeSQL('insert into public.metadata ("tablename", "attribute with gis_id") values (%s, %s)', (table,gisIdField), hasResult=False)
562717546168 refactoring...
casties
parents:
diff changeset
73
562717546168 refactoring...
casties
parents:
diff changeset
74
562717546168 refactoring...
casties
parents:
diff changeset
75 def showTable(self,resultFormat='XML',schema='public',table=None,REQUEST=None,RESPONSE=None):
562717546168 refactoring...
casties
parents:
diff changeset
76 """returns PageTemplate with tables"""
562717546168 refactoring...
casties
parents:
diff changeset
77 logging.debug("showtable")
562717546168 refactoring...
casties
parents:
diff changeset
78 if REQUEST is None:
562717546168 refactoring...
casties
parents:
diff changeset
79 REQUEST = self.REQUEST
562717546168 refactoring...
casties
parents:
diff changeset
80
562717546168 refactoring...
casties
parents:
diff changeset
81 # should be cross-site accessible
562717546168 refactoring...
casties
parents:
diff changeset
82 if RESPONSE is None:
562717546168 refactoring...
casties
parents:
diff changeset
83 RESPONSE = self.REQUEST.RESPONSE
562717546168 refactoring...
casties
parents:
diff changeset
84
562717546168 refactoring...
casties
parents:
diff changeset
85 RESPONSE.setHeader('Access-Control-Allow-Origin', '*')
562717546168 refactoring...
casties
parents:
diff changeset
86
562717546168 refactoring...
casties
parents:
diff changeset
87 # GIS gets special treatment
562717546168 refactoring...
casties
parents:
diff changeset
88 if resultFormat=="GIS":
562717546168 refactoring...
casties
parents:
diff changeset
89 id = REQUEST.get('id',[])
562717546168 refactoring...
casties
parents:
diff changeset
90 doc = REQUEST.get('doc',None)
562717546168 refactoring...
casties
parents:
diff changeset
91 return self.showGoogleMap(schema=schema,table=table,id=id,doc=doc)
562717546168 refactoring...
casties
parents:
diff changeset
92
562717546168 refactoring...
casties
parents:
diff changeset
93 elif resultFormat=="KML_URL":
562717546168 refactoring...
casties
parents:
diff changeset
94 id = REQUEST.get('id',[])
562717546168 refactoring...
casties
parents:
diff changeset
95 doc = REQUEST.get('doc',None)
562717546168 refactoring...
casties
parents:
diff changeset
96 return self.getKmlUrl(schema=schema,table=table,id=id,doc=doc)
562717546168 refactoring...
casties
parents:
diff changeset
97
562717546168 refactoring...
casties
parents:
diff changeset
98 # everything else has its own template
562717546168 refactoring...
casties
parents:
diff changeset
99 pt = getattr(self.template, '%s_schema_table'%resultFormat, None)
562717546168 refactoring...
casties
parents:
diff changeset
100 if pt is None:
562717546168 refactoring...
casties
parents:
diff changeset
101 return "ERROR!! template %s_schema_table not found"%resultFormat
562717546168 refactoring...
casties
parents:
diff changeset
102
562717546168 refactoring...
casties
parents:
diff changeset
103 data = self.getTable(schema,table)
562717546168 refactoring...
casties
parents:
diff changeset
104 return pt(data=data,tablename=table)
562717546168 refactoring...
casties
parents:
diff changeset
105
562717546168 refactoring...
casties
parents:
diff changeset
106
562717546168 refactoring...
casties
parents:
diff changeset
107 def createEmptyTable(self,schema,table,fields):
562717546168 refactoring...
casties
parents:
diff changeset
108 """create a table with the given fields
562717546168 refactoring...
casties
parents:
diff changeset
109 returns list of created fields"""
562717546168 refactoring...
casties
parents:
diff changeset
110 logging.debug("createEmptyTable")
562717546168 refactoring...
casties
parents:
diff changeset
111 sqlFields = []
562717546168 refactoring...
casties
parents:
diff changeset
112 for f in fields:
562717546168 refactoring...
casties
parents:
diff changeset
113 if isinstance(f,dict):
562717546168 refactoring...
casties
parents:
diff changeset
114 # {name: XX, type: YY}
562717546168 refactoring...
casties
parents:
diff changeset
115 name = sqlName(f['name'])
562717546168 refactoring...
casties
parents:
diff changeset
116 type = f['type']
562717546168 refactoring...
casties
parents:
diff changeset
117 sqltype = gisToSqlTypeMap[type]
562717546168 refactoring...
casties
parents:
diff changeset
118
562717546168 refactoring...
casties
parents:
diff changeset
119 else:
562717546168 refactoring...
casties
parents:
diff changeset
120 # name only
562717546168 refactoring...
casties
parents:
diff changeset
121 name = sqlName(f)
562717546168 refactoring...
casties
parents:
diff changeset
122 type = 'text'
562717546168 refactoring...
casties
parents:
diff changeset
123 sqltype = 'text'
562717546168 refactoring...
casties
parents:
diff changeset
124
562717546168 refactoring...
casties
parents:
diff changeset
125 sqlFields.append({'name':name, 'type':type, 'sqltype':sqltype})
562717546168 refactoring...
casties
parents:
diff changeset
126
562717546168 refactoring...
casties
parents:
diff changeset
127 if self.checkTableMetaPermission("create", schema, table):
562717546168 refactoring...
casties
parents:
diff changeset
128 self.executeSQL('drop table if exists "%s"."%s"'%(schema,table),hasResult=False)
562717546168 refactoring...
casties
parents:
diff changeset
129 fieldString = ", ".join(['"%s" %s'%(f['name'],f['sqltype']) for f in sqlFields])
562717546168 refactoring...
casties
parents:
diff changeset
130 sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString)
562717546168 refactoring...
casties
parents:
diff changeset
131 logging.debug("createemptytable: SQL=%s"%sqlString)
562717546168 refactoring...
casties
parents:
diff changeset
132 self.executeSQL(sqlString,hasResult=False)
562717546168 refactoring...
casties
parents:
diff changeset
133 self.setTableMetaTypes(schema,table,sqlFields)
562717546168 refactoring...
casties
parents:
diff changeset
134 return sqlFields
562717546168 refactoring...
casties
parents:
diff changeset
135 else:
562717546168 refactoring...
casties
parents:
diff changeset
136 logging.warning("create table not allowed!")
562717546168 refactoring...
casties
parents:
diff changeset
137 # throw exception?
562717546168 refactoring...
casties
parents:
diff changeset
138 return None
562717546168 refactoring...
casties
parents:
diff changeset
139
562717546168 refactoring...
casties
parents:
diff changeset
140
562717546168 refactoring...
casties
parents:
diff changeset
141
562717546168 refactoring...
casties
parents:
diff changeset
142 # Methods for GoogleMaps creation
562717546168 refactoring...
casties
parents:
diff changeset
143 def showGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None):
562717546168 refactoring...
casties
parents:
diff changeset
144 logging.debug("showGoogleMap")
562717546168 refactoring...
casties
parents:
diff changeset
145 data = self.getDataForGoogleMap(schema,table,id,doc)
562717546168 refactoring...
casties
parents:
diff changeset
146 kmlFileName=self.getKMLname(data=data,table=table)
562717546168 refactoring...
casties
parents:
diff changeset
147 initializeStringForGoogleMaps="""onload=\"initialize(\'http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kmlFileName+"""\')\""""#+str(data)
562717546168 refactoring...
casties
parents:
diff changeset
148 initializeStringForGoogleMaps=initializeStringForGoogleMaps.replace("None","0")
562717546168 refactoring...
casties
parents:
diff changeset
149 googleMap_page=self.htmlHead()+str(self.getGoogleMapString(kml=initializeStringForGoogleMaps))
562717546168 refactoring...
casties
parents:
diff changeset
150 return googleMap_page
562717546168 refactoring...
casties
parents:
diff changeset
151
562717546168 refactoring...
casties
parents:
diff changeset
152 def getKmlUrl(self,schema='chgis',table='mpdl',id=[],doc=None):
562717546168 refactoring...
casties
parents:
diff changeset
153 logging.debug("getKmlUrl")
562717546168 refactoring...
casties
parents:
diff changeset
154 data = self.getDataForGoogleMap(schema,table,id,doc)
562717546168 refactoring...
casties
parents:
diff changeset
155 kml=self.getKMLname(data=data,table=table)
562717546168 refactoring...
casties
parents:
diff changeset
156 baseUrl = self.absolute_url()
562717546168 refactoring...
casties
parents:
diff changeset
157 return "%s/daten/%s"%(baseUrl,kml)
562717546168 refactoring...
casties
parents:
diff changeset
158
562717546168 refactoring...
casties
parents:
diff changeset
159 def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None):
562717546168 refactoring...
casties
parents:
diff changeset
160 logging.debug("getDataForGoogleMap")
562717546168 refactoring...
casties
parents:
diff changeset
161 qstr="SELECT * FROM "+schema+"."+table
562717546168 refactoring...
casties
parents:
diff changeset
162 try:
562717546168 refactoring...
casties
parents:
diff changeset
163 if id!=[]:
562717546168 refactoring...
casties
parents:
diff changeset
164 qstr=qstr+" WHERE "
562717546168 refactoring...
casties
parents:
diff changeset
165 for id_item in id.split(","):
562717546168 refactoring...
casties
parents:
diff changeset
166 if table=='mpdl':
562717546168 refactoring...
casties
parents:
diff changeset
167 qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR"
562717546168 refactoring...
casties
parents:
diff changeset
168 else:
562717546168 refactoring...
casties
parents:
diff changeset
169 qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR"
562717546168 refactoring...
casties
parents:
diff changeset
170 qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and "
562717546168 refactoring...
casties
parents:
diff changeset
171 data=self.ZSQLSimpleSearch(qstr)
562717546168 refactoring...
casties
parents:
diff changeset
172 return data
562717546168 refactoring...
casties
parents:
diff changeset
173 except:
562717546168 refactoring...
casties
parents:
diff changeset
174 return qstr
562717546168 refactoring...
casties
parents:
diff changeset
175
562717546168 refactoring...
casties
parents:
diff changeset
176 def getKMLname(self,data=[],table=""):
562717546168 refactoring...
casties
parents:
diff changeset
177 logging.debug("getKMLname")
562717546168 refactoring...
casties
parents:
diff changeset
178 #session=context.REQUEST.SESSION
562717546168 refactoring...
casties
parents:
diff changeset
179 kml4Marker="<kml xmlns=\'http://www.opengis.net/kml/2.2\'><Document><Style id=\'marker_icon\'><IconStyle><scale>15</scale><Icon><href>http://chinagis.mpiwg-berlin.mpg.de/chinagis/images/dot_red.png</href></Icon></IconStyle></Style>\n"
562717546168 refactoring...
casties
parents:
diff changeset
180 initializeStringForGoogleMaps=""
562717546168 refactoring...
casties
parents:
diff changeset
181 #doLine=container.getVar('doLine')
562717546168 refactoring...
casties
parents:
diff changeset
182 # Mapping a set of points from table-based SQL-query:
562717546168 refactoring...
casties
parents:
diff changeset
183 if data!=None:
562717546168 refactoring...
casties
parents:
diff changeset
184 try:
562717546168 refactoring...
casties
parents:
diff changeset
185 SQL='SELECT "attribute with gis_id" FROM public.metadata WHERE tablename = %s'
562717546168 refactoring...
casties
parents:
diff changeset
186 res = self.executeSQL(SQL, (table,))
562717546168 refactoring...
casties
parents:
diff changeset
187 gisIDattribute = res['rows'][0][0]
562717546168 refactoring...
casties
parents:
diff changeset
188 except:
562717546168 refactoring...
casties
parents:
diff changeset
189 return "table not registered within metadata"
562717546168 refactoring...
casties
parents:
diff changeset
190
562717546168 refactoring...
casties
parents:
diff changeset
191 for dataset in data:
562717546168 refactoring...
casties
parents:
diff changeset
192 try:
562717546168 refactoring...
casties
parents:
diff changeset
193 xCoord=getattr(dataset,'longitude')
562717546168 refactoring...
casties
parents:
diff changeset
194 yCoord=getattr(dataset,'latitude')
562717546168 refactoring...
casties
parents:
diff changeset
195 except:
562717546168 refactoring...
casties
parents:
diff changeset
196 try:
562717546168 refactoring...
casties
parents:
diff changeset
197 xCoord=getattr(dataset,'x_coord')
562717546168 refactoring...
casties
parents:
diff changeset
198 yCoord=getattr(dataset,'y_coord')
562717546168 refactoring...
casties
parents:
diff changeset
199 except:
562717546168 refactoring...
casties
parents:
diff changeset
200 #try:
562717546168 refactoring...
casties
parents:
diff changeset
201 gisID=getattr(dataset,gisIDattribute)
562717546168 refactoring...
casties
parents:
diff changeset
202 coords=self.getPoint4GISid(gisID)
562717546168 refactoring...
casties
parents:
diff changeset
203 if coords!=None:
562717546168 refactoring...
casties
parents:
diff changeset
204 xCoord=coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
205 yCoord=coords[1]
562717546168 refactoring...
casties
parents:
diff changeset
206 # except:
562717546168 refactoring...
casties
parents:
diff changeset
207 # return "no coordinates found"
562717546168 refactoring...
casties
parents:
diff changeset
208
562717546168 refactoring...
casties
parents:
diff changeset
209 if float(xCoord)!=0:
562717546168 refactoring...
casties
parents:
diff changeset
210 if float(yCoord)!=0:
562717546168 refactoring...
casties
parents:
diff changeset
211 kml4Marker=kml4Marker+"<Placemark>"
562717546168 refactoring...
casties
parents:
diff changeset
212 kml4Marker=kml4Marker+"<description> <![CDATA[<b>"
562717546168 refactoring...
casties
parents:
diff changeset
213 for values in dataset:
562717546168 refactoring...
casties
parents:
diff changeset
214 if values != (None, None):
562717546168 refactoring...
casties
parents:
diff changeset
215 if str(values).find('name')>-1:
562717546168 refactoring...
casties
parents:
diff changeset
216 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
562717546168 refactoring...
casties
parents:
diff changeset
217 continue
562717546168 refactoring...
casties
parents:
diff changeset
218 elif str(values).find('place')>-1:
562717546168 refactoring...
casties
parents:
diff changeset
219 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
562717546168 refactoring...
casties
parents:
diff changeset
220 continue
562717546168 refactoring...
casties
parents:
diff changeset
221
562717546168 refactoring...
casties
parents:
diff changeset
222 kml4Marker=kml4Marker+str(values)+": "
562717546168 refactoring...
casties
parents:
diff changeset
223 attribute_string=str(values).replace("'","__Apostroph__")
562717546168 refactoring...
casties
parents:
diff changeset
224 attribute_string=str(attribute_string).replace('"','__DoubleApostroph__')
562717546168 refactoring...
casties
parents:
diff changeset
225 attribute_string=str(attribute_string).replace(';','__$$__')
562717546168 refactoring...
casties
parents:
diff changeset
226 attribute_string=str(attribute_string).replace('&','&amp;')
562717546168 refactoring...
casties
parents:
diff changeset
227 if str(attribute_string).find('http')>-1:
562717546168 refactoring...
casties
parents:
diff changeset
228 attribute_string='<A HREF=' + str(attribute_string) + ' target=_blank>' + str(attribute_string) + '</A>'
562717546168 refactoring...
casties
parents:
diff changeset
229 kml4Marker=kml4Marker+attribute_string+"</a><br>\n"
562717546168 refactoring...
casties
parents:
diff changeset
230
562717546168 refactoring...
casties
parents:
diff changeset
231 kml4Marker=kml4Marker+"]]></description>\n"
562717546168 refactoring...
casties
parents:
diff changeset
232 kml4Marker=kml4Marker+"<styleURL>#marker_icon</styleURL>\n"
562717546168 refactoring...
casties
parents:
diff changeset
233 kml4Marker=kml4Marker+"<Point>"
562717546168 refactoring...
casties
parents:
diff changeset
234
562717546168 refactoring...
casties
parents:
diff changeset
235 kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n"
562717546168 refactoring...
casties
parents:
diff changeset
236 kml4Marker=kml4Marker+"</Point>\n"
562717546168 refactoring...
casties
parents:
diff changeset
237 kml4Marker=kml4Marker+"</Placemark>\n"
562717546168 refactoring...
casties
parents:
diff changeset
238
562717546168 refactoring...
casties
parents:
diff changeset
239 kml4Marker=kml4Marker+"</Document>\n</kml>"
562717546168 refactoring...
casties
parents:
diff changeset
240 kmlFileName="marker"+str(time.time())+".kml"
562717546168 refactoring...
casties
parents:
diff changeset
241
562717546168 refactoring...
casties
parents:
diff changeset
242 #kml4Marker=str(kml4Marker).replace('&','$$')
562717546168 refactoring...
casties
parents:
diff changeset
243 #kml4Marker=str(kml4Marker).replace(';','__$$__')
562717546168 refactoring...
casties
parents:
diff changeset
244 #kml4Marker=str(kml4Marker).replace('#','__SHARP__')
562717546168 refactoring...
casties
parents:
diff changeset
245 isLoadReady='false'
562717546168 refactoring...
casties
parents:
diff changeset
246 while isLoadReady=='false':
562717546168 refactoring...
casties
parents:
diff changeset
247 isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker)
562717546168 refactoring...
casties
parents:
diff changeset
248
562717546168 refactoring...
casties
parents:
diff changeset
249 return kmlFileName
562717546168 refactoring...
casties
parents:
diff changeset
250
562717546168 refactoring...
casties
parents:
diff changeset
251 def getGoogleMapString(self,kml):
562717546168 refactoring...
casties
parents:
diff changeset
252 logging.debug("getGoogleMapString")
562717546168 refactoring...
casties
parents:
diff changeset
253 printed= '<body %s> '%kml +"""\n <div id="map_canvas" style="width: 98%; height: 95%"> </div> \n </body>" \n </html>"""
562717546168 refactoring...
casties
parents:
diff changeset
254 return printed
562717546168 refactoring...
casties
parents:
diff changeset
255
562717546168 refactoring...
casties
parents:
diff changeset
256 def getPoint4GISid(self,gis_id):
562717546168 refactoring...
casties
parents:
diff changeset
257 j=0
562717546168 refactoring...
casties
parents:
diff changeset
258 coords=(0,0)
562717546168 refactoring...
casties
parents:
diff changeset
259 if gis_id != None:
562717546168 refactoring...
casties
parents:
diff changeset
260 while (True):
562717546168 refactoring...
casties
parents:
diff changeset
261 j=j+1
562717546168 refactoring...
casties
parents:
diff changeset
262 if (j>100): # FJK: just to prevent endless loops
562717546168 refactoring...
casties
parents:
diff changeset
263 break
562717546168 refactoring...
casties
parents:
diff changeset
264 if (gis_id.isdigit()): # FJK: regular exit from while-loop
562717546168 refactoring...
casties
parents:
diff changeset
265 break
562717546168 refactoring...
casties
parents:
diff changeset
266 else:
562717546168 refactoring...
casties
parents:
diff changeset
267 gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters
562717546168 refactoring...
casties
parents:
diff changeset
268 gis_id=gis_id.strip() # FJK: to strip all whitespaces
562717546168 refactoring...
casties
parents:
diff changeset
269 resultpoint = [0,0]
562717546168 refactoring...
casties
parents:
diff changeset
270 results = None
562717546168 refactoring...
casties
parents:
diff changeset
271 try:
562717546168 refactoring...
casties
parents:
diff changeset
272 if int(gis_id)>0:
562717546168 refactoring...
casties
parents:
diff changeset
273 SQL="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE cast("+ str(gis_id) +" as text);"
562717546168 refactoring...
casties
parents:
diff changeset
274 results=self.ZSQLSimpleSearch(SQL)
562717546168 refactoring...
casties
parents:
diff changeset
275 #print results
562717546168 refactoring...
casties
parents:
diff changeset
276 if results != None:
562717546168 refactoring...
casties
parents:
diff changeset
277 for result in results:
562717546168 refactoring...
casties
parents:
diff changeset
278 resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))]
562717546168 refactoring...
casties
parents:
diff changeset
279 if resultpoint !=[0,0]:
562717546168 refactoring...
casties
parents:
diff changeset
280 return resultpoint
562717546168 refactoring...
casties
parents:
diff changeset
281 else:
562717546168 refactoring...
casties
parents:
diff changeset
282 coords=self.getCoordsFromREST_gisID(joinid)
562717546168 refactoring...
casties
parents:
diff changeset
283 SQL="INSERT INTO chgis.chgis_coords (gis_id,x_coord,y_coord) VALUES (" +gis_id+ "," +coords[0][1]+ "," +coords[0][0]+ "); ANALYZE chgis.chgis_coords;"
562717546168 refactoring...
casties
parents:
diff changeset
284 returnstring=self.ZSQLSimpleSearch(SQL)
562717546168 refactoring...
casties
parents:
diff changeset
285 return coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
286 except:
562717546168 refactoring...
casties
parents:
diff changeset
287 return "gis_id not to interpretable:"+str(gis_id)
562717546168 refactoring...
casties
parents:
diff changeset
288 else:
562717546168 refactoring...
casties
parents:
diff changeset
289 return coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
290
562717546168 refactoring...
casties
parents:
diff changeset
291 def getCoordsFromREST_gisID(self,gis_id):
562717546168 refactoring...
casties
parents:
diff changeset
292 coordlist=[]
562717546168 refactoring...
casties
parents:
diff changeset
293 i=0
562717546168 refactoring...
casties
parents:
diff changeset
294 while (i<5 and coordlist==[]):
562717546168 refactoring...
casties
parents:
diff changeset
295
562717546168 refactoring...
casties
parents:
diff changeset
296 urlresponse=container.urlFunctions.zUrlopenParseString(container.urlFunctions.zUrlopenRead("http://chgis.hmdc.harvard.edu/xml/id/"+gis_id))
562717546168 refactoring...
casties
parents:
diff changeset
297 baseDocElement=container.urlFunctions.zUrlopenDocumentElement(urlresponse)
562717546168 refactoring...
casties
parents:
diff changeset
298 childnodes=container.urlFunctions.zUrlopenChildNodes(baseDocElement)
562717546168 refactoring...
casties
parents:
diff changeset
299 itemnodes=container.urlFunctions.zUrlopenGetElementsByTagName(baseDocElement,'item')
562717546168 refactoring...
casties
parents:
diff changeset
300
562717546168 refactoring...
casties
parents:
diff changeset
301 for i in range(0,container.urlFunctions.zUrlopenLength(itemnodes)):
562717546168 refactoring...
casties
parents:
diff changeset
302 itemnode=container.urlFunctions.zUrlopenGetItem(itemnodes,i)
562717546168 refactoring...
casties
parents:
diff changeset
303 itemspatialnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemnode,'spatial')
562717546168 refactoring...
casties
parents:
diff changeset
304 for j in range(0,container.urlFunctions.zUrlopenLength(itemspatialnodes)):
562717546168 refactoring...
casties
parents:
diff changeset
305 coord=[]
562717546168 refactoring...
casties
parents:
diff changeset
306 itemspatialnode= container.urlFunctions.zUrlopenGetItem(itemspatialnodes,j)
562717546168 refactoring...
casties
parents:
diff changeset
307 itemspatiallatnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_latitude')
562717546168 refactoring...
casties
parents:
diff changeset
308 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallatnodes)):
562717546168 refactoring...
casties
parents:
diff changeset
309 itemspatiallatnode= container.urlFunctions.zUrlopenGetItem(itemspatiallatnodes,k)
562717546168 refactoring...
casties
parents:
diff changeset
310 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallatnode))
562717546168 refactoring...
casties
parents:
diff changeset
311 itemspatiallngnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_longitude')
562717546168 refactoring...
casties
parents:
diff changeset
312 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallngnodes)):
562717546168 refactoring...
casties
parents:
diff changeset
313 itemspatiallngnode= container.urlFunctions.zUrlopenGetItem(itemspatiallngnodes,k)
562717546168 refactoring...
casties
parents:
diff changeset
314 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallngnode))
562717546168 refactoring...
casties
parents:
diff changeset
315 coordlist.append(coord)
562717546168 refactoring...
casties
parents:
diff changeset
316 gis_id= "_"+gis_id
562717546168 refactoring...
casties
parents:
diff changeset
317 return coordlist
562717546168 refactoring...
casties
parents:
diff changeset
318
562717546168 refactoring...
casties
parents:
diff changeset
319 # End for GoogleMaps creation
562717546168 refactoring...
casties
parents:
diff changeset
320
562717546168 refactoring...
casties
parents:
diff changeset
321 def RESTwrite2File(self,datadir, name,text):
562717546168 refactoring...
casties
parents:
diff changeset
322 logging.debug("RESTwrite2File datadir=%s name=%s"%(datadir,name))
562717546168 refactoring...
casties
parents:
diff changeset
323 try:
562717546168 refactoring...
casties
parents:
diff changeset
324 import cStringIO as StringIO
562717546168 refactoring...
casties
parents:
diff changeset
325 except:
562717546168 refactoring...
casties
parents:
diff changeset
326 import StringIO
562717546168 refactoring...
casties
parents:
diff changeset
327
562717546168 refactoring...
casties
parents:
diff changeset
328 # make filehandle from string
562717546168 refactoring...
casties
parents:
diff changeset
329 textfile = StringIO.StringIO(text)
562717546168 refactoring...
casties
parents:
diff changeset
330 fileid=name
562717546168 refactoring...
casties
parents:
diff changeset
331 if fileid in datadir.objectIds():
562717546168 refactoring...
casties
parents:
diff changeset
332 datadir.manage_delObjects(fileid)
562717546168 refactoring...
casties
parents:
diff changeset
333 fileInZope=datadir.manage_addFile(id=fileid,file=textfile)
562717546168 refactoring...
casties
parents:
diff changeset
334 return "Write successful"
562717546168 refactoring...
casties
parents:
diff changeset
335
562717546168 refactoring...
casties
parents:
diff changeset
336 def manage_editRestDbGisApi(self, title=None, connection_id=None,
562717546168 refactoring...
casties
parents:
diff changeset
337 REQUEST=None):
562717546168 refactoring...
casties
parents:
diff changeset
338 """Change the object"""
562717546168 refactoring...
casties
parents:
diff changeset
339 if title is not None:
562717546168 refactoring...
casties
parents:
diff changeset
340 self.title = title
562717546168 refactoring...
casties
parents:
diff changeset
341
562717546168 refactoring...
casties
parents:
diff changeset
342 if connection_id is not None:
562717546168 refactoring...
casties
parents:
diff changeset
343 self.connection_id = connection_id
562717546168 refactoring...
casties
parents:
diff changeset
344
562717546168 refactoring...
casties
parents:
diff changeset
345 #checkPermission=getSecurityManager().checkPermission
562717546168 refactoring...
casties
parents:
diff changeset
346 REQUEST.RESPONSE.redirect('manage_main')
562717546168 refactoring...
casties
parents:
diff changeset
347
562717546168 refactoring...
casties
parents:
diff changeset
348
562717546168 refactoring...
casties
parents:
diff changeset
349 manage_addRestDbGisApiForm=PageTemplateFile('zpt/addRestDbGisApi',globals())
562717546168 refactoring...
casties
parents:
diff changeset
350
562717546168 refactoring...
casties
parents:
diff changeset
351 def manage_addRestDbGisApi(self, id, title='', label='', description='',
562717546168 refactoring...
casties
parents:
diff changeset
352 createPublic=0,
562717546168 refactoring...
casties
parents:
diff changeset
353 createUserF=0,
562717546168 refactoring...
casties
parents:
diff changeset
354 REQUEST=None):
562717546168 refactoring...
casties
parents:
diff changeset
355 """Add a new object with id *id*."""
562717546168 refactoring...
casties
parents:
diff changeset
356
562717546168 refactoring...
casties
parents:
diff changeset
357 ob=RestDbGisApi(str(id),title)
562717546168 refactoring...
casties
parents:
diff changeset
358 self._setObject(id, ob)
562717546168 refactoring...
casties
parents:
diff changeset
359
562717546168 refactoring...
casties
parents:
diff changeset
360 #checkPermission=getSecurityManager().checkPermission
562717546168 refactoring...
casties
parents:
diff changeset
361 REQUEST.RESPONSE.redirect('manage_main')
562717546168 refactoring...
casties
parents:
diff changeset
362
562717546168 refactoring...
casties
parents:
diff changeset
363