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

Hi,

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:

echo "L 2 1
     -273157 -994455
     -255458 -989423
     1 100000" | v.edit -n tool=add map=test input=-

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,

  -k.

Hi Ken,
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.

Good luck,
M.

pirmd., 2024. g. 15. janv., plkst. 04:56 — lietotājs Ken Mankoff via
grass-user (<grass-user@lists.osgeo.org>) rakstīja:

Hi,

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:

echo "L 2 1
     -273157 -994455
     -255458 -989423
     1 100000" | v.edit -n tool=add map=test input=-

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,

  -k.
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

Hi Maris,

On 2024-01-15 at 02:10 -08, Maris Nartiss <maris.gis@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,

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@gmail.com>) rakstīja:

Hi Maris,

On 2024-01-15 at 02:10 -08, Maris Nartiss <maris.gis@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,
>>