attributes.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. Attributes
  2. ===========
  3. It is possible to access to the vector attributes with: ::
  4. >>> from pygrass.vector import Vector
  5. >>> municip = Vector('boundary_municp_sqlite')
  6. >>> municip.open()
  7. >>> municip.dblinks
  8. DBlinks([[Link(1, boundary_municp, sqlite)]])
  9. The vector map have a ``table`` attributes that contain a Table object, that
  10. have some useful attributes like: layer, name, driver, etc.
  11. >>> link = municip.dblinks[1]
  12. >>> link.number
  13. 1
  14. >>> link.name
  15. 'boundary_municp'
  16. >>> link.table_name
  17. 'boundary_municp_sqlite'
  18. >>> link.driver
  19. 'sqlite'
  20. >>> link.database # doctest: +ELLIPSIS
  21. '.../sqlite.db'
  22. >>> link.key
  23. 'cat'
  24. It is possible to change values, like: ::
  25. >>> link.name = 'boundary'
  26. >>> link.driver = 'pg'
  27. >>> link.database = 'host=localhost,dbname=grassdb'
  28. >>> link.key = 'gid'
  29. Now change again to old values: ::
  30. >>> link.name = 'boundary_municp_sqlite'
  31. >>> link.driver = 'sqlite'
  32. >>> link.database = '$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db'
  33. >>> link.key = 'cat'
  34. Link object have methods that return a
  35. :ref:`Connection object <python:library:sqlite3:connection-objects>`, and to
  36. return a Table object: ::
  37. >>> conn = link.connection()
  38. >>> cur = conn.cursor()
  39. >>> import sql
  40. >>> cur.execute(sql.SELECT.format(cols=', '.join(['cat', 'AREA']),
  41. ... tname=link.name)) # doctest: +ELLIPSIS
  42. <sqlite3.Cursor object at ...>
  43. >>> cur.fetchone()
  44. (1, 0.0)
  45. >>> cur.close()
  46. >>> conn.close()
  47. From the Link object we can instantiate a Table object that allow user to
  48. make simple query with the Filters object: ::
  49. >>> table = link.table()
  50. >>> table.filters.select('cat', 'COUNTY',
  51. ... 'AREA','PERIMETER').order_by('AREA').limit(3)
  52. Filters('SELECT cat, COUNTY, AREA, PERIMETER FROM boundary_municp_sqlite ORDER BY AREA LIMIT 3;')
  53. >>> cur = table.execute()
  54. >>> for row in cur.fetchall():
  55. ... print repr(row)
  56. ...
  57. (1, u'SURRY', 0.0, 1415.331)
  58. (2, u'SURRY', 0.0, 48286.011)
  59. (3, u'CASWELL', 0.0, 5750.087)
  60. Then we can get table information about table columns, from the columns
  61. attribute that is an instantiation of a Columns class.
  62. >>> table.columns # doctest: +ELLIPSIS
  63. Columns([(u'cat', u'integer'), ..., (u'ACRES', u'double precision')])
  64. >>> table.columns.names() # doctest: +ELLIPSIS
  65. [u'cat', u'OBJECTID', u'AREA', u'PERIMETER', ..., u'ACRES']
  66. >>> table.columns.types() # doctest: +ELLIPSIS
  67. [u'integer', u'integer', u'double precision', ..., u'double precision']
  68. .. note ::
  69. If the map use postgresql it is possible to: add/rename/cast/remove columns
  70. the sqlite does not support these operations.
  71. For people that are used to the standardized Python SQL 2.0 interface:
  72. * http://docs.python.org/library/sqlite3.html
  73. * http://www.python.org/dev/peps/pep-0249/
  74. Therefore advanced user can just use, the connect attribute to build
  75. a new cursor object and interact with the database. ::
  76. >>> cur = table.conn.cursor()
  77. >>> cur.execute("SELECT * FROM %s" % table.name) # doctest: +ELLIPSIS
  78. <sqlite3.Cursor object at ...>
  79. >>> cur.fetchone()[:5] # doctest: +ELLIPSIS
  80. (1, 1, 0.0, 1415.331, 2.0)
  81. >>> # Close communication with the database
  82. >>> cur.close()
  83. >>> conn.close()
  84. Link
  85. -------
  86. .. autoclass:: pygrass.vector.table.Link
  87. :members:
  88. DBlinks
  89. -------
  90. .. autoclass:: pygrass.vector.table.DBlinks
  91. :members:
  92. Filters
  93. -------
  94. .. autoclass:: pygrass.vector.table.Filters
  95. :members:
  96. Columns
  97. -------
  98. .. autoclass:: pygrass.vector.table.Columns
  99. :members:
  100. Table
  101. -----
  102. .. autoclass:: pygrass.vector.table.Table
  103. :members: