db.in.ogr.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: db.in.ogr
  5. # AUTHOR(S): Markus Neteler
  6. # PURPOSE: imports attribute tables in various formats
  7. # Converted to Python by Glynn Clements
  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: Imports attribute tables in various formats.
  17. #% keyword: database
  18. #% keyword: import
  19. #% keyword: attribute table
  20. #%End
  21. #%option G_OPT_F_BIN_INPUT
  22. #% description: Table file to be imported or DB connection string
  23. #%end
  24. #%option
  25. #% key: db_table
  26. #% type: string
  27. #% key_desc : name
  28. #% description: Name of table from given DB to be imported
  29. #% required : no
  30. #%end
  31. #%option
  32. #% key: output
  33. #% type: string
  34. #% key_desc : name
  35. #% description: Name for output table
  36. #% required : no
  37. #%end
  38. #%option
  39. #% key: key
  40. #% type: string
  41. #% description: Name for auto-generated unique key column
  42. #% required : no
  43. #%end
  44. import os
  45. import grass.script as grass
  46. from grass.exceptions import CalledModuleError
  47. def main():
  48. input = options['input']
  49. db_table = options['db_table']
  50. output = options['output']
  51. key = options['key']
  52. mapset = grass.gisenv()['MAPSET']
  53. if db_table:
  54. input = db_table
  55. if not output:
  56. tmpname = input.replace('.', '_')
  57. output = grass.basename(tmpname)
  58. if not grass.overwrite():
  59. s = grass.read_command('db.tables', flags = 'p')
  60. for l in s.splitlines():
  61. if l == output:
  62. grass.fatal(_("Table <%s> already exists") % output)
  63. else:
  64. grass.write_command('db.execute', input = '-', stdin = "DROP TABLE %s" % output)
  65. # treat DB as real vector map...
  66. if db_table:
  67. layer = db_table
  68. else:
  69. layer = None
  70. try:
  71. grass.run_command('v.in.ogr', flags='o', input=input, output=output,
  72. layer=layer, quiet=True)
  73. except CalledModuleError:
  74. if db_table:
  75. grass.fatal(_("Input table <%s> not found or not readable") % input)
  76. else:
  77. grass.fatal(_("Input DSN <%s> not found or not readable") % input)
  78. # rename ID col if requested from cat to new name
  79. if key:
  80. grass.write_command('db.execute', quiet = True,
  81. input = '-',
  82. stdin = "ALTER TABLE %s ADD COLUMN %s integer" % (output, key) )
  83. grass.write_command('db.execute', quiet = True,
  84. input = '-',
  85. stdin = "UPDATE %s SET %s=cat" % (output, key) )
  86. # ... and immediately drop the empty geometry
  87. vectfile = grass.find_file(output, element = 'vector', mapset = mapset)['file']
  88. if not vectfile:
  89. grass.fatal(_("Something went wrong. Should not happen"))
  90. else:
  91. # remove the vector part
  92. grass.run_command('v.db.connect', quiet = True, map = output, layer = '1', flags = 'd')
  93. grass.run_command('g.remove', flags = 'f', quiet = True, type = 'vector', name = output)
  94. # get rid of superfluous auto-added cat column (and cat_ if present)
  95. nuldev = file(os.devnull, 'w+')
  96. grass.run_command('db.dropcolumn', quiet = True, flags = 'f', table = output,
  97. column = 'cat', stdout = nuldev, stderr = nuldev)
  98. nuldev.close()
  99. records = grass.db_describe(output)['nrows']
  100. grass.message(_("Imported table <%s> with %d rows") % (output, records))
  101. if __name__ == "__main__":
  102. options, flags = grass.parser()
  103. main()