<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 30, 2015 at 9:27 AM, Paulo van Breugel <span dir="ltr"><<a href="mailto:p.vanbreugel@gmail.com" target="_blank">p.vanbreugel@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Wed, Sep 30, 2015 at 11:53 AM, Pietro <span dir="ltr"><<a href="mailto:peter.zamb@gmail.com" target="_blank">peter.zamb@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">On Wed, Sep 30, 2015 at 9:51 AM, Paulo van Breugel<br>
<span><<a href="mailto:p.vanbreugel@gmail.com" target="_blank">p.vanbreugel@gmail.com</a>> wrote:<br>
><br>
><br>
> On Wed, Sep 30, 2015 at 2:02 AM, Anna Petrášová <<a href="mailto:kratochanna@gmail.com" target="_blank">kratochanna@gmail.com</a>><br>
> wrote:<br>
>><br>
>><br>
>><br>
>> On Tue, Sep 29, 2015 at 6:09 PM, Paulo van Breugel<br>
>> <<a href="mailto:p.vanbreugel@gmail.com" target="_blank">p.vanbreugel@gmail.com</a>> wrote:<br>
>>><br>
>>> This must be a very basic question, but I can't find an easy/direct way<br>
>>> to do this. In python, if I have an array with values with a length equal to<br>
>>> the number of rows in an attribute table of a (point) vector layer, how can<br>
>>> I write those values to a new column in that attribute table. I can of<br>
>>> course first create the column, but than how to update that column with the<br>
>>> values in the array?<br>
>><br>
>><br>
>> it should be pretty easy to do with pygrass, unfortunately there is no<br>
>> example on assigning attributes in the official documentation [1], but it<br>
>> should be pretty easy, something like that (not tested):<br>
>><br>
>> with VectorTopo('myvector', mode='w') as vectormap:<br>
>> for feature in vectormap:<br>
>> feature.attrs['mycolumn'] = value<br>
>><br>
>><br>
> Thanks, but that seems to write the vector back without attribute table<br>
<br>
</span>You have to save the changes in the database out from your cycle, with:<br>
<br>
vectormap.table.conn.commit()<br></blockquote><div><br></div></span><div>Thanks Pietro. I am, however, not sure I understand (I tried to use it, but thanks to my limited experience in Python / pygrass not much luck). Just to be more specific, I am trying to create a script that divides points in training and test groups, similar to v.kcv, but with points clustered in space. E.g.,<br><br><span style="font-family:monospace,monospace"># Create vector<br>grass.run_command("v.random", output="testB", npoints=10, overwrite=True)<br>grass.run_command("v.db.addtable", map="testB", columns="X DOUBLE PRECISION,Y DOUBLE PRECISION,GR INTEGER")<br>grass.run_command("v.to.db", map="test", option="coor", columns="X,Y")<br><br># Create groups<br>vectmap = 'test'<br>cvals = array(grass.vector_db_select(vectmap, layer = int(1), columns = 'X,Y')['values'].values()).astype(np.float)<br>centroids,_ = kmeans(cvals,2)<br>idx,_ = vq(cvals,centroids)<br><br></span></div><div><span style="font-family:monospace,monospace"># write results to tabel<br></span></div><div><span style="font-family:monospace,monospace">Now I would like to write idx to the column 'GR' in the attribute table of 'test'.<br></span><br></div><div>p.s. I am first creating the XY columns now, but is there a function to get the coordinates (cvals) in pygrass directly?<br></div><div><br><br></div></div></div></div></blockquote><div>I don't fully understand the example, but yes, you can get coordinates:</div><div><br></div><div><font face="monospace, monospace"><span style="font-size:12.8px">with VectorTopo('myvector', mode='w') as vectormap:</span><br style="font-size:12.8px"><span style="font-size:12.8px"> for feature in vectormap:</span><br style="font-size:12.8px"><span style="font-size:12.8px"> print feature.x</span></font><br></div><div><span style="font-family:monospace,monospace;font-size:12.8px"> print feature.y</span><font face="monospace, monospace"><span style="font-size:12.8px"><br></span></font></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<span><font color="#888888"><br>
Pietro<br>
</font></span></blockquote></div><br></div></div>
</blockquote></div><br></div></div>