annotate RestDbGisApi.py @ 261:5b38b50052e4

Display Polygons and Lines via chgis_id Reload button alternative changed to "R"
author fknauft
date Fri, 07 Oct 2011 15:58:07 +0200
parents e9a1ac7d2ab2
children 6613b9204bda
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
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
14 import urlFunctions
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
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
38
44
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
39 # and scripts
254
901c1f745d13 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 252
diff changeset
40 def KML_URL_schema_table(self,schema,table, useTimestamp=True, args=None):
44
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
41 """KML_URL table function"""
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
42 self.REQUEST.RESPONSE.setHeader("Content-Type", "text/plain")
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
43 id = self.REQUEST.get('id',[])
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
44 doc = self.REQUEST.get('doc',None)
245
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
45 # return self.getLiveKmlUrl(schema=schema,table=table, useTimestamp=useTimestamp)
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
46 return self.getLiveKmlUrl(schema=schema,table=table, useTimestamp=False)
43
562717546168 refactoring...
casties
parents:
diff changeset
47
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
48 #
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
49 # database methods
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
50 #
70
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
51 toSqlTypeMap = {
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
52 "text": "text",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
53 "number": "numeric",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
54 "id": "text",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
55 "gis_id": "text",
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
56 "coord_lat": "numeric",
252
efd2469d1722 geometry-column of tables will be displayed as string
fknauft
parents: 250
diff changeset
57 "coord_lon": "numeric",
efd2469d1722 geometry-column of tables will be displayed as string
fknauft
parents: 250
diff changeset
58 "the_geom": "the_geom"
70
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
59 }
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
60
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
61 def getTableOwner(self,schema,table):
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
62 """returns the owner of the table"""
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
63 # what do we do with the schema?
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
64 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
65 res = self.executeSQL(sql,(table,))
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
66 if len(res['rows']) > 0:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
67 return res['rows'][0][0]
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
68 return None
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
69
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
70 def isAllowed(self,action,schema,table,user=None,owner=None):
43
562717546168 refactoring...
casties
parents:
diff changeset
71 """returns if the requested action on the table is allowed"""
562717546168 refactoring...
casties
parents:
diff changeset
72 if user is None:
562717546168 refactoring...
casties
parents:
diff changeset
73 user = self.REQUEST.get('AUTHENTICATED_USER',None)
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
74 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
75 if action == "create":
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
76 if user is not None and str(user) != 'Anonymous User':
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
77 # any authenticated user can create
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
78 return True
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
79 else:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
80 return False
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
81
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
82 if action == "update":
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
83 if owner is None:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
84 owner = self.getTableOwner(schema,table)
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
85 logging.debug("isAllowed user=%s owner=%s"%(user,owner))
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
86 if user is not None and str(user) == str(owner):
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
87 # update only your own table
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
88 return True
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
89 else:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
90 return False
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
91
62
3905385c8854 small fish
casties
parents: 61
diff changeset
92 # anything else is allowed
43
562717546168 refactoring...
casties
parents:
diff changeset
93 return True
562717546168 refactoring...
casties
parents:
diff changeset
94
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
95 def setTableMetaTypes(self,schema,table,fields,user=None):
43
562717546168 refactoring...
casties
parents:
diff changeset
96 """sets the GIS meta information for table"""
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
97 if user is None:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
98 user = self.REQUEST.get('AUTHENTICATED_USER',None)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
99
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
100 logging.debug("settablemetatypes schema=%s, table=%s, fields=%s user=%s"%(schema,table,fields,user))
43
562717546168 refactoring...
casties
parents:
diff changeset
101
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
102 today = datetime.date.today().isoformat()
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
103
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
104 res = self.executeSQL('select * from public.gis_table_meta where table_name = %s', (table,))
43
562717546168 refactoring...
casties
parents:
diff changeset
105 if len(res['rows']) > 0:
562717546168 refactoring...
casties
parents:
diff changeset
106 # meta record exists
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
107 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
108 self.executeSQL(sql, (str(user),today,table), hasResult=False)
43
562717546168 refactoring...
casties
parents:
diff changeset
109 else:
562717546168 refactoring...
casties
parents:
diff changeset
110 # new meta record
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
111 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
112 self.executeSQL(sql, (table,str(user),today), hasResult=False)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
113
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
114 # update row info
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
115 sql = 'delete from public.gis_table_meta_rows where table_name=%s'
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
116 self.executeSQL(sql,(table,),hasResult=False)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
117 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
118 for f in fields:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
119 t = f['type']
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
120 fn = f['name']
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
121 self.executeSQL(sql, (table,fn,t), hasResult=False)
43
562717546168 refactoring...
casties
parents:
diff changeset
122
562717546168 refactoring...
casties
parents:
diff changeset
123
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
124 def getListOfUserTables(self,schema='public',username='guest'):
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
125 """return list of tables"""
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
126 logging.debug("getlistofusertables")
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
127 # get list of db tables
100
4c125fbb2c5b Views are listed in "getListOfUserTables"
fknauft
parents: 93
diff changeset
128 # 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
129 # 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
130 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
131 WHERE t.table_schema = %s AND t.table_name = m.table_name ORDER BY 1"""
43
562717546168 refactoring...
casties
parents:
diff changeset
132
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
133 data=self.executeSQL(qstr,(schema,))
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
134 return data
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
135
43
562717546168 refactoring...
casties
parents:
diff changeset
136
562717546168 refactoring...
casties
parents:
diff changeset
137 def createEmptyTable(self,schema,table,fields):
562717546168 refactoring...
casties
parents:
diff changeset
138 """create a table with the given fields
562717546168 refactoring...
casties
parents:
diff changeset
139 returns list of created fields"""
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
140 sqlFields = RestDbInterface.createEmptyTable(self,schema,table,fields)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
141 if sqlFields is not None:
43
562717546168 refactoring...
casties
parents:
diff changeset
142 self.setTableMetaTypes(schema,table,sqlFields)
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
143
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
144 return sqlFields
43
562717546168 refactoring...
casties
parents:
diff changeset
145
562717546168 refactoring...
casties
parents:
diff changeset
146
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
147 def getLiveKmlUrl(self,schema,table,useTimestamp=True,REQUEST=None):
254
901c1f745d13 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 252
diff changeset
148 return self.getLiveUrl(schema,table,useTimestamp,REQUEST)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
149
259
17b2c2dba0fd GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 258
diff changeset
150 def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None, geomField="point", colorField="red_big"):
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
151 """returns data structure for KML template"""
249
bba7dd7b6b4c Layer gui reordered, point order can be defined
fknauft
parents: 247
diff changeset
152 logging.debug("getKMLdata gid=%s lat=%s lon=%s sortBy=%s geom=%s color=%s"%(gisIdField,latField,lonField,sortBy,geomField,colorField))
148
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
153 if geomField is None:
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
154 geomField="point"
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
155 if colorField is None:
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
156 colorField="red"
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
157 # Mapping a set of points from table-based SQL-query:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
158 qstr='SELECT * FROM "%s"."%s"'%(schema,table)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
159 idList = None
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
160 if ids is not None:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
161 qstr += ' WHERE '
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
162 if schema=='mpdl':
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
163 qstr += 'mpdl_xmlsource_id IN ('
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
164 else:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
165 qstr += 'CAST(id AS text) IN ('
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
166
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
167 idList = ids.split(",")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
168 qstr += ','.join(['%s' for i in idList])
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
169 qstr += ')'
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
170
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
171 if sortBy:
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
172 # add sort clause
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
173 if sortBy == 1:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
174 qstr += ' ORDER BY 1'
249
bba7dd7b6b4c Layer gui reordered, point order can be defined
fknauft
parents: 247
diff changeset
175 elif sortBy == 'Default':
bba7dd7b6b4c Layer gui reordered, point order can be defined
fknauft
parents: 247
diff changeset
176 qstr += ' ORDER BY 1'
250
d7ceeb8ccbd7 Layer gui reordered, point order can be defined, minor edit
fknauft
parents: 249
diff changeset
177 elif sortBy == 'undefined':
d7ceeb8ccbd7 Layer gui reordered, point order can be defined, minor edit
fknauft
parents: 249
diff changeset
178 qstr += ' ORDER BY 1'
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
179 else:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
180 # TODO: proper quoting for names
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
181 qstr += ' ORDER BY "%s"'%sortBy.replace('"','')
245
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
182 bad_luck=True
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
183 bl_counter=0
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
184 while (bad_luck):
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
185 try:
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
186 data = self.executeSQL(qstr,idList)
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
187 bad_luck=False
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
188 bl_counter=bl_counter+1
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
189 except:
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
190 if (bl_counter<5):
245
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
191 bad_luck=True
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
192 else:
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
193 bad_luck=False
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
194
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
195 fieldMap = self.getFieldNameMap(data['fields'])
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
196 geomdata=None
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
197 if (geomField!="point"):
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
198 # first check if the file is registered as geo-dataset (which then should have an attribute "the_geom")
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
199 requeststring="select f_geometry_column from geometry_columns where f_table_schema=%s and f_table_name=%s "
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
200 geocolumn_res=self.executeSQL(requeststring,(schema,table))
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
201 if len(geocolumn_res['rows'])>0:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
202 geocolumn=geocolumn_res['rows'][0][0]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
203 try:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
204 geomstr="select astext(st_simplify(transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
205 geomdata=self.executeSQL(geomstr)
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
206 teststr=geomdata.values()[1][0]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
207 if (teststr == (u'MULTIPOLYGON EMPTY',)):
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
208 geomstr="select astext(st_simplify(transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
209 geomdata=self.executeSQL(geomstr)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
210
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
211 except:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
212 try:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
213 geomstr="select chgis.astext(chgis.st_simplify(chgis.transform(%s,4326),0.05)) from %s.%s"%(geocolumn,schema,table) # the string variables have to be added here and not in executeSQL!
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
214 geomdata=self.executeSQL(geomstr)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
215 except:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
216 geomdata=None
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
217
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
218 if (gisIdField is None) and (latField is None or lonField is None) and geomField=='point':
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
219 # no fields given - choose automagically
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
220 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
221 # gis id in metadata first
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
222 res = self.executeSQL(sql, (table,'gis_id'))
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
223 if len(res['rows']) > 0:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
224 gisIdField = res['rows'][0][0]
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
225 # latitude in metadata
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
226 res = self.executeSQL(sql, (table,'coord_lat'))
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
227 if len(res['rows']) > 0:
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
228 latField = res['rows'][0][0]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
229 # longitude in metadata
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
230 res = self.executeSQL(sql, (table,'coord_lon'))
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
231 if len(res['rows']) > 0:
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
232 lonField = res['rows'][0][0]
93
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
233
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
234 if (gisIdField is None) and (latField is None or lonField is None) and geomField=='point':
93
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
235 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
236 # still no fields - try field names
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
237 if 'latitude' in fieldMap and 'longitude' in fieldMap:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
238 latField = 'latitude'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
239 lonField = 'longitude'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
240 elif 'x_coord' in fieldMap and 'y_coord' in fieldMap:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
241 latField = 'x_coord'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
242 lonField = 'y_coord'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
243 else:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
244 logging.error("getKMLdata unable to find position fields")
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
245 return None
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
246
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
247
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
248 # convert field names to row indexes
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
249 gisIdIdx = fieldMap.get(gisIdField,None)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
250 latIdx = fieldMap.get(latField,None)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
251 lonIdx = fieldMap.get(lonField,None)
117
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
252 the_geom=fieldMap.get("the_geom",None)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
253 logging.debug("gisidfield=%s idx=%s"%(gisIdField,gisIdIdx))
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
254
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
255 # convert data
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
256 kmlData = []
151
348a11a5e49c polygon-Layers
fknauft
parents: 150
diff changeset
257 geom_list = {}
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
258 # try:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
259 # if geomField=='poly' or geomField=='line':
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
260 # geom_list=geomdata.values()[1]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
261 # except:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
262 # return "no geomdata in RestDbGisApi Line 254"
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
263
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
264 data_list=data['rows']
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
265 for k in range (len(data_list)):
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
266 dataset = data_list[k]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
267 if len(geom_list)>k:
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
268 geom_value = geom_list[k]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
269
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
270
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
271 if gisIdIdx != None and geomField!='point':
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
272 gisID = dataset[gisIdIdx]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
273 geomdata = self.getLineForChGisId(gisID)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
274
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
275 # for dataset in data['rows']:
65
2f477270cc0c adding layers to maps works now
casties
parents: 62
diff changeset
276 xCoord = 0.0
2f477270cc0c adding layers to maps works now
casties
parents: 62
diff changeset
277 yCoord = 0.0
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
278 if gisIdIdx != None and geomField=='point' :
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
279 gisID = dataset[gisIdIdx]
257
61525f0ca492 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 256
diff changeset
280 if gisID != " " and gisID != " 0":
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
281 coords=self.getPointForChGisId(gisID)
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
282 if coords!=None:
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
283 xCoord=coords[0]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
284 yCoord=coords[1]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
285 elif latIdx != None:
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
286 xCoord = dataset[lonIdx]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
287 yCoord = dataset[latIdx]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
288 elif latIdx != None:
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
289 xCoord = dataset[lonIdx]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
290 yCoord = dataset[latIdx]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
291
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
292 elif geomField=='point' :
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
293 logging.error("getKMLdata unable to find position")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
294 return None
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
295
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
296 if geomField=='point' :
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
297 if float(xCoord) == 0.0:
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
298 continue
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
299 if float(yCoord) == 0.0:
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
300 continue
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
301
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
302 kmlPlace = {}
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
303
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
304 # description
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
305 desc = ''
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
306
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
307
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
308 for i in range (len(dataset)):
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
309 value = dataset[i]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
310
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
311 name = data['fields'][i][0]
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
312 #logging.debug("value=%s"%value)
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
313 if name != 'the_geom':
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
314 if value != None:
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
315 #if name.find('name')>-1:
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
316 # desc += "<name>%s</name>\n"%value
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
317 # continue
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
318 #elif name.find('place')>-1:
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
319 # desc += "<name>%s</name>\n"%value
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
320 # continue
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
321
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
322 val = "%s: %s"%(name, value)
187
f080901d8163 corrected links in desc (RestDbGisApi)
fknauft
parents: 186
diff changeset
323 value=unicode(value)
188
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
324
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
325 # 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
326 if value.find('http://')>-1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
327 link_str_beg=value.find('http://')
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
328 link_str_end = -1
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
329 link_str_end0=value.find(' ',link_str_beg)
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
330 link_str_end1=value.find('>',link_str_beg)
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
331 if link_str_end0 <link_str_end1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
332 link_str_end=link_str_end0
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
333 else:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
334 link_str_end=link_str_end1
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
335 if link_str_end > -1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
336 link_str=value[link_str_beg:link_str_end]
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
337 val =name+": "+value[0:link_str_beg]+'<a href=' + link_str + '> ' + link_str.replace('http://','') + ' </a>' + value[link_str_end:]
191
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
338 else:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
339 link_str=value[link_str_beg:]
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
340 val =name+': '+value[0:link_str_beg]+'<a href=' + link_str + '> ' + link_str.replace('http://','') + ' </a>'
188
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
341
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
342
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
343 #desc += kmlEncode(val)
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
344 desc += val
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
345 desc += '<br/>\n'
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
346
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
347 #kmlPlace['description'] = "<![CDATA[%s]]>"%desc
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
348
124
8668d76a7b6a polygon-Layers
fknauft
parents: 123
diff changeset
349
133
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
350 if geomField=='point':
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
351 kmlPlace['description'] = "<![CDATA[%s]]>"%desc
193
e6a754b05ec8 corrected links in desc (RestDbGisApi)
fknauft
parents: 192
diff changeset
352
133
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
353 kmlPlace['icon'] = '#marker_icon_'+colorField
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
354 kmlPlace['coord_x'] = str(xCoord)
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
355 kmlPlace['coord_y'] = str(yCoord)
258
ed99a2468264 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 257
diff changeset
356 kmlPlace['coord_z'] = '50'
134
649681baae76 polygon-Layers
fknauft
parents: 133
diff changeset
357 kmlData.append(kmlPlace)
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
358 if (geomField=='poly' or geomField=='line') and geomdata is not None:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
359 polys=str(geomdata).split('(')
144
abc95f483315 polygon-Layers
fknauft
parents: 142
diff changeset
360 aaa=len(polys)
142
3adea92d2067 polygon-Layers
fknauft
parents: 138
diff changeset
361 for poly in polys:
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
362 kmlPlace = {}
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
363 kmlPlace['description'] = desc
152
7fddf8a60481 polygon-Layers
fknauft
parents: 151
diff changeset
364 coords=poly.replace(')','').replace("'","").split(',')
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
365 coord_string=''
144
abc95f483315 polygon-Layers
fknauft
parents: 142
diff changeset
366 if len(coords)>1:
146
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
367 for coord in coords:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
368 coord=coord.split(' ')
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
369 try:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
370 x_coord=coord[0]
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
371 y_coord=coord[1]
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
372 except:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
373 break
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
374 coord_string+=x_coord+','+y_coord+','+'0 '
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
375 if coord_string != '':
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
376 kmlPlace['LinearRing']=coord_string
245
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
377 kmlPlace['lineColor']='#'+colorField+'_'+geomField
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
378 kmlData.append(kmlPlace)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
379 #logging.debug("kmlData=%s"%(repr(kmlData)))
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
380 return kmlData
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
381
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
382 def getPointForChGisId(self, gis_id):
260
e9a1ac7d2ab2 Bug resolved in request point_id from Harvard ChGIS
fknauft
parents: 259
diff changeset
383 """returns coordinate pair for given gis_id""" # gets called by getKml
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
384 def getPoint(id):
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
385 str_gis_id=str(id).split('.')[0]
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
386 sql="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE CAST('%s' AS text)"%str(str_gis_id)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
387 # logging.error("sql:",sql)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
388 # res = self.executeSQL(sql, (str(str_gis_id),))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
389 res = self.executeSQL(sql)
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
390 if len(res['rows']) > 0:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
391 return res['rows'][0]
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
392 else:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
393 #logging.error("error on sql:",sql%(str(str_gis_id),))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
394 return None
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
395
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
396 if gis_id is None or gis_id == "":
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
397 return None
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
398 if len(str(gis_id)) < 4:
260
e9a1ac7d2ab2 Bug resolved in request point_id from Harvard ChGIS
fknauft
parents: 259
diff changeset
399 return None
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
400
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
401 # try gis_id
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
402 coords = getPoint(gis_id)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
403 if coords is None:
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
404 # try to clean gis_id...
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
405 gis_id_short = re.sub(r'[^0-9]','',gis_id)
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
406 # try again
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
407 coords = getPoint(gis_id_short)
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
408 if coords is None:
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
409 #logging.error("CH-GIS ID %s not found!"%str(gis_id_short))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
410
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
411 # this will ask the Harvard-Service for the Coords of this gis_id and write it into our coords-list
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
412 try:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
413 coords=self.getCoordsFromREST_gisID(gis_id)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
414 #logging.error("coords from REST"%str(coords))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
415 except:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
416 logging.error("coords from REST did not work for "%str(gis_id))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
417 if coords[0] is None:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
418 logging.error("CH-GIS ID %s not found in Harvard"%str(gis_id))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
419 else:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
420 try:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
421 SQL="INSERT INTO chgis.chgis_coords (gis_id,x_coord,y_coord) VALUES (CAST(%s AS text),CAST(%s AS numeric),CAST(%s AS numeric))"
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
422 SQL_analyze="ANALYZE chgis.chgis_coords"
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
423 self.executeSQL(SQL,(str(gis_id_short),str(coords[0][1]),str(coords[0][0])),False)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
424 self.executeSQL(SQL_analyze,(),False)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
425 except:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
426 logging.error("Could not write into chgis_coords: ", str(gis_id_short))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
427 logging.error("coords[0][0]:"%coords[0][0])
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
428 logging.error("coords[0][1]:"%coords[0][1])
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
429 return coords[0]
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
430 coords_test = getPoint(gis_id)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
431 #logging.error("SQL results now:", str(coords_test))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
432 return coords[0]
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
433
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
434 return coords
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
435
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
436
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
437 def getLineForChGisId(self, gis_id):
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
438 """returns line/poly coordinates for given gis_id""" # gets called by getKml
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
439 def getLine(id):
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
440 str_gis_id=str(id).split('.')[0]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
441 sql="SELECT astext(st_simplify(transform(the_geom,4326),0.05)) FROM chgis.chgis_linesandpolys WHERE gis_id LIKE CAST('%s' AS text)"%(str_gis_id)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
442 # logging.error("sql:",sql)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
443 # res = self.executeSQL(sql, (str(str_gis_id),))
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
444 res = self.executeSQL(sql)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
445 if len(res['rows']) > 0:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
446 return res['rows'][0]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
447 else:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
448 #logging.error("error on sql:",sql%(str(str_gis_id),))
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
449 return None
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
450
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
451 if gis_id is None or gis_id == "":
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
452 return None
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
453 if len(str(gis_id)) < 4:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
454 return None
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
455
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
456 # try gis_id
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
457 line_coords = getLine(gis_id)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
458 if line_coords is None:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
459 # try to clean gis_id...
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
460 gis_id_short = re.sub(r'[^0-9]','',gis_id)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
461 # try again
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
462 line_coords = getLine(gis_id_short)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
463 if line_coords is None:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
464 logging.error("CH-GIS ID %s not found!"%(gis_id_short))
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
465
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
466
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
467 return line_coords
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
468
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
469 ## legacy methods...
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
470
254
901c1f745d13 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 252
diff changeset
471 def getKmlUrl(self,schema='chgis',table='mpdl',args={'doc':None,'id':None}):
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
472 logging.debug("getKmlUrl")
254
901c1f745d13 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 252
diff changeset
473 id=args.get('id')
901c1f745d13 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 252
diff changeset
474 doc=args.get('doc')
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
475 data = self.getDataForGoogleMap(schema,table,id,doc)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
476 kml=self.getKMLname(data=data,table=table)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
477 baseUrl = self.absolute_url()
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
478 return "%s/daten/%s"%(baseUrl,kml)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
479
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
480 def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=None,doc=None):
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
481 logging.debug("getDataForGoogleMap")
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
482 qstr="SELECT * FROM "+schema+"."+table
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
483 try:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
484 if id is not None:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
485 qstr=qstr+" WHERE "
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
486 for id_item in id.split(","):
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
487 if table=='mpdl':
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
488 qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR"
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
489 else:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
490 qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR"
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
491 qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and "
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
492 data=self.ZSQLSimpleSearch(qstr)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
493 return data
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
494 except:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
495 return qstr
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
496
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
497
43
562717546168 refactoring...
casties
parents:
diff changeset
498 def getKMLname(self,data=[],table=""):
562717546168 refactoring...
casties
parents:
diff changeset
499 logging.debug("getKMLname")
562717546168 refactoring...
casties
parents:
diff changeset
500 #session=context.REQUEST.SESSION
109
9db303437b32 changable color for dots in map
fknauft
parents: 108
diff changeset
501 kml4Marker="<kml xmlns=\'http://www.opengis.net/kml/2.2\'><Document>"
9db303437b32 changable color for dots in map
fknauft
parents: 108
diff changeset
502 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
503 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
504 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
505 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
506 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
507 initializeStringForGoogleMaps=""
562717546168 refactoring...
casties
parents:
diff changeset
508 #doLine=container.getVar('doLine')
562717546168 refactoring...
casties
parents:
diff changeset
509 # Mapping a set of points from table-based SQL-query:
562717546168 refactoring...
casties
parents:
diff changeset
510 if data!=None:
562717546168 refactoring...
casties
parents:
diff changeset
511 try:
562717546168 refactoring...
casties
parents:
diff changeset
512 SQL='SELECT "attribute with gis_id" FROM public.metadata WHERE tablename = %s'
562717546168 refactoring...
casties
parents:
diff changeset
513 res = self.executeSQL(SQL, (table,))
562717546168 refactoring...
casties
parents:
diff changeset
514 gisIDattribute = res['rows'][0][0]
562717546168 refactoring...
casties
parents:
diff changeset
515 except:
562717546168 refactoring...
casties
parents:
diff changeset
516 return "table not registered within metadata"
562717546168 refactoring...
casties
parents:
diff changeset
517
562717546168 refactoring...
casties
parents:
diff changeset
518 for dataset in data:
562717546168 refactoring...
casties
parents:
diff changeset
519 try:
562717546168 refactoring...
casties
parents:
diff changeset
520 xCoord=getattr(dataset,'longitude')
562717546168 refactoring...
casties
parents:
diff changeset
521 yCoord=getattr(dataset,'latitude')
562717546168 refactoring...
casties
parents:
diff changeset
522 except:
562717546168 refactoring...
casties
parents:
diff changeset
523 try:
562717546168 refactoring...
casties
parents:
diff changeset
524 xCoord=getattr(dataset,'x_coord')
562717546168 refactoring...
casties
parents:
diff changeset
525 yCoord=getattr(dataset,'y_coord')
562717546168 refactoring...
casties
parents:
diff changeset
526 except:
562717546168 refactoring...
casties
parents:
diff changeset
527 #try:
562717546168 refactoring...
casties
parents:
diff changeset
528 gisID=getattr(dataset,gisIDattribute)
562717546168 refactoring...
casties
parents:
diff changeset
529 coords=self.getPoint4GISid(gisID)
562717546168 refactoring...
casties
parents:
diff changeset
530 if coords!=None:
562717546168 refactoring...
casties
parents:
diff changeset
531 xCoord=coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
532 yCoord=coords[1]
562717546168 refactoring...
casties
parents:
diff changeset
533 # except:
562717546168 refactoring...
casties
parents:
diff changeset
534 # return "no coordinates found"
562717546168 refactoring...
casties
parents:
diff changeset
535
562717546168 refactoring...
casties
parents:
diff changeset
536 if float(xCoord)!=0:
562717546168 refactoring...
casties
parents:
diff changeset
537 if float(yCoord)!=0:
562717546168 refactoring...
casties
parents:
diff changeset
538 kml4Marker=kml4Marker+"<Placemark>"
562717546168 refactoring...
casties
parents:
diff changeset
539 kml4Marker=kml4Marker+"<description> <![CDATA[<b>"
562717546168 refactoring...
casties
parents:
diff changeset
540 for values in dataset:
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
541 #logging.debug("values=%s"%repr(values))
43
562717546168 refactoring...
casties
parents:
diff changeset
542 if values != (None, None):
562717546168 refactoring...
casties
parents:
diff changeset
543 if str(values).find('name')>-1:
562717546168 refactoring...
casties
parents:
diff changeset
544 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
562717546168 refactoring...
casties
parents:
diff changeset
545 continue
562717546168 refactoring...
casties
parents:
diff changeset
546 elif str(values).find('place')>-1:
562717546168 refactoring...
casties
parents:
diff changeset
547 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
562717546168 refactoring...
casties
parents:
diff changeset
548 continue
562717546168 refactoring...
casties
parents:
diff changeset
549
562717546168 refactoring...
casties
parents:
diff changeset
550 kml4Marker=kml4Marker+str(values)+": "
562717546168 refactoring...
casties
parents:
diff changeset
551 attribute_string=str(values).replace("'","__Apostroph__")
562717546168 refactoring...
casties
parents:
diff changeset
552 attribute_string=str(attribute_string).replace('"','__DoubleApostroph__')
562717546168 refactoring...
casties
parents:
diff changeset
553 attribute_string=str(attribute_string).replace(';','__$$__')
562717546168 refactoring...
casties
parents:
diff changeset
554 attribute_string=str(attribute_string).replace('&','&amp;')
562717546168 refactoring...
casties
parents:
diff changeset
555 if str(attribute_string).find('http')>-1:
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
556 attribute_string='<A HREF=' + str(attribute_string) + ' target="_blank">' + str(attribute_string) + '</A>'
43
562717546168 refactoring...
casties
parents:
diff changeset
557 kml4Marker=kml4Marker+attribute_string+"</a><br>\n"
562717546168 refactoring...
casties
parents:
diff changeset
558
562717546168 refactoring...
casties
parents:
diff changeset
559 kml4Marker=kml4Marker+"]]></description>\n"
117
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
560 kml4Marker=kml4Marker+"<styleURL>#marker_icon_red</styleURL>\n"
43
562717546168 refactoring...
casties
parents:
diff changeset
561 kml4Marker=kml4Marker+"<Point>"
562717546168 refactoring...
casties
parents:
diff changeset
562
562717546168 refactoring...
casties
parents:
diff changeset
563 kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n"
562717546168 refactoring...
casties
parents:
diff changeset
564 kml4Marker=kml4Marker+"</Point>\n"
562717546168 refactoring...
casties
parents:
diff changeset
565 kml4Marker=kml4Marker+"</Placemark>\n"
562717546168 refactoring...
casties
parents:
diff changeset
566
562717546168 refactoring...
casties
parents:
diff changeset
567 kml4Marker=kml4Marker+"</Document>\n</kml>"
562717546168 refactoring...
casties
parents:
diff changeset
568 kmlFileName="marker"+str(time.time())+".kml"
562717546168 refactoring...
casties
parents:
diff changeset
569
562717546168 refactoring...
casties
parents:
diff changeset
570 #kml4Marker=str(kml4Marker).replace('&','$$')
562717546168 refactoring...
casties
parents:
diff changeset
571 #kml4Marker=str(kml4Marker).replace(';','__$$__')
562717546168 refactoring...
casties
parents:
diff changeset
572 #kml4Marker=str(kml4Marker).replace('#','__SHARP__')
562717546168 refactoring...
casties
parents:
diff changeset
573 isLoadReady='false'
562717546168 refactoring...
casties
parents:
diff changeset
574 while isLoadReady=='false':
562717546168 refactoring...
casties
parents:
diff changeset
575 isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker)
562717546168 refactoring...
casties
parents:
diff changeset
576
562717546168 refactoring...
casties
parents:
diff changeset
577 return kmlFileName
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
578
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
579 def trydatahas_key(self,data,index,key_string):
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
580 try:
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
581 return data[index].has_key(key_string)
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
582 except:
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
583 return 'false'
43
562717546168 refactoring...
casties
parents:
diff changeset
584
44
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
585 # def getGoogleMapString(self,kml):
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
586 # logging.debug("getGoogleMapString")
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
587 # 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
588 # return printed
43
562717546168 refactoring...
casties
parents:
diff changeset
589
562717546168 refactoring...
casties
parents:
diff changeset
590 def getPoint4GISid(self,gis_id):
562717546168 refactoring...
casties
parents:
diff changeset
591 j=0
562717546168 refactoring...
casties
parents:
diff changeset
592 coords=(0,0)
562717546168 refactoring...
casties
parents:
diff changeset
593 if gis_id != None:
562717546168 refactoring...
casties
parents:
diff changeset
594 while (True):
562717546168 refactoring...
casties
parents:
diff changeset
595 j=j+1
562717546168 refactoring...
casties
parents:
diff changeset
596 if (j>100): # FJK: just to prevent endless loops
562717546168 refactoring...
casties
parents:
diff changeset
597 break
562717546168 refactoring...
casties
parents:
diff changeset
598 if (gis_id.isdigit()): # FJK: regular exit from while-loop
562717546168 refactoring...
casties
parents:
diff changeset
599 break
562717546168 refactoring...
casties
parents:
diff changeset
600 else:
562717546168 refactoring...
casties
parents:
diff changeset
601 gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters
562717546168 refactoring...
casties
parents:
diff changeset
602 gis_id=gis_id.strip() # FJK: to strip all whitespaces
562717546168 refactoring...
casties
parents:
diff changeset
603 resultpoint = [0,0]
562717546168 refactoring...
casties
parents:
diff changeset
604 results = None
562717546168 refactoring...
casties
parents:
diff changeset
605 try:
562717546168 refactoring...
casties
parents:
diff changeset
606 if int(gis_id)>0:
562717546168 refactoring...
casties
parents:
diff changeset
607 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
608 results=self.ZSQLSimpleSearch(SQL)
562717546168 refactoring...
casties
parents:
diff changeset
609 #print results
562717546168 refactoring...
casties
parents:
diff changeset
610 if results != None:
562717546168 refactoring...
casties
parents:
diff changeset
611 for result in results:
562717546168 refactoring...
casties
parents:
diff changeset
612 resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))]
562717546168 refactoring...
casties
parents:
diff changeset
613 if resultpoint !=[0,0]:
562717546168 refactoring...
casties
parents:
diff changeset
614 return resultpoint
562717546168 refactoring...
casties
parents:
diff changeset
615 else:
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
616 coords=self.getCoordsFromREST_gisID(gis_id)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
617 SQL="INSERT INTO chgis.chgis_coords (chgis_coords_pkey,gis_id,x_coord,y_coord) VALUES (default," +gis_id+ "," +coords[0][1]+ "," +coords[0][0]+ "); ANALYZE chgis.chgis_coords;"
43
562717546168 refactoring...
casties
parents:
diff changeset
618 returnstring=self.ZSQLSimpleSearch(SQL)
562717546168 refactoring...
casties
parents:
diff changeset
619 return coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
620 except:
562717546168 refactoring...
casties
parents:
diff changeset
621 return "gis_id not to interpretable:"+str(gis_id)
562717546168 refactoring...
casties
parents:
diff changeset
622 else:
562717546168 refactoring...
casties
parents:
diff changeset
623 return coords[0]
562717546168 refactoring...
casties
parents:
diff changeset
624
562717546168 refactoring...
casties
parents:
diff changeset
625 def getCoordsFromREST_gisID(self,gis_id):
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
626 gis_id=gis_id.strip()
43
562717546168 refactoring...
casties
parents:
diff changeset
627 coordlist=[]
562717546168 refactoring...
casties
parents:
diff changeset
628 i=0
562717546168 refactoring...
casties
parents:
diff changeset
629 while (i<5 and coordlist==[]):
256
cb953cc153a6 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 255
diff changeset
630 urlinfo=urlFunctions.zUrlopenInfo(self,"http://chgis.hmdc.harvard.edu/xml/id/"+gis_id)
cb953cc153a6 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 255
diff changeset
631 urlinfoLength=urlinfo.get('content-length')
cb953cc153a6 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 255
diff changeset
632 if int(urlinfoLength)<500:
cb953cc153a6 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 255
diff changeset
633 urlresponse=urlFunctions.zUrlopenRead(self,"http://chgis.hmdc.harvard.edu/xml/id/cts_"+gis_id)
cb953cc153a6 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 255
diff changeset
634 else:
cb953cc153a6 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 255
diff changeset
635 urlresponse=urlFunctions.zUrlopenRead(self,"http://chgis.hmdc.harvard.edu/xml/id/"+gis_id)
cb953cc153a6 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 255
diff changeset
636 urlresponseString=urlFunctions.zUrlopenParseString(self,urlresponse)
cb953cc153a6 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 255
diff changeset
637 baseDocElement= urlFunctions.zUrlopenDocumentElement(self,urlresponseString)
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
638 childnodes=urlFunctions.zUrlopenChildNodes(self,baseDocElement)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
639 itemnodes=urlFunctions.zUrlopenGetElementsByTagName(self,baseDocElement,'item')
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
640 itemspatialnodes=None
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
641
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
642 for i in range(0,urlFunctions.zUrlopenLength(self,itemnodes)):
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
643 itemnode=urlFunctions.zUrlopenGetItem(self,itemnodes,i)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
644 itemspatialnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemnode,'spatial')
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
645 if itemspatialnodes is not None:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
646 for j in range(0,urlFunctions.zUrlopenLength(self,itemspatialnodes)):
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
647 coord=[]
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
648 itemspatialnode= urlFunctions.zUrlopenGetItem(self,itemspatialnodes,j)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
649 itemspatiallatnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemspatialnode,'degrees_latitude')
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
650 for k in range(0,urlFunctions.zUrlopenLength(self,itemspatiallatnodes)):
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
651 itemspatiallatnode= urlFunctions.zUrlopenGetItem(self,itemspatiallatnodes,k)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
652 coord.append(urlFunctions.zUrlopenGetTextData(self,itemspatiallatnode))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
653 itemspatiallngnodes=urlFunctions.zUrlopenGetElementsByTagName(self,itemspatialnode,'degrees_longitude')
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
654 for k in range(0,urlFunctions.zUrlopenLength(self,itemspatiallngnodes)):
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
655 itemspatiallngnode= urlFunctions.zUrlopenGetItem(self,itemspatiallngnodes,k)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
656 coord.append(urlFunctions.zUrlopenGetTextData(self,itemspatiallngnode))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
657 coordlist.append(coord)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
658 gis_id= "_"+gis_id
43
562717546168 refactoring...
casties
parents:
diff changeset
659 return coordlist
562717546168 refactoring...
casties
parents:
diff changeset
660
562717546168 refactoring...
casties
parents:
diff changeset
661 # End for GoogleMaps creation
562717546168 refactoring...
casties
parents:
diff changeset
662
562717546168 refactoring...
casties
parents:
diff changeset
663 def RESTwrite2File(self,datadir, name,text):
562717546168 refactoring...
casties
parents:
diff changeset
664 logging.debug("RESTwrite2File datadir=%s name=%s"%(datadir,name))
562717546168 refactoring...
casties
parents:
diff changeset
665 try:
562717546168 refactoring...
casties
parents:
diff changeset
666 import cStringIO as StringIO
562717546168 refactoring...
casties
parents:
diff changeset
667 except:
562717546168 refactoring...
casties
parents:
diff changeset
668 import StringIO
562717546168 refactoring...
casties
parents:
diff changeset
669
562717546168 refactoring...
casties
parents:
diff changeset
670 # make filehandle from string
562717546168 refactoring...
casties
parents:
diff changeset
671 textfile = StringIO.StringIO(text)
562717546168 refactoring...
casties
parents:
diff changeset
672 fileid=name
562717546168 refactoring...
casties
parents:
diff changeset
673 if fileid in datadir.objectIds():
562717546168 refactoring...
casties
parents:
diff changeset
674 datadir.manage_delObjects(fileid)
562717546168 refactoring...
casties
parents:
diff changeset
675 fileInZope=datadir.manage_addFile(id=fileid,file=textfile)
562717546168 refactoring...
casties
parents:
diff changeset
676 return "Write successful"
562717546168 refactoring...
casties
parents:
diff changeset
677
562717546168 refactoring...
casties
parents:
diff changeset
678 def manage_editRestDbGisApi(self, title=None, connection_id=None,
562717546168 refactoring...
casties
parents:
diff changeset
679 REQUEST=None):
562717546168 refactoring...
casties
parents:
diff changeset
680 """Change the object"""
562717546168 refactoring...
casties
parents:
diff changeset
681 if title is not None:
562717546168 refactoring...
casties
parents:
diff changeset
682 self.title = title
562717546168 refactoring...
casties
parents:
diff changeset
683
562717546168 refactoring...
casties
parents:
diff changeset
684 if connection_id is not None:
562717546168 refactoring...
casties
parents:
diff changeset
685 self.connection_id = connection_id
562717546168 refactoring...
casties
parents:
diff changeset
686
562717546168 refactoring...
casties
parents:
diff changeset
687 #checkPermission=getSecurityManager().checkPermission
562717546168 refactoring...
casties
parents:
diff changeset
688 REQUEST.RESPONSE.redirect('manage_main')
562717546168 refactoring...
casties
parents:
diff changeset
689
562717546168 refactoring...
casties
parents:
diff changeset
690
562717546168 refactoring...
casties
parents:
diff changeset
691 manage_addRestDbGisApiForm=PageTemplateFile('zpt/addRestDbGisApi',globals())
562717546168 refactoring...
casties
parents:
diff changeset
692
562717546168 refactoring...
casties
parents:
diff changeset
693 def manage_addRestDbGisApi(self, id, title='', label='', description='',
562717546168 refactoring...
casties
parents:
diff changeset
694 createPublic=0,
562717546168 refactoring...
casties
parents:
diff changeset
695 createUserF=0,
562717546168 refactoring...
casties
parents:
diff changeset
696 REQUEST=None):
562717546168 refactoring...
casties
parents:
diff changeset
697 """Add a new object with id *id*."""
562717546168 refactoring...
casties
parents:
diff changeset
698
562717546168 refactoring...
casties
parents:
diff changeset
699 ob=RestDbGisApi(str(id),title)
562717546168 refactoring...
casties
parents:
diff changeset
700 self._setObject(id, ob)
562717546168 refactoring...
casties
parents:
diff changeset
701
562717546168 refactoring...
casties
parents:
diff changeset
702 #checkPermission=getSecurityManager().checkPermission
562717546168 refactoring...
casties
parents:
diff changeset
703 REQUEST.RESPONSE.redirect('manage_main')
562717546168 refactoring...
casties
parents:
diff changeset
704
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
705
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
706
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
707