Skip to content

Commit

Permalink
Merge pull request #3 from mundialis/v_example_attributes
Browse files Browse the repository at this point in the history
add examples for fns in vector.py and db.py
  • Loading branch information
metzm authored Mar 12, 2024
2 parents 5e6e3b7 + a551546 commit c7f1877
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ exclude = .git
max-line-length = 80

per-file-ignores =
./v.example.py: F821
./v.example.py: F821, E501
107 changes: 105 additions & 2 deletions v.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,31 @@

# %option
# % key: box
# % key_desc: width,height
# % type: double
# % required: yes
# % multiple: yes
# % multiple: no
# % description: Width and height of boxes in grid
# %end

# %option
# %option G_OPT_V_INPUT
# % key: polygon_aoi
# % required: no
# % description: Polygon to extract grid-tiles for
# %end

# %option G_OPT_V_FIELD
# %end

# %option G_OPT_DB_COLUMN
# %end

# %flag
# % key: p
# % description: Print attribute values of given column for input vector polygon_aoi
# %end


# import needed libraries
import atexit
import os
Expand All @@ -55,6 +69,7 @@
rm_vec = []


# cleanup function (can be extended)
def cleanup():
"""Cleanup fuction (can be extended)"""
nuldev = open(os.devnull, "w", encoding="utf-8")
Expand All @@ -68,6 +83,94 @@ def main():
"""Main function of v.example"""
global rm_vec

# print attribute values if requested
if flags["p"] and options["polygon_aoi"] and options["column"]:
aoi_vector = options["polygon_aoi"]
layer = options["layer"]
column = options["column"]

# examples for interfaces in
# python/grass/script/vector.py
# python/grass/script/db.py

# get all database connections as dictionary
db_connections = grass.vector_db(aoi_vector)
if len(db_connections) == 0:
grass.fatal(_("No database connections for map %s") % aoi_vector)

# check if there is an attribute table at the given layer
# grass.vector_layer_db() will exit with fatal error if there is
# no database connection for the given layer
db_connection = grass.vector_layer_db(aoi_vector, layer)

# get attribute columns as dictionary (default)
columns = grass.vector_columns(aoi_vector, layer, getDict=True)
if column not in columns.keys():
grass.fatal(
_("Column %s does not exist in layer %s of vector %s")
% (column, layer, aoi_vector)
)

# get attribute columns as list
columns = grass.vector_columns(aoi_vector, layer, getDict=False)
colidx = -1
try:
colidx = columns.index(column)
except ValueError:
grass.fatal(
_("Column %s does not exist in layer %s of vector %s")
% (column, layer, aoi_vector)
)

if colidx >= 0:
grass.verbose(
_("Found column %s in vector %s, layer %s")
% (column, aoi_vector, layer)
)

# query the database and table directly using information in the
# database connection of the vector
database = db_connection["database"]
table = db_connection["table"]
driver = db_connection["driver"]

table_description = grass.db_describe(
table=table, database=database, driver=driver
)
found = False
# TODO: pythonize this for loop
for i in range(len(table_description["cols"])):
column_description = table_description["cols"][i]
if column_description[0] == column:
found = True
break

if found is False:
grass.fatal(
_("Column %s does not exist in layer %s of vector %s")
% (column, layer, aoi_vector)
)

# select attribute values with vector_db_select()
grass.message(
_("Print attribute values using %s") % "vector_db_select()"
)
column_values = grass.vector_db_select(
aoi_vector, int(layer), columns=column
)
# go over table rows
for key in column_values["values"]:
# print value of first selected column
print(column_values["values"][key][0])

# select attribute values with SQL statement
grass.message(_("Print attribute values using %s") % "db_select()")
sql = f"select {column} from {table}"
values = grass.db_select(sql=sql, database=database, driver=driver)
for value in values:
print(value[0])

# use process ID as suffix for unique names of temporary maps
pid = os.getpid()

# set grid
Expand Down

0 comments on commit c7f1877

Please sign in to comment.