[GRASS-user] SQL query in Python script

Charlie Shobe Charles.Shobe at Colorado.EDU
Sun Nov 30 13:33:07 PST 2014


I have managed to figure it out, and I'm sharing the solution to hopefully
clear up confusion for other beginners like myself. When specifying an SQL
query for GRASS in Python, correct syntax in this case is:

where=""""dir"<0"""

Meaning, the entire expression should be enclosed in 3 quotation marks, and
the names of any columns or attributes should be enclosed in a set of
quotes as well. A number such as 0 requires to quotation marks.

I hope this helps someone avoid my confusion.

Charlie

On Sun, Nov 30, 2014 at 12:21 PM, Charlie Shobe <Charles.Shobe at colorado.edu>
wrote:

> Hi all,
>
> I am attempting to extract a subset of points from a points vector based
> on the values in the column "dir". I am running GRASS 6.4.4 on Mac OS
> 10.9.5.
>
> In the GRASS command line, I can successfully make my selection with the
> following command:
> --------------------------------------------------------------
> GRASS 6.4.4 (Yellowstone):~ > *v.extract input=streams_points
> output=outlets_vect type=point where="dir<0"*
>
> 92 categories loaded from table <streams_points>
>
> Extracting features...
>
> Building topology for vector map <outlets_vect>...
>
> Registering primitives...
>
> 92 primitives registered
>
> 92 vertices registered
>
> Building areas...
>
>  100%
>
> 0 areas built
>
> 0 isles built
>
> Attaching islands...
>
> Attaching centroids...
>
>  100%
>
> Number of nodes: 92
>
> Number of primitives: 92
>
> Number of points: 92
>
> Number of lines: 0
>
> Number of boundaries: 0
>
> Number of centroids: 0
>
> Number of areas: 0
>
> Number of isles: 0
>
> Writing attributes...
>
> v.extract complete.
>
> -------------------------------------------------------
> However, due to my incomplete knowledge of how Python and SQL interact, I
> am having trouble executing the same command in a Python script. Here is
> what I am trying:
>
> *g.run_command("v.extract", input="streams_points", output="outlets_vect",
> type="point", where="dir<0")*
>
> When I run this command, it fails to extract any points. I know that this
> must be due to some problem with the syntax of my SQL statement.
>
> Can anyone clear this up for me?
>
> Thanks,
>
> Charlie
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20141130/a4ab0bbd/attachment.html>


More information about the grass-user mailing list