v.in.wfs.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #% keywords: vector
  25. #% keywords: import
  26. #% keywords: wfs
  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 import core as grass
  75. import urllib
  76. def main():
  77. out = options['output']
  78. wfs_url = options['url']
  79. request_base = 'REQUEST=GetFeature&SERVICE=WFS&VERSION=1.0.0'
  80. wfs_url += request_base
  81. if options['name']:
  82. wfs_url += '&TYPENAME=' + options['name']
  83. if options['srs']:
  84. wfs_url += '&SRS=' + options['srs']
  85. if options['maximum_features']:
  86. wfs_url += '&MAXFEATURES=' + options['maximum_features']
  87. if int(options['maximum_features']) < 1:
  88. grass.fatal('Invalid maximum number of features')
  89. if options['start_index']:
  90. wfs_url += '&STARTINDEX=' + options['start_index']
  91. if int(options['start_index']) < 1:
  92. grass.fatal('Features begin with index "1"')
  93. if flags['r']:
  94. bbox = grass.read_command("g.region", flags = 'w').split('=')[1]
  95. wfs_url += '&BBOX=' + bbox
  96. if flags['l']:
  97. wfs_url = options['url'] + 'REQUEST=GetCapabilities&SERVICE=WFS&VERSION=1.0.0'
  98. tmp = grass.tempfile()
  99. tmpxml = tmp + '.xml'
  100. grass.debug(wfs_url)
  101. grass.message(_("Retrieving data..."))
  102. inf = urllib.urlopen(wfs_url)
  103. outf = file(tmpxml, 'wb')
  104. while True:
  105. s = inf.read()
  106. if not s:
  107. break
  108. outf.write(s)
  109. inf.close()
  110. outf.close()
  111. if flags['l']:
  112. import shutil
  113. if os.path.exists('wms_capabilities.xml'):
  114. grass.fatal('A file called "wms_capabilities.xml" already exists here')
  115. # os.move() might fail if the temp file is on another volume, so we copy instead
  116. shutil.copy(tmpxml, 'wms_capabilities.xml')
  117. grass.try_remove(tmpxml)
  118. sys.exit(0)
  119. grass.message(_("Importing data..."))
  120. ret = grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out)
  121. grass.try_remove(tmpxml)
  122. if ret == 0:
  123. grass.message(_("Vector points map <%s> imported from WFS.") % out)
  124. else:
  125. grass.message(_("WFS import failed"))
  126. if __name__ == "__main__":
  127. options, flags = grass.parser()
  128. main()