db.in.ogr.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, 2008 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. #% keywords: database, attribute table
  18. #%End
  19. #%option
  20. #% key: dsn
  21. #% type: string
  22. #% key_desc : name
  23. #% gisprompt: old_file,file,input
  24. #% description: Table file to be imported or DB connection string
  25. #% required : yes
  26. #%end
  27. #%option
  28. #% key: db_table
  29. #% type: string
  30. #% key_desc : name
  31. #% description: Table name of SQL DB table
  32. #% required : no
  33. #%end
  34. #%option
  35. #% key: output
  36. #% type: string
  37. #% description: Name for output table
  38. #% required : no
  39. #%end
  40. #%option
  41. #% key: key
  42. #% type: string
  43. #% description: Name for auto-generated unique key column
  44. #% required : no
  45. #%end
  46. import sys
  47. import os
  48. import grass
  49. def main():
  50. dsn = options['dsn']
  51. db_table = options['db_table']
  52. output = options['output']
  53. key = options['key']
  54. mapset = grass.gisenv()['MAPSET']
  55. if db_table:
  56. input = db_table
  57. else:
  58. input = dsn
  59. if not output:
  60. tmpname = input.replace('.', '_')
  61. output = grass.basename(tmpname)
  62. if not grass.overwrite():
  63. s = grass.read_command('db.tables', flags = 'p')
  64. for l in s.splitlines():
  65. if l == output:
  66. grass.fatal("Table <%s> already exists" % output)
  67. else:
  68. grass.write_command('db.execute', stdin = "DROP TABLE %s" % output)
  69. # treat DB as real vector map...
  70. layer_opt = {}
  71. if db_table:
  72. layer_opt['layer'] = db_table
  73. if grass.run_command('v.in.ogr', flags = 'o', dsn = dsn, output = output,
  74. quiet = True, **layer_opt) != 0:
  75. if db_table:
  76. grass.fatal("Input table <%s> not found or not readable" % input)
  77. else:
  78. grass.fatal("Input DSN <%s> not found or not readable" % input)
  79. nuldev = file(os.devnull, 'w')
  80. # rename ID col if requested from cat to new name
  81. if key:
  82. grass.run_command('v.db.renamecol', quiet = True, map = output, layer = 1,
  83. column = (cat, key), stdout = nuldev, stderr = nuldev)
  84. # ... and immediately drop the empty geometry
  85. vectfile = grass.find_file(output, element = 'vector', mapset = mapset)['file']
  86. if not file:
  87. grass.fatal("Something went wrong. Should not happen")
  88. else:
  89. # remove the vector part
  90. grass.try_remove(file)
  91. # get rid of superfluous auto-added cat column (and cat_ if present)
  92. grass.run_command('db.dropcol', quiet = True, flags = 'f', table = output,
  93. colum = 'cat', stdout = nuldev, stderr = nuldev)
  94. s = grass.read_command('db.describe', flags = 'c', table = output)
  95. kv = grass.parse_key_val(s, sep = ':')
  96. records = int(kv['nrows'].strip())
  97. grass.message("Imported table <%s> with %d rows" % (output, records))
  98. if __name__ == "__main__":
  99. options, flags = grass.parser()
  100. main()