db.out.ogr.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: db.out.ogr
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: exports attribute tables into various formats
  8. # COPYRIGHT: (C) 2007-2014 by Markus Neteler and the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. #%module
  16. #% description: Exports attribute tables into various formats.
  17. #% keyword: database
  18. #% keyword: export
  19. #% keyword: attribute table
  20. #%end
  21. #%option G_OPT_V_INPUT
  22. #% key: input
  23. #% label: GRASS table name
  24. #% required: yes
  25. #%end
  26. #%option G_OPT_F_OUTPUT
  27. #% key: output
  28. #% description: Output table file name or DB connection string
  29. #% required : yes
  30. #%end
  31. #%option G_OPT_V_FIELD
  32. #% required: no
  33. #%end
  34. #%option
  35. #% key: format
  36. #% type: string
  37. #% description: Table format
  38. #% required: yes
  39. #% options: CSV,DBF,GML,MySQL,PostgreSQL,SQLite
  40. #% answer: CSV
  41. #%end
  42. #%option
  43. #% key: table
  44. #% type: string
  45. #% key_desc: name
  46. #% description: Name for output table (default: input name)
  47. #% required: no
  48. #%end
  49. import sys
  50. import os
  51. from grass.script.utils import try_remove, basename
  52. from grass.script import core as grass
  53. from grass.exceptions import CalledModuleError
  54. def main():
  55. input = options['input']
  56. layer = options['layer']
  57. format = options['format']
  58. output = options['output']
  59. table = options['table']
  60. if format.lower() == 'dbf':
  61. format = "ESRI_Shapefile"
  62. if format.lower() == 'csv':
  63. olayer = basename(output, 'csv')
  64. else:
  65. olayer = None
  66. #is there a simpler way of testing for --overwrite?
  67. dbffile = input + '.dbf'
  68. if os.path.exists(dbffile) and not grass.overwrite():
  69. grass.fatal(_("File <%s> already exists") % dbffile)
  70. if olayer:
  71. try:
  72. grass.run_command('v.out.ogr', quiet=True, input=input, layer=layer,
  73. output=output,
  74. format=format, type='point,line,area',
  75. olayer=olayer)
  76. except CalledModuleError:
  77. grass.fatal(_("Module <%s> failed") % 'v.out.ogr')
  78. else:
  79. try:
  80. grass.run_command('v.out.ogr', quiet=True, input=input,
  81. layer=layer, output=output,
  82. format=format, type='point,line,area')
  83. except CalledModuleError:
  84. grass.fatal(_("Module <%s> failed") % 'v.out.ogr')
  85. if format == "ESRI_Shapefile":
  86. exts = ['shp', 'shx', 'prj']
  87. if output.endswith('.dbf'):
  88. outname = basename(output, 'dbf')
  89. for ext in exts:
  90. try_remove("%s.%s" % (outname, ext))
  91. outname += '.dbf'
  92. else:
  93. for ext in exts:
  94. try_remove(os.path.join(output, "%s.%s" % (input, ext)))
  95. outname = os.path.join(output, input + ".dbf")
  96. elif format.lower() == 'csv':
  97. outname = output + '.csv'
  98. else:
  99. outname = input
  100. grass.message(_("Exported table <%s>") % outname)
  101. if __name__ == "__main__":
  102. options, flags = grass.parser()
  103. main()