[GRASS-user] SQL query in python scripts using v.to.rast

Dear all
i have a vector (downloaded from OpenStreetmap) with all the streets in my study area. Streets are divided in categories (there is a specific string column in the db). now i want to convert to raster each category separately. I now how to do that separately, but i was not able to automate that in a python script. here it is what i wrote:

**>>>**import grass.script as gscript
**>>>**types = (“primary” , “secondary” , “tertiary” , “service” , “pedestrian” , “footway” , “residential” , “path”)
**>>>**for typ in types:
>>>> gscript.run_command (“v.to.rast” , input = “road_clip” , type = “line” , where =“type”=typ , output = types, use = “attr” , attribute_column = “cat”).

I got the following traceback: File
“/home/alessandro/GRASSDATA/5_citta_def/5_citt_def/.tmp
/alessandro-Lenovo-G50-70/5500.6.py”, line 6
gscript.run_command (“v.to.rast” , input = “road_clip” ,
type = “line” , where =“type”=typ , output = types, use =
“attr” , attribute_column = “cat”)
^
SyntaxError: invalid syntax

i guess the syntax error is in the way i set the query, but i’m not sure. Thanks to everybody,

Hi Alessandro,
seems you oversaw a typo:

   gscript.run_command ("v.to.rast" , input = "road_clip" , type = "line" , where ="type"=typ , output = types, use = "attr" , attribute_column = "cat")

where ="type"=typ schould rather be where ="type=%s"%typ

Regards.

Stefan

Alessandro Sebastiani <alessandro.sebastiani@uniroma1.it> hat am 9. November 2018 um 12:49 geschrieben:

Dear all
i have a vector (downloaded from OpenStreetmap) with all the streets in my
study area. Streets are divided in categories (there is a specific string
column in the db). now i want to convert to raster each category
separately. I now how to do that separately, but i was not able to automate
that in a python script. here it is what i wrote:
*>>>*import grass.script as gscript
*>>>*types = ("primary" , "secondary" , "tertiary" , "service" ,
"pedestrian" , "footway" , "residential" , "path")
*>>>*for typ in types:
*>>>> * gscript.run_command ("v.to.rast" , input = "road_clip" , type =
"line" , where ="type"=typ , output = types, use = "attr" ,
attribute_column = "cat").
*I got the following traceback:* File
"/home/alessandro/GRASSDATA/5_citta_def/5_citt_def/.tmp
/alessandro-Lenovo-G50-70/5500.6.py", line 6
    gscript.run_command ("v.to.rast" , input = "road_clip" ,
type = "line" , where ="type"=typ , output = types, use =
"attr" , attribute_column = "cat")
                              ^
SyntaxError: invalid syntax

i guess the syntax error is in the way i set the query, but i'm not sure.
Thanks to everybody,
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

I would do as follows:

(Notice that each street type will become a separate raster)

···

On 11/9/18 1:49 PM, Alessandro Sebastiani wrote:

Dear all
i have a vector (downloaded from OpenStreetmap) with all the streets in my study area. Streets are divided in categories (there is a specific string column in the db). now i want to convert to raster each category separately. I now how to do that separately, but i was not able to automate that in a python script. here it is what i wrote:

**>>>**import grass.script as gscript
**>>>**types = (“primary” , “secondary” , “tertiary” , “service” , “pedestrian” , “footway” , “residential” , “path”)
**>>>**for typ in types:

where_str = (“type = %s” % typ)

outrast = “_”.join([“type”, typ])

gscript.run_command(“v.to.rast”,

input = “road_clip”, type = “line”, where = where_str,

output = outrast, use=“attr”, attribute_column = “cat” )

>>>> gscript.run_command (“v.to.rast” , input = “road_clip” , type = “line” , where =“type”=typ , output = types, use = “attr” , attribute_column = “cat”).

I got the following traceback: File
“/home/alessandro/GRASSDATA/5_citta_def/5_citt_def/.tmp
/alessandro-Lenovo-G50-70/5500.6.py”, line 6
gscript.run_command (“v.to.rast” , input = “road_clip” ,
type = “line” , where =“type”=typ , output = types, use =
“attr” , attribute_column = “cat”)
^
SyntaxError: invalid syntax

