db.out.ogr.py 2.5 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
  21. #% key: input
  22. #% type: string
  23. #% key_desc : name
  24. #% description: GRASS table name
  25. #% required : yes
  26. #%end
  27. #%option
  28. #% key: dsn
  29. #% type: string
  30. #% key_desc : name
  31. #% gisprompt: new_file,file,input
  32. #% description: Table file to be exported or DB connection string
  33. #% required : yes
  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: DBF
  42. #%end
  43. #%option
  44. #% key: db_table
  45. #% type: string
  46. #% key_desc : name
  47. #% description: Name for output table
  48. #% required : no
  49. #%end
  50. import sys
  51. import os
  52. from grass.script import core as grass
  53. def main():
  54. input = options['input']
  55. format = options['format']
  56. dsn = options['dsn']
  57. table = options['db_table']
  58. if format.lower() == 'dbf':
  59. format = "ESRI_Shapefile"
  60. if format.lower() == 'csv':
  61. olayer = grass.basename(dsn, 'csv')
  62. else:
  63. olayer = None
  64. #is there a simpler way of testing for --overwrite?
  65. dbffile = input + '.dbf'
  66. if os.path.exists(dbffile) and not grass.overwrite():
  67. grass.fatal(_("File <%s> already exists") % dbffile)
  68. if grass.run_command('v.out.ogr', quiet = True, input = input, dsn = dsn,
  69. format = format, type = 'point', olayer = olayer) != 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. try_remove("%s.%s" % (outname, ext))
  77. outname += '.dbf'
  78. else:
  79. for ext in exts:
  80. 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()