db.in.ogr.py 3.3 KB

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