[GRASS-user] Use of v.in.ascii in python with stdin

Hi,

I want to create a point via python at a specific location using the v.in.ascii command. How can I also specify the colum names or how can a header be used in combination with the standard input. As it is only one single point I don’t want to create a txt file containing the header and the info. I tried following in GRASS7:

grass.write_command(“v.in.ascii”,
overwrite=True,
format=“point”,
#flags=“n”,
input=“-”,
x=1,
y=2,
stdin=“”“X,Y,Name\n
10:20:08E,53:25:27N,my_point_1"”“,
output=“my_point”,
separator=”,")

Hi Johannes,

On Mon, Jun 17, 2013 at 1:24 PM, Johannes Radinger
<johannesradinger@gmail.com> wrote:

I want to create a point via python at a specific location using the
v.in.ascii command. How can I also specify the colum names or how can a
header be used in combination with the standard input. As it is only one
single point I don't want to create a txt file containing the header and the
info. I tried following in GRASS7:

I think that some of your parameters are not correct...

{{{
from grass.pygrass.modules import Module

ASCII = """cat,X,Y,Name
1,635828.3,223067.0,pub
2,643553.4,220711.5,pizzeria"""

COLS = 'cat int, x double precision, y double precision, name varchar(10)'

vina = Module('v.in.ascii', input='-', output='pnt__v_in_ascii',
                        format='point', separator=',', skip=1, x=2, y=3, cat=1,
                        columns=COLS, stdin_=ASCII, overwrite=True,
finish_=True)
}}}

Mostly with the same parameters you can run grass.write_command.
But If you are looking for a tool to "play" and experiment I highly
suggest pygrass:

{{{
from grass.pygrass.vector import VectorTopo
from grass.pygrass.geometry import Point

new = VectorTopo('mynewvect')
COLS = [(u'cat', 'INTEGER PRIMARY KEY'), (u'name', 'VAR CHAR')]
new.open('w', tab_cols=COLS)
new.write(Point(635828.3, 223067.0), ('pub', ))
new.write(Point(643553.4, 220711.5), ('restaurant', ))
new.close()

new.open('r')
new.cat(1).attrs['name'] # return: pub
new.close()
}}}

If you are interested please have a look of the ipython notebook:

https://github.com/zarch/workshop-pygrass

you have just to clone, and from a running session of grass enter in
the directory and launch:

ipython notebook

to have a briefly introduction to the library.

Have fun!

Pietro

Hi,

···

On Mon, Jun 17, 2013 at 3:39 PM, Pietro <peter.zamb@gmail.com> wrote:

Hi Johannes,

On Mon, Jun 17, 2013 at 1:24 PM, Johannes Radinger
<johannesradinger@gmail.com> wrote:

I want to create a point via python at a specific location using the
v.in.ascii command. How can I also specify the colum names or how can a
header be used in combination with the standard input. As it is only one
single point I don’t want to create a txt file containing the header and the
info. I tried following in GRASS7:

I think that some of your parameters are not correct…

{{{
from grass.pygrass.modules import Module

ASCII = “”“cat,X,Y,Name
1,635828.3,223067.0,pub
2,643553.4,220711.5,pizzeria”“”

COLS = ‘cat int, x double precision, y double precision, name varchar(10)’

vina = Module(‘v.in.ascii’, input=‘-’, output=‘pnt__v_in_ascii’,
format=‘point’, separator=‘,’, skip=1, x=2, y=3, cat=1,
columns=COLS, stdin_=ASCII, overwrite=True,
finish_=True)
}}}

I thought I found the problem by seeing the difference between the parameter: “stdin_” and

“stdin”. However here the result with “_”:

grass.write_command(“v.in.ascii”,
overwrite=True,
format=“point”,
#flags=“n”,
input=“-”,
cat=1,
x=2,
y=3,
stdin_=ascii,
columns=cols,
output=“mypoint”,
separator=“,”)
Traceback (most recent call last):
File “”, line 12, in
File “/usr/local/grass-7.0.svn/etc/python/grass/script/core.py”, line 331, in write_command
stdin = kwargs[‘stdin’]
KeyError: ‘stdin’

So I tried without:

ascii = “”“cat,X,Y,Name
1,10:20:08E,53:25:27N,mypoint”“”

cols = ‘cat int, x varchar(10), y varchar(10), name varchar(10)’

grass.write_command(“v.in.ascii”,
overwrite=True,
format=“point”,
#flags=“n”,
input=“-”,
cat=1,
x=2,
y=3,
stdin=ascii,
columns=cols,
output=“mypoint”,
separator=“,”)

which at least executes but gives me following error: GRASS_INFO_ERROR(13239,2): Unparsable longitude value in column num 2: X

Everything seems to be similar to your example except for the fact that I am using lat/long values instead of projected double precision X
Y coordinates.

Mostly with the same parameters you can run grass.write_command.
But If you are looking for a tool to “play” and experiment I highly
suggest pygrass:

{{{
from grass.pygrass.vector import VectorTopo
from grass.pygrass.geometry import Point

new = VectorTopo(‘mynewvect’)
COLS = [(u’cat’, ‘INTEGER PRIMARY KEY’), (u’name’, ‘VAR CHAR’)]
new.open(‘w’, tab_cols=COLS)
new.write(Point(635828.3, 223067.0), (‘pub’, ))
new.write(Point(643553.4, 220711.5), (‘restaurant’, ))
new.close()

new.open(‘r’)
new.cat(1).attrs[‘name’] # return: pub
new.close()
}}}

If you are interested please have a look of the ipython notebook:

https://github.com/zarch/workshop-pygrass

you have just to clone, and from a running session of grass enter in
the directory and launch:

ipython notebook

to have a briefly introduction to the library.

Have fun!

Pietro

So I tried without:

ascii = """cat,X,Y,Name
1,10:20:08E,53:25:27N,mypoint"""

cols = 'cat int, x varchar(10), y varchar(10), name varchar(10)'

grass.write_command("v.in.ascii",
overwrite=True,
format="point",
#flags="n",
input="-",
cat=1,
x=2,
y=3,
stdin=ascii,
columns=cols,
output="mypoint",
separator=",")

which at least executes but gives me following error:
GRASS_INFO_ERROR(13239,2): Unparsable longitude
value in column num 2: X

you need skip=1 or put a # in front of the first line, it's trying
to parse the header line.

style: since the stdin= option is a virtual one for
grass.write_command(), consider putting it last.

Hamish