db.out.ogr.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 import core as grass
  50. def main():
  51. input = options['input']
  52. layer = options['layer']
  53. format = options['format']
  54. dsn = options['dsn']
  55. table = options['table']
  56. if format.lower() == 'dbf':
  57. format = "ESRI_Shapefile"
  58. if format.lower() == 'csv':
  59. olayer = grass.basename(dsn, 'csv')
  60. else:
  61. olayer = None
  62. #is there a simpler way of testing for --overwrite?
  63. dbffile = input + '.dbf'
  64. if os.path.exists(dbffile) and not grass.overwrite():
  65. grass.fatal(_("File <%s> already exists") % dbffile)
  66. if olayer:
  67. if grass.run_command('v.out.ogr', quiet = True, input = input, layer = layer,
  68. dsn = dsn,
  69. format = format, type = 'point,line,area', olayer = olayer) != 0:
  70. sys.exit(1)
  71. else:
  72. if grass.run_command('v.out.ogr', quiet = True, input = input, layer = layer,
  73. dsn = dsn, format = format, type = 'point,line,area') != 0:
  74. sys.exit(1)
  75. if format == "ESRI_Shapefile":
  76. exts = ['shp', 'shx', 'prj']
  77. if dsn.endswith('.dbf'):
  78. outname = grass.basename(dsn, 'dbf')
  79. for ext in exts:
  80. grass.try_remove("%s.%s" % (outname, ext))
  81. outname += '.dbf'
  82. else:
  83. for ext in exts:
  84. grass.try_remove(os.path.join(dsn, "%s.%s" % (input, ext)))
  85. outname = os.path.join(dsn, input + ".dbf")
  86. elif format.lower() == 'csv':
  87. outname = dsn + '.csv'
  88. else:
  89. outname = input
  90. grass.message(_("Exported table <%s>") % outname)
  91. if __name__ == "__main__":
  92. options, flags = grass.parser()
  93. main()