Opened 15 years ago

Closed 15 years ago

#2808 closed defect (fixed)

QueryByAttributes

Reported by: dstrevinas Owned by: dmorissette
Priority: high Milestone: 5.4 release
Component: MapServer C Library Version: unspecified
Severity: normal Keywords:
Cc: hobu, sdlime

Description

I came across a problem while using python mapscript's queryByAttribute function. I have a layer containing the fieldname "STATE" amongst others. Issuing:

try:
   layer.queryByAttribute(map,"STA","AZ",MS_MULTIPLE)
except:
   print "attribute error"
   pass

By now the mapserver exception is caught for the faulty attribute. If a draw function is now issued:

map.draw()

The following error occurs:

<class '_mapscript.MapServerError'>: msDrawMap(): Image handling error. Failed to draw layer named 'countries'. msDBFGetItemIndex(): DBASE file error. Item 'STA' not found.

I reproduced it many times and in many occasions. I tried to clear any result cache but in vain. Shouldn;t mapserver handle the situation more logically?

Attachments (2)

mapquery.c (6.6 KB ) - added by dstrevinas 15 years ago.
mapquery.patch (3.2 KB ) - added by dstrevinas 15 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 by dmorissette, 15 years ago

Cc: hobu sdlime added
Milestone: 5.4 release
Resolution: fixed
Status: newclosed

I'm not setup to test with Python MapScript, but it seems to me that calling msResetErrorList() in the exception handling block might do the trick.

Closing for now. Please try adding a call to msResetErrorList() and if that doesn't work then please reopen and we'll reassign to one of the Python guys.

comment:2 by dstrevinas, 15 years ago

Priority: normalhigh
Resolution: fixed
Status: closedreopened

At last i managed to get through the source and realized that QueryByAttributes sets a FILTERITEM within the layer object. If a map is drawn and the filteritem is not cleared, then mapserver draw functions complain (when the layer is queried with false fieldname). So the manual solution is:

try:
   layer.queryByAttribute(map,"STA","AZ",MS_MULTIPLE)
except:
   '''layer.filteritem=""'''
   print "attribute error"
   pass

Something that will confuse mapscript users. I didn't modify anything in source. Wait for any comment.

by dstrevinas, 15 years ago

Attachment: mapquery.c added

comment:3 by dstrevinas, 15 years ago

Component: MapScriptMapServer C Library

Finally, setting filteritem to doesn't either work. The problem is that msQueryByAttributes in mapquery.c does not restore the previous state of the filters whenever it returns with an error condition. I create a function called msRestoreOldFilter and call it whenever msQueryByAttributes returns resolving the problem successfully. I attached a modified mapquery.c

by dstrevinas, 15 years ago

Attachment: mapquery.patch added

comment:4 by dmorissette, 15 years ago

The patch seems to make sense, but I still can't reproduce this with my own script and mapfile, so I am hesitant to apply a patch that I can't test.

Can you please share your layer definition? Perhaps that will give me some hints on what I may be missing in my testcase to reproduce this.

comment:5 by dmorissette, 15 years ago

I am using PHP MapScript whose queryByAttributes() implementation is identical to the Python one, and it always cleans up the filter properly, so I wonder if it could be something related to the Python exception handling. Steve, Howard, does the try/except let the underlying C function finish normally or does it abort it?

comment:6 by dmorissette, 15 years ago

Resolution: fixed
Status: reopenedclosed

Jim sent me a copy of his mapfile and data and I was able to reproduce the error with PHP (and determine that I had made a stupid error in my initial test script).

I have tested and committed the patch with a minor change to remove unnecessary strdup/frees on the old_filteritem and old_filter buffers in msRestoreOldFilter() (those existed before the patch).

Committed in trunk (future v6.0) r8837 and branch-5-4 (future 5.4.0) r8838.

Note: See TracTickets for help on using tickets.