[GRASS-user] Pygrass vector package: write geometric feature in a specific layer
Laurent C.
lrntct at gmail.com
Wed Dec 3 13:14:43 PST 2014
2014-12-03 0:28 GMT-06:00 Pietro <peter.zamb at gmail.com>:
> # you have to set the category for the second layer
> cats = Cats(pump.c_cats)
> cats.reset()
> cats.set(vct.nlines + 1, lpumps.layer)
> # finally write the pump
> vct.write(pump, attrs=pumpattr, set_cats=False)
>
Hi Pietro,
Thanks for your answer, and for your work on pygrass.
It's the use of Cats that I was missing. However when I tried that :
{{{
cats = Cats(new_conduit.c_cats)
cats.reset()
cats.set(new_vect_map.nlines + 1, lconduit.layer)
new_vect_map.write(new_conduit, attrs=dbline_content, set_cats=False)
}}}
I got the following error :
"cats.set(new_vect_map.nlines + 1, lconduit.layer)
AttributeError: 'VectorTopo' object has no attribute 'nlines' "
What 'nlines' is supposed to return?
I worked around by using a int variable incremented at each object. It
worked fine.
Thanks again !
Regards,
Laurent
2014-12-03 0:28 GMT-06:00 Pietro <peter.zamb at gmail.com>:
> Hi Laurent,
>
> On Tue, Dec 2, 2014 at 4:41 PM, Laurent C. <lrntct at gmail.com> wrote:
> > With pygrass I managed to set DB links and import the attributes in
> > different tables.
> > But the only way I've found to change the layer of geometric features is
> > with .open(mode='w', layer=X). This works well for one layer, but I've to
> > write different objects in different layers.
> > I tried to close the map and re-open it with another layer, but if I pass
> > mode='rw', the map refuse to open because the layer not yet exist, and
> with
> > mode='w', it overwrite the existing map.
>
> I didn't have the need to do this, so far, so probably I should
> rewrite the write method to make it easier.
> The first time that you open the map, you should create links and
> table and save the link between the vector map and these tables, so
> something like (note: this is pseudo-code, not tested! so use it as a
> draft idea):
>
> {{{
> with VectorTopo('mymap', mode='w', overwrite=True) as vct:
> # create links
> lpipes = Link(layer=1, name='pipes', table=vct.name + 'pipes',
> key='cat')
> lpumps = Link(layer=2, name='pumps', table=vct.name + 'pumps',
> key='cat')
>
> # add the links to the vector map
> if lpipes not in vct.dblinks:
> vct.dblinks.add(lpipes)
> if lpumps not in vct.dblinks:
> vct.dblinks.add(lpumps)
>
> # create tables removing them if already created
> tpipes = lpipes.table()
> if tpipes.exist():
> tpipes.drop(force=True)
> tpipes.create([('cat', 'PRIMARY KEY'), ('col1', 'VARCHAR(8)'), etc...])
> tpumps = lpumps.table()
> if tpumps.exist():
> tpumps.drop(force=True)
> tpumps.create([('cat', 'PRIMARY KEY'), ('col1', 'VARCHAR(8)'), etc...])
>
> # write your pipes
> vct.table = tpipes
> for pipe, pipeattr in zip(pipes, pipesattrs):
> vct.write(pipe, attrs=pipeattr)
>
> # write the pumps
> vct.table = tpumps
> for pump, pumpattr in zip(pumps, pumpattrs):
> # you have to set the category for the second layer
> cats = Cats(pump.c_cats)
> cats.reset()
> cats.set(vct.nlines + 1, lpumps.layer)
> # finally write the pump
> vct.write(pump, attrs=pumpattr, set_cats=False)
>
> }}}
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20141203/2abe2914/attachment.html>
More information about the grass-user
mailing list