Re: Copy and concatenate a vector attribute between layers

Peter Tittmann <[email protected]>
Newsgroups gmane.comp.gis.grass.user
Message-ID <[email protected]>
Thank you both for your suggestions. I found another way around this. My goal was to generate a topologically valid vector (POLYGON) layer containing stand as a predicate of strata (all stands exist within a strata, no stand overlaps strata). To achieve this, I used v.what.rast using centroid after converting the strata raster to polygons with the strata as a mask. Then I remove the mask and patch all the strata polygons with stand attributes together to create a table containing all strata. 

pseudocode:

for all S in STRATA:
	create mask using S
	r.to.vect area — a polygon layer reflecting stands within the strata 
	v.db.acccolumn ‘strata’ — create a new column to store strata values
	v.what.rast raster=STRATA — add the strata attribute to the polygon

v.patch all S from above..

Now I have another issue, though. Will post to a different thread.

Thank you again,

Peter
 

> On Dec 7, 2017, at 7:00 AM, Markus Metz <[email protected]> wrote:
> 
> 
> 
> On Thu, Dec 7, 2017 at 3:20 PM, Moritz Lennert <[email protected] <mailto:[email protected]>> wrote:
> >
> >
> >
> > Am 7. Dezember 2017 14:42:38 MEZ schrieb Markus Metz <[email protected] <mailto:[email protected]>>:
> > >On Thu, Dec 7, 2017 at 9:21 AM, Moritz Lennert
> > ><[email protected] <mailto:[email protected]>>
> > >wrote:
> > >>
> > >>
> > >>
> > >> Am 6. Dezember 2017 19:45:49 MEZ schrieb Peter Tittmann <
> > >[email protected] <mailto:[email protected]>>:
> > >> >Moritz,
> > >> >
> > >> >v.db.connect -p c34_patch
> > >> >Vector map <c34_patch> is connected by:
> > >> >layer <1/c34_patch> table <c34_patch> in database
> > >> ></Users/pete/grassdata/<FOO>/project_area/sqlite/sqlite.db> through
> > >> >driver <sqlite> with key <cat>
> > >> >
> > >> >So it seems that only layer 1 is connected.
> > >> >
> > >> >When try to connect layer 2:
> > >> >
> > >> >v.db.connect map=c34_patch@project_area table=c34_patch layer=2
> > >> >ERROR: Use -o to overwrite existing link for layer <2>
> > >>
> > >> This is weird. This message normally only comes when there really is
> > >a
> > >connection defined for this layer:
> > >
> > >The reason for the error could be that the table c34_patch is already
> > >attached to layer 1.
> >
> > In my NC data example you can see that it works as expected when attaching layer 2 to the same table as layer 1.
> 
> OK, I see.
> 
> >
> > >
> > >I guess what is needed here is v.db.addtable map=c34_patch layer=2
> >
> > v.db.addtable will create a new, empty table containing only the vector features' cat values. In the OP, Peter indicated that he had two layers with respective attribute tables, containing the attributes Strata and Stand.
> 
> In the OP, it says
> 
> c34_patch@project_area: 
>   Type: Point
>   Id: 21622
>   Layer: 1
>   Category: 21623
>   Driver: sqlite
>   Database: /Users/pete/grassdata/PortGraham/project_area/sqlite/sqlite.db
>   Table: c34_patch
>   Key_column: cat
>   Attributes: 
>     cat: 21623
>     strata: 3  — STRATA
> c34_patch@project_area: 
>   Type: Point
>   Id: 21622
>   Layer: 2
>   Category: 10  — STAND
> 
> --> no attributes in layer 2, only categories in layer 2. IIUC, the attribute 'strata' from layer 1 must be combined with the category value from layer 2. Your suggestion should work, after a new table has been added to layer 2.
> 
> Markus M
> 
> > Unless these two attributes are included in the two layers as the respective category values, I don't think v.db.addtable will help.
> >
> > But that's why we need more info...
> >
> > Moritz
> >
> >
> > >
> > >Now you could proceed as suggested by Moritz:
> > >
> > >- Create a new column in layer 1 where you can put the info from layer
> > >2
> > >- Fill this column with v.to.db option=query query_layer=2, etc
> > >- Create a second new column to hold the concatenation
> > >- v.db.update to update this new column with the concatenation of the
> > >two
> > >others (something like qcol = "STRATA || '_' || STAND")
> > >
> > >Markus M
> > >
> > >>
> > >> GRASS 7.2.2 (nc_spm_08):~ > g.copy v=hospitals,test
> > >> Copy vector <hospitals@PERMANENT> to current mapset as
> > >> <test>
> > >>  GRASS 7.2.2 (nc_spm_08):~ > v.db.connect -p test
> > >> Vector map <test> is connected by:
> > >> layer <1/hospitals> table <test> in database
> > ></data/home/mlennert/GRASSDATA/nc_spm_08/user1/sqlite/sqlite.db>
> > >> through driver <sqlite> with key <cat>
> > >> GRASS 7.2.2 (nc_spm_08):~ > v.db.connect test layer=2 table=test
> > >> The table <test> is now part of vector map <test> and may be deleted
> > >or
> > >> overwritten by GRASS modules
> > >> Select privileges were granted on the table
> > >> GRASS 7.2.2 (nc_spm_08):~ > v.db.connect -p test
> > >> Vector map <test> is connected by:
> > >> layer <1/hospitals> table <test> in database </data/hom
> > >> e/mlennert/GRASSDATA/nc_spm_08/user1/sqlite/sqlite.db>
> > >> through driver <sqlite> with key <cat>
> > >> layer <2/test> table <test> in database
> > ></data/home/mlennert/GRASSDATA/nc_spm_08/user1/sqlite/sqlite.db>
> > >through
> > >driver <sqlite> with key <cat>
> > >> GRASS 7.2.2 (nc_spm_08):~ > v.db.connect test layer=2 table=test
> > >> ERROR: Use -o to overwrite existing link for layer <2>
> > >>
> > >> So I don't really understand why you get the error message...
> > >>
> > >> >
> > >> >I’m little unclear on the ‘table’ argument, maybe that should be a
> > >now
> > >> >table?
> > >>
> > >> Although there might be situations where one could want to connect
> > >two
> > >layers to the same table, generally each layer is connected to a
> > >different
> > >table.
> > >>
> > >> In your original post you said the vector had two layers with
> > >attributes.
> > >This does not seem to be the case when I see the v.db.connect -p
> > >output.
> > >Maybe if you go a bit back and explain to us in what format the
> > >original
> > >data is and how you imported it, this might help us help you.
> > >>
> > >> Moritz
> > >>
> > >> >
> > >> >Thanks very much.
> > >> >
> > >> >Peter
> > >> >> On Dec 6, 2017, at 10:27 AM, Moritz Lennert
> > >> ><[email protected] <mailto:[email protected]>> wrote:
> > >> >>
> > >> >>
> > >> >>
> > >> >> Am 6. Dezember 2017 18:42:22 MEZ schrieb Peter Tittmann
> > >> ><[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>:
> > >> >>> Thank you, Moritz.
> > >> >>>
> > >> >>> I'm pretty familiar with SQL but can’t figure out how layer 2 is
> > >> >stored
> > >> >>> in the database. When I `pragma table_info(<vector>)` I only see
> > >the
> > >> >>> layer 1 attributes.
> > >> >>
> > >> >> v.db.connect -p (or grass.script.vector_db() )will show you all
> > >> >connections between the map and database tables.
> > >> >>
> > >> >>
> > >> >> Moritz
> > >> >>>
> > >> >>> I’ll work with the GRASS approach you suggested.
> > >> >>>
> > >> >>> Best,
> > >> >>> Peter
> > >> >>>
> > >> >>>
> > >> >>>> On Dec 1, 2017, at 11:53 PM, Moritz Lennert
> > >> >>> <[email protected] <mailto:[email protected]>> wrote:
> > >> >>>>
> > >> >>>> On 02/12/17 02:03, Peter Tittmann wrote:
> > >> >>>>> Hi,
> > >> >>>>> I have a vector (`POINT`) which has two layers containing
> > >relevant
> > >> >>> attributes (’strata’ and ’stand’). I would like to concatenate
> > >the
> > >> >two
> > >> >>> values into a new attribute for Layer 1 (’<STRATA>_<STAND>’).
> > >I’ve
> > >> >been
> > >> >>> struggling to sort this out for too long and am tapping out. Its
> > >> >>> probably blatantly obvious but it has escaped me.
> > >> >>>>
> > >> >>>>
> > >> >>>> Probably the easiest way if your not very familiar with SQL:
> > >> >>>>
> > >> >>>> - Create a new column in layer 1 where you can put the info from
> > >> >>> layer 2
> > >> >>>> - Fill this column with v.to.db option=query query_layer=2, etc
> > >> >>>> - Create a second new column to hold the concatenation
> > >> >>>> - v.db.update to update this new column with the concatenation
> > >of
> > >> >the
> > >> >>> two others (something like qcol = "STRATA || '_' || STAND")
> > >> >>>>
> > >> >>>> Otherwise, if there is a common key in both tables, you can just
> > >> >>> create a new column and use db.execute to launch a UPDATE TABLE
> > >> >>> directly, grabbing the info from the two table and concatenating
> > >it
> > >> >>> directly in the query.
> > >> >>>>
> > >> >>>> Moritz
> > >> >>>>
> > >> >>>>
> > >> >>>>> Thank you!
> > >> >>>>> Here are some specifics.
> > >> >>>>> east, north: 651849.842735, 6604949.78485
> > >> >>>>> c34_patch@project_area:
> > >> >>>>>  Type: Point
> > >> >>>>>  Id: 21622
> > >> >>>>>  Layer: 1
> > >> >>>>>  Category: 21623
> > >> >>>>>  Driver: sqlite
> > >> >>>>>  Database:
> > >> >>> /Users/pete/grassdata/PortGraham/project_area/sqlite/sqlite.db
> > >> >>>>>  Table: c34_patch
> > >> >>>>>  Key_column: cat
> > >> >>>>>  Attributes:
> > >> >>>>>    cat: 21623
> > >> >>>>> *strata: 3  — STRATA*
> > >> >>>>> c34_patch@project_area:
> > >> >>>>>  Type: Point
> > >> >>>>>  Id: 21622
> > >> >>>>>  Layer: 2
> > >> >>>>> *Category: 10  — STAND*
> > >> >>>>> _______________________________________________
> > >> >>>>> grass-user mailing list
> > >> >>>>> [email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>
> > >> ><mailto:[email protected] <mailto:[email protected]>
> > ><mailto:[email protected] <mailto:[email protected]>>>
> > >> >>>>> https://lists.osgeo.org/mailman/listinfo/grass-user <https://lists.osgeo.org/mailman/listinfo/grass-user>
> > >> ><https://lists.osgeo.org/mailman/listinfo/grass-user <https://lists.osgeo.org/mailman/listinfo/grass-user>>
> > >> >>> <https://lists.osgeo.org/mailman/listinfo/grass-user <https://lists.osgeo.org/mailman/listinfo/grass-user>
> > >> ><https://lists.osgeo.org/mailman/listinfo/grass-user <https://lists.osgeo.org/mailman/listinfo/grass-user>>>
> > >> _______________________________________________
> > >> grass-user mailing list
> > >> [email protected] <mailto:[email protected]>
> > >> https://lists.osgeo.org/mailman/listinfo/grass-user <https://lists.osgeo.org/mailman/listinfo/grass-user>
>

_______________________________________________
grass-user mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/grass-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.