db.out.ogr.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 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. #% keywords: database
  18. #% keywords: attribute table
  19. #%end
  20. #%option G_OPT_V_INPUT
  21. #% key: input
  22. #% required: yes
  23. #%end
  24. #%option G_OPT_F_OUTPUT
  25. #% key: dsn
  26. #% description: Table file to be exported or DB connection string
  27. #% required : yes
  28. #%end
  29. #%option G_OPT_V_FIELD
  30. #% required: no
  31. #%end
  32. #%option
  33. #% key: format
  34. #% type: string
  35. #% description: Table format
  36. #% required: yes
  37. #% options: CSV,DBF,GML,MySQL,PostgreSQL,SQLite
  38. #% answer: CSV
  39. #%end
  40. #%option
  41. #% key: table
  42. #% type: string
  43. #% key_desc: name
  44. #% description: Name for output table (defaut: input)
  45. #% required: no
  46. #%end
  47. import sys
  48. import os
  49. from grass.script.utils import try_remove, basename
  50. from grass.script import core as grass
  51. def main():
  52. input = options['input']
  53. layer = options['layer']
  54. format = options['format']
  55. dsn = options['dsn']
  56. table = options['table']
  57. if format.lower() == 'dbf':
  58. format = "ESRI_Shapefile"
  59. if format.lower() == 'csv':
  60. olayer = basename(dsn, 'csv')
  61. else:
  62. olayer = None
  63. #is there a simpler way of testing for --overwrite?
  64. dbffile = input + '.dbf'
  65. if os.path.exists(dbffile) and not grass.overwrite():
  66. grass.fatal(_("File <%s> already exists") % dbffile)
  67. if olayer:
  68. if grass.run_command('v.out.ogr', quiet = True, input = input, layer = layer,
  69. dsn = dsn,
  70. format = format, type = 'point,line,area', olayer = olayer) != 0:
  71. sys.exit(1)
  72. else:
  73. if grass.run_command('v.out.ogr', quiet = True, input = input, layer = layer,
  74. dsn = dsn, format = format, type = 'point,line,area') != 0:
  75. sys.exit(1)
  76. if format == "ESRI_Shapefile":
  77. exts = ['shp', 'shx', 'prj']
  78. if dsn.endswith('.dbf'):
  79. outname = basename(dsn, 'dbf')
  80. for ext in exts:
  81. try_remove("%s.%s" % (outname, ext))
  82. outname += '.dbf'
  83. else:
  84. for ext in exts:
  85. try_remove(os.path.join(dsn, "%s.%s" % (input, ext)))
  86. outname = os.path.join(dsn, input + ".dbf")
  87. elif format.lower() == 'csv':
  88. outname = dsn + '.csv'
  89. else:
  90. outname = input
  91. grass.message(_("Exported table <%s>") % outname)
  92. if __name__ == "__main__":
  93. options, flags = grass.parser()
  94. main()