db.out.ogr.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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: output
  20. #% keyword: attribute table
  21. #%end
  22. #%option G_OPT_V_INPUT
  23. #% key: input
  24. #% label: GRASS table name
  25. #% required: yes
  26. #%end
  27. #%option G_OPT_F_OUTPUT
  28. #% key: output
  29. #% description: Output table file name or DB connection string
  30. #% required : yes
  31. #%end
  32. #%option G_OPT_V_FIELD
  33. #% required: no
  34. #%end
  35. #%option
  36. #% key: format
  37. #% type: string
  38. #% description: Table format
  39. #% required: yes
  40. #% options: CSV,DBF,GML,MySQL,PostgreSQL,SQLite
  41. #% answer: CSV
  42. #%end
  43. #%option
  44. #% key: table
  45. #% type: string
  46. #% key_desc: name
  47. #% description: Name for output table (default: input name)
  48. #% required: no
  49. #%end
  50. import os
  51. from grass.script.utils import try_remove, basename
  52. from grass.script import core as gcore
  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 gcore.overwrite():
  69. gcore.fatal(_("File <%s> already exists") % dbffile)
  70. if olayer:
  71. try:
  72. gcore.run_command('v.out.ogr', quiet=True, input=input,
  73. layer=layer, output=output, format=format,
  74. type='point,line,area', olayer=olayer)
  75. except CalledModuleError:
  76. gcore.fatal(_("Module <%s> failed") % 'v.out.ogr')
  77. else:
  78. try:
  79. gcore.run_command('v.out.ogr', quiet=True, input=input,
  80. layer=layer, output=output,
  81. format=format, type='point,line,area')
  82. except CalledModuleError:
  83. gcore.fatal(_("Module <%s> failed") % 'v.out.ogr')
  84. if format == "ESRI_Shapefile":
  85. exts = ['shp', 'shx', 'prj']
  86. if output.endswith('.dbf'):
  87. outname = basename(output, 'dbf')
  88. for ext in exts:
  89. try_remove("%s.%s" % (outname, ext))
  90. outname += '.dbf'
  91. else:
  92. for ext in exts:
  93. try_remove(os.path.join(output, "%s.%s" % (input, ext)))
  94. outname = os.path.join(output, input + ".dbf")
  95. elif format.lower() == 'csv':
  96. outname = output + '.csv'
  97. else:
  98. outname = input
  99. gcore.message(_("Exported table <%s>") % outname)
  100. if __name__ == "__main__":
  101. options, flags = gcore.parser()
  102. main()