[GRASS-user] sql join in v.extract

Tyler Smith tyler at plantarum.ca
Tue Jun 21 14:18:13 PDT 2016


To answer my own questions:

> 1. Is there a way to use a complex sql statement (i.e., that includes a
> join directive) in v.extract?

Maybe not?

> 2. If not, is the most sensible approach here to collect the output of
> db.select in a python script and use that to generate the corresponding
> where directive? i.e., where='(cat = 391598) OR (cat=77652) OR ...'?

Not sure if this is the best approach, but as a Python newbie it turned
out to be pretty simple, especially after I realized sqlite has an 'in'
operator:

  import grass.script as gscript
  gscript.run_command('g.region', vector='UTM_1KM_GRID')

  ## collect the keys from the sql table:
  grids = gscript.parse_command('db.select',
                              sql=
                              "SELECT SQUARE1KM FROM EOs WHERE SCI_NAME
                              = 'Carex folliculata'").keys()

  ## insert the keys into the query string for the shapefile:
  gscript.run_command('v.extract', input='UTM_1KM_GRID',
                    output='c_folliculata',
                    where='ATLAS_83 in (\'%s\')' % '\', \''.join(grids))

Best,

Tyler


More information about the grass-user mailing list