db.out.ogr.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_DB_TABLE
  21. #% key: input
  22. #% required: yes
  23. #%end
  24. #%option
  25. #% key: dsn
  26. #% type: string
  27. #% key_desc: name
  28. #% gisprompt: new_file,file,input
  29. #% description: Table file to be exported or DB connection string
  30. #% required : yes
  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: DBF
  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. format = options['format']
  53. dsn = options['dsn']
  54. table = options['table']
  55. if format.lower() == 'dbf':
  56. format = "ESRI_Shapefile"
  57. if format.lower() == 'csv':
  58. olayer = grass.basename(dsn, 'csv')
  59. else:
  60. olayer = None
  61. #is there a simpler way of testing for --overwrite?
  62. dbffile = input + '.dbf'
  63. if os.path.exists(dbffile) and not grass.overwrite():
  64. grass.fatal(_("File <%s> already exists") % dbffile)
  65. if olayer:
  66. if grass.run_command('v.out.ogr', quiet = True, input = input, dsn = dsn,
  67. format = format, type = 'point', olayer = olayer) != 0:
  68. sys.exit(1)
  69. else:
  70. if grass.run_command('v.out.ogr', quiet = True, input = input, dsn = dsn,
  71. format = format, type = 'point') != 0:
  72. sys.exit(1)
  73. if format == "ESRI_Shapefile":
  74. exts = ['shp', 'shx', 'prj']
  75. if dsn.endswith('.dbf'):
  76. outname = grass.basename(dsn, 'dbf')
  77. for ext in exts:
  78. grass.try_remove("%s.%s" % (outname, ext))
  79. outname += '.dbf'
  80. else:
  81. for ext in exts:
  82. grass.try_remove(os.path.join(dsn, "%s.%s" % (input, ext)))
  83. outname = os.path.join(dsn, input + ".dbf")
  84. elif format.lower() == 'csv':
  85. outname = dsn + '.csv'
  86. else:
  87. outname = input
  88. grass.message(_("Exported table <%s>") % outname)
  89. if __name__ == "__main__":
  90. options, flags = grass.parser()
  91. main()