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

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)

Thanks and regards,
Zoltan
PS: Should these type of questions go to grass-dev ??

--

=============================================
Zoltan Szecsei GPrGISc 0031
Geograph (Pty) Ltd.
GIS and Photogrammetric Services

P.O. Box 7, Muizenberg 7950, South Africa.

Mobile: +27-83-6004028 (WhatsApp only)
Qatar: +974 5083 2722 www.geograph.co.za

Hi Zoltan,

On Sat, Nov 30, 2019 at 4:05 PM Zoltan Szecsei <zoltans@geograph.co.za> 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)

(writing in a rush, just to send some ideas)

Perhaps something like this:

v.edit:
tool=delete
   Delete selected features from vector map
   https://grass.osgeo.org/grass78/manuals/v.edit.html

with the related WHERE condition.

The attribute-from-geometry for this WHERE condition you may get with
https://grass.osgeo.org/grass78/manuals/v.univar.html

For the line lengths, perhaps to consider
https://grass.osgeo.org/grass78/manuals/v.build.polylines.html

Thanks and regards,
Zoltan
PS: Should these type of questions go to grass-dev ??

Here is fine as well!

Cheers
Markus

--

=============================================
Zoltan Szecsei GPrGISc 0031
Geograph (Pty) Ltd.
GIS and Photogrammetric Services

P.O. Box 7, Muizenberg 7950, South Africa.

Mobile: +27-83-6004028 (WhatsApp only)
Qatar: +974 5083 2722 www.geograph.co.za

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

--
Markus Neteler, PhD
https://www.mundialis.de - free data with free software
https://grass.osgeo.org
https://courses.neteler.org/blog

···

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)
Data element 'vector/roads' was found in more mapsets (also found in
<PERMANENT>)
Using <roads@Arava>...
Data element 'vector/roads' was found in more mapsets (also found in
<PERMANENT>)
Using <roads@Arava>...
Extracting features...
100%
Building topology for vector map <roads_long@Arava>...
Registering primitives...
Writing attributes...
Out[6]: 0

Note the quoting around the column header “length”: One double quote and one single quote. The inner single quote is for the SQL expression.

Cheers, Micha

Thanks and regards,
Zoltan
PS: Should these type of questions go to grass-dev ??

-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918
[https://orcid.org/0000-0002-1128-1325](https://orcid.org/0000-0002-1128-1325)

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

Hi All,
Thanks go to Markus, Micha and Moritz for contribution.
Having been guided to v.edit, it was quite trivial:

gscript.run_command('v.edit', map=gen, type='line', tool='delete', threshold='-1,0,-50', query='length', overwrite=True)

Note the triplet value for Threshold.
Thanks again,
Zoltan

On 2019/11/30 18:43, Markus Neteler wrote:

Hi Zoltan,

On Sat, Nov 30, 2019 at 4:05 PM Zoltan Szecsei <zoltans@geograph.co.za> 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)

(writing in a rush, just to send some ideas)

Perhaps something like this:

v.edit:
tool=delete
    Delete selected features from vector map
    https://grass.osgeo.org/grass78/manuals/v.edit.html

with the related WHERE condition.

The attribute-from-geometry for this WHERE condition you may get with
https://grass.osgeo.org/grass78/manuals/v.univar.html

For the line lengths, perhaps to consider
https://grass.osgeo.org/grass78/manuals/v.build.polylines.html

Thanks and regards,
Zoltan
PS: Should these type of questions go to grass-dev ??

Here is fine as well!

Cheers
Markus

--

=============================================
Zoltan Szecsei GPrGISc 0031
Geograph (Pty) Ltd.
GIS and Photogrammetric Services

P.O. Box 7, Muizenberg 7950, South Africa.

Mobile: +27-83-6004028 (WhatsApp only)
Qatar: +974 5083 2722 www.geograph.co.za

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

--

=============================================
Zoltan Szecsei GPrGISc 0031
Geograph (Pty) Ltd.
GIS and Photogrammetric Services

P.O. Box 7, Muizenberg 7950, South Africa.

Mobile: +27-83-6004028 (WhatsApp only)
Qatar: +974 5083 2722 www.geograph.co.za