annotate RestDbGisApi.py @ 278:4ade9b80b563 default tip

more cleanup. descriptions work better now.
author casties
date Fri, 24 Feb 2012 16:41:30 +0100
parents 55bc9972fb1b
children
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
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
16 from Products.ZDBInterface.WritableRestDbInterface import *
43
562717546168 refactoring...
casties
parents:
diff changeset
17
562717546168 refactoring...
casties
parents:
diff changeset
18
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
19 def kmlEncode(s):
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
20 """returns string encoded for displaying in KML attribute"""
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
21 res = s.replace("'","__Apostroph__")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
22 res = res.replace('"','__DoubleApostroph__')
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
23 res = res.replace(';','__$$__')
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 return res
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
26
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
27
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
28 class RestDbGisApi(WritableRestDbInterface):
43
562717546168 refactoring...
casties
parents:
diff changeset
29 """Object for RESTful GIS database queries
562717546168 refactoring...
casties
parents:
diff changeset
30 path schema: /db/{schema}/{table}/
562717546168 refactoring...
casties
parents:
diff changeset
31 omitting table gives a list of schemas
562717546168 refactoring...
casties
parents:
diff changeset
32 omitting table and schema gives a list of schemas
562717546168 refactoring...
casties
parents:
diff changeset
33 """
562717546168 refactoring...
casties
parents:
diff changeset
34
562717546168 refactoring...
casties
parents:
diff changeset
35 meta_type="RESTgis"
562717546168 refactoring...
casties
parents:
diff changeset
36
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
37 # data templates
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
38 GIS_schema_table = PageTemplateFile('zpt/GIS_schema_table', globals())
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
39 KML_schema_table = PageTemplateFile('zpt/KML_schema_table', globals())
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
40 HTML_schema_usertables = PageTemplateFile('zpt/HTML_schema_usertables', globals())
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
41
44
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
42 # and scripts
254
901c1f745d13 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 252
diff changeset
43 def KML_URL_schema_table(self,schema,table, useTimestamp=True, args=None):
44
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
44 """KML_URL table function"""
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
45 self.REQUEST.RESPONSE.setHeader("Content-Type", "text/plain")
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
46 id = self.REQUEST.get('id',[])
c6c47034d2a4 more refactoring of templates
casties
parents: 43
diff changeset
47 doc = self.REQUEST.get('doc',None)
245
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
48 # return self.getLiveKmlUrl(schema=schema,table=table, useTimestamp=useTimestamp)
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
49 return self.getLiveKmlUrl(schema=schema,table=table, useTimestamp=False)
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",
252
efd2469d1722 geometry-column of tables will be displayed as string
fknauft
parents: 250
diff changeset
60 "coord_lon": "numeric",
263
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
61 "the_geom": "the_geom",
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
62 "from_year":"text",
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
63 "until_year":"text"
70
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
64 }
9ec7e32e8ad3 working on maps
casties
parents: 65
diff changeset
65
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
66 def getTableOwner(self,schema,table):
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
67 """returns the owner of the table"""
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
68 # what do we do with the schema?
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
69 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
70 res = self.executeSQL(sql,(table,))
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
71 if len(res['rows']) > 0:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
72 return res['rows'][0][0]
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
73 return None
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
74
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
75 def isAllowed(self,action,schema,table,user=None,owner=None):
43
562717546168 refactoring...
casties
parents:
diff changeset
76 """returns if the requested action on the table is allowed"""
562717546168 refactoring...
casties
parents:
diff changeset
77 if user is None:
562717546168 refactoring...
casties
parents:
diff changeset
78 user = self.REQUEST.get('AUTHENTICATED_USER',None)
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
79 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
80 if action == "create":
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
81 if user is not None and str(user) != 'Anonymous User':
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
82 # any authenticated user can create
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
83 return True
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
84 else:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
85 return False
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
86
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
87 if action == "update":
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
88 if owner is None:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
89 owner = self.getTableOwner(schema,table)
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
90 logging.debug("isAllowed user=%s owner=%s"%(user,owner))
60
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
91 if user is not None and str(user) == str(owner):
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
92 # update only your own table
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
93 return True
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
94 else:
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
95 return False
9fdadb60529f working on authentication and authorization
casties
parents: 59
diff changeset
96
62
3905385c8854 small fish
casties
parents: 61
diff changeset
97 # anything else is allowed
43
562717546168 refactoring...
casties
parents:
diff changeset
98 return True
562717546168 refactoring...
casties
parents:
diff changeset
99
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
100 def setTableMetaTypes(self,schema,table,fields,user=None):
43
562717546168 refactoring...
casties
parents:
diff changeset
101 """sets the GIS meta information for table"""
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
102 if user is None:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
103 user = self.REQUEST.get('AUTHENTICATED_USER',None)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
104
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
105 logging.debug("settablemetatypes schema=%s, table=%s, fields=%s user=%s"%(schema,table,fields,user))
43
562717546168 refactoring...
casties
parents:
diff changeset
106
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
107 today = datetime.date.today().isoformat()
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
108
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
109 res = self.executeSQL('select * from public.gis_table_meta where table_name = %s', (table,))
43
562717546168 refactoring...
casties
parents:
diff changeset
110 if len(res['rows']) > 0:
562717546168 refactoring...
casties
parents:
diff changeset
111 # meta record exists
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
112 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
113 self.executeSQL(sql, (str(user),today,table), hasResult=False)
43
562717546168 refactoring...
casties
parents:
diff changeset
114 else:
562717546168 refactoring...
casties
parents:
diff changeset
115 # new meta record
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
116 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
117 self.executeSQL(sql, (table,str(user),today), hasResult=False)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
118
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
119 # update row info
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
120 sql = 'delete from public.gis_table_meta_rows where table_name=%s'
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
121 self.executeSQL(sql,(table,),hasResult=False)
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
122 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
123 for f in fields:
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
124 t = f['type']
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
125 fn = f['name']
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
126 self.executeSQL(sql, (table,fn,t), hasResult=False)
43
562717546168 refactoring...
casties
parents:
diff changeset
127
562717546168 refactoring...
casties
parents:
diff changeset
128
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
129 def getListOfUserTables(self,schema='public',username='guest'):
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
130 """return list of tables"""
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
131 logging.debug("getlistofusertables")
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
132 # get list of db tables
100
4c125fbb2c5b Views are listed in "getListOfUserTables"
fknauft
parents: 93
diff changeset
133 # 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
134 # 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
135 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
136 WHERE t.table_schema = %s AND t.table_name = m.table_name ORDER BY 1"""
43
562717546168 refactoring...
casties
parents:
diff changeset
137
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
138 data=self.executeSQL(qstr,(schema,))
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
139 return data
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
140
43
562717546168 refactoring...
casties
parents:
diff changeset
141
562717546168 refactoring...
casties
parents:
diff changeset
142 def createEmptyTable(self,schema,table,fields):
562717546168 refactoring...
casties
parents:
diff changeset
143 """create a table with the given fields
562717546168 refactoring...
casties
parents:
diff changeset
144 returns list of created fields"""
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
145 sqlFields = WritableRestDbInterface.createEmptyTable(self,schema,table,fields)
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
146 if sqlFields is not None:
43
562717546168 refactoring...
casties
parents:
diff changeset
147 self.setTableMetaTypes(schema,table,sqlFields)
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
148
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
149 return sqlFields
43
562717546168 refactoring...
casties
parents:
diff changeset
150
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
151 # TODO: move this
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
152 def getAttributeNames(self,schema='public',table=None):
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
153 return self.executeSQL("SELECT attname FROM pg_attribute, pg_class WHERE pg_class.oid = attrelid AND attnum>0 AND relname = %s", (table))
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
154
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
155 # TODO: move this
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
156 def getAttributeTypes(self,schema='public',table=None):
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
157 return self.executeSQL("SELECT field_name, gis_type FROM public.gis_table_meta_rows WHERE table_name = %s", (table))
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
158
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
159 # TODO: move back to inherited version
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
160 def showTable(self,format='XML',schema='public',table=None,REQUEST=None,RESPONSE=None):
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
161 """returns PageTemplate with tables"""
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
162 logging.debug("showtable")
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
163 if REQUEST is None:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
164 REQUEST = self.REQUEST
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
165 queryArgs={'doc':None,'id':None}
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
166 queryArgs['doc'] = REQUEST.get('doc')
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
167 queryArgs['id'] = REQUEST.get('id')
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
168
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
169 # should be cross-site accessible
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
170 if RESPONSE is None:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
171 RESPONSE = self.REQUEST.RESPONSE
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
172
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
173 RESPONSE.setHeader('Access-Control-Allow-Origin', '*')
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
174
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
175 # everything else has its own template
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
176 pt = getattr(self.template, '%s_schema_table'%format, REQUEST)
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
177 if pt is None:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
178 return "ERROR!! template %s_schema_table not found at %s"%(format, self.template )
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
179 #data = self.getTable(schema,table)
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
180 return pt(schema=schema,table=table,args=queryArgs)
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
181
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
182 #TODO: move to parent class
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
183 def getLiveUrl(self,schema,table,useTimestamp=True,REQUEST=None):
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
184 if REQUEST is None:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
185 REQUEST = self.REQUEST
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
186 logging.debug("getLiveUrl")
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
187 baseUrl = self.absolute_url()
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
188 timestamp = time.time()
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
189 # filter parameters in URL and add to new URL
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
190 params = [p for p in REQUEST.form.items() if p[0] not in ('format','timestamp')]
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
191 params.append(('format','KML'))
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
192 if useTimestamp:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
193 # add timestamp so URL changes every time
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
194 params.append(('timestamp',timestamp))
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
195 paramstr = urllib.urlencode(params)
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
196 return "%s/db/%s/%s?%s"%(baseUrl,schema,table,paramstr)
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
197
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
198
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
199 # TODO: remove changes from parent version
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
200 def getTable(self,schema='public',table=None,sortBy=1,username='guest'):
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
201 """return table data"""
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
202 logging.debug("gettable")
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
203 attrNames=self.getAttributeNames(schema,table)
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
204 attrTypes=self.getAttributeTypes(schema,table)
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
205 attrString=""
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
206 # try:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
207 for name in attrNames['rows']:
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
208 logging.debug("name: %s"%name[0])
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
209 not_added=True
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
210 if name[0] == "the_geom": #FJK: the table column is "the_geom"
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
211 attrString=attrString+"ST_AsText("+name[0]+"),"
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
212 not_added=False
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
213 break
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
214 for a_iter in attrTypes['rows']:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
215 not_added = True
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
216 logging.debug("attrTypes.field_name: %s"%a_iter[0])
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
217 if a_iter[0]==name[0]:
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
218 logging.debug("attrTypes.gis_type: %s"%a_iter[1])
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
219 if a_iter[1] == "the_geom": #FJK: the table column is registered in gis_table_meta_rows as type "the_geom"
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
220 attrString=attrString+"ST_AsText("+name[0]+"),"
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
221 not_added=False
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
222 if not_added:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
223 if name[0].find('pg.dropped')==-1:
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
224 attrString=attrString+name[0]+","
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
225 attrString=str(attrString).rsplit(",",1)[0] #to remove last ","
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
226 if sortBy:
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
227 data = self.executeSQL('select %s from "%s"."%s" order by %%s'%(attrString,sqlName(schema),sqlName(table)),(sortBy,))
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
228 else:
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
229 data = self.executeSQL('select %s from "%s"."%s"'%(attrString,sqlName(schema),sqlName(table)))
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
230 # except:
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
231 # """ table does not exist """
274
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
232 # fields=self.get
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
233 # self.createEmptyTable(schema, table, fields)
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
234 return data
9b7db308d2e6 refactor RestDbGisApi to use RestDbInterface from ZDbInterface Product.
casties
parents: 264
diff changeset
235
43
562717546168 refactoring...
casties
parents:
diff changeset
236
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
237 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
238 return self.getLiveUrl(schema,table,useTimestamp,REQUEST)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
239
273
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
240 def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None, geomField="point", colorField="red_big",from_year_name='from_year',until_year_name=''):
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
241 """returns data structure for KML template"""
249
bba7dd7b6b4c Layer gui reordered, point order can be defined
fknauft
parents: 247
diff changeset
242 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
243 if geomField is None:
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
244 geomField="point"
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
245 if colorField is None:
2269db3ad5ba polygon-Layers
fknauft
parents: 146
diff changeset
246 colorField="red"
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
247 # Mapping a set of points from table-based SQL-query:
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
248 qstr='SELECT * FROM "%s"."%s"'%(sqlName(schema),sqlName(table))
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
249 idList = None
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
250 if ids is not None:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
251 qstr += ' WHERE '
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
252 if schema=='mpdl':
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
253 qstr += 'mpdl_xmlsource_id IN ('
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
254 else:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
255 qstr += 'CAST(id AS text) IN ('
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
256
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
257 idList = ids.split(",")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
258 qstr += ','.join(['%s' for i in idList])
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
259 qstr += ')'
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
260
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
261 if sortBy:
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
262 # add sort clause
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
263 if sortBy == 1:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
264 qstr += ' ORDER BY 1'
249
bba7dd7b6b4c Layer gui reordered, point order can be defined
fknauft
parents: 247
diff changeset
265 elif sortBy == 'Default':
bba7dd7b6b4c Layer gui reordered, point order can be defined
fknauft
parents: 247
diff changeset
266 qstr += ' ORDER BY 1'
250
d7ceeb8ccbd7 Layer gui reordered, point order can be defined, minor edit
fknauft
parents: 249
diff changeset
267 elif sortBy == 'undefined':
d7ceeb8ccbd7 Layer gui reordered, point order can be defined, minor edit
fknauft
parents: 249
diff changeset
268 qstr += ' ORDER BY 1'
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
269 else:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
270 # TODO: proper quoting for names
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
271 qstr += ' ORDER BY "%s"'%sortBy.replace('"','')
245
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
272 bad_luck=True
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
273 bl_counter=0
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
274 while (bad_luck):
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
275 try:
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
276 data = self.executeSQL(qstr,idList)
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
277 bad_luck=False
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
278 bl_counter=bl_counter+1
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
279 except:
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
280 if (bl_counter<5):
245
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
281 bad_luck=True
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
282 else:
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
283 bad_luck=False
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
284
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
285 fieldMap = self.getFieldNameMap(data['fields'])
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
286 geomdata=None
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
287 if (geomField!="point"):
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
288 # 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
289 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
290 geocolumn_res=self.executeSQL(requeststring,(schema,table))
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
291 if len(geocolumn_res['rows'])>0:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
292 geocolumn=geocolumn_res['rows'][0][0]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
293 try:
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
294 geomstr="select astext(st_simplify(transform(%s,4326),0.05)) from %s.%s"%(geocolumn,sqlName(schema),sqlName(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
295 geomdata=self.executeSQL(geomstr)
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
296 teststr=geomdata.values()[1][0]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
297 if (teststr == (u'MULTIPOLYGON EMPTY',)):
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
298 geomstr="select astext(st_simplify(transform(%s,4326),0.05)) from %s.%s"%(geocolumn,sqlName(schema),sqlName(table)) # the string variables have to be added here and not in executeSQL!
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
299 geomdata=self.executeSQL(geomstr)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
300
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
301 except:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
302 try:
275
eb8a18f94d2d fix some insufficient quoting and other problems.
casties
parents: 274
diff changeset
303 geomstr="select chgis.astext(chgis.st_simplify(chgis.transform(%s,4326),0.05)) from %s.%s"%(geocolumn,sqlName(schema),sqlName(table)) # the string variables have to be added here and not in executeSQL!
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
304 geomdata=self.executeSQL(geomstr)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
305 except:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
306 geomdata=None
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
307
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
308 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
309 # no fields given - choose automagically
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
310 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
311 # gis id in metadata first
61
e81d034b28a5 more permission handling and table metadata
casties
parents: 60
diff changeset
312 res = self.executeSQL(sql, (table,'gis_id'))
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
313 if len(res['rows']) > 0:
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
314 gisIdField = res['rows'][0][0]
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
315 # latitude in metadata
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
316 res = self.executeSQL(sql, (table,'coord_lat'))
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
317 if len(res['rows']) > 0:
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
318 latField = res['rows'][0][0]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
319 # longitude in metadata
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
320 res = self.executeSQL(sql, (table,'coord_lon'))
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
321 if len(res['rows']) > 0:
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
322 lonField = res['rows'][0][0]
93
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
323
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
324 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
325 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
326 # still no fields - try field names
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
327 if 'latitude' in fieldMap and 'longitude' in fieldMap:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
328 latField = 'latitude'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
329 lonField = 'longitude'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
330 elif 'x_coord' in fieldMap and 'y_coord' in fieldMap:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
331 latField = 'x_coord'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
332 lonField = 'y_coord'
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
333 else:
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
334 logging.error("getKMLdata unable to find position fields")
263
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
335 self.REQUEST.RESPONSE.write("Please define Position field")
93
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
336 return None
c23bcd6030a4 automatic map field selection also uses lat/lon
casties
parents: 78
diff changeset
337
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
338
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
339 # convert field names to row indexes
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
340 gisIdIdx = fieldMap.get(gisIdField,None)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
341 latIdx = fieldMap.get(latField,None)
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
342 lonIdx = fieldMap.get(lonField,None)
117
3369c21b66e0 polygon-Layers
fknauft
parents: 109
diff changeset
343 the_geom=fieldMap.get("the_geom",None)
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
344 logging.debug("gisidfield=%s idx=%s"%(gisIdField,gisIdIdx))
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
345
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
346 # convert data
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
347 kmlData = []
151
348a11a5e49c polygon-Layers
fknauft
parents: 150
diff changeset
348 geom_list = {}
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
349 # try:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
350 # if geomField=='poly' or geomField=='line':
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
351 # geom_list=geomdata.values()[1]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
352 # except:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
353 # return "no geomdata in RestDbGisApi Line 254"
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
354
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
355 data_list=data['rows']
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
356 for k in range (len(data_list)):
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
357 dataset = data_list[k]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
358 if len(geom_list)>k:
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
359 geom_value = geom_list[k]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
360
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
361
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
362 if gisIdIdx != None and geomField!='point':
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
363 gisID = dataset[gisIdIdx]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
364 geomdata = self.getLineForChGisId(gisID)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
365
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
366 # for dataset in data['rows']:
65
2f477270cc0c adding layers to maps works now
casties
parents: 62
diff changeset
367 xCoord = 0.0
2f477270cc0c adding layers to maps works now
casties
parents: 62
diff changeset
368 yCoord = 0.0
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
369 if gisIdIdx != None and geomField=='point' :
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
370 gisID = dataset[gisIdIdx]
257
61525f0ca492 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 256
diff changeset
371 if gisID != " " and gisID != " 0":
255
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
372 coords=self.getPointForChGisId(gisID)
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
373 if coords!=None:
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
374 xCoord=coords[0]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
375 yCoord=coords[1]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
376 elif latIdx != None:
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
377 xCoord = dataset[lonIdx]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
378 yCoord = dataset[latIdx]
ec7b63319fad GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 254
diff changeset
379 elif latIdx != None:
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
380 xCoord = dataset[lonIdx]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
381 yCoord = dataset[latIdx]
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
382
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
383 elif geomField=='point' :
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
384 logging.error("getKMLdata unable to find position")
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
385 return None
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
386
233
bdab16f83512 show line and polygon-layer
fknauft
parents: 209
diff changeset
387 if geomField=='point' :
262
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
388 if type(xCoord).__name__=='string':
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
389 if float(xCoord) == 0.0:
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
390 continue
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
391 else:
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
392 if xCoord == 0.0:
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
393 continue
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
394 if type(yCoord).__name__=='string':
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
395 if float(yCoord) == 0.0:
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
396 continue
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
397 else:
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
398 if yCoord == 0.0:
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
399 continue
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
400
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
401 kmlPlace = {}
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
402
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
403 # description
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
404 desc = ''
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
405
262
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
406 timestring = ''
264
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
407 beg_yr = '-9999'
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
408 end_yr = '9999'
263
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
409 from_year = ''
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
410 until_year = ''
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
411 sql = "SELECT field_name FROM public.gis_table_meta_rows WHERE table_name = %s and gis_type = %s"
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
412 # from_year and until_year in metadata first
264
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
413 try:
273
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
414 res = self.executeSQL(sql, (table,from_year_name))
264
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
415 if len(res['rows']) > 0:
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
416 from_year = res['rows'][0][0]
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
417 except:
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
418 from_year = "from_year_dummy"
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
419 try:
273
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
420 res = self.executeSQL(sql, (table,until_year_name))
264
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
421 if len(res['rows']) > 0:
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
422 until_year = res['rows'][0][0]
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
423 except:
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
424 until_year = "until_year_dummy"
276
55bc9972fb1b Merge with d1b43624cc63b829f64cc952540748e15272e389
casties
parents: 275 273
diff changeset
425
273
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
426 #DW added for testing E4D with names
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
427 from_year=from_year_name
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
428 until_year=until_year_name
276
55bc9972fb1b Merge with d1b43624cc63b829f64cc952540748e15272e389
casties
parents: 275 273
diff changeset
429
273
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
430 logging.debug("from_year:"+from_year)
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
431 logging.debug("until_year:"+until_year)
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
432 for i in range (len(dataset)):
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
433 value = dataset[i]
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
434
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
435 name = data['fields'][i][0]
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
436 #logging.debug("value=%s"%value)
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
437 if name != 'the_geom':
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
438 if value != None:
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
439 #if name.find('name')>-1:
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
440 # desc += "<name>%s</name>\n"%value
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
441 # continue
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
442 #elif name.find('place')>-1:
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
443 # desc += "<name>%s</name>\n"%value
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
444 # continue
262
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
445
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
446 val = "%s: %s"%(name, value)
273
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
447 logging.debug(name)
187
f080901d8163 corrected links in desc (RestDbGisApi)
fknauft
parents: 186
diff changeset
448 value=unicode(value)
263
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
449 if name == from_year:
262
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
450 beg_yr= value
263
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
451 if name == until_year:
262
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
452 end_yr=value
188
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
453 # 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
454 if value.find('http://')>-1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
455 link_str_beg=value.find('http://')
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
456 link_str_end = -1
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
457 link_str_end0=value.find(' ',link_str_beg)
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
458 link_str_end1=value.find('>',link_str_beg)
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
459 if link_str_end0 <link_str_end1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
460 link_str_end=link_str_end0
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
461 else:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
462 link_str_end=link_str_end1
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
463 if link_str_end > -1:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
464 link_str=value[link_str_beg:link_str_end]
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
465 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
466 else:
03976fe6ef2d corrected links in desc (RestDbGisApi)
fknauft
parents: 190
diff changeset
467 link_str=value[link_str_beg:]
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
468 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
469
f87dda6f05d6 corrected links in desc (RestDbGisApi)
fknauft
parents: 187
diff changeset
470
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
471 #desc += kmlEncode(val)
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
472 desc += val
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
473 desc += '<br/>\n'
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
474
57
b47314bcff37 connect lines works now
casties
parents: 55
diff changeset
475 #kmlPlace['description'] = "<![CDATA[%s]]>"%desc
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
476
264
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
477 if end_yr!='9999':
263
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
478 kmlPlace['TimeSpan0'] = '%s'%beg_yr
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
479 kmlPlace['TimeSpan1'] = '%s'%end_yr
264
52b1247140b7 Multilayer for Europeana4D
fknauft
parents: 263
diff changeset
480 else:
263
3a10287447b1 Integration von Europeana4D UserInterface
fknauft
parents: 262
diff changeset
481 kmlPlace['TimeStamp'] = '%s'%beg_yr
124
8668d76a7b6a polygon-Layers
fknauft
parents: 123
diff changeset
482
133
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
483 if geomField=='point':
278
4ade9b80b563 more cleanup.
casties
parents: 276
diff changeset
484 #kmlPlace['description'] = "<![CDATA[%s]]>"%desc
4ade9b80b563 more cleanup.
casties
parents: 276
diff changeset
485 kmlPlace['description'] = desc
193
e6a754b05ec8 corrected links in desc (RestDbGisApi)
fknauft
parents: 192
diff changeset
486
133
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
487 kmlPlace['icon'] = '#marker_icon_'+colorField
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
488 kmlPlace['coord_x'] = str(xCoord)
8a4201f31ca9 polygon-Layers
fknauft
parents: 131
diff changeset
489 kmlPlace['coord_y'] = str(yCoord)
258
ed99a2468264 GIS-Links from MPDL-Documents now served by Mappit-Server
fknauft
parents: 257
diff changeset
490 kmlPlace['coord_z'] = '50'
134
649681baae76 polygon-Layers
fknauft
parents: 133
diff changeset
491 kmlData.append(kmlPlace)
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
492 if (geomField=='poly' or geomField=='line') and geomdata is not None:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
493 polys=str(geomdata).split('(')
144
abc95f483315 polygon-Layers
fknauft
parents: 142
diff changeset
494 aaa=len(polys)
142
3adea92d2067 polygon-Layers
fknauft
parents: 138
diff changeset
495 for poly in polys:
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
496 kmlPlace = {}
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
497 kmlPlace['description'] = desc
152
7fddf8a60481 polygon-Layers
fknauft
parents: 151
diff changeset
498 coords=poly.replace(')','').replace("'","").split(',')
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
499 coord_string=''
144
abc95f483315 polygon-Layers
fknauft
parents: 142
diff changeset
500 if len(coords)>1:
146
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
501 for coord in coords:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
502 coord=coord.split(' ')
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
503 try:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
504 x_coord=coord[0]
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
505 y_coord=coord[1]
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
506 except:
24828625c0ce polygon-Layers
fknauft
parents: 145
diff changeset
507 break
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
508 coord_string+=x_coord+','+y_coord+','+'0 '
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
509 if coord_string != '':
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
510 kmlPlace['LinearRing']=coord_string
245
7c5d825a0083 show line and polygon-layer
fknauft
parents: 233
diff changeset
511 kmlPlace['lineColor']='#'+colorField+'_'+geomField
150
96311f3d627b polygon-Layers
fknauft
parents: 148
diff changeset
512 kmlData.append(kmlPlace)
262
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
513
6613b9204bda Small bugs
fknauft
parents: 261
diff changeset
514
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
515 #logging.debug("kmlData=%s"%(repr(kmlData)))
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
516 return kmlData
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
517
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
518 def getPointForChGisId(self, gis_id):
260
e9a1ac7d2ab2 Bug resolved in request point_id from Harvard ChGIS
fknauft
parents: 259
diff changeset
519 """returns coordinate pair for given gis_id""" # gets called by getKml
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
520 def getPoint(id):
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
521 str_gis_id=str(id).split('.')[0]
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
522 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
523 # logging.error("sql:",sql)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
524 # res = self.executeSQL(sql, (str(str_gis_id),))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
525 res = self.executeSQL(sql)
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
526 if len(res['rows']) > 0:
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
527 return res['rows'][0]
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
528 else:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
529 #logging.error("error on sql:",sql%(str(str_gis_id),))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
530 return None
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
531
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
532 if gis_id is None or gis_id == "":
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
533 return None
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
534 if len(str(gis_id)) < 4:
260
e9a1ac7d2ab2 Bug resolved in request point_id from Harvard ChGIS
fknauft
parents: 259
diff changeset
535 return None
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
536
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
537 # try gis_id
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
538 coords = getPoint(gis_id)
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
539 if coords is None:
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
540 # try to clean gis_id...
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
541 gis_id_short = re.sub(r'[^0-9]','',gis_id)
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
542 # try again
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
543 coords = getPoint(gis_id_short)
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
544 if coords is None:
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
545 #logging.error("CH-GIS ID %s not found!"%str(gis_id_short))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
546
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
547 # 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
548 try:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
549 coords=self.getCoordsFromREST_gisID(gis_id)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
550 #logging.error("coords from REST"%str(coords))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
551 except:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
552 logging.error("coords from REST did not work for "%str(gis_id))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
553 if coords[0] is None:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
554 logging.error("CH-GIS ID %s not found in Harvard"%str(gis_id))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
555 else:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
556 try:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
557 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
558 SQL_analyze="ANALYZE chgis.chgis_coords"
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
559 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
560 self.executeSQL(SQL_analyze,(),False)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
561 except:
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
562 logging.error("Could not write into chgis_coords: ", str(gis_id_short))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
563 logging.error("coords[0][0]:"%coords[0][0])
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
564 logging.error("coords[0][1]:"%coords[0][1])
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
565 return coords[0]
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
566 coords_test = getPoint(gis_id)
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
567 #logging.error("SQL results now:", str(coords_test))
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
568 return coords[0]
58
5ed0769f5ad3 more cleanup in kml generation
casties
parents: 57
diff changeset
569
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
570 return coords
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
571
55
2f4c427dec44 wrangled kml construction into template and method
casties
parents: 44
diff changeset
572
261
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
573 def getLineForChGisId(self, gis_id):
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
574 """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
575 def getLine(id):
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
576 str_gis_id=str(id).split('.')[0]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
577 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
578 # logging.error("sql:",sql)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
579 # res = self.executeSQL(sql, (str(str_gis_id),))
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
580 res = self.executeSQL(sql)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
581 if len(res['rows']) > 0:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
582 return res['rows'][0]
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
583 else:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
584 #logging.error("error on sql:",sql%(str(str_gis_id),))
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
585 return None
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
586
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
587 if gis_id is None or gis_id == "":
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
588 return None
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
589 if len(str(gis_id)) < 4:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
590 return None
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
591
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
592 # try gis_id
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
593 line_coords = getLine(gis_id)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
594 if line_coords is None:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
595 # try to clean gis_id...
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
596 gis_id_short = re.sub(r'[^0-9]','',gis_id)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
597 # try again
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
598 line_coords = getLine(gis_id_short)
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
599 if line_coords is None:
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
600 logging.error("CH-GIS ID %s not found!"%(gis_id_short))
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
601
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
602
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
603 return line_coords
5b38b50052e4 Display Polygons and Lines via chgis_id
fknauft
parents: 260
diff changeset
604
59
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
605 ## legacy methods...
a5f2550a5b44 more cleanup in kml generation
casties
parents: 58
diff changeset
606
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
607 def trydatahas_key(self,data,index,key_string):
273
d1b43624cc63 some hacks to make the european4D connection work
dwinter
parents: 264
diff changeset
608 logging.debug("trying:"+key_string)
207
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
609 try:
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
610 return data[index].has_key(key_string)
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
611 except:
dd235873514b Repair: loading of chgis-polys
fknauft
parents: 193
diff changeset
612 return 'false'
43
562717546168 refactoring...
casties
parents:
diff changeset
613
562717546168 refactoring...
casties
parents:
diff changeset
614 # End for GoogleMaps creation
562717546168 refactoring...
casties
parents:
diff changeset
615
562717546168 refactoring...
casties
parents:
diff changeset
616 def RESTwrite2File(self,datadir, name,text):
562717546168 refactoring...
casties
parents:
diff changeset
617 logging.debug("RESTwrite2File datadir=%s name=%s"%(datadir,name))
562717546168 refactoring...
casties
parents:
diff changeset
618 try:
562717546168 refactoring...
casties
parents:
diff changeset
619 import cStringIO as StringIO
562717546168 refactoring...
casties
parents:
diff changeset
620 except:
562717546168 refactoring...
casties
parents:
diff changeset
621 import StringIO
562717546168 refactoring...
casties
parents:
diff changeset
622
562717546168 refactoring...
casties
parents:
diff changeset
623 # make filehandle from string
562717546168 refactoring...
casties
parents:
diff changeset
624 textfile = StringIO.StringIO(text)
562717546168 refactoring...
casties
parents:
diff changeset
625 fileid=name
562717546168 refactoring...
casties
parents:
diff changeset
626 if fileid in datadir.objectIds():
562717546168 refactoring...
casties
parents:
diff changeset
627 datadir.manage_delObjects(fileid)
562717546168 refactoring...
casties
parents:
diff changeset
628 fileInZope=datadir.manage_addFile(id=fileid,file=textfile)
562717546168 refactoring...
casties
parents:
diff changeset
629 return "Write successful"
562717546168 refactoring...
casties
parents:
diff changeset
630
562717546168 refactoring...
casties
parents:
diff changeset
631 def manage_editRestDbGisApi(self, title=None, connection_id=None,
562717546168 refactoring...
casties
parents:
diff changeset
632 REQUEST=None):
562717546168 refactoring...
casties
parents:
diff changeset
633 """Change the object"""
562717546168 refactoring...
casties
parents:
diff changeset
634 if title is not None:
562717546168 refactoring...
casties
parents:
diff changeset
635 self.title = title
562717546168 refactoring...
casties
parents:
diff changeset
636
562717546168 refactoring...
casties
parents:
diff changeset
637 if connection_id is not None:
562717546168 refactoring...
casties
parents:
diff changeset
638 self.connection_id = connection_id
562717546168 refactoring...
casties
parents:
diff changeset
639
562717546168 refactoring...
casties
parents:
diff changeset
640 #checkPermission=getSecurityManager().checkPermission
562717546168 refactoring...
casties
parents:
diff changeset
641 REQUEST.RESPONSE.redirect('manage_main')
562717546168 refactoring...
casties
parents:
diff changeset
642
562717546168 refactoring...
casties
parents:
diff changeset
643
562717546168 refactoring...
casties
parents:
diff changeset
644 manage_addRestDbGisApiForm=PageTemplateFile('zpt/addRestDbGisApi',globals())
562717546168 refactoring...
casties
parents:
diff changeset
645
562717546168 refactoring...
casties
parents:
diff changeset
646 def manage_addRestDbGisApi(self, id, title='', label='', description='',
562717546168 refactoring...
casties
parents:
diff changeset
647 createPublic=0,
562717546168 refactoring...
casties
parents:
diff changeset
648 createUserF=0,
562717546168 refactoring...
casties
parents:
diff changeset
649 REQUEST=None):
562717546168 refactoring...
casties
parents:
diff changeset
650 """Add a new object with id *id*."""
562717546168 refactoring...
casties
parents:
diff changeset
651
562717546168 refactoring...
casties
parents:
diff changeset
652 ob=RestDbGisApi(str(id),title)
562717546168 refactoring...
casties
parents:
diff changeset
653 self._setObject(id, ob)
562717546168 refactoring...
casties
parents:
diff changeset
654
562717546168 refactoring...
casties
parents:
diff changeset
655 #checkPermission=getSecurityManager().checkPermission
562717546168 refactoring...
casties
parents:
diff changeset
656 REQUEST.RESPONSE.redirect('manage_main')
562717546168 refactoring...
casties
parents:
diff changeset
657
247
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
658
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
659
47bba001d718 show line and polygon-layer
fknauft
parents: 245
diff changeset
660