db.out.ogr.py 2.6 KB

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