[GRASSLIST:4630] creating a table from ascii data?

Hello,

I am trying to convert a multi column text file[*] into a table which I
can then connect to a (5.7) vector map. I can't quite see how to do
this. Any ideas?
This is for a script, so "use OpenOffice & save as DBF" won't work.

[*] say .csv file; col1=cat col2=attr, etc.

thanks,
Hamish

On Mon, 25 Oct 2004 17:08:03 +1300, Hamish <hamish_nospam@yahoo.com> wrote:

Hello,

I am trying to convert a multi column text file[*] into a table which I
can then connect to a (5.7) vector map. I can't quite see how to do
this. Any ideas?
This is for a script, so "use OpenOffice & save as DBF" won't work.

[*] say .csv file; col1=cat col2=attr, etc.

thanks,
Hamish

How about the PostgreSQL copy command?

--
Richard Greenwood
richard.greenwood@gmail.com
www.greenwoodmap.com

> I am trying to convert a multi column text file[*] into a table
> which I can then connect to a (5.7) vector map. I can't quite see
> how to do this. Any ideas?
> This is for a script, so "use OpenOffice & save as DBF" won't work.
>
> [*] say .csv file; col1=cat col2=attr, etc.

How about the PostgreSQL copy command?

I have no idea.

I'd prefer (require actually) not to have PostgreSQL installed as a
prerequisite; I was hoping there would by a DBF (ie simple) solution.
(+ I have no idea about Postgres)

All the functionality seems to be in v.in.ascii for importing in points
mode. This is for line attributes, so points mode doesn't really make
sense, but a bad hack might to be to assign dummy coordinate values to
get the table made via v.in.ascii and then link the columns I need from
that. A better solution might be to add a '-c' flag to v.in.ascii that
created a non-georeferenced table from stdin + columns='' + catcol= ??

I don't know the database stuff well so I am just guessing here.

Hamish

> I am trying to convert a multi column text file[*] into a table
> which I can then connect to a (5.7) vector map. I can't quite see
> how to do this. Any ideas?
> This is for a script, so "use OpenOffice & save as DBF" won't work.
>
> [*] say .csv file; col1=cat col2=attr, etc.

How about the PostgreSQL copy command?

[yes, that is the command I was looking for]

I'm still having problems:

NAME=vector_map
ATTR_FILE="${TMP}.route_atts" # includes absolute path
ATTR_COLS='cat int, route_id int, name varchar(20), start_wpt varchar(10)'

# check that 'db.connect -p' exists

# create attr database
echo "CREATE TABLE $NAME ($ATTR_COLS)" | db.execute

# next populate it
echo "COPY $NAME FROM '$ATTR_FILE'" | db.execute

# finally connect it to existing vector map
v.db.connect map="$NAME" table="$NAME"

The COPY step gives an error though:
($GISDBASE/$LOCATION/$MAPSET/.tmp/$HOSTNAME/ shortened to /tmp here)

[...]
v.in.ascii: line vector "test_rte" successfully created
D2/5: add_table(): table = test_rte name = test_rte.dbf
D3/5: sql: COPY test_rte FROM '/tmp/23569.0.route_atts'

DBMI-DBF driver error:
SQL parser error in statement:
COPY test_rte FROM '/tmp/23569.0.route_atts'
Error in db_execute_immediate()

WARNING: Error while executing: "COPY test_rte FROM
         '/tmp/23569.0.route_atts'
         "

I don't see the parser error.

I could probably do "INSERT INTO table VALUES (values, ..)" line by
line, but that seems wasteful and slow.

Hamish

On Tuesday 26 October 2004 01:28, Hamish wrote:

I'd prefer (require actually) not to have PostgreSQL installed as a
prerequisite; I was hoping there would by a DBF (ie simple) solution.
(+ I have no idea about Postgres)

All the functionality seems to be in v.in.ascii for importing in points
mode. This is for line attributes, so points mode doesn't really make
sense, but a bad hack might to be to assign dummy coordinate values to
get the table made via v.in.ascii and then link the columns I need from
that. A better solution might be to add a '-c' flag to v.in.ascii that
created a non-georeferenced table from stdin + columns='' + catcol= ??

I don't know the database stuff well so I am just guessing here.

I prefer db.in.ascii as copy-paste from v.in.ascii (cca 300 rows).

Radim

On Mon, Oct 25, 2004 at 06:58:24AM -0600, Richard Greenwood wrote:

On Mon, 25 Oct 2004 17:08:03 +1300, Hamish <hamish_nospam@yahoo.com> wrote:
> Hello,
>
> I am trying to convert a multi column text file[*] into a table which I
> can then connect to a (5.7) vector map. I can't quite see how to do
> this. Any ideas?
> This is for a script, so "use OpenOffice & save as DBF" won't work.
>
> [*] say .csv file; col1=cat col2=attr, etc.
>
> thanks,
> Hamish

Hi Hamish,

not sure if this is what you need, but v.in.ascii accepts multiple
attributes:

# Generate a 3D points vector file 'points3d.dat' with attributes as ASCII file:
# (here Spearfish)
593493.1|4914730.2|123.1|studna|well
591950.2|4923000.5|222.3|kadibudka|closet
589860.5|4922000.0|232.3|hruska|pear
590400.5|4922820.8|143.2|mysi dira|mouse hole
593549.3|4925500.7|442.6|mineralni pramen|mineral spring
600375.7|4925235.6|342.2|kozi stezka|goat path

#Import into GRASS:
cat points3d.dat | v.in.ascii -z zcol=3 catcol=0 out=mypoints3D \
                   columns='x double, y double, z double, label_cz varchar(20), label_en varchar(20)'
v.info -c mypoints3D
v.info mypoints3D

Markus

Hamish wrote:

> > I am trying to convert a multi column text file[*] into a table
> > which I can then connect to a (5.7) vector map. I can't quite see
> > how to do this. Any ideas?
> > This is for a script, so "use OpenOffice & save as DBF" won't work.
> >
> > [*] say .csv file; col1=cat col2=attr, etc.
>
> How about the PostgreSQL copy command?

                  ^^^^^^^^^^

[yes, that is the command I was looking for]

I'm still having problems:

NAME=vector_map
ATTR_FILE="${TMP}.route_atts" # includes absolute path
ATTR_COLS='cat int, route_id int, name varchar(20), start_wpt varchar(10)'

# check that 'db.connect -p' exists

# create attr database
echo "CREATE TABLE $NAME ($ATTR_COLS)" | db.execute

# next populate it
echo "COPY $NAME FROM '$ATTR_FILE'" | db.execute

# finally connect it to existing vector map
v.db.connect map="$NAME" table="$NAME"

The COPY step gives an error though:
($GISDBASE/$LOCATION/$MAPSET/.tmp/$HOSTNAME/ shortened to /tmp here)

[...]
v.in.ascii: line vector "test_rte" successfully created
D2/5: add_table(): table = test_rte name = test_rte.dbf
D3/5: sql: COPY test_rte FROM '/tmp/23569.0.route_atts'

DBMI-DBF driver error:

       ^^^

The DBF driver doesn't support the COPY command.

The PostgreSQL documentation for the COPY command says:

  Compatibility

  There is no COPY statement in the SQL standard.

I.e. it's a PostgreSQL extension.

--
Glynn Clements <glynn@gclements.plus.com>