vector_map = 'landcover_corine'

value_field = 'value'

output_filename = ""

"""
Explanation: Get unique values
This notebook gets the unique values from a specified field in a GRASS vector map.
This notebook uses GRASS GIS (7.0.4), and must be run inside of a GRASS environment (start the jupyter notebook server from the GRASS command line).
<a href="https://grass.osgeo.org">GRASS GIS</a>
Required packages
<a href="https://github.com/numpy/numpy">numpy</a> <br />
<a href="https://github.com/pydata/pandas">pandas</a> <br />
Variable declarations
vector_map – GRASS vector map to get unique values from <br />
value_field – field in 'vector_map' to get unique values from <br />
output_filename – path to export a list of unique values in csv format
End of explanation
"""


import pandas
import numpy as np

import grass.script as gscript
from grass.pygrass.vector import VectorTopo
from grass.pygrass.vector.table import DBlinks

"""
Explanation: Import statements
End of explanation
"""


def connectToAttributeTable(map):
    vector = VectorTopo(map)
    vector.open(mode='r')
    dblinks = DBlinks(vector.c_mapinfo)
    link = dblinks[0]
    return link.table()

"""
Explanation: Function declarations
connect to attribute table
End of explanation
"""


table = connectToAttributeTable(map=vector_map)
table.filters.select(value_field)
cursor = table.execute()
result = np.array(cursor.fetchall())
cursor.close()

"""
Explanation: Get unique values
query to attribute table
End of explanation
"""


data = np.unique(result)

data

"""
Explanation: get unique values
End of explanation
"""


np.savetxt(output_filename, data)

"""
Explanation: export unique values to csv
End of explanation
"""


