v.in.wfs.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.in.wfs
  5. # AUTHOR(S): Markus Neteler. neteler itc it
  6. # Hamish Bowman
  7. # Converted to Python by Glynn Clements
  8. # PURPOSE: WFS support
  9. # COPYRIGHT: (C) 2006-2012 Markus Neteler and the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General
  12. # Public License (>=v2). Read the file COPYING that
  13. # comes with GRASS for details.
  14. #
  15. # GetFeature example:
  16. # http://mapserver.gdf-hannover.de/cgi-bin/grassuserwfs?REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0
  17. #############################################################################
  18. #
  19. # TODO: suggest to depend on the OWSLib for OGC web service needs
  20. # http://pypi.python.org/pypi/OWSLib
  21. #
  22. #%Module
  23. #% description: Imports GetFeature from a WFS server.
  24. #% keyword: vector
  25. #% keyword: import
  26. #% keyword: OGC web services
  27. #% keyword: OGC WFS
  28. #%end
  29. #%option
  30. #% key: url
  31. #% type: string
  32. #% description: Base URL starting with 'http' and ending in '?'
  33. #% required: yes
  34. #%end
  35. #%option G_OPT_V_OUTPUT
  36. #%end
  37. #%option
  38. #% key: name
  39. #% type: string
  40. #% description: Comma separated names of data layers to download
  41. #% multiple: yes
  42. #% required: no
  43. #%end
  44. #%option
  45. #% key: srs
  46. #% type: string
  47. #% label: Specify alternate spatial reference system (example: EPSG:4326)
  48. #% description: The given code must be supported by the server, consult the capabilities file
  49. #% required: no
  50. #%end
  51. #%option
  52. #% key: maximum_features
  53. #% type: integer
  54. #% label: Maximum number of features to download
  55. #% description: (default: unlimited)
  56. #%end
  57. #%option
  58. #% key: start_index
  59. #% type: integer
  60. #% label: Skip earlier feature IDs and start downloading at this one
  61. #% description: (default: start with the first feature)
  62. #%end
  63. #%flag
  64. #% key: l
  65. # todo #% description: List available layers and exit
  66. #% description: Download server capabilities to 'wms_capabilities.xml' in the current directory and exit
  67. #% suppress_required: yes
  68. #%end
  69. #%flag
  70. #% key: r
  71. #% description: Restrict fetch to features which touch the current region
  72. #%end
  73. import os
  74. import sys
  75. from grass.script.utils import try_remove
  76. from grass.script import core as grass
  77. try:
  78. from urllib2 import urlopen, URLError, HTTPError
  79. except ImportError:
  80. from urllib.request import urlopen
  81. from urllib.error import URLError, HTTPError
  82. def main():
  83. out = options['output']
  84. wfs_url = options['url']
  85. request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0'
  86. wfs_url += request_base
  87. if options['name']:
  88. wfs_url += '&TYPENAME=' + options['name']
  89. if options['srs']:
  90. wfs_url += '&SRS=' + options['srs']
  91. if options['maximum_features']:
  92. wfs_url += '&MAXFEATURES=' + options['maximum_features']
  93. if int(options['maximum_features']) < 1:
  94. # GTC Invalid WFS maximum features parameter
  95. grass.fatal(_("Invalid maximum number of features"))
  96. if options['start_index']:
  97. wfs_url += '&STARTINDEX=' + options['start_index']
  98. if int(options['start_index']) < 1:
  99. # GTC Invalid WFS start index parameter
  100. grass.fatal(_('Features begin with index "1"'))
  101. if flags['r']:
  102. bbox = grass.read_command("g.region", flags='w').split('=')[1]
  103. wfs_url += '&BBOX=' + bbox
  104. if flags['l']:
  105. wfs_url = options['url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0'
  106. tmp = grass.tempfile()
  107. tmpxml = tmp + '.xml'
  108. grass.debug(wfs_url)
  109. # GTC Downloading WFS features
  110. grass.message(_("Retrieving data..."))
  111. try:
  112. inf = urlopen(wfs_url)
  113. except HTTPError as e:
  114. # GTC WFS request HTTP failure
  115. grass.fatal(_("The server couldn't fulfill the request.\nError code: %s") % e.code)
  116. except URLError as e:
  117. # GTC WFS request network failure
  118. grass.fatal(_("Failed to reach the server.\nReason: %s") % e.reason)
  119. outf = open(tmpxml, 'wb')
  120. while True:
  121. s = inf.read()
  122. if not s:
  123. break
  124. outf.write(s)
  125. inf.close()
  126. outf.close()
  127. if flags['l']:
  128. import shutil
  129. if os.path.exists('wms_capabilities.xml'):
  130. grass.fatal(_('A file called "wms_capabilities.xml" already exists here'))
  131. # os.move() might fail if the temp file is on another volume, so we copy instead
  132. shutil.copy(tmpxml, 'wms_capabilities.xml')
  133. try_remove(tmpxml)
  134. sys.exit(0)
  135. grass.message(_("Importing data..."))
  136. try:
  137. grass.run_command('v.in.ogr', flags='o', input=tmpxml, output=out)
  138. grass.message(_("Vector map <%s> imported from WFS.") % out)
  139. except:
  140. grass.message(_("WFS import failed"))
  141. finally:
  142. try_remove(tmpxml)
  143. if __name__ == "__main__":
  144. options, flags = grass.parser()
  145. main()