[GRASS-dev] [GRASS GIS] #3226: v.select: how to handle situation where no features are found

#3226: v.select: how to handle situation where no features are found
-------------------------+-------------------------
Reporter: mlennert | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.2.1
Component: Vector | Version: svn-trunk
Keywords: v.select | CPU: Unspecified
Platform: Unspecified |
-------------------------+-------------------------
Luca described the following scenario [https://lists.osgeo.org/pipermail
/grass-dev/2016-November/083116.html [1]]:

{{{
g.region vect=zipcodes -ap

v.mkgrid map=zipcodes_grid box=10000,10000

v.category zipcodes_grid opt=report
Layer/table: 1/zipcodes_grid
type count min max
point 0 0 0
line 0 0 0
boundary 0 0 0
centroid 42 1 42
area 0 0 0
face 0 0 0
kernel 0 0 0
all 42 1 42

v.select ain=zipcodes_grid bin=zipcodes operator=touches
out=zipcodes_grid_final --o
WARNING: Vector map <zipcodes_grid_final> already exists and will be
          overwritten
Processing features...
  100%
Processing areas...
  100%
Writing selected features...
  100%
Writing attributes...
WARNING: Array of values to select from column <cat> is empty
WARNING: Unable to copy table for layer 1
DBMI-SQLite driver error:
Unable to create index:
create unique index if not exists zipcodes_grid_final_cat on
zipcodes_grid_final ( cat )
no such table: main.zipcodes_grid_final

DBMI-SQLite driver error:
Unable to create index:
create unique index if not exists zipcodes_grid_final_cat on
zipcodes_grid_final ( cat )
no such table: main.zipcodes_grid_final

WARNING: Unable to create index
WARNING: 97 features from <zipcodes_grid@user1> without category skipped
Building topology for vector map <zipcodes_grid_final@user1>...
Registering primitives...
0 primitives registered
0 vertices registered
Building areas...
  100%
0 areas built
0 isles built
Attaching islands...
Attaching centroids...
Number of nodes: 0
Number of primitives: 0
Number of points: 0
Number of lines: 0
Number of boundaries: 0
Number of centroids: 0
Number of areas: 0
Number of isles: 0
v.select complete. 0 features written to output.
}}}

I explained that with the operator 'touches', it is normal that no
features were found. However, the error message was awkward. In r70103 a
patch was applied to trunk to count the number of features found and if
that number = 0 then the user is informed and no output map is created.

However, Vaclav remarks:

> Isn't an empty vector map an expected result in this case? What happens
> when you do this in GUI (where output is added automatically to Map
> Display)?

I don't think that a module should output an empty map. But if you think
that it should than we can change the patch.

And yes, the output is automatically added even if it doesn't exist, but I
would actually consider this a bug in the GUI, not of the module.

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.2.1
Component: Vector | Version: svn-trunk
Resolution: | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------

Comment (by mmetz):

Replying to [ticket:3226 mlennert]:
> [...] In r70103 a patch was applied to trunk to count the number of
features found and if that number = 0 then the user is informed and no
output map is created.
>
> However, Vaclav remarks:
>
> > Isn't an empty vector map an expected result in this case? What
happens
> > when you do this in GUI (where output is added automatically to Map
> > Display)?
>
> I don't think that a module should output an empty map. But if you think
that it should than we can change the patch.
>
> And yes, the output is automatically added even if it doesn't exist, but
I would actually consider this a bug in the GUI, not of the module.

I agree. If no features are selected, there should be no output map. This
is particularly useful for automated processing, otherwise you would need
to check if the output map contains any features at al. It is much simpler
to check if the expected output map exists, granted that v.select finished
successfully.

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.2.1
Component: Vector | Version: svn-trunk
Resolution: | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------

Comment (by lucadelu):

Replying to [comment:1 mmetz]:

>
> I agree. If no features are selected, there should be no output map.
This is particularly useful for automated processing, otherwise you would
need to check if the output map contains any features at al. It is much
simpler to check if the expected output map exists, granted that v.select
finished successfully.

+1

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: normal | Milestone: 7.2.1
Component: Vector | Version: svn-trunk
Resolution: | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------

Comment (by mlennert):

Can we close this bug ?

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: closed
  Priority: normal | Milestone: 7.2.4
Component: Vector | Version: svn-trunk
Resolution: fixed | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------
Changes (by mlennert):

* status: new => closed
* resolution: => fixed

Comment:

