v.in.wfs.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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: Imports 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. #% required: yes
  29. #%end
  30. #%option G_OPT_V_OUTPUT
  31. #%end
  32. import os
  33. from grass.script import core as grass
  34. import urllib
  35. def main():
  36. out = options['output']
  37. wfs_url = options['url']
  38. tmp = grass.tempfile()
  39. tmpxml = tmp + '.xml'
  40. grass.message(_("Retrieving data..."))
  41. inf = urllib.urlopen(wfs_url)
  42. outf = file(tmpxml, 'wb')
  43. while True:
  44. s = inf.read()
  45. if not s:
  46. break
  47. outf.write(s)
  48. inf.close()
  49. outf.close()
  50. grass.message(_("Importing data..."))
  51. ret = grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out)
  52. grass.try_remove(tmpxml)
  53. if ret == 0:
  54. grass.message(_("Vector points map <%s> imported from WFS.") % out)
  55. else:
  56. grass.message(_("WFS import failed"))
  57. if __name__ == "__main__":
  58. options, flags = grass.parser()
  59. main()