db.out.ogr.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python3
  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-2014 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. # % keyword: database
  18. # % keyword: export
  19. # % keyword: output
  20. # % keyword: attribute table
  21. # %end
  22. # %option G_OPT_V_INPUT
  23. # % key: input
  24. # % label: GRASS table name
  25. # % required: yes
  26. # %end
  27. # %option G_OPT_F_OUTPUT
  28. # % key: output
  29. # % description: Output table file name or DB connection string
  30. # % required : yes
  31. # %end
  32. # %option G_OPT_V_FIELD
  33. # % required: no
  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: CSV
  42. # %end
  43. # %option
  44. # % key: table
  45. # % type: string
  46. # % key_desc: name
  47. # % description: Name for output table (default: input name)
  48. # % required: no
  49. # %end
  50. import os
  51. from grass.script.utils import try_remove, basename
  52. from grass.script import core as gcore
  53. from grass.exceptions import CalledModuleError
  54. def main():
  55. input = options["input"]
  56. layer = options["layer"]
  57. format = options["format"]
  58. output = options["output"]
  59. table = options["table"]
  60. if format.lower() == "dbf":
  61. format = "ESRI_Shapefile"
  62. if format.lower() == "csv":
  63. olayer = basename(output, "csv")
  64. else:
  65. olayer = None
  66. # is there a simpler way of testing for --overwrite?
  67. dbffile = input + ".dbf"
  68. if os.path.exists(dbffile) and not gcore.overwrite():
  69. gcore.fatal(_("File <%s> already exists") % dbffile)
  70. if olayer:
  71. try:
  72. gcore.run_command(
  73. "v.out.ogr",
  74. quiet=True,
  75. input=input,
  76. layer=layer,
  77. output=output,
  78. format=format,
  79. type="point,line,area",
  80. olayer=olayer,
  81. )
  82. except CalledModuleError:
  83. gcore.fatal(_("Module <%s> failed") % "v.out.ogr")
  84. else:
  85. try:
  86. gcore.run_command(
  87. "v.out.ogr",
  88. quiet=True,
  89. input=input,
  90. layer=layer,
  91. output=output,
  92. format=format,
  93. type="point,line,area",
  94. )
  95. except CalledModuleError:
  96. gcore.fatal(_("Module <%s> failed") % "v.out.ogr")
  97. if format == "ESRI_Shapefile":
  98. exts = ["shp", "shx", "prj"]
  99. if output.endswith(".dbf"):
  100. outname = basename(output, "dbf")
  101. for ext in exts:
  102. try_remove("%s.%s" % (outname, ext))
  103. outname += ".dbf"
  104. else:
  105. for ext in exts:
  106. try_remove(os.path.join(output, "%s.%s" % (input, ext)))
  107. outname = os.path.join(output, input + ".dbf")
  108. elif format.lower() == "csv":
  109. outname = output + ".csv"
  110. else:
  111. outname = input
  112. gcore.message(_("Exported table <%s>") % outname)
  113. if __name__ == "__main__":
  114. options, flags = gcore.parser()
  115. main()