[GRASS-user] Pygrass vector package: write geometric feature in a specific layer
Pietro
peter.zamb at gmail.com
Tue Dec 2 22:28:27 PST 2014
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)
}}}
More information about the grass-user
mailing list