v.in.wfs.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #%end
  28. #%option
  29. #% key: url
  30. #% type: string
  31. #% description: Base URL starting with 'http' and ending in '?'
  32. #% required: yes
  33. #%end
  34. #%option G_OPT_V_OUTPUT
  35. #%end
  36. #%option
  37. #% key: name
  38. #% type: string
  39. #% description: Comma separated names of data layers to download
  40. #% multiple: yes
  41. #% required: no
  42. #%end
  43. #%option
  44. #% key: srs
  45. #% type: string
  46. #% label: Specify alternate spatial reference system (example: EPSG:4326)
  47. #% description: The given code must be supported by the server, consult the capabilities file
  48. #% required: no
  49. #%end
  50. #%option
  51. #% key: maximum_features
  52. #% type: integer
  53. #% label: Maximum number of features to download
  54. #% description: (default: unlimited)
  55. #%end
  56. #%option
  57. #% key: start_index
  58. #% type: integer
  59. #% label: Skip earlier feature IDs and start downloading at this one
  60. #% description: (default: start with the first feature)
  61. #%end
  62. #%flag
  63. #% key: l
  64. #todo #% description: List available layers and exit
  65. #% description: Download server capabilities to 'wms_capabilities.xml' in the current directory and exit
  66. #% suppress_required: yes
  67. #%end
  68. #%flag
  69. #% key: r
  70. #% description: Restrict fetch to features which touch the current region
  71. #%end
  72. import os
  73. import sys
  74. from grass.script.utils import try_remove
  75. from grass.script import core as grass
  76. import urllib
  77. def main():
  78. out = options['output']
  79. wfs_url = options['url']
  80. request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0'
  81. wfs_url += request_base
  82. if options['name']:
  83. wfs_url += '&TYPENAME=' + options['name']
  84. if options['srs']:
  85. wfs_url += '&SRS=' + options['srs']
  86. if options['maximum_features']:
  87. wfs_url += '&MAXFEATURES=' + options['maximum_features']
  88. if int(options['maximum_features']) < 1:
  89. grass.fatal('Invalid maximum number of features')
  90. if options['start_index']:
  91. wfs_url += '&STARTINDEX=' + options['start_index']
  92. if int(options['start_index']) < 1:
  93. grass.fatal('Features begin with index "1"')
  94. if flags['r']:
  95. bbox = grass.read_command("g.region", flags = 'w').split('=')[1]
  96. wfs_url += '&BBOX=' + bbox
  97. if flags['l']:
  98. wfs_url = options['url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0'
  99. tmp = grass.tempfile()
  100. tmpxml = tmp + '.xml'
  101. grass.debug(wfs_url)
  102. grass.message(_("Retrieving data..."))
  103. inf = urllib.urlopen(wfs_url)
  104. outf = file(tmpxml, 'wb')
  105. while True:
  106. s = inf.read()
  107. if not s:
  108. break
  109. outf.write(s)
  110. inf.close()
  111. outf.close()
  112. if flags['l']:
  113. import shutil
  114. if os.path.exists('wms_capabilities.xml'):
  115. grass.fatal('A file called "wms_capabilities.xml" already exists here')
  116. # os.move() might fail if the temp file is on another volume, so we copy instead
  117. shutil.copy(tmpxml, 'wms_capabilities.xml')
  118. try_remove(tmpxml)
  119. sys.exit(0)
  120. grass.message(_("Importing data..."))
  121. try:
  122. grass.run_command('v.in.ogr', flags='o', input=tmpxml, output=out)
  123. grass.message(_("Vector points map <%s> imported from WFS.") % out)
  124. except:
  125. grass.message(_("WFS import failed"))
  126. finally:
  127. try_remove(tmpxml)
  128. if __name__ == "__main__":
  129. options, flags = grass.parser()
  130. main()