[GRASS-user] Pygrass vector package: write geometric feature in a specific layer
Markus Metz
markus.metz.giswork at gmail.com
Tue Dec 2 23:25:31 PST 2014
On Wed, Dec 3, 2014 at 7:28 AM, Pietro <peter.zamb at gmail.com> wrote:
> 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):
How can you write a single feature with multiple links to different
layers? Your example assumes one link per feature. GRASS supports M:N
mapping of vector features to attributes: several features can have
the same category, thus the same attirbutes, and one feature can have
several categories, all in one layer or distributed across several
layers.
Markus M
>
> {{{
> 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)
>
> }}}
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
More information about the grass-user
mailing list