[GRASS-dev] [GRASS GIS] #3694: WHERE statement in v.generalize do not work

#3694: WHERE statement in v.generalize do not work
--------------------------+-------------------------
Reporter: BastienFR | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Keywords: v.generalize | CPU: x86-64
Platform: MSWindows |
--------------------------+-------------------------
The `WHERE` statement from `v.generalize` do not seem to work anymore.

A good description of the problem was done at:

https://gis.stackexchange.com/questions/301858/cannot-manage-to-get-the-
where-statement-to-work-in-v-generalize

Another example:

The attached shape was produced in R with:

{{{
library(sf)
library(dplyr)

# preparing the shapefile
nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>%
   mutate(CNTY = as.numeric(CNTY_<median(CNTY_))) %>%
   select(CNTY) %>%
   st_transform(st_crs(2264)) %>%
   st_write("d:/temp/NC_test.shp")
}}}

Using QGIS to call `v.generalize` (the same problem happens in grass
directly, however I prefer working directly in QGIS):

`C:\OSGEO4~1\bin>v.generalize input=vector_5beaccc5750337
type="line,boundary,area" where="CNTY<1" method="douglas" threshold=15000
look_ahead=7 reduction=50 slide=0.5 angle_thresh=3 degree_thresh=0
closeness_thresh=0 betweeness_thresh=0 alpha=1 beta=1 iterations=1 -l
output=outputdc6669bcd1974052b171ef4c11d51846
error=errordc6669bcd1974052b171ef4c11d51846 --overwrite`

I get generalization over all the territory and not just limited to the
polygons where `CNTY<1`

(see image attached)

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------
Changes (by BastienFR):

* Attachment "Capture.PNG" added.

result_image

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------
Changes (by BastienFR):

* Attachment "NC_test.dbf" added.

NC_test.dbf

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------
Changes (by BastienFR):

* Attachment "NC_test.prj" added.

prj

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------
Changes (by BastienFR):

* Attachment "NC_test.shp" added.

shp

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------
Changes (by BastienFR):

* Attachment "NC_test.shx" added.

shx

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------

Comment (by mmetz):

Replying to [ticket:3694 BastienFR]:
> The `WHERE` statement from `v.generalize` do not seem to work anymore.

Looking at the manual G7:v.generalize:

layer=string
     Layer number or name ('-1' for all layers)

     A single vector map can be connected to multiple database tables. This
number determines which table to use. When used with direct OGR access
this is the layer name.

     Default: -1

By default all layers are processed, in which case the cats and where
options are ignored because they require a single layer to be selected.

Results are as expected when using `v.generalize layer=1 where="CNTY<1"
...`.

Note that a boundary is generalized as soon as any of the areas it belongs
to fulfils the where condition. In a topological vector model as in GRASS,
the modification of a single boundary results in the modification of two
areas.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694#comment:1&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------

Comment (by mmetz):

Replying to [comment:1 mmetz]:
> Replying to [ticket:3694 BastienFR]:
> > The `WHERE` statement from `v.generalize` do not seem to work anymore.
>
> Looking at the manual G7:v.generalize:
>
> layer=string
> Layer number or name ('-1' for all layers)
>
> A single vector map can be connected to multiple database tables.
This number determines which table to use. When used with direct OGR
access this is the layer name.
>
> Default: -1
>
> By default all layers are processed, in which case the cats and where
options are ignored because they require a single layer to be selected.

FWIW, I have added a warning in trunk r73673 if the cats and where options
are ignored.

The QGIS interface description for v.generalize needs to be fixed to use
`layer=1`.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694#comment:2&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------

Comment (by BastienFR):

Thanks for your answer. I did try with the good option `layer=1` directly
in GRASS and it worked as expected. I'll update my question on
StackExchange to give the answer (or if you want to do it, I'll accept
your answer).

However, note that your tutorial do not state that properly
(https://grasswiki.osgeo.org/wiki/V.generalize_tutorial#Gereneral_Parameters).
There is no `layer` option in the call given.

`v.generalize -c input=roads output=roads_douglas_reduction2
method=douglas_reduction threshold=0 reduction=50 type=line where="cat<3"`

The fact the the QGIS v.generalize tool do not allow to set the `layer`
option even if allowing setting the `WHERE` statement, how or where should
we document the problem so it's fix there?

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694#comment:3&gt;
GRASS GIS <https://grass.osgeo.org>

#3694: WHERE statement in v.generalize do not work
------------------------+--------------------------
  Reporter: BastienFR | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone:
Component: Default | Version: 7.4.1
Resolution: | Keywords: v.generalize
       CPU: x86-64 | Platform: MSWindows
------------------------+--------------------------

Comment (by mmetz):

Replying to [comment:3 BastienFR]:
> Thanks for your answer. I did try with the good option `layer=1`
directly in GRASS and it worked as expected. I'll update my question on
StackExchange to give the answer (or if you want to do it, I'll accept
your answer).

You can update your question to give the answer, maybe with a link to the
manual and the (by now updated) tutorial :wink:
>
> However, note that your tutorial do not state that properly
(https://grasswiki.osgeo.org/wiki/V.generalize_tutorial#Gereneral_Parameters).
There is no `layer` option in the call given.
>
> {{{
> v.generalize -c input=roads output=roads_douglas_reduction2
method=douglas_reduction threshold=0 reduction=50 type=line where="cat<3"
> }}}

Thanks for pointing this out, now fixed.

The tutorial has been written for the initial version of v.generalize
where the default answer for the layer option was 1. The default has been
changed from 1 to -1 (all layers) in r39888 (9 years ago).
>
> The fact the the QGIS v.generalize tool do not allow to set the `layer`
option even if allowing setting the `WHERE` statement, how or where should
we document the problem so it's fix there?

The QGIS interface description for v.generalize needs to have layer=1
hard-coded (not a user-option).

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3694#comment:4&gt;
GRASS GIS <https://grass.osgeo.org>