Opened 20 years ago

Closed 20 years ago

#514 closed defect (fixed)

setExpression with a variable

Reported by: tmelhuish@… Owned by: sdlime
Priority: high Milestone:
Component: MapScript Version: 4.1
Severity: normal Keywords:
Cc:

Description

I'm trying to set a class expression in mapscript using a variable. I can copy 
the value if my variable into the setExpression() method and it works fine but 
I get nothing when substituting the variable. I tried all sorts of delimiter 
combinations with no sucess. Also, is thier a better way to define this 
statement nore like an SQL statement? (ie "[nodeid] in(595.0,436.1,...) My 
mapscript code is as follows:

+++++++++++++++++++++++++++++++
$expstr="'([nodeid] eq 436.4 OR [nodeid] eq 436.2 OR [nodeid] eq 436.1 OR 
[nodeid] eq 434.0 OR [nodeid] eq 595.0 OR [nodeid] eq 553.0 OR [nodeid] eq 
435.4 OR [nodeid] eq 452.0 OR [nodeid] eq 426.0 OR [nodeid] eq 435.2)'";
			
print "**ExpStr:$_,$minctr, $maxctr, $expstr <br>\n";

$class=$layer->getClass(0);

## This DOES NOT work!!			
$rv = $class->setExpression( $expstr );

## Either of these statements work fine and produce the results expected
#
#$rv = $class->setExpression('([nodeid] eq 436.1 OR [nodeid] eq 436.2)');
#$rv = $class->setExpression('([nodeid] eq 436.4 OR [nodeid] eq 436.2 OR 
[nodeid] eq 436.1 OR [nodeid] eq 434.0 OR [nodeid] eq 595.0 OR [nodeid] eq 
553.0 OR [nodeid] eq 435.4 OR [nodeid] eq 452.0 OR [nodeid] eq 426.0 OR 
[nodeid] eq 435.2)');
			
$rv == $mapscript::MS_SUCCESS or &Error('can not setExpression in class index 
$classindx layer nodes_oldnew',"error");

+++++++++++++++++++++++++++++++++++++++++++++++++++

Change History (9)

comment:1 by sdlime, 20 years ago

I assume this is perl? Sometimes PHP and Perl look awfully similar. There 
already is an IN operator in MapServer exressions. It just assumes the right 
hand operator is a comma delimited string. So this should work:

  [nodeid] in "595.0,436.1,..."

The comparison is simply a string match so this should work ok for strings and 
integers. It's not particularly efficient though since the list is regenerated 
for each feature (i.e. tokenized based on commas).

If the comparison stuff I mention is a problem then let me know and we can 
update the parser...

Steve 

comment:2 by tmelhuish@…, 20 years ago

Yes, I'm using perl,

I tried the following with no help.

$rv = $class->setExpression('([nodeid] in "436.1,436.2")');
$rv = $class->setExpression('([nodeid] in (436.1,436.2))');
$rv = $class->setExpression('([nodeid] in "436.1"');
$rv = $class->setExpression('([nodeid] in (436.1)');

but the following work.
$rv = $class->setExpression('([nodeid] eq 436.1');
$rv = $class->setExpression('([nodeid] eq 436.1 OR [nodeid] eq 436.2)');

My primary concern is that can't get:
      $rv = $class->setExpression( $expstr );
to work as stated earlier. Am I missing some simple syntax or something?



comment:3 by sdlime, 20 years ago

IN only works works with strings, otherwise you'll get a parse error.

Steve

comment:4 by tmelhuish@…, 20 years ago

Can you provide me the proper syntax on these 2 setExpression statements? I'm 
at a loss. I thought I was passing a string value in these examples?

    $rv = $class->setExpression('([nodeid] in "436.1,436.2")');
and 
    $rv = $class->setExpression( $expstr );

Thanks.



comment:5 by tmelhuish@…, 20 years ago

I'm only using POSTGIS for all of my data. I have no shapefiles, if this would 
make a difference in parsing the data in the setExpression method.

comment:6 by tmelhuish@…, 20 years ago

I finally figured it out! Here is the syntax that works with Postgis.

    $rv = $class->setExpression('(\'[nodeid]\' in "436.1,436.2")');
and 

    $expstr = qq( (\'[nodeid]\' in "436.1,436.2") );
    $rv = $class->setExpression( $expstr );

Thanks


comment:7 by sdlime, 20 years ago

*** Bug 515 has been marked as a duplicate of this bug. ***

comment:8 by dmorissette, 20 years ago

Cc: morissette@… added
Steve, Tom, is there any action required here?  Can this be closed as invalid?

comment:9 by sdlime, 20 years ago

Resolution: fixed
Status: newclosed
Yes, I've marked as fixed. The original problems were in the expressions
themselves, not MapScript (unless you think not quoting strings is a flaw). I
did fix that too. You can use numbers with the IN operator, so Tom's original
expressions should work. Sorry for not cleaning this up earlier...

Steve
Note: See TracTickets for help on using tickets.