Counter for identical registers in a field, using Scripting in gvSIG

If we have an attribute table in gvSIG we can obtain the number of registers for each different value of a field, how many times they are repeated.

For that we will have to create a new script in gvSIG, from the Tools->Scripting->Scripting Composer menu.

Once it is created, naming it as we want, we copy this source code on it:

from gvsig import *
import commonsdialog
from org.gvsig.andami import Utilities
import os, time

from gvsig import *

from import TableManager

def loadTable(format, **parameters): 

        application = ApplicationLocator.getManager()
        datamanager =  application.getDataManager()
        # Loding the data store
        store_parameters = datamanager.createStoreParameters(format)
        copyToDynObject(parameters, store_parameters)
        store = datamanager.openStore(format, store_parameters)       

        # Creating a Table document and initialize with the data store
        project = currentProject()
        table = project().createDocument(TableManager.TYPENAME)

        # Add the Table document to the project
    except Throwable, ex:
        raise RuntimeException("Can't load table, "+ str(ex)) 
    return table

def loadDBF(dbffile):
  table = loadTable(format="DBF",DbfFile=dbffile)
  return table

def getTempFile(name, ext):
    tempdir = Utilities.TEMPDIRECTORYPATH
    if not os.path.isdir(tempdir):
    t = time.time()
    f = os.path.join(
      "%s-%x%x%s" % (name,t,(t-int(t)) * 10000,ext)
    return f
def main(*args):
    print "Count values"
    table = currentTable()
    field = str(commonsdialog.inputbox("Name of the field"))
        features = table.features()
        commonsdialog.msgbox("Table not opened")
    count = dict()
    for f in features:
        ff = str(f.get(field))
        if ff in count.keys():
            count[ff] += 1
            count[ff] = 1
    print count
    sch = createSchema()
    dbf = createDBF(sch, getTempFile("count"+field,".dbf"))
    print type(dbf())
    print "full name: ", dbf.getFullName()
    #Insert table data

    for k,v in count.iteritems():
      f = dbf.createNewFeature()


    d = loadDBF( dbf.getFullName())
    print dbf.getFullName()

Finally we save the script.

Then from the View in gvSIG we put the layer that we want to use for the calculation active, and we open its attribute table. After that, we open the Scripting Launcher (Tools->Scripting->Scripting Launcher menu).

Double-clicking on the script that we had created, a new window will be opened, where we have to write the name of the field over which we can apply the counter. After accepting a new table will be created with the results. We will open the Project Manager (from Show menu), and we will have the new table at the “Table” document.


That table is a temporary element, so we will have to export it to a dbf file in order to have it on disk (Table->Export to menu).



This entry was posted in development, english, gvSIG Desktop, scripting. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s