[GRASSLIST:4704] Re: v.in.ascii and dig_cats file

Hamish hamish_nospam at yahoo.com
Tue Nov 2 18:34:34 EST 2004


> I'm trying to create a vector file through v.in.ascii, following the 
> template files in Example 1 from the v.in.ascii manual page:
> 
> http://grass.itc.it/grass57/manuals/html57_user/v.in.ascii.html
> 
> How does the category input from dig_cats work? There was a similar
> thread last week started by Ian MacMillan but I didn't see a solution
> to getting a dig_cats file working. I've appended head output for the
> ascii, att and cats files that I have created, in case there is
> problem there.
> 
> (Recent build of Grass 5.7 running on RHEL 3).


see also
http://freegis.org/cgi-bin/viewcvs.cgi/~checkout~/grass51/doc/vector/vector.html#ascii


namely, you can assign category values on the feature line, e.g.:

> VERTI:
> A 7  1
>  3000000 202512
>  3000000 250000
>  3000000 300000
>  3050000 300000
>  3050000 250000
>  3050000 203715
>  3000000 202512
> A 5  2
>  3000000 300000
[...]

assigns the first area to cat 1, the second to cat 2, etc.
You might also have to assign centroids for areas?

I don't know if the dig_cats and dig_att support work(?). If you are
importing fresh data, it is probably better to avoid the old 5.3 support
files altogether and import category attribues into a 5.7 table directly
to give you access to multiple attributes per category.


For points, you can just use v.in.ascii's points mode to load attribues.
For lines, areas, etc., you need to make a table. Hopefully soon there
will be db.in.ascii, but for now you can use db.execute to create and
populate, and v.db.connect to connect the cat table to the new vector file.
It is a bit complicated, but gets the job done.


for example:


NAME=mapname
ATTR_COLS='cat int, route_id int, name varchar(20), start_wpt varchar(10)'
ATTR_FILE=datafile_atts.txt

#input file must be in $MAPSET/dig_ascii/ 
v.in.ascii in=datafile.txt out=mapname


# check that a default databse connection exists first
db.connect -p


# Create new table
echo "CREATE TABLE $NAME ($ATTR_COLS)" | db.execute



# Populate table with attributes
##following doesn't work for DBF:
##echo "COPY $NAME FROM '$ATTR_FILE'" | db.execute
#
# so we use the SQL 'INSERT' command instead, 1 record at a time (make a loop):
#
# echo "INSERT INTO $NAME VALUES (2, 1, 'AB', 'B')" | db.execute
#===========================================
# you can do the above in a loop or use the trick below:
cat "$ATTR_FILE" | \
  awk -F'\t' '{ printf("echo \"INSERT INTO $NAME VALUES (%d, %d, ^%s^, ^%s^)\" | db.execute\n", $1, $2, $3,
$4) }' \
    | tr '^' \' >  "$ATTR_FILE"_exe

. "$ATTR_FILE"_exe

(depends on $ATTR_FILE format being stable)

#===========================================
#



#Connect attribute table to vector file
v.db.connect map="$NAME" table="$NAME"



Look at the latest 5.7 v.in.garmin for a (complicated) example.



good luck,
Hamish




More information about the grass-user mailing list