Opened 7 years ago

Closed 5 years ago

Last modified 5 years ago

#3226 closed defect (fixed)

v.select: how to handle situation where no features are found

Reported by: mlennert Owned by: grass-dev@…
Priority: normal Milestone: 7.2.4
Component: Vector Version: svn-trunk
Keywords: v.select Cc:
CPU: Unspecified Platform: Unspecified

Description

Luca described the following scenario [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.

Change History (14)

in reply to:  description ; comment:1 by mmetz, 7 years ago

Replying to 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.

in reply to:  1 comment:2 by lucadelu, 7 years ago

Replying to 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

comment:3 by mlennert, 7 years ago

Can we close this bug ?

comment:4 by martinl, 7 years ago

Milestone: 7.2.17.2.2

comment:5 by neteler, 7 years ago

Milestone: 7.2.27.2.3

Ticket retargeted after milestone closed

comment:6 by martinl, 6 years ago

Milestone: 7.2.3

Ticket retargeted after milestone closed

comment:7 by martinl, 6 years ago

Milestone: 7.2.4

comment:8 by mlennert, 6 years ago

Resolution: fixed
Status: newclosed

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

comment:9 by mlennert, 5 years ago

Resolution: fixed
Status: closedreopened

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 ?

comment:10 by mlennert, 5 years ago

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

in reply to:  9 ; comment:11 by mmetz, 5 years ago

Replying to 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

in reply to:  11 comment:12 by mlennert, 5 years ago

Resolution: fixed
Status: reopenedclosed

Replying to mmetz:

Replying to 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

comment:13 by hcho, 5 years ago

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.

comment:14 by hcho, 5 years ago

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?

Note: See TracTickets for help on using tickets.