db.in.ogr.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python3
  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-2016 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. #% overwrite: yes
  21. #%End
  22. #%option G_OPT_F_BIN_INPUT
  23. #% description: Table file to be imported or DB connection string
  24. #%end
  25. #%option
  26. #% key: db_table
  27. #% type: string
  28. #% key_desc : name
  29. #% description: Name of table from given DB to be imported
  30. #% required : no
  31. #%end
  32. #%option G_OPT_DB_TABLE
  33. #% key: output
  34. #% description: Name for output table
  35. #% required : no
  36. #% guisection: Output
  37. #% gisprompt: new,dbtable,dbtable
  38. #%end
  39. #%option
  40. #% key: key
  41. #% type: string
  42. #% description: Name for auto-generated unique key column
  43. #% required : no
  44. #% guisection: Output
  45. #%end
  46. #%option
  47. #% key: encoding
  48. #% type: string
  49. #% label: Encoding value for attribute data
  50. #% descriptions: Overrides encoding interpretation, useful when importing DBF tables
  51. #% guisection: Output
  52. #%end
  53. import os
  54. import grass.script as grass
  55. from grass.script.utils import decode
  56. from grass.exceptions import CalledModuleError
  57. def main():
  58. input = options['input']
  59. db_table = options['db_table']
  60. output = options['output']
  61. key = options['key']
  62. mapset = grass.gisenv()['MAPSET']
  63. if db_table:
  64. input = db_table
  65. if not output:
  66. tmpname = input.replace('.', '_')
  67. output = grass.basename(tmpname)
  68. # check if table exists
  69. try:
  70. nuldev = open(os.devnull, 'w+')
  71. s = grass.read_command('db.tables', flags='p', quiet=True, stderr=nuldev)
  72. nuldev.close()
  73. except CalledModuleError:
  74. # check connection parameters, set if uninitialized
  75. grass.read_command('db.connect', flags='c')
  76. s = grass.read_command('db.tables', flags='p', quiet=True)
  77. for l in decode(s).splitlines():
  78. if l == output:
  79. if grass.overwrite():
  80. grass.warning(_("Table <%s> already exists and will be "
  81. "overwritten") % output)
  82. grass.write_command('db.execute', input='-',
  83. stdin="DROP TABLE %s" % output)
  84. break
  85. else:
  86. grass.fatal(_("Table <%s> already exists") % output)
  87. # treat DB as real vector map...
  88. layer = db_table if db_table else None
  89. vopts = {}
  90. if options['encoding']:
  91. vopts['encoding'] = options['encoding']
  92. try:
  93. grass.run_command('v.in.ogr', flags='o', input=input, output=output,
  94. layer=layer, quiet=True, **vopts)
  95. except CalledModuleError:
  96. if db_table:
  97. grass.fatal(
  98. _("Input table <%s> not found or not readable") %
  99. input)
  100. else:
  101. grass.fatal(_("Input DSN <%s> not found or not readable") % input)
  102. # rename ID col if requested from cat to new name
  103. if key:
  104. grass.write_command('db.execute', quiet=True, input='-',
  105. stdin="ALTER TABLE %s ADD COLUMN %s integer" %
  106. (output, key))
  107. grass.write_command('db.execute', quiet=True, input='-',
  108. stdin="UPDATE %s SET %s=cat" % (output, key))
  109. # ... and immediately drop the empty geometry
  110. vectfile = grass.find_file(output, element='vector', mapset=mapset)['file']
  111. if not vectfile:
  112. grass.fatal(_("Something went wrong. Should not happen"))
  113. else:
  114. # remove the vector part
  115. grass.run_command('v.db.connect', quiet=True, map=output, layer='1',
  116. flags='d')
  117. grass.run_command('g.remove', flags='f', quiet=True, type='vector',
  118. name=output)
  119. # get rid of superfluous auto-added cat column (and cat_ if present)
  120. nuldev = open(os.devnull, 'w+')
  121. grass.run_command('db.dropcolumn', quiet=True, flags='f', table=output,
  122. column='cat', stdout=nuldev, stderr=nuldev)
  123. nuldev.close()
  124. records = grass.db_describe(output)['nrows']
  125. grass.message(_("Imported table <%s> with %d rows") % (output, records))
  126. if __name__ == "__main__":
  127. options, flags = grass.parser()
  128. main()