[GRASS-user] deleting lines by length, from a vector map

Moritz Lennert mlennert at club.worldonline.be
Sun Dec 1 03:17:43 PST 2019


On 1/12/19 08:39, Micha Silver wrote:
> 
> On 30/11/2019 16:11, Zoltan Szecsei wrote:
>> Hi,
>> I'm trying to write my first Grass python script to load Shape files 
>> and delete all lines shorter than 50m
>> I'm trying various permutations of the command below, but  no success 
>> (No error message and no lines deleted).
>> Any guidance would be welcome.
>>
>> gscript.run_command('v.extract','r',input=gen,output=lin, where="( 
>> $LENGTH > 50 )",overwrite=True)
>> gscript.run_command('v.out.ogr', 'sce2', input=lin, type='line', 
>> output=dir_rclout + rcl + '.shp', format='ESRI_Shapefile', 
>> output_type='line',overwrite=True)
>>
> 
> The term $LENGTH looks wrong to me.
> 
> In python I usually would do something like:
> 
> 
> In [1]: import grass.script as gscript
> 
> In [4]: where_expr = "%s > %d" % ("'length'", 5000)
> 
> In [5]: where_expr
> Out[5]: "'length' > 5000"
> 
> In [6]: gscript.run_command('v.extract', input="roads", 
> output="roads_long", where=where_expr)

In order for this to work, you need a column 'length' in your attribute 
table. Do you have such a column. Otherwise, you can create it with v.to.db.

Moritz


More information about the grass-user mailing list