v.in.wfs.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.in.wfs
  5. # AUTHOR(S): Markus Neteler. neteler itc it
  6. # Hamish Bowman (fixes)
  7. # Converted to Python by Glynn Clements
  8. # PURPOSE: WFS support
  9. # COPYRIGHT: (C) 2006, 2007, 2008, 2010 Markus Neteler and 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. #%Module
  19. #% description: Import GetFeature from WFS.
  20. #% keywords: vector
  21. #% keywords: import
  22. #% keywords: wfs
  23. #%end
  24. #%option
  25. #% key: url
  26. #% type: string
  27. #% description: GetFeature URL starting with 'http'
  28. #%end
  29. #%option
  30. #% key: output
  31. #% type: string
  32. #% gisprompt: new,vector,vector
  33. #% description: Name for output vector map
  34. #% required : yes
  35. #%end
  36. import os
  37. from grass.script import core as grass
  38. import urllib
  39. def main():
  40. out = options['output']
  41. wfs_url = options['url']
  42. tmp = grass.tempfile()
  43. tmpxml = tmp + '.xml'
  44. grass.message(_("Retrieving data..."))
  45. inf = urllib.urlopen(wfs_url)
  46. outf = file(tmpxml, 'wb')
  47. while True:
  48. s = inf.read()
  49. if not s:
  50. break
  51. outf.write(s)
  52. inf.close()
  53. outf.close()
  54. grass.message(_("Importing data..."))
  55. ret = grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out)
  56. grass.try_remove(tmpxml)
  57. if ret == 0:
  58. grass.message(_("Vector points map <%s> imported from WFS.") % out)
  59. else:
  60. grass.message(_("WFS import failed"))
  61. if __name__ == "__main__":
  62. options, flags = grass.parser()
  63. main()