i guess the syntax error is in the way i set the query, but i’m not sure. Thanks to everybody,

_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)
-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

Hello to everybody ,
i tried with this piece of code, following suggestios by both:

note that for the moment i reduced the street types to 3

types = (“primary” , “secondary” , “tertiary”)
for typ in types:

gscript.run_command (“v.to.rast” , input = “road_clip” , type = “line” , where = “type=%s” % typ , output = (“r”+typ) , use = “attr” , attribute_column = “cat”)

nevertheless, i still get the following error message

DBMI-SQLite driver error:
Error in sqlite3_prepare():
SELECT cat FROM road_clip WHERE type=primary
near “primary”: syntax error
DBMI-SQLite errore del driver:
Error in sqlite3_prepare():
SELECT cat FROM road_clip WHERE type=primary
near “primary”: syntax error
ERROR: Impossible to select recod from table <road_clip>
Traceback (most recent call last):
File “/home/alessandro/Scrivania/script_grass/rast_quer_st
ats_exp.py”, line 6, in
gscript.run_command (“v.to.rast” , input = “road_clip” ,
type = “line” , where = “type=%s” % typ , output = (“r”+typ)
, use = “attr” , attribute_column = “cat”)
File “/usr/lib/grass74/etc/python/grass/script/core.py”,
line 421, in run_command
return handle_errors(returncode, returncode, args,
kwargs)
File “/usr/lib/grass74/etc/python/grass/script/core.py”,
line 332, in handle_errors
returncode=returncode)
grass.exceptions.CalledModuleError: L’esecuzione del modulo
None [‘v.to.rast’, ‘use=attr’, ‘attribute_column=cat’,
‘type=line’, ‘input=road_clip’, ‘output=rprimary’,
‘where=type=primary’] è terminata con errori
Processo terminato con codice di return diverso da zero 1.

(Sun Nov 11 10:53:56 2018) Comando terminato (0 sec)

I read both error message and my piece of code time again and again, i was not able to detect what i am doing wong!

Il giorno ven 9 nov 2018 alle ore 13:15 Micha Silver <tsvibar@gmail.com> ha scritto:

I would do as follows:

(Notice that each street type will become a separate raster)

On 11/9/18 1:49 PM, Alessandro Sebastiani wrote:

Dear all
i have a vector (downloaded from OpenStreetmap) with all the streets in my study area. Streets are divided in categories (there is a specific string column in the db). now i want to convert to raster each category separately. I now how to do that separately, but i was not able to automate that in a python script. here it is what i wrote:

**>>>**import grass.script as gscript
**>>>**types = (“primary” , “secondary” , “tertiary” , “service” , “pedestrian” , “footway” , “residential” , “path”)
**>>>**for typ in types:

where_str = (“type = %s” % typ)

outrast = “_”.join([“type”, typ])

gscript.run_command(“v.to.rast”,

input = “road_clip”, type = “line”, where = where_str,

output = outrast, use=“attr”, attribute_column = “cat” )

>>>> gscript.run_command (“v.to.rast” , input = “road_clip” , type = “line” , where =“type”=typ , output = types, use = “attr” , attribute_column = “cat”).

I got the following traceback: File
“/home/alessandro/GRASSDATA/5_citta_def/5_citt_def/.tmp
/alessandro-Lenovo-G50-70/5500.6.py”, line 6
gscript.run_command (“v.to.rast” , input = “road_clip” ,
type = “line” , where =“type”=typ , output = types, use =
“attr” , attribute_column = “cat”)
^
SyntaxError: invalid syntax

i guess the syntax error is in the way i set the query, but i’m not sure. Thanks to everybody,

_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)
-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

Here’s a correction to the format of the “where” clause that should work:

**i**``mport grass.script as gscript
types = ("primary" , "secondary" , "tertiary" , "service" , "pedestrian" , "footway" , "residential" , "path")
for typ in types:

``

where_str = ("type = '%s'" % typ) # Added single quotes around the %s

``

outrast = "_".join(["type", typ])


```gscript.run_command (“v.to.rast”, input = “road_clip”,`

type = "line",
where = where_str,
output = outrast,
use = "attr" , attribute_column = "cat")

···

On 11/11/18 12:02 PM, Alessandro Sebastiani wrote:

Hello to everybody ,
i tried with this piece of code, following suggestios by both:

note that for the moment i reduced the street types to 3

types = (“primary” , “secondary” , “tertiary”)
for typ in types:

gscript.run_command (“v.to.rast” , input = “road_clip” , type = “line” , where = “type=%s” % typ , output = (“r”+typ) , use = “attr” , attribute_column = “cat”)

nevertheless, i still get the following error message

DBMI-SQLite driver error:
Error in sqlite3_prepare():
SELECT cat FROM road_clip WHERE type=primary
near “primary”: syntax error
DBMI-SQLite errore del driver:
Error in sqlite3_prepare():
SELECT cat FROM road_clip WHERE type=primary
near “primary”: syntax error
ERROR: Impossible to select recod from table <road_clip>
Traceback (most recent call last):
File “/home/alessandro/Scrivania/script_grass/rast_quer_st
ats_exp.py”, line 6, in
gscript.run_command (“v.to.rast” , input = “road_clip” ,
type = “line” , where = “type=%s” % typ , output = (“r”+typ)
, use = “attr” , attribute_column = “cat”)
File “/usr/lib/grass74/etc/python/grass/script/core.py”,
line 421, in run_command
return handle_errors(returncode, returncode, args,
kwargs)
File “/usr/lib/grass74/etc/python/grass/script/core.py”,
line 332, in handle_errors
returncode=returncode)
grass.exceptions.CalledModuleError: L’esecuzione del modulo
None [‘v.to.rast’, ‘use=attr’, ‘attribute_column=cat’,
‘type=line’, ‘input=road_clip’, ‘output=rprimary’,
‘where=type=primary’] è terminata con errori
Processo terminato con codice di return diverso da zero 1.

(Sun Nov 11 10:53:56 2018) Comando terminato (0 sec)

I read both error message and my piece of code time again and again, i was not able to detect what i am doing wong!

Il giorno ven 9 nov 2018 alle ore 13:15 Micha Silver <tsvibar@gmail.com> ha scritto:

I would do as follows:

(Notice that each street type will become a separate raster)

On 11/9/18 1:49 PM, Alessandro Sebastiani wrote:

Dear all
i have a vector (downloaded from OpenStreetmap) with all the streets in my study area. Streets are divided in categories (there is a specific string column in the db). now i want to convert to raster each category separately. I now how to do that separately, but i was not able to automate that in a python script. here it is what i wrote:

**>>>**import grass.script as gscript
**>>>**types = (“primary” , “secondary” , “tertiary” , “service” , “pedestrian” , “footway” , “residential” , “path”)
**>>>**for typ in types:

where_str = (“type = %s” % typ)

outrast = “_”.join([“type”, typ])

gscript.run_command(“v.to.rast”,

input = “road_clip”, type = “line”, where = where_str,

output = outrast, use=“attr”, attribute_column = “cat” )

>>>> gscript.run_command (“v.to.rast” , input = “road_clip” , type = “line” , where =“type”=typ , output = types, use = “attr” , attribute_column = “cat”).

I got the following traceback: File
“/home/alessandro/GRASSDATA/5_citta_def/5_citt_def/.tmp
/alessandro-Lenovo-G50-70/5500.6.py”, line 6
gscript.run_command (“v.to.rast” , input = “road_clip” ,
type = “line” , where =“type”=typ , output = types, use =
“attr” , attribute_column = “cat”)
^
SyntaxError: invalid syntax

i guess the syntax error is in the way i set the query, but i’m not sure. Thanks to everybody,

_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)
-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918
_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)
-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

thank you Mika, it’s working!

Il giorno dom 11 nov 2018 alle ore 13:40 Micha Silver <tsvibar@gmail.com> ha scritto:

Here’s a correction to the format of the “where” clause that should work:

**i**``mport grass.script as gscript
types = ("primary" , "secondary" , "tertiary" , "service" , "pedestrian" , "footway" , "residential" , "path")
for typ in types:

``

where_str = ("type = '%s'" % typ) # Added single quotes around the %s

``

outrast = "_".join(["type", typ])


```gscript.run_command (“v.to.rast”, input = “road_clip”,`

type = "line",
where = where_str,
output = outrast,
use = "attr" , attribute_column = "cat")

On 11/11/18 12:02 PM, Alessandro Sebastiani wrote:

Hello to everybody ,
i tried with this piece of code, following suggestios by both:

note that for the moment i reduced the street types to 3

types = (“primary” , “secondary” , “tertiary”)
for typ in types:

gscript.run_command (“v.to.rast” , input = “road_clip” , type = “line” , where = “type=%s” % typ , output = (“r”+typ) , use = “attr” , attribute_column = “cat”)

nevertheless, i still get the following error message

DBMI-SQLite driver error:
Error in sqlite3_prepare():
SELECT cat FROM road_clip WHERE type=primary
near “primary”: syntax error
DBMI-SQLite errore del driver:
Error in sqlite3_prepare():
SELECT cat FROM road_clip WHERE type=primary
near “primary”: syntax error
ERROR: Impossible to select recod from table <road_clip>
Traceback (most recent call last):
File “/home/alessandro/Scrivania/script_grass/rast_quer_st
ats_exp.py”, line 6, in
gscript.run_command (“v.to.rast” , input = “road_clip” ,
type = “line” , where = “type=%s” % typ , output = (“r”+typ)
, use = “attr” , attribute_column = “cat”)
File “/usr/lib/grass74/etc/python/grass/script/core.py”,
line 421, in run_command
return handle_errors(returncode, returncode, args,
kwargs)
File “/usr/lib/grass74/etc/python/grass/script/core.py”,
line 332, in handle_errors
returncode=returncode)
grass.exceptions.CalledModuleError: L’esecuzione del modulo
None [‘v.to.rast’, ‘use=attr’, ‘attribute_column=cat’,
‘type=line’, ‘input=road_clip’, ‘output=rprimary’,
‘where=type=primary’] è terminata con errori
Processo terminato con codice di return diverso da zero 1.

(Sun Nov 11 10:53:56 2018) Comando terminato (0 sec)

I read both error message and my piece of code time again and again, i was not able to detect what i am doing wong!

Il giorno ven 9 nov 2018 alle ore 13:15 Micha Silver <tsvibar@gmail.com> ha scritto:

I would do as follows:

(Notice that each street type will become a separate raster)

On 11/9/18 1:49 PM, Alessandro Sebastiani wrote:

Dear all
i have a vector (downloaded from OpenStreetmap) with all the streets in my study area. Streets are divided in categories (there is a specific string column in the db). now i want to convert to raster each category separately. I now how to do that separately, but i was not able to automate that in a python script. here it is what i wrote:

**>>>**import grass.script as gscript
**>>>**types = (“primary” , “secondary” , “tertiary” , “service” , “pedestrian” , “footway” , “residential” , “path”)
**>>>**for typ in types:

where_str = (“type = %s” % typ)

outrast = “_”.join([“type”, typ])

gscript.run_command(“v.to.rast”,

input = “road_clip”, type = “line”, where = where_str,

output = outrast, use=“attr”, attribute_column = “cat” )

>>>> gscript.run_command (“v.to.rast” , input = “road_clip” , type = “line” , where =“type”=typ , output = types, use = “attr” , attribute_column = “cat”).

I got the following traceback: File
“/home/alessandro/GRASSDATA/5_citta_def/5_citt_def/.tmp
/alessandro-Lenovo-G50-70/5500.6.py”, line 6
gscript.run_command (“v.to.rast” , input = “road_clip” ,
type = “line” , where =“type”=typ , output = types, use =
“attr” , attribute_column = “cat”)
^
SyntaxError: invalid syntax

i guess the syntax error is in the way i set the query, but i’m not sure. Thanks to everybody,

_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)
-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918
_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[https://lists.osgeo.org/mailman/listinfo/grass-user](https://lists.osgeo.org/mailman/listinfo/grass-user)
-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918