PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/models/geoserver.py

https://github.com/dotskapes/dotSkapes
Python | 104 lines | 102 code | 2 blank | 0 comment | 7 complexity | f8e98fb210f2af5435d4547a1aa72f4f MD5 | raw file
  1. from urllib import urlencode
  2. from urllib2 import urlopen
  3. from BeautifulSoup import BeautifulStoneSoup
  4. db.define_table ('geoserver_sources',
  5. Field ('loc', 'string', required = True))
  6. def sync_geoserver (path):
  7. file = urlopen (path + '?SERVICE=WFS&REQUEST=GetCapabilities')
  8. buffer = file.read ()
  9. soup = BeautifulStoneSoup (buffer)
  10. layers = soup.findAll (name = 'featuretype')
  11. results = []
  12. for l in layers:
  13. name = l.find ('title')
  14. id = l.find ('name')
  15. if name and id:
  16. text = name.string
  17. if not text:
  18. text = id.string
  19. m = match ('^([^\:]+)\:(.+)$', id.string)
  20. if m:
  21. p = m.group (1)
  22. f = m.group (2)
  23. else:
  24. p = '',
  25. f = id.string
  26. if dm.query ('maps', prefix = p, filename = f, name = text, src = path).first ():
  27. pass
  28. else:
  29. id = dm.insert ('maps', prefix = p, filename = f, name = text, src = path, public = True)
  30. keywords = l.findAll (name = 'keyword')
  31. kw = []
  32. for k in keywords:
  33. kw.append (k.string)
  34. #dm.keywords ('maps', id, kw)
  35. def load_fields (data):
  36. map_data = urlopen (data.src + '/ows', urlencode ({
  37. 'service': 'wfs',
  38. 'version': '1.1.0',
  39. 'request': 'DescribeFeatureType',
  40. 'typename': data.prefix + ':' + data.filename,
  41. #'outputformat': 'json',
  42. })
  43. )
  44. doc = BeautifulStoneSoup (map_data.read ())
  45. elements = doc.find ('xsd:complextype')
  46. tags = elements.findAll ('xsd:element')
  47. names = []
  48. for t in tags:
  49. if match ('^gml:', t['type']):
  50. continue
  51. names.append (str (t['name']))
  52. return names
  53. def load_map (data):
  54. map_data = urlopen (data.src + '?', urlencode ({
  55. 'service': 'wfs',
  56. 'version': '1.1.0',
  57. 'request': 'GetFeature',
  58. 'typename': data.prefix + ':' + data.filename,
  59. 'outputformat': 'JSON',
  60. })
  61. )
  62. return map_data.read ()
  63. def load_map_attributes (data, start = None, limit = None):
  64. if not session.has_key ('current_map') or not session['current_map'][0] == data.id:
  65. map_data = urlopen (data.src + '/ows', urlencode ({
  66. 'service': 'wfs',
  67. 'version': '1.1.0',
  68. 'request': 'GetFeature',
  69. 'typename': data.prefix + ':' + data.filename,
  70. 'outputformat': 'json',
  71. })
  72. )
  73. map_attr = []
  74. for ob in json.loads (map_data.read ())['features']:
  75. result = {'id': ob['id']}
  76. result.update (ob['properties'])
  77. map_attr.append (result)
  78. #session['current_map'] = (data.id, map_attr)
  79. else:
  80. map_attr = session['current_map'][1]
  81. if not limit:
  82. return {'features': map_attr}
  83. else:
  84. return {'features': map_attr[start:start + limit]}
  85. def gen_choropleth (map_index, sort_field, low_color, high_color):
  86. from savage.graphics.color import ColorMap, color_to_css
  87. key = map_index.src + map_index.filename + sort_field + color_to_css (low_color) + color_to_css (high_color)
  88. def create_choro ():
  89. attr = load_map_attributes (map_index)
  90. data_list = []
  91. for item in attr['features']:
  92. data_list.append ((item['id'], item[sort_field]))
  93. data_list.sort (key = lambda x: x[0])
  94. cm = ColorMap (low_color, high_color, len (data_list))
  95. choro = json.dumps ({'layer': map_index.prefix + ':' + map_index.filename, 'filter': zip (map (lambda x: x[0], data_list), map (color_to_css, cm))})
  96. return urlopen ('http://127.0.0.1:' + str (deployment_settings.web2py.port) + '/' + request.application + '/geoserver/xsd/choropleth', choro).read ()
  97. return cache.ram (key, create_choro, time_expire = 20);