153GIS2 - 10. cvičení - GRASS GIS

Z GeoWikiCZ

Stránka obsahuje ukázky skriptů v Pythonu pro GRASS GIS z 10. cvičení GIS2. Viz také Skriptování v GRASS GIS.

1

from grass.script import vector as grass

for nazev in grass.vector_columns(map = 'obce').keys():
    print nazev

2

import grass.script as grass
 
select = grass.vector_db_select(map = "obce",
                                where = "NAZEV = 'Liberec'")

idx = select['columns'].index('OB01')
ob01 = 0
for record in select['values'].itervalues():
    ob01 += int(record[idx])

nazev_idx = select['columns'].index('NAZEV')
for record in grass.vector_db_select(map = "obce",
                                     where = "OB01 > %d" % ob01)['values'].itervalues():
    print unicode(record[nazev_idx], 'latin2').ljust(15), record[idx]

3

import sys
import grass.script as grass

input = sys.argv[1]     # String - nazev obce
output = sys.argv[2]    # cesta k textovemu souboru

select = grass.vector_db_select(map = "obce",
                                where = "NAZEV = 'Liberec'")

idx = select['columns'].index('OB01')
ob01 = 0
for record in select['values'].itervalues():
    ob01 += int(record[idx])

nazev_idx = select['columns'].index('NAZEV')
f = open(output, 'w')
try:
    for record in grass.vector_db_select(map = "obce",
                                         where = "OB01 > %d" % ob01)['values'].itervalues():
        f.write("%s%s\n" % (record[nazev_idx].ljust(15), record[idx]))
finally:
    f.close()

4

import grass.script as grass

for record in grass.vector_db_select(map = 'kraje',
                                     columns = "NAZEV_ENG")['values'].itervalues():
    val = unicode(record[0], 'ascii')
    print val
    grass.run_command('v.extract',
                      quiet = True,
                      overwrite = True,
                      input = 'kraje',
                      output = 'kraje_' + val,
                      where = "NAZEV_ENG = '%s'" % val)