<div dir="ltr">2014-12-03 0:28 GMT-06:00 Pietro <<a href="mailto:peter.zamb@gmail.com" target="_blank">peter.zamb@gmail.com</a>>:<br><div><span class="im"><br>>         # you have to set the category for the second layer<br>>         cats = Cats(pump.c_cats)<br>>         cats.reset()<br>>         cats.set(vct.nlines + 1, lpumps.layer)<br>>         # finally write the pump<br>>         vct.write(pump, attrs=pumpattr, set_cats=False)<br>><br><br></span>Hi Pietro,<br><br>Thanks for your answer, and for your work on pygrass.<br></div>It's the use of Cats that I was missing. However when I tried that :<br><div>{{{<br>cats = Cats(new_conduit.c_cats)<br>cats.reset()<br>cats.set(new_vect_map.nlines + 1, lconduit.layer)<br>new_vect_map.write(new_conduit, attrs=dbline_content, set_cats=False)<br>}}}<br><br>I got the following error :<br></div><div>"cats.set(new_vect_map.nlines + 1, lconduit.layer)<br>AttributeError: 'VectorTopo' object has no attribute 'nlines' "<br><br></div><div>What 'nlines' is supposed to return?<br></div><div><br></div><div>I worked around by using a int variable incremented at each object. It worked fine.<br></div><div>Thanks again !<br><br></div><div>Regards,<br></div><div>Laurent</div></div><div class="gmail_extra"><br><div class="gmail_quote">2014-12-03 0:28 GMT-06:00 Pietro <span dir="ltr"><<a href="mailto:peter.zamb@gmail.com" target="_blank">peter.zamb@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Laurent,<br>
<span class=""><br>
On Tue, Dec 2, 2014 at 4:41 PM, Laurent C. <<a href="mailto:lrntct@gmail.com">lrntct@gmail.com</a>> wrote:<br>
> With pygrass I managed to set DB links and import the attributes in<br>
> different tables.<br>
> But the only way I've found to change the layer of geometric features is<br>
> with .open(mode='w', layer=X). This works well for one layer, but I've to<br>
> write different objects in different layers.<br>
> I tried to close the map and re-open it with another layer, but if I pass<br>
> mode='rw', the map refuse to open because the layer not yet exist, and with<br>
> mode='w', it overwrite the existing map.<br>
<br>
</span>I didn't have the need to do this, so far, so probably I should<br>
rewrite the write method to make it easier.<br>
The first time that you open the map, you should create links and<br>
table and save the link between the vector map and these tables, so<br>
something like (note: this is pseudo-code, not tested! so use it as a<br>
draft idea):<br>
<br>
{{{<br>
with VectorTopo('mymap', mode='w', overwrite=True) as vct:<br>
    # create links<br>
    lpipes = Link(layer=1, name='pipes', table=<a href="http://vct.name" target="_blank">vct.name</a> + 'pipes', key='cat')<br>
    lpumps = Link(layer=2, name='pumps', table=<a href="http://vct.name" target="_blank">vct.name</a> + 'pumps', key='cat')<br>
<br>
    # add the links to the vector map<br>
    if lpipes not in vct.dblinks:<br>
        vct.dblinks.add(lpipes)<br>
    if lpumps not in vct.dblinks:<br>
        vct.dblinks.add(lpumps)<br>
<br>
    # create tables removing them if already created<br>
    tpipes = lpipes.table()<br>
    if tpipes.exist():<br>
        tpipes.drop(force=True)<br>
    tpipes.create([('cat', 'PRIMARY KEY'), ('col1', 'VARCHAR(8)'), etc...])<br>
    tpumps = lpumps.table()<br>
    if tpumps.exist():<br>
        tpumps.drop(force=True)<br>
    tpumps.create([('cat', 'PRIMARY KEY'), ('col1', 'VARCHAR(8)'), etc...])<br>
<br>
    # write your pipes<br>
    vct.table = tpipes<br>
    for pipe, pipeattr in zip(pipes, pipesattrs):<br>
        vct.write(pipe, attrs=pipeattr)<br>
<br>
    # write the pumps<br>
    vct.table = tpumps<br>
    for pump, pumpattr in zip(pumps, pumpattrs):<br>
        # you have to set the category for the second layer<br>
        cats = Cats(pump.c_cats)<br>
        cats.reset()<br>
        cats.set(vct.nlines + 1, lpumps.layer)<br>
        # finally write the pump<br>
        vct.write(pump, attrs=pumpattr, set_cats=False)<br>
<br>
}}}<br>
</blockquote></div><br></div>