[GRASS-user] How to replace some vector & DB features, keep attributes

Maris Nartiss maris.gis at gmail.com
Tue Jan 16 02:22:34 PST 2024


Hi Ken,
GRASS has a loose coupling between geometry and attributes. Use v.edit
to get your geometries correct and set a CAT value of your liking to
the new geometry. Then just make a database entry for that CAT. Note –
geometries are identified by IDs (single CAT can point to 0, 1 or n
geometries and single geometry can have multiple CATs), attributes are
always identified by CAT.

As for the SQL, try something like this (cat_value replace with CAT
set by v.edit tool=addcat):
db.execute sql="INSERT INTO new_vector (cat, attr1, attr2) SELECT
cat_value, attr1, attr2 FROM old_vector WHERE cat = old_cat"

M.

pirmd., 2024. g. 15. janv., plkst. 16:48 — lietotājs Ken Mankoff
(<mankoff at gmail.com>) rakstīja:
>
> Hi Maris,
>
> On 2024-01-15 at 02:10 -08, Maris Nartiss <maris.gis at gmail.com> wrote...
> > v.edit tool=catadd/catdel is the thing you are looking for. Delete old
> > geometry, delete category of new geometry (if there is one), set old
> > cat value to the new geometry.
>
> Thank you for the suggestion. I don't need cat to be the same. I'm deleting ~100 cats (old vector lines) and replacing with only one new one anyway.
>
> More importantly, in my simplified example I neglected to mention that although I want to keep some attributes, I don't want to keep all. There are several old attributes that are no longer relevant for this one manually created line.
>
> I was going to create the new line (as below) and then in a for loop:
>
> for a in attr1 attr2 attr3 attr4; do # attrs I want to keep or copy
>     # db.select statement to get attr1 value from $old_cat
>     # db.execute sql='UPDATE test set a = $att where cat=$new_cat
> done
>
> Alternatively, if I follow your advice, then I'd need to:
>
> for a in attr1 attr2 attr3 attr4; do # attrs I want to delete
>     # db.execute sql='UPDATE test set $attr = NULL where cat=$new_cat
> done
>
> So... is this looping method correct, or is there some SQL statement that will copy attr1,attr2,attrn from row x (defined by an ID or cat) to row y?
>
> Thanks,
>
>   -k.
>
>
> >> I'd like to replace multiple vector lines with ID == 42 with one new
> >> one, and keep the attributes (date, year, name) from one of the
> >> replaced/removed lines.
> >>
> >> I'm able to create my one new line with:
> >>
> >>
> >> I'm not sure how to copy over the relevant attributes from one of the
> >> lines (first, last, doesn't matter) that has the relevant ID.
> >>
> >> Once I do that, I think I know how to delete the lines I don't want,
> >> but I'm not sure I'm doing it correct. It seems I have to run two
> >> commands to delete the lines from both the vectors (when displaying)
> >> and the database (when querying). I'm doing this:
> >>
> >> v.edit map=test tool=delete where="ID == 42" db.execute sql="DELETE
> >> FROM test WHERE ID == 42"
> >>
> >> Can anyone help with the middle step?
> >>
> >> Thanks,
> >>


More information about the grass-user mailing list