I'm closing this as fixed. If the new behavior causes any problems, please
create a new report.

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.4
Component: Vector | Version: svn-trunk
Resolution: | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------
Changes (by mlennert):

* status: closed => reopened
* resolution: fixed =>

Comment:

Actually, the applied patch did not solve the problem as the (empty) map
was still created, but not correctly. In r73652 and r73652 I've applied a
further modification which only goes the different aspects of creating the
new map if elements were found. I would consider this a bug fix, so it
should go into 74 as well, but I haven't had the time to test extensively.
I'm thus not sure whether I should upload it into release74. I don't think
it will cause any trouble, but MarkusM, could you have a look ?

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.4
Component: Vector | Version: svn-trunk
Resolution: | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------

Comment (by mlennert):

FYI: I did run the testsuite on v.select and it did not find any issues.

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: reopened
  Priority: normal | Milestone: 7.2.4
Component: Vector | Version: svn-trunk
Resolution: | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------

Comment (by mmetz):

Replying to [comment:9 mlennert]:
> Actually, the applied patch did not solve the problem as the (empty) map
was still created, but not correctly. In r73652 and r73652 I've applied a
further modification which only goes the different aspects of creating the
new map if elements were found. I would consider this a bug fix, so it
should go into 74 as well, but I haven't had the time to test extensively.
I'm thus not sure whether I should upload it into release74. I don't think
it will cause any trouble, but MarkusM, could you have a look ?

Trunk r73652 looks good to me and it works as expected.

Another reason why no output is created could be that the module failed
for any reason (return code != 0), i.e. the return code needs to be
checked as usual.

+1 for backporting

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: closed
  Priority: normal | Milestone: 7.2.4
Component: Vector | Version: svn-trunk
Resolution: fixed | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------
Changes (by mlennert):

* status: reopened => closed
* resolution: => fixed

Comment:

Replying to [comment:11 mmetz]:
> Replying to [comment:9 mlennert]:
> > Actually, the applied patch did not solve the problem as the (empty)
map was still created, but not correctly. In r73652 and r73652 I've
applied a further modification which only goes the different aspects of
creating the new map if elements were found. I would consider this a bug
fix, so it should go into 74 as well, but I haven't had the time to test
extensively. I'm thus not sure whether I should upload it into release74.
I don't think it will cause any trouble, but MarkusM, could you have a
look ?
>
> Trunk r73652 looks good to me and it works as expected.
>
> Another reason why no output is created could be that the module failed
for any reason (return code != 0), i.e. the return code needs to be
checked as usual.
>
> +1 for backporting

Thanks for the feedback. I committed to release74 in r73665.

Closing the ticket again.

Moritz

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: closed
  Priority: normal | Milestone: 7.2.4
Component: Vector | Version: svn-trunk
Resolution: fixed | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------

Comment (by hcho):

When no features are selected and the overwrite flag (--o) is given,
shouldn't this module delete the output vector then? Recently, I ran into
this problem and used the wrong vector from a previous iteration.

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

#3226: v.select: how to handle situation where no features are found
--------------------------+-------------------------
  Reporter: mlennert | Owner: grass-dev@…
      Type: defect | Status: closed
  Priority: normal | Milestone: 7.2.4
Component: Vector | Version: svn-trunk
Resolution: fixed | Keywords: v.select
       CPU: Unspecified | Platform: Unspecified
--------------------------+-------------------------

Comment (by hcho):

I'm not sure if "not" creating an empty vector is more efficient. For
example,
{{{
v.select ainput=vect1 binput=vect2 output=outvect operator=disjoint --o

# the existence and validity of outvect would be guaranteed if v.select
created
# an empty output for no features
for cat in `v.db.select -c map=outvect column=cat`; do
     echo $cat
done
}}}
needs to be changed like this:
{{{
# make sure there is no previous output vector because v.select won't
# delete or create a new empty vector when no features are selected
g.remove -f type=vector name=outvect

v.select ainput=vect1 binput=vect2 output=outvect operator=disjoint --o

# check if v.select selected any features
eval `g.findfile element=vector file=outvect`
if [ "x$file" != "x" ]; then
     for cat in `v.db.select -c map=outvect column=cat`; do
         echo $cat
     done
fi
}}}

I think "at least" this module should delete the output vector if no
features are selected with --o flag to avoid a false positive "by mistake"
and save one line, but for me, it makes more sense to create an empty
output for easier scripting. Maybe a new flag like -e for empty output?

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