queryByPoint on dynamic layer from mysql

Eric Bridger eric at GOMOOS.ORG
Fri Feb 17 13:46:37 PST 2006


I don't use PHP mapscript (Perl mapscript) but I believe you would need
to use the shape index to retrieve the feature data from mysql yourself.

In perl mapscript I had no trouble setting shape->{index} to any numeric
value. They did not have to be sequential. So I would use some unique
numeric key from my database table. Alternatively you could create a
lookup array where your mysql keys are stored.

Eric


On Fri, 2006-02-17 at 13:58, Doug Williams wrote:
> Hi,
> Trying to query points that I have added to my map with php_mapscript from a
> mysql table. 
> I've bumbled through the "set index" problem in older versions, 
> have installed MapServer 4.8.1, 
> and now seem to be able to get records from my query, but... 
> while the points and labels show up on the map properly, 
> indicating that the layer of points is being created and the lat/lon and label
> values are associated properly,... 
> when I process the records obtained from my queryByPoint, 
> using getShape to get at the details, 
> There is no data in the shape object's members for the dynamic layer, although
> the result objects from the point query do have the correct index values that
> I programmatically set (incrementing from zero by 1 ... i tried incrementing
> by five to see if index values needed to be 'just so', and that broke the
> point query - yes, i believe they need to increment by one from zero).
> 
> 
> Querying layers (from standard shape files added to the map from a mapfile)
> with the same code works as expected. I can get a shapes member values with
> either values() or getValue() ... works the same both ways.
> 
> 
> It was not possible to query dynamically added points in phpmapscript in the
> old version I last installed (4.4) because the index for the shapes was
> readonly and incorrectly set to -1. Now I can set the index values and the
> queryByPoint does grab the point, but the result record seems to have no data.
> I must be doing something wrong. 
> 
> 
> Any help is greatly appreciated!
> 
> 
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ==================================================================================
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 
> 
> Here's the bit of code where I can't get the details for the dynamic layer,
> though the code works for the mapfile layers. 
> This code was copied from the list serve and modified slightly
> 
> 
> for ($iRes=0; $iRes < $numResults; $iRes++) 
> { 
> $oRes = $oLayer->getResult($iRes); 
> //echo "<script>window.alert('oRes tile index: " . $oRes->tileindex . " oRes
> shape index: " . $oRes->shapeindex . "')</script>"; 
> 
> 
> // the shape index values I 'set' when adding the points (in prior code, shown
> below) are correct, tileindex values were -1
> 
> 
> $oShape = $oLayer->getShape($oRes->tileindex,$oRes->shapeindex);
> printf("<TR>\n");
> //printf("<script>window.alert(' bounds(%f, %f)-(%f, %f)')</script>", 
> // $oShape->bounds->minx, $oShape->bounds->miny, 
> // $oShape->bounds->maxx, $oShape->bounds->maxy); 
> // bounds values are good for all layer records EXCEPT dynamic, for which they
> are all 0 (zero)
> 
> 
> for($iField=0; $iField < sizeof($selFields); $iField++) 
> { 
> // $tempval = $oShape->getValue($oLayer, $selFields[$iField]); // both these
> methods give the same results 
> $tempval = $oShape->values[$selFields[$iField]]; // both these methods give
> the same results 
> 
> 
> // in both cases I get the field name properly from $selFields 
> // but not the value from the shape object 
> // again, the problem only occurs for the dynamic layer 
> // data is properly read for the other layers
> 
> 
> if ($iLayer == "points") { // points is the dynamic layer - this is just for
> debugging as with the other alerts
> echo "<script>window.alert('$iLayer : $selFields[$iField] : $tempval
> :')</script>"; 
> } 
> printf("<TD BGCOLOR=#FFFFFF>"); 
> printf("%s", $tempval); 
> printf("</TD>"); 
> } 
> printf("</TR>\n"); // yes, i know they say not to use tables anymore
> $oShape->free();
> $numResultsTotal++; 
> }
> 
> 
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ==================================================================================
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 
> 
> here is the code where I create the dynamic layer:
> 
> 
> // this function came from the list server, although I added input variables
> for label ($labeltext) and index ($pointnumber)
> function add_point_to_layer($layer, $longitude, $latitude, $labeltext,
> $point_number) {
>   // create and initialize new point object
>   $point = ms_newPointObj();
>   $point->setXY($longitude, $latitude);
>   // create and initialize new line object
>   $line = ms_newLineObj();
>   $line->add($point);
>   // create and initialize new shape object
>   $shape = ms_newShapeObj(MS_SHAPE_POINT);
>   $shape->add($line);
> //echo "<script>window.alert('label text: " . $labeltext . "')</script>";
>   $shape->set('text', $labeltext);
>   $shape->set('index', $point_number);
>   // add our shape
>   $layer->addFeature($shape);
> }
> 
> 
> if ($pointshow) {
> 
> 
>    if (!$orderby) $orderby = "id";
> //   echo $orderby . "<br>";
> 
> 
>    $query = "select * from peop order by $orderby";
> 
> 
>    $points = mysql_query($query);
> 
> 
>    if ($points) {
>       $num_points = mysql_num_rows($points);
>       //echo "$num_points";
> 
> 
>       if ($num_points > 0) {   // this code was copied from the list serve and
> modified slightly
>          $ptlayer = ms_newLayerObj($map);
>          $ptlayer->set("name", "points");
>          $ptlayer->set("type", MS_LAYER_POINT);
>          //$ptlayer->setProjection("init=epsg:4326");
>          $ptlayer->set("status",  MS_ON);
>          $ptlayer->setMetaData("DESCRIPTION",  "Points");
>          $ptlayer->setMetaData("RESULT_FIELDS",  "text index");  // these are
> the shape members that I wish to read in the query
>          $ptlayer->set("template",  "points_template.html");
> 
> 
>          $ptclass = ms_newClassObj($ptlayer);
>          $ptclass->set("status", MS_ON);
>          $ptclass->set("name", "Points");
>          $ptclass->label->set("type", MS_TRUETYPE);
>          $ptclass->label->set("font", 'times');
>          $ptclass->label->color->setRGB( 200,100,200);
>          $ptclass->label->outlinecolor->setRGB(255, 255, 255);
>          $ptclass->label->set("size", 11);
>          $ptclass->label->set("position", 1);
> 
> 
>          $ptstyle = ms_newStyleObj($ptclass);
>          $ptstyle->set("symbol", $map->getsymbolbyname( 'circle'));
>          $ptstyle->set("size", 8);
>          $ptstyle->color->setRGB( 200,100,200);
>          $ptstyle->outlinecolor->setRGB(255, 255, 255);
> 
> 
>          for ($i=0; $i < $num_points; $i++) {
>             $row = mysql_fetch_array($points);
> //echo "<script>window.alert('outer- label text: " . $row["username"] .
> "')</script>";
>             $indexforshape = $i;
>             $labeltxt = $row["username"];
>             add_point_to_layer($ptlayer, $row["lon"], $row["lat"], $labeltxt,
> $indexforshape);
>          }
> 
> 
>       } else {
>          //echo "$num_points points in database.<br><br>";  // should be zero
>       }
> 
> 
>    } else {
>       //echo "Query error.";
>    }
> 
> 
> }
> 
> 
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ==================================================================================
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



More information about the MapServer-users mailing list