db.in.ogr.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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-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.exceptions import CalledModuleError
  56. # i18N
  57. import os
  58. import gettext
  59. gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
  60. def main():
  61. input = options['input']
  62. db_table = options['db_table']
  63. output = options['output']
  64. key = options['key']
  65. mapset = grass.gisenv()['MAPSET']
  66. if db_table:
  67. input = db_table
  68. if not output:
  69. tmpname = input.replace('.', '_')
  70. output = grass.basename(tmpname)
  71. # check if table exists
  72. try:
  73. nuldev = file(os.devnull, 'w+')
  74. s = grass.read_command('db.tables', flags='p', quiet=True, stderr=nuldev)
  75. nuldev.close()
  76. except CalledModuleError:
  77. # check connection parameters, set if uninitialized
  78. grass.read_command('db.connect', flags='c')
  79. s = grass.read_command('db.tables', flags='p', quiet=True)
  80. for l in s.splitlines():
  81. if l == output:
  82. if grass.overwrite():
  83. grass.warning(_("Table <%s> already exists and will be "
  84. "overwritten") % output)
  85. grass.write_command('db.execute', input='-',
  86. stdin="DROP TABLE %s" % output)
  87. break
  88. else:
  89. grass.fatal(_("Table <%s> already exists") % output)
  90. # treat DB as real vector map...
  91. layer = db_table if db_table else None
  92. vopts = {}
  93. if options['encoding']:
  94. vopts['encoding'] = options['encoding']
  95. try:
  96. grass.run_command('v.in.ogr', flags='o', input=input, output=output,
  97. layer=layer, quiet=True, **vopts)
  98. except CalledModuleError:
  99. if db_table:
  100. grass.fatal(
  101. _("Input table <%s> not found or not readable") %
  102. input)
  103. else:
  104. grass.fatal(_("Input DSN <%s> not found or not readable") % input)
  105. # rename ID col if requested from cat to new name
  106. if key:
  107. grass.write_command('db.execute', quiet=True, input='-',
  108. stdin="ALTER TABLE %s ADD COLUMN %s integer" %
  109. (output, key))
  110. grass.write_command('db.execute', quiet=True, input='-',
  111. stdin="UPDATE %s SET %s=cat" % (output, key))
  112. # ... and immediately drop the empty geometry
  113. vectfile = grass.find_file(output, element='vector', mapset=mapset)['file']
  114. if not vectfile:
  115. grass.fatal(_("Something went wrong. Should not happen"))
  116. else:
  117. # remove the vector part
  118. grass.run_command('v.db.connect', quiet=True, map=output, layer='1',
  119. flags='d')
  120. grass.run_command('g.remove', flags='f', quiet=True, type='vector',
  121. name=output)
  122. # get rid of superfluous auto-added cat column (and cat_ if present)
  123. nuldev = file(os.devnull, 'w+')
  124. grass.run_command('db.dropcolumn', quiet=True, flags='f', table=output,
  125. column='cat', stdout=nuldev, stderr=nuldev)
  126. nuldev.close()
  127. records = grass.db_describe(output)['nrows']
  128. grass.message(_("Imported table <%s> with %d rows") % (output, records))
  129. if __name__ == "__main__":
  130. options, flags = grass.parser()
  131. main()