annotate RestDbGisApi.py @ 191:03976fe6ef2d

corrected links in desc (RestDbGisApi)
author fknauft
date Wed, 16 Feb 2011 14:51:39 +0100
parents 30f669d9978f
children 96aea3e864cc
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 time
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
13 import datetime
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
14 import urllib
43
562717546168 refactoring...
casties
parents:
diff changeset
15
562717546168 refactoring...
casties
parents:
diff changeset
16 from RestDbInterface import *
562717546168 refactoring...
casties
parents:
diff changeset
17
562717546168 refactoring...
casties
parents:
diff changeset
18
562717546168 refactoring...
casties
parents:
diff changeset
19
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
20 def kmlEncode(s):
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
21 """returns string encoded for displaying in KML attribute"""
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
22 res = s.replace("'","__Apostroph__")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
23 res = res.replace('"','__DoubleApostroph__')
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
24 res = res.replace(';','__$$__')
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
25 res = res.replace('&','&')
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
26 return res
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
27
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
28
43
562717546168 refactoring...
casties
parents:
diff changeset
29 class RestDbGisApi(RestDbInterface):
562717546168 refactoring...
casties
parents:
diff changeset
30 """Object for RESTful GIS database queries
562717546168 refactoring...
casties
parents:
diff changeset
31 path schema: /db/{schema}/{table}/
562717546168 refactoring...
casties
parents:
diff changeset
32 omitting table gives a list of schemas
562717546168 refactoring...
casties
parents:
diff changeset
33 omitting table and schema gives a list of schemas
562717546168 refactoring...
casties
parents:
diff changeset
34 """
562717546168 refactoring...
casties
parents:
diff changeset
35
562717546168 refactoring...
casties
parents:
diff changeset
36 meta_type="RESTgis"
562717546168 refactoring...
casties
parents:
diff changeset
37
562717546168 refactoring...
casties
parents:
diff changeset
38 # data templates
562717546168 refactoring...
casties
parents:
diff changeset
39 GIS_schema_table = PageTemplateFile('zpt/GIS_schema_table', globals())
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
40 KML_schema_table = PageTemplateFile('zpt/KML_schema_table', globals())
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
41 HTML_schema_usertables = PageTemplateFile('zpt/HTML_schema_usertables', globals())
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
42
44
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
43 # and scripts
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
44 def KML_URL_schema_table(self,schema,table):
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
45 """KML_URL table function"""
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
46 self.REQUEST.RESPONSE.setHeader("Content-Type", "text/plain")
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
47 id = self.REQUEST.get('id',[])
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
48 doc = self.REQUEST.get('doc',None)
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
49 return self.getLiveKmlUrl(schema=schema,table=table)
43
562717546168 refactoring...
casties
parents:
diff changeset
50
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
51 #
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
52 # database methods
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
53 #
70
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
54 toSqlTypeMap = {
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
55 "text": "text",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
56 "number": "numeric",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
57 "id": "text",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
58 "gis_id": "text",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
59 "coord_lat": "numeric",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
60 "coord_lon": "numeric"
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
61 }
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
62
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
63 def getTableOwner(self,schema,table):
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
64 """returns the owner of the table"""
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
65 # what do we do with the schema?
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
66 sql = 'select table_owner from public.gis_table_meta where table_name = %s'
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
67 res = self.executeSQL(sql,(table,))
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
68 if len(res['rows']) > 0:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
69 return res['rows'][0][0]
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
70 return None
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
71
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
72 def isAllowed(self,action,schema,table,user=None,owner=None):
43
562717546168 refactoring...
casties
parents:
diff changeset
73 """returns if the requested action on the table is allowed"""
562717546168 refactoring...
casties
parents:
diff changeset
74 if user is None:
562717546168 refactoring...
casties
parents:
diff changeset
75 user = self.REQUEST.get('AUTHENTICATED_USER',None)
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
76 logging.debug("isAllowed action=%s schema=%s table=%s user=%s"%(action,schema,table,user))
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
77 if action == "create":
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
78 if user is not None and str(user) != 'Anonymous User':
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
79 # any authenticated user can create
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
80 return True
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
81 else:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
82 return False
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
83
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
84 if action == "update":
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
85 if owner is None:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
86 owner = self.getTableOwner(schema,table)
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
87 logging.debug("isAllowed user=%s owner=%s"%(user,owner))
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
88 if user is not None and str(user) == str(owner):
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
89 # update only your own table
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
90 return True
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
91 else:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
92 return False
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
93
62
3905385c8854 small fish
casties
parents: 61
diff changeset
94 # anything else is allowed
43
562717546168 refactoring...
casties
parents:
diff changeset
95 return True
562717546168 refactoring...
casties
parents:
diff changeset
96
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
97 def setTableMetaTypes(self,schema,table,fields,user=None):
43
562717546168 refactoring...
casties
parents:
diff changeset
98 """sets the GIS meta information for table"""
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
99 if user is None:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
100 user = self.REQUEST.get('AUTHENTICATED_USER',None)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
101
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
102 logging.debug("settablemetatypes schema=%s, table=%s, fields=%s user=%s"%(schema,table,fields,user))
43
562717546168 refactoring...
casties
parents:
diff changeset
103
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
104 today = datetime.date.today().isoformat()
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
105
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
106 res = self.executeSQL('select * from public.gis_table_meta where table_name = %s', (table,))
43
562717546168 refactoring...
casties
parents:
diff changeset
107 if len(res['rows']) > 0:
562717546168 refactoring...
casties
parents:
diff changeset
108 # meta record exists
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
109 sql = 'update public.gis_table_meta set table_owner=%s, table_modified=%s where table_name=%s'
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
110 self.executeSQL(sql, (str(user),today,table), hasResult=False)
43
562717546168 refactoring...
casties
parents:
diff changeset
111 else:
562717546168 refactoring...
casties
parents:
diff changeset
112 # new meta record
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
113 sql = 'insert into public.gis_table_meta (table_name,table_owner,table_created) values (%s,%s,%s)'
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
114 self.executeSQL(sql, (table,str(user),today), hasResult=False)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
115
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
116 # update row info
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
117 sql = 'delete from public.gis_table_meta_rows where table_name=%s'
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
118 self.executeSQL(sql,(table,),hasResult=False)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
119 sql = 'insert into public.gis_table_meta_rows (table_name,field_name,gis_type) values (%s,%s,%s)'
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
120 for f in fields:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
121 t = f['type']
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
122 fn = f['name']
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
123 self.executeSQL(sql, (table,fn,t), hasResult=False)
43
562717546168 refactoring...
casties
parents:
diff changeset
124
562717546168 refactoring...
casties
parents:
diff changeset
125
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
126 def getListOfUserTables(self,schema='public',username='guest'):
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
127 """return list of tables"""
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
128 logging.debug("getlistofusertables")
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
129 # get list of db tables
100
4c125fbb2c5b Views are listed in "getListOfUserTables"
fknauft
parents: 93
diff changeset
130 # qstr = """SELECT t.table_name FROM information_schema.tables t, public.gis_table_meta m WHERE t.table_type = 'BASE TABLE'
4c125fbb2c5b Views are listed in "getListOfUserTables"
fknauft
parents: 93
diff changeset
131 # AND t.table_schema = %s AND t.table_name = m.table_name ORDER BY 1"""
4c125fbb2c5b Views are listed in "getListOfUserTables"
fknauft
parents: 93
diff changeset
132 qstr = """SELECT t.table_name FROM information_schema.tables t, public.gis_table_meta m
4c125fbb2c5b Views are listed in "getListOfUserTables"
fknauft
parents: 93
diff changeset
133 WHERE t.table_schema = %s AND t.table_name = m.table_name ORDER BY 1"""
43
562717546168 refactoring...
casties
parents:
diff changeset
134
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
135 data=self.executeSQL(qstr,(schema,))
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
136 return data
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
137
43
562717546168 refactoring...
casties
parents:
diff changeset
138
562717546168 refactoring...
casties
parents:
diff changeset
139 def createEmptyTable(self,schema,table,fields):
562717546168 refactoring...
casties
parents:
diff changeset
140 """create a table with the given fields
562717546168 refactoring...
casties
parents:
diff changeset
141 returns list of created fields"""
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
142 sqlFields = RestDbInterface.createEmptyTable(self,schema,table,fields)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
143 if sqlFields is not None:
43
562717546168 refactoring...
casties
parents:
diff changeset
144 self.setTableMetaTypes(schema,table,sqlFields)
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
145
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
146 return sqlFields
43
562717546168 refactoring...
casties
parents:
diff changeset
147
562717546168 refactoring...
casties
parents:
diff changeset
148
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
149 def getLiveKmlUrl(self,schema,table,useTimestamp=True,REQUEST=None):
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
150 if REQUEST is None:
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
151 REQUEST = self.REQUEST
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
152 logging.debug("getLiveKmlUrl")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
153 baseUrl = self.absolute_url()
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
154 timestamp = time.time()
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
155 # filter parameters in URL and add to new URL
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
156 params = [p for p in REQUEST.form.items() if p[0] not in ('format','timestamp')]
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
157 params.append(('format','KML'))
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
158 if useTimestamp:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
159 # add timestamp so URL changes every time
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
160 params.append(('timestamp',timestamp))
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
161 paramstr = urllib.urlencode(params)
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
162 return "%s/db/%s/%s?%s"%(baseUrl,schema,table,paramstr)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
163
127
7ed8de30af56 polygon-Layers
fknauft
parents: 126
diff changeset
164 def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None, geomField="point", colorField="red"):
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
165 """returns data structure for KML template"""
148
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
166 logging.debug("getKMLdata gid=%s lat=%s lon=%s geom=%s color=%s"%(gisIdField,latField,lonField,geomField,colorField))
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
167 if geomField is None:
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
168 geomField="point"
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
169 if colorField is None:
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
170 colorField="red"
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
171 # Mapping a set of points from table-based SQL-query:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
172 qstr='SELECT * FROM "%s"."%s"'%(schema,table)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
173 idList = None
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
174 if ids is not None:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
175 qstr += ' WHERE '
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
176 if table=='mpdl':
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
177 qstr += 'mpdl_xmlsource_id IN ('
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
178 else:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
179 qstr += 'CAST(id AS text) IN ('
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
180
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
181 idList = ids.split(",")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
182 qstr += ','.join(['%s' for i in idList])
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
183 qstr += ')'
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
184
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
185 if sortBy:
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
186 # add sort clause
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
187 if sortBy == 1:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
188 qstr += ' ORDER BY 1'
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
189 else:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
190 # TODO: proper quoting for names
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
191 qstr += ' ORDER BY "%s"'%sortBy.replace('"','')
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
192
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
193 data = self.executeSQL(qstr,idList)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
194
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
195 fieldMap = self.getFieldNameMap(data['fields'])
117
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
196 try:
153
cca88caad152 polygon-Layers
fknauft
parents: 152
diff changeset
197 geomstr='select astext(st_simplify(transform(the_geom,4326),0.01)) from "%s"."%s"'%(schema,table)
117
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
198 geomdata=self.executeSQL(geomstr)
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
199 except:
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
200 geomdata=None
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
201
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
202 if (gisIdField is None) and (latField is None or lonField is None):
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
203 # no fields given - choose automagically
93
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
204 sql = 'SELECT field_name FROM public.gis_table_meta_rows WHERE table_name = %s and gis_type = %s'
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
205 # gis id in metadata first
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
206 res = self.executeSQL(sql, (table,'gis_id'))
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
207 if len(res['rows']) > 0:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
208 gisIdField = res['rows'][0][0]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
209 else:
93
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
210 # latitude in metadata
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
211 res = self.executeSQL(sql, (table,'coord_lat'))
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
212 if len(res['rows']) > 0:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
213 latField = res['rows'][0][0]
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
214 # longitude in metadata
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
215 res = self.executeSQL(sql, (table,'coord_lon'))
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
216 if len(res['rows']) > 0:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
217 lonField = res['rows'][0][0]
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
218
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
219 if (gisIdField is None) and (latField is None or lonField is None):
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
220 logging.warning("no entry in metadata table for table %s" % table)
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
221 # still no fields - try field names
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
222 if 'latitude' in fieldMap and 'longitude' in fieldMap:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
223 latField = 'latitude'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
224 lonField = 'longitude'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
225 elif 'x_coord' in fieldMap and 'y_coord' in fieldMap:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
226 latField = 'x_coord'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
227 lonField = 'y_coord'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
228 else:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
229 logging.error("getKMLdata unable to find position fields")
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
230 return None
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
231
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
232
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
233 # convert field names to row indexes
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
234 gisIdIdx = fieldMap.get(gisIdField,None)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
235 latIdx = fieldMap.get(latField,None)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
236 lonIdx = fieldMap.get(lonField,None)
117
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
237 the_geom=fieldMap.get("the_geom",None)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
238 logging.debug("gisidfield=%s idx=%s"%(gisIdField,gisIdIdx))
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
239
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
240 # convert data
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
241 kmlData = []
151
348a11a5e49c polygon-Layers
fknauft
parents: 150
diff changeset
242 geom_list = {}
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
243 if geomField=='poly' and len(geomdata)>0:
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
244 geom_list=geomdata.values()[1]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
245 data_list=data['rows']
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
246 for k in range (len(data_list)):
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
247 dataset = data_list[k]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
248 if len(geom_list)>k:
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
249 geom_value = geom_list[k]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
250
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
251
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
252
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
253 # for dataset in data['rows']:
65
2f477270cc0c adding layers to maps works now
casties
parents: 62
diff changeset
254 xCoord = 0.0
2f477270cc0c adding layers to maps works now
casties
parents: 62
diff changeset
255 yCoord = 0.0
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
256 if gisIdIdx is not None:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
257 gisID = dataset[gisIdIdx]
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
258 coords=self.getPointForChGisId(gisID)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
259 if coords!=None:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
260 xCoord=coords[0]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
261 yCoord=coords[1]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
262
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
263 elif latIdx is not None:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
264 xCoord = dataset[lonIdx]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
265 yCoord = dataset[latIdx]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
266
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
267 else:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
268 logging.error("getKMLdata unable to find position")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
269 return None
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
270
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
271 if float(xCoord) == 0.0:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
272 continue
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
273
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
274 if float(yCoord) == 0.0:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
275 continue
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
276
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
277 kmlPlace = {}
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
278
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
279 # description
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
280 desc = ''
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
281
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
282
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
283 for i in range (len(dataset)):
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
284 value = dataset[i]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
285
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
286 name = data['fields'][i][0]
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
287 #logging.debug("value=%s"%value)
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
288 if name != 'the_geom':
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
289 if value != None:
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
290 #if name.find('name')>-1:
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
291 # desc += "<name>%s</name>\n"%value
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
292 # continue
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
293 #elif name.find('place')>-1:
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
294 # desc += "<name>%s</name>\n"%value
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
295 # continue
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
296
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
297 val = "%s: %s"%(name, value)
187
f080901d8163 corrected links in desc (RestDbGisApi)
fknauft
parents: 186
diff changeset
298 value=unicode(value)
188
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
299
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
300 # If there is a link within the description data, create a valid href
191
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
301 if value.find('http://')>-1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
302 link_str_beg=value.find('http://')
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
303 link_str_end = -1
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
304 link_str_end0=value.find(' ',link_str_beg)
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
305 link_str_end1=value.find('>',link_str_beg)
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
306 if link_str_end0 <link_str_end1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
307 link_str_end=link_str_end0
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
308 else:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
309 link_str_end=link_str_end1
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
310 if link_str_end > -1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
311 link_str=value[link_str_beg:link_str_end]
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
312 val ='<![CDATA['+name+": "+value[0:link_str_beg]+'<a href="' + link_str + ' " target="_blank">" ' + link_str + ' "</a>' + value[link_str_end:]+ ']]'
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
313 else:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
314 link_str=value[link_str_beg:]
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
315 val ='<![CDATA['+name+': '+value[0:link_str_beg]+'<a href="' + link_str + ' " target="_blank">" ' + link_str + ' "</a>' + ']]'
188
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
316
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
317
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
318 #desc += kmlEncode(val)
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
319 desc += val
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
320 desc += '<br/>\n'
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
321
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
322 #kmlPlace['description'] = "<![CDATA[%s]]>"%desc
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
323
124
8668d76a7b6a polygon-Layers
fknauft
parents: 123
diff changeset
324
133
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
325 if geomField=='point':
189
53fc023c9114 corrected links in desc (RestDbGisApi)
fknauft
parents: 188
diff changeset
326 kmlPlace['description'] = desc
133
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
327 kmlPlace['icon'] = '#marker_icon_'+colorField
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
328 kmlPlace['coord_x'] = str(xCoord)
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
329 kmlPlace['coord_y'] = str(yCoord)
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
330 kmlPlace['coord_z'] = '0'
134
649681baae76 polygon-Layers
fknauft
parents: 133
diff changeset
331 kmlData.append(kmlPlace)
135
ac22bb1e3212 polygon-Layers
fknauft
parents: 134
diff changeset
332 if geomField=='poly' and len(geomdata)>0:
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
333 polys=str(geom_value).split('(')
144
abc95f483315 polygon-Layers
fknauft
parents: 142
diff changeset
334 aaa=len(polys)
142
3adea92d2067 polygon-Layers
fknauft
parents: 138
diff changeset
335 for poly in polys:
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
336 kmlPlace = {}
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
337 kmlPlace['description'] = desc
152
7fddf8a60481 polygon-Layers
fknauft
parents: 151
diff changeset
338 coords=poly.replace(')','').replace("'","").split(',')
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
339 coord_string=''
144
abc95f483315 polygon-Layers
fknauft
parents: 142
diff changeset
340 if len(coords)>1:
146
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
341 for coord in coords:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
342 coord=coord.split(' ')
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
343 try:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
344 x_coord=coord[0]
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
345 if x_coord=='121.675615842265':
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
346 a='pause'
146
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
347 y_coord=coord[1]
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
348 except:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
349 break
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
350 coord_string+=x_coord+','+y_coord+','+'0 '
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
351 if coord_string != '':
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
352 kmlPlace['LinearRing']=coord_string
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
353 kmlPlace['LineColor']=colorField
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
354 kmlData.append(kmlPlace)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
355 #logging.debug("kmlData=%s"%(repr(kmlData)))
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
356 return kmlData
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
357
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
358 def getPointForChGisId(self, gis_id):
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
359 """returns coordinate pair for given gis_id"""
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
360 def getPoint(id):
157
8ef6fbc8b89a polygon-Layers
fknauft
parents: 153
diff changeset
361 sql="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id = CAST(%s AS text)"
8ef6fbc8b89a polygon-Layers
fknauft
parents: 153
diff changeset
362 str_gis_id=str(gis_id).split('.')[0]
8ef6fbc8b89a polygon-Layers
fknauft
parents: 153
diff changeset
363 res = self.executeSQL(sql, (str_gis_id,))
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
364 if len(res['rows']) > 0:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
365 return res['rows'][0]
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
366 return None
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
367
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
368 if gis_id is None or gis_id == "":
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
369 return None
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
370
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
371 # try gis_id
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
372 coords = getPoint(gis_id)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
373 if coords is None:
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
374 # try to clean gis_id...
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
375 gis_id = re.sub(r'[^0-9]','',gis_id)
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
376 # try again
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
377 coords = getPoint(gis_id)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
378 if coords is None:
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
379 logging.error("CH-GIS ID %s not found!"%repr(gis_id))
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
380
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
381 # TODO: do we need the getCoordsFromREST_gisID stuff?
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
382
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
383 return coords
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
384
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
385
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
386 ## legacy methods...
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
387
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
388 def getKmlUrl(self,schema='chgis',table='mpdl',id=[],doc=None):
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
389 logging.debug("getKmlUrl")
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
390 data = self.getDataForGoogleMap(schema,table,id,doc)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
391 kml=self.getKMLname(data=data,table=table)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
392 baseUrl = self.absolute_url()
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
393 return "%s/daten/%s"%(baseUrl,kml)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
394
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
395 def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=None,doc=None):
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
396 logging.debug("getDataForGoogleMap")
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
397 qstr="SELECT * FROM "+schema+"."+table
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
398 try:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
399 if id is not None:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
400 qstr=qstr+" WHERE "
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
401 for id_item in id.split(","):
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
402 if table=='mpdl':
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
403 qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR"
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
404 else:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
405 qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR"
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
406 qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and "
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
407 data=self.ZSQLSimpleSearch(qstr)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
408 return data
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
409 except:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
410 return qstr
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
411
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
412
43
562717546168 refactoring...
casties
parents:
diff changeset
413 def getKMLname(self,data=[],table=""):
562717546168 refactoring...
casties
parents:
diff changeset
414 logging.debug("getKMLname")
562717546168 refactoring...
casties
parents:
diff changeset
415 #session=context.REQUEST.SESSION
109
9db303437b32 changable color for dots in map
fknauft
parents: 108
diff changeset
416 kml4Marker="<kml xmlns=\'http://www.opengis.net/kml/2.2\'><Document>"
9db303437b32 changable color for dots in map
fknauft
parents: 108
diff changeset
417 kml4Marker+="<Style id=\'marker_icon_red\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_red.png</href></Icon></IconStyle></Style>\n"
9db303437b32 changable color for dots in map
fknauft
parents: 108
diff changeset
418 kml4Marker+="<Style id=\'marker_icon_black\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_black.png</href></Icon></IconStyle></Style>\n"
9db303437b32 changable color for dots in map
fknauft
parents: 108
diff changeset
419 kml4Marker+="<Style id=\'marker_icon_blue\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_blue.png</href></Icon></IconStyle></Style>\n"
9db303437b32 changable color for dots in map
fknauft
parents: 108
diff changeset
420 kml4Marker+="<Style id=\'marker_icon_green\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_green.png</href></Icon></IconStyle></Style>\n"
9db303437b32 changable color for dots in map
fknauft
parents: 108
diff changeset
421 kml4Marker+="<Style id=\'marker_icon_violett\'><IconStyle><scale>15</scale><Icon><href>http://mappit.mpiwg-berlin.mpg.de/mappit/icons/dot_violett.png</href></Icon></IconStyle></Style>\n"
43
562717546168 refactoring...
casties
parents:
diff changeset
422 initializeStringForGoogleMaps=""
562717546168 refactoring...
casties
parents:
diff changeset
423 #doLine=container.getVar('doLine')
562717546168 refactoring...
casties
parents:
diff changeset
424 # Mapping a set of points from table-based SQL-query:
562717546168 refactoring...
casties
parents:
diff changeset
425 if data!=None:
562717546168 refactoring...
casties
parents:
diff changeset
426 try:
562717546168 refactoring...
casties
parents:
diff changeset
427 SQL='SELECT "attribute with gis_id" FROM public.metadata WHERE tablename = %s'
562717546168 refactoring...
casties
parents:
diff changeset
428 res = self.executeSQL(SQL, (table,))
562717546168 refactoring...
casties
parents:
diff changeset
429 gisIDattribute = res['rows'][0][0]
562717546168 refactoring...
casties
parents:
diff changeset
430 except:
562717546168 refactoring...
casties
parents:
diff changeset
431 return "table not registered within metadata"
562717546168 refactoring...
casties
parents:
diff changeset
432
562717546168 refactoring...
casties
parents:
diff changeset
433 for dataset in data:
562717546168 refactoring...
casties
parents:
diff changeset
434 try:
562717546168 refactoring...
casties
parents:
diff changeset
435 xCoord=getattr(dataset,'longitude')
562717546168 refactoring...
casties
parents:
diff changeset
436 yCoord=getattr(dataset,'latitude')
562717546168 refactoring...
casties
parents:
diff changeset
437 except:
562717546168 refactoring...
casties
parents:
diff changeset
438 try:
562717546168 refactoring...
casties
parents:
diff changeset
439 xCoord=getattr(dataset,'x_coord')
562717546168 refactoring...
casties
parents:
diff changeset
440 yCoord=getattr(dataset,'y_coord')
562717546168 refactoring...
casties
parents:
diff changeset
441 except:
562717546168 refactoring...
casties
parents:
diff changeset
442 #try:
562717546168 refactoring...
casties
parents:
diff changeset
443 gisID=getattr(dataset,gisIDattribute)
562717546168 refactoring...
casties
parents:
diff changeset
444 coords=self.getPoint4GISid(gisID)
562717546168 refactoring...
casties
parents:
diff changeset
445 if coords!=None:
562717546168 refactoring...
casties
parents:
diff changeset
446 xCoord=coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
447 yCoord=coords[1]
562717546168 refactoring...
casties
parents:
diff changeset
448 # except:
562717546168 refactoring...
casties
parents:
diff changeset
449 # return "no coordinates found"
562717546168 refactoring...
casties
parents:
diff changeset
450
562717546168 refactoring...
casties
parents:
diff changeset
451 if float(xCoord)!=0:
562717546168 refactoring...
casties
parents:
diff changeset
452 if float(yCoord)!=0:
562717546168 refactoring...
casties
parents:
diff changeset
453 kml4Marker=kml4Marker+"<Placemark>"
562717546168 refactoring...
casties
parents:
diff changeset
454 kml4Marker=kml4Marker+"<description> <![CDATA[<b>"
562717546168 refactoring...
casties
parents:
diff changeset
455 for values in dataset:
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
456 #logging.debug("values=%s"%repr(values))
43
562717546168 refactoring...
casties
parents:
diff changeset
457 if values != (None, None):
562717546168 refactoring...
casties
parents:
diff changeset
458 if str(values).find('name')>-1:
562717546168 refactoring...
casties
parents:
diff changeset
459 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
562717546168 refactoring...
casties
parents:
diff changeset
460 continue
562717546168 refactoring...
casties
parents:
diff changeset
461 elif str(values).find('place')>-1:
562717546168 refactoring...
casties
parents:
diff changeset
462 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
562717546168 refactoring...
casties
parents:
diff changeset
463 continue
562717546168 refactoring...
casties
parents:
diff changeset
464
562717546168 refactoring...
casties
parents:
diff changeset
465 kml4Marker=kml4Marker+str(values)+": "
562717546168 refactoring...
casties
parents:
diff changeset
466 attribute_string=str(values).replace("'","__Apostroph__")
562717546168 refactoring...
casties
parents:
diff changeset
467 attribute_string=str(attribute_string).replace('"','__DoubleApostroph__')
562717546168 refactoring...
casties
parents:
diff changeset
468 attribute_string=str(attribute_string).replace(';','__$$__')
562717546168 refactoring...
casties
parents:
diff changeset
469 attribute_string=str(attribute_string).replace('&','&amp;')
562717546168 refactoring...
casties
parents:
diff changeset
470 if str(attribute_string).find('http')>-1:
562717546168 refactoring...
casties
parents:
diff changeset
471 attribute_string='<A HREF=' + str(attribute_string) + ' target=_blank>' + str(attribute_string) + '</A>'
562717546168 refactoring...
casties
parents:
diff changeset
472 kml4Marker=kml4Marker+attribute_string+"</a><br>\n"
562717546168 refactoring...
casties
parents:
diff changeset
473
562717546168 refactoring...
casties
parents:
diff changeset
474 kml4Marker=kml4Marker+"]]></description>\n"
117
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
475 kml4Marker=kml4Marker+"<styleURL>#marker_icon_red</styleURL>\n"
43
562717546168 refactoring...
casties
parents:
diff changeset
476 kml4Marker=kml4Marker+"<Point>"
562717546168 refactoring...
casties
parents:
diff changeset
477
562717546168 refactoring...
casties
parents:
diff changeset
478 kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n"
562717546168 refactoring...
casties
parents:
diff changeset
479 kml4Marker=kml4Marker+"</Point>\n"
562717546168 refactoring...
casties
parents:
diff changeset
480 kml4Marker=kml4Marker+"</Placemark>\n"
562717546168 refactoring...
casties
parents:
diff changeset
481
562717546168 refactoring...
casties
parents:
diff changeset
482 kml4Marker=kml4Marker+"</Document>\n</kml>"
562717546168 refactoring...
casties
parents:
diff changeset
483 kmlFileName="marker"+str(time.time())+".kml"
562717546168 refactoring...
casties
parents:
diff changeset
484
562717546168 refactoring...
casties
parents:
diff changeset
485 #kml4Marker=str(kml4Marker).replace('&','$$')
562717546168 refactoring...
casties
parents:
diff changeset
486 #kml4Marker=str(kml4Marker).replace(';','__$$__')
562717546168 refactoring...
casties
parents:
diff changeset
487 #kml4Marker=str(kml4Marker).replace('#','__SHARP__')
562717546168 refactoring...
casties
parents:
diff changeset
488 isLoadReady='false'
562717546168 refactoring...
casties
parents:
diff changeset
489 while isLoadReady=='false':
562717546168 refactoring...
casties
parents:
diff changeset
490 isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker)
562717546168 refactoring...
casties
parents:
diff changeset
491
562717546168 refactoring...
casties
parents:
diff changeset
492 return kmlFileName
562717546168 refactoring...
casties
parents:
diff changeset
493
44
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
494 # def getGoogleMapString(self,kml):
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
495 # logging.debug("getGoogleMapString")
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
496 # printed= '<body %s> '%kml +"""\n <div id="map_canvas" style="width: 98%; height: 95%"> </div> \n </body>" \n </html>"""
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
497 # return printed
43
562717546168 refactoring...
casties
parents:
diff changeset
498
562717546168 refactoring...
casties
parents:
diff changeset
499 def getPoint4GISid(self,gis_id):
562717546168 refactoring...
casties
parents:
diff changeset
500 j=0
562717546168 refactoring...
casties
parents:
diff changeset
501 coords=(0,0)
562717546168 refactoring...
casties
parents:
diff changeset
502 if gis_id != None:
562717546168 refactoring...
casties
parents:
diff changeset
503 while (True):
562717546168 refactoring...
casties
parents:
diff changeset
504 j=j+1
562717546168 refactoring...
casties
parents:
diff changeset
505 if (j>100): # FJK: just to prevent endless loops
562717546168 refactoring...
casties
parents:
diff changeset
506 break
562717546168 refactoring...
casties
parents:
diff changeset
507 if (gis_id.isdigit()): # FJK: regular exit from while-loop
562717546168 refactoring...
casties
parents:
diff changeset
508 break
562717546168 refactoring...
casties
parents:
diff changeset
509 else:
562717546168 refactoring...
casties
parents:
diff changeset
510 gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters
562717546168 refactoring...
casties
parents:
diff changeset
511 gis_id=gis_id.strip() # FJK: to strip all whitespaces
562717546168 refactoring...
casties
parents:
diff changeset
512 resultpoint = [0,0]
562717546168 refactoring...
casties
parents:
diff changeset
513 results = None
562717546168 refactoring...
casties
parents:
diff changeset
514 try:
562717546168 refactoring...
casties
parents:
diff changeset
515 if int(gis_id)>0:
562717546168 refactoring...
casties
parents:
diff changeset
516 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
517 results=self.ZSQLSimpleSearch(SQL)
562717546168 refactoring...
casties
parents:
diff changeset
518 #print results
562717546168 refactoring...
casties
parents:
diff changeset
519 if results != None:
562717546168 refactoring...
casties
parents:
diff changeset
520 for result in results:
562717546168 refactoring...
casties
parents:
diff changeset
521 resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))]
562717546168 refactoring...
casties
parents:
diff changeset
522 if resultpoint !=[0,0]:
562717546168 refactoring...
casties
parents:
diff changeset
523 return resultpoint
562717546168 refactoring...
casties
parents:
diff changeset
524 else:
562717546168 refactoring...
casties
parents:
diff changeset
525 coords=self.getCoordsFromREST_gisID(joinid)
562717546168 refactoring...
casties
parents:
diff changeset
526 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
527 returnstring=self.ZSQLSimpleSearch(SQL)
562717546168 refactoring...
casties
parents:
diff changeset
528 return coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
529 except:
562717546168 refactoring...
casties
parents:
diff changeset
530 return "gis_id not to interpretable:"+str(gis_id)
562717546168 refactoring...
casties
parents:
diff changeset
531 else:
562717546168 refactoring...
casties
parents:
diff changeset
532 return coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
533
562717546168 refactoring...
casties
parents:
diff changeset
534 def getCoordsFromREST_gisID(self,gis_id):
562717546168 refactoring...
casties
parents:
diff changeset
535 coordlist=[]
562717546168 refactoring...
casties
parents:
diff changeset
536 i=0
562717546168 refactoring...
casties
parents:
diff changeset
537 while (i<5 and coordlist==[]):
562717546168 refactoring...
casties
parents:
diff changeset
538
562717546168 refactoring...
casties
parents:
diff changeset
539 urlresponse=container.urlFunctions.zUrlopenParseString(container.urlFunctions.zUrlopenRead("http://chgis.hmdc.harvard.edu/xml/id/"+gis_id))
562717546168 refactoring...
casties
parents:
diff changeset
540 baseDocElement=container.urlFunctions.zUrlopenDocumentElement(urlresponse)
562717546168 refactoring...
casties
parents:
diff changeset
541 childnodes=container.urlFunctions.zUrlopenChildNodes(baseDocElement)
562717546168 refactoring...
casties
parents:
diff changeset
542 itemnodes=container.urlFunctions.zUrlopenGetElementsByTagName(baseDocElement,'item')
562717546168 refactoring...
casties
parents:
diff changeset
543
562717546168 refactoring...
casties
parents:
diff changeset
544 for i in range(0,container.urlFunctions.zUrlopenLength(itemnodes)):
562717546168 refactoring...
casties
parents:
diff changeset
545 itemnode=container.urlFunctions.zUrlopenGetItem(itemnodes,i)
562717546168 refactoring...
casties
parents:
diff changeset
546 itemspatialnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemnode,'spatial')
562717546168 refactoring...
casties
parents:
diff changeset
547 for j in range(0,container.urlFunctions.zUrlopenLength(itemspatialnodes)):
562717546168 refactoring...
casties
parents:
diff changeset
548 coord=[]
562717546168 refactoring...
casties
parents:
diff changeset
549 itemspatialnode= container.urlFunctions.zUrlopenGetItem(itemspatialnodes,j)
562717546168 refactoring...
casties
parents:
diff changeset
550 itemspatiallatnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_latitude')
562717546168 refactoring...
casties
parents:
diff changeset
551 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallatnodes)):
562717546168 refactoring...
casties
parents:
diff changeset
552 itemspatiallatnode= container.urlFunctions.zUrlopenGetItem(itemspatiallatnodes,k)
562717546168 refactoring...
casties
parents:
diff changeset
553 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallatnode))
562717546168 refactoring...
casties
parents:
diff changeset
554 itemspatiallngnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_longitude')
562717546168 refactoring...
casties
parents:
diff changeset
555 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallngnodes)):
562717546168 refactoring...
casties
parents:
diff changeset
556 itemspatiallngnode= container.urlFunctions.zUrlopenGetItem(itemspatiallngnodes,k)
562717546168 refactoring...
casties
parents:
diff changeset
557 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallngnode))
562717546168 refactoring...
casties
parents:
diff changeset
558 coordlist.append(coord)
562717546168 refactoring...
casties
parents:
diff changeset
559 gis_id= "_"+gis_id
562717546168 refactoring...
casties
parents:
diff changeset
560 return coordlist
562717546168 refactoring...
casties
parents:
diff changeset
561
562717546168 refactoring...
casties
parents:
diff changeset
562 # End for GoogleMaps creation
562717546168 refactoring...
casties
parents:
diff changeset
563
562717546168 refactoring...
casties
parents:
diff changeset
564 def RESTwrite2File(self,datadir, name,text):
562717546168 refactoring...
casties
parents:
diff changeset
565 logging.debug("RESTwrite2File datadir=%s name=%s"%(datadir,name))
562717546168 refactoring...
casties
parents:
diff changeset
566 try:
562717546168 refactoring...
casties
parents:
diff changeset
567 import cStringIO as StringIO
562717546168 refactoring...
casties
parents:
diff changeset
568 except:
562717546168 refactoring...
casties
parents:
diff changeset
569 import StringIO
562717546168 refactoring...
casties
parents:
diff changeset
570
562717546168 refactoring...
casties
parents:
diff changeset
571 # make filehandle from string
562717546168 refactoring...
casties
parents:
diff changeset
572 textfile = StringIO.StringIO(text)
562717546168 refactoring...
casties
parents:
diff changeset
573 fileid=name
562717546168 refactoring...
casties
parents:
diff changeset
574 if fileid in datadir.objectIds():
562717546168 refactoring...
casties
parents:
diff changeset
575 datadir.manage_delObjects(fileid)
562717546168 refactoring...
casties
parents:
diff changeset
576 fileInZope=datadir.manage_addFile(id=fileid,file=textfile)
562717546168 refactoring...
casties
parents:
diff changeset
577 return "Write successful"
562717546168 refactoring...
casties
parents:
diff changeset
578
562717546168 refactoring...
casties
parents:
diff changeset
579 def manage_editRestDbGisApi(self, title=None, connection_id=None,
562717546168 refactoring...
casties
parents:
diff changeset
580 REQUEST=None):
562717546168 refactoring...
casties
parents:
diff changeset
581 """Change the object"""
562717546168 refactoring...
casties
parents:
diff changeset
582 if title is not None:
562717546168 refactoring...
casties
parents:
diff changeset
583 self.title = title
562717546168 refactoring...
casties
parents:
diff changeset
584
562717546168 refactoring...
casties
parents:
diff changeset
585 if connection_id is not None:
562717546168 refactoring...
casties
parents:
diff changeset
586 self.connection_id = connection_id
562717546168 refactoring...
casties
parents:
diff changeset
587
562717546168 refactoring...
casties
parents:
diff changeset
588 #checkPermission=getSecurityManager().checkPermission
562717546168 refactoring...
casties
parents:
diff changeset
589 REQUEST.RESPONSE.redirect('manage_main')
562717546168 refactoring...
casties
parents:
diff changeset
590
562717546168 refactoring...
casties
parents:
diff changeset
591
562717546168 refactoring...
casties
parents:
diff changeset
592 manage_addRestDbGisApiForm=PageTemplateFile('zpt/addRestDbGisApi',globals())
562717546168 refactoring...
casties
parents:
diff changeset
593
562717546168 refactoring...
casties
parents:
diff changeset
594 def manage_addRestDbGisApi(self, id, title='', label='', description='',
562717546168 refactoring...
casties
parents:
diff changeset
595 createPublic=0,
562717546168 refactoring...
casties
parents:
diff changeset
596 createUserF=0,
562717546168 refactoring...
casties
parents:
diff changeset
597 REQUEST=None):
562717546168 refactoring...
casties
parents:
diff changeset
598 """Add a new object with id *id*."""
562717546168 refactoring...
casties
parents:
diff changeset
599
562717546168 refactoring...
casties
parents:
diff changeset
600 ob=RestDbGisApi(str(id),title)
562717546168 refactoring...
casties
parents:
diff changeset
601 self._setObject(id, ob)
562717546168 refactoring...
casties
parents:
diff changeset
602
562717546168 refactoring...
casties
parents:
diff changeset
603 #checkPermission=getSecurityManager().checkPermission
562717546168 refactoring...
casties
parents:
diff changeset
604 REQUEST.RESPONSE.redirect('manage_main')
562717546168 refactoring...
casties
parents:
diff changeset
605
562717546168 refactoring...
casties
parents:
diff changeset
606