v.in.wfs.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, import, wfs
  21. #%end
  22. #%option
  23. #% key: url
  24. #% type: string
  25. #% description: GetFeature URL starting with 'http'
  26. #%end
  27. #%option
  28. #% key: output
  29. #% type: string
  30. #% gisprompt: new,vector,vector
  31. #% description: Name for output vector map
  32. #% required : yes
  33. #%end
  34. import os
  35. from grass.script import core as grass
  36. import urllib
  37. def main():
  38. out = options['output']
  39. wfs_url = options['url']
  40. tmp = grass.tempfile()
  41. tmpxml = tmp + '.xml'
  42. grass.message(_("Retrieving data..."))
  43. inf = urllib.urlopen(wfs_url)
  44. outf = file(tmpxml, 'wb')
  45. while True:
  46. s = inf.read()
  47. if not s:
  48. break
  49. outf.write(s)
  50. inf.close()
  51. outf.close()
  52. grass.message(_("Importing data..."))
  53. ret = grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out)
  54. grass.try_remove(tmpxml)
  55. if ret == 0:
  56. grass.message(_("Vector points map <%s> imported from WFS.") % out)
  57. else:
  58. grass.message(_("WFS import failed"))
  59. if __name__ == "__main__":
  60. options, flags = grass.parser()
  61. main()