Mercurial > hg > ChinaGisRestApi
comparison RestDbGisApi.py @ 274:9b7db308d2e6
refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
| author | casties |
|---|---|
| date | Thu, 23 Feb 2012 08:35:26 +0100 |
| parents | 52b1247140b7 |
| children | eb8a18f94d2d |
comparison
equal
deleted
inserted
replaced
| 270:3a5d51c60e40 | 274:9b7db308d2e6 |
|---|---|
| 11 import re | 11 import re |
| 12 import time | 12 import time |
| 13 import datetime | 13 import datetime |
| 14 import urlFunctions | 14 import urlFunctions |
| 15 | 15 |
| 16 from RestDbInterface import * | 16 from Products.ZDBInterface.WritableRestDbInterface import * |
| 17 | 17 |
| 18 | 18 |
| 19 def kmlEncode(s): | 19 def kmlEncode(s): |
| 20 """returns string encoded for displaying in KML attribute""" | 20 """returns string encoded for displaying in KML attribute""" |
| 21 res = s.replace("'","__Apostroph__") | 21 res = s.replace("'","__Apostroph__") |
| 23 res = res.replace(';','__$$__') | 23 res = res.replace(';','__$$__') |
| 24 res = res.replace('&','&') | 24 res = res.replace('&','&') |
| 25 return res | 25 return res |
| 26 | 26 |
| 27 | 27 |
| 28 class RestDbGisApi(RestDbInterface): | 28 class RestDbGisApi(WritableRestDbInterface): |
| 29 """Object for RESTful GIS database queries | 29 """Object for RESTful GIS database queries |
| 30 path schema: /db/{schema}/{table}/ | 30 path schema: /db/{schema}/{table}/ |
| 31 omitting table gives a list of schemas | 31 omitting table gives a list of schemas |
| 32 omitting table and schema gives a list of schemas | 32 omitting table and schema gives a list of schemas |
| 33 """ | 33 """ |
| 34 | 34 |
| 35 meta_type="RESTgis" | 35 meta_type="RESTgis" |
| 36 | 36 |
| 37 # data templates | |
| 38 GIS_schema_table = PageTemplateFile('zpt/GIS_schema_table', globals()) | |
| 39 KML_schema_table = PageTemplateFile('zpt/KML_schema_table', globals()) | |
| 40 HTML_schema_usertables = PageTemplateFile('zpt/HTML_schema_usertables', globals()) | |
| 37 | 41 |
| 38 # and scripts | 42 # and scripts |
| 39 def KML_URL_schema_table(self,schema,table, useTimestamp=True, args=None): | 43 def KML_URL_schema_table(self,schema,table, useTimestamp=True, args=None): |
| 40 """KML_URL table function""" | 44 """KML_URL table function""" |
| 41 self.REQUEST.RESPONSE.setHeader("Content-Type", "text/plain") | 45 self.REQUEST.RESPONSE.setHeader("Content-Type", "text/plain") |
| 136 | 140 |
| 137 | 141 |
| 138 def createEmptyTable(self,schema,table,fields): | 142 def createEmptyTable(self,schema,table,fields): |
| 139 """create a table with the given fields | 143 """create a table with the given fields |
| 140 returns list of created fields""" | 144 returns list of created fields""" |
| 141 sqlFields = RestDbInterface.createEmptyTable(self,schema,table,fields) | 145 sqlFields = WritableRestDbInterface.createEmptyTable(self,schema,table,fields) |
| 142 if sqlFields is not None: | 146 if sqlFields is not None: |
| 143 self.setTableMetaTypes(schema,table,sqlFields) | 147 self.setTableMetaTypes(schema,table,sqlFields) |
| 144 | 148 |
| 145 return sqlFields | 149 return sqlFields |
| 146 | 150 |
| 151 # TODO: move this | |
| 152 def getAttributeNames(self,schema='public',table=None): | |
| 153 return self.executeSQL("SELECT attname FROM pg_attribute, pg_class WHERE pg_class.oid = attrelid AND attnum>0 AND relname = '%s';"%(table)) | |
| 154 | |
| 155 # TODO: move this | |
| 156 def getAttributeTypes(self,schema='public',table=None): | |
| 157 return self.executeSQL("SELECT field_name, gis_type FROM public.gis_table_meta_rows WHERE table_name = '%s';"%(table)) | |
| 158 | |
| 159 # TODO: move back to inherited version | |
| 160 def showTable(self,format='XML',schema='public',table=None,REQUEST=None,RESPONSE=None): | |
| 161 """returns PageTemplate with tables""" | |
| 162 logging.debug("showtable") | |
| 163 if REQUEST is None: | |
| 164 REQUEST = self.REQUEST | |
| 165 queryArgs={'doc':None,'id':None} | |
| 166 queryArgs['doc'] = REQUEST.get('doc') | |
| 167 queryArgs['id'] = REQUEST.get('id') | |
| 168 | |
| 169 # should be cross-site accessible | |
| 170 if RESPONSE is None: | |
| 171 RESPONSE = self.REQUEST.RESPONSE | |
| 172 | |
| 173 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') | |
| 174 | |
| 175 # everything else has its own template | |
| 176 pt = getattr(self.template, '%s_schema_table'%format, REQUEST) | |
| 177 if pt is None: | |
| 178 return "ERROR!! template %s_schema_table not found at %s"%(format, self.template ) | |
| 179 #data = self.getTable(schema,table) | |
| 180 return pt(schema=schema,table=table,args=queryArgs) | |
| 181 | |
| 182 #TODO: move to parent class | |
| 183 def getLiveUrl(self,schema,table,useTimestamp=True,REQUEST=None): | |
| 184 if REQUEST is None: | |
| 185 REQUEST = self.REQUEST | |
| 186 logging.debug("getLiveUrl") | |
| 187 baseUrl = self.absolute_url() | |
| 188 timestamp = time.time() | |
| 189 # filter parameters in URL and add to new URL | |
| 190 params = [p for p in REQUEST.form.items() if p[0] not in ('format','timestamp')] | |
| 191 params.append(('format','KML')) | |
| 192 if useTimestamp: | |
| 193 # add timestamp so URL changes every time | |
| 194 params.append(('timestamp',timestamp)) | |
| 195 paramstr = urllib.urlencode(params) | |
| 196 return "%s/db/%s/%s?%s"%(baseUrl,schema,table,paramstr) | |
| 197 | |
| 198 | |
| 199 # TODO: remove changes from parent version | |
| 200 def getTable(self,schema='public',table=None,sortBy=1,username='guest'): | |
| 201 """return table data""" | |
| 202 logging.debug("gettable") | |
| 203 attrNames=self.getAttributeNames(schema,table) | |
| 204 attrTypes=self.getAttributeTypes(schema,table) | |
| 205 attrString="" | |
| 206 # try: | |
| 207 for name in attrNames['rows']: | |
| 208 logging.debug("name: ", name[0]) | |
| 209 not_added=True | |
| 210 if name[0] == "the_geom": #FJK: the table column is "the_geom" | |
| 211 attrString=attrString+"ST_AsText("+name[0]+")," | |
| 212 not_added=False | |
| 213 break | |
| 214 for a_iter in attrTypes['rows']: | |
| 215 not_added = True | |
| 216 logging.debug("attrTypes.field_name: ", a_iter[0]) | |
| 217 if a_iter[0]==name[0]: | |
| 218 logging.debug("attrTypes.gis_type: ", a_iter[1]) | |
| 219 if a_iter[1] == "the_geom": #FJK: the table column is registered in gis_table_meta_rows as type "the_geom" | |
| 220 attrString=attrString+"ST_AsText("+name[0]+")," | |
| 221 not_added=False | |
| 222 if not_added: | |
| 223 if name[0].find('pg.dropped')==-1: | |
| 224 attrString=attrString+name[0]+"," | |
| 225 attrString=str(attrString).rsplit(",",1)[0] #to remove last "," | |
| 226 if sortBy: | |
| 227 data = self.executeSQL('select %s from "%s"."%s" order by %s'%(attrString,schema,table,sortBy)) | |
| 228 else: | |
| 229 data = self.executeSQL('select %s from "%s"."%s"'%(attrString,schema,table)) | |
| 230 # except: | |
| 231 """ table does not exist """ | |
| 232 # fields=self.get | |
| 233 # self.createEmptyTable(schema, table, fields) | |
| 234 return data | |
| 235 | |
| 147 | 236 |
| 148 def getLiveKmlUrl(self,schema,table,useTimestamp=True,REQUEST=None): | 237 def getLiveKmlUrl(self,schema,table,useTimestamp=True,REQUEST=None): |
| 149 return self.getLiveUrl(schema,table,useTimestamp,REQUEST) | 238 return self.getLiveUrl(schema,table,useTimestamp,REQUEST) |
| 150 | 239 |
| 151 def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None, geomField="point", colorField="red_big"): | 240 def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None, geomField="point", colorField="red_big"): |
