[mapserver-users] PHP-Mapscript, Posgresql, Postgis, dynamic query without a map file

Armin Burger armin.burger at gmx.net
Sun Jan 15 05:51:20 EST 2012


I think setting the layer template to an *empty* string does not make 
the layer queryable. Set it to whatever non-empty string and try again, like

$layer->set("template", "pippo");

PHP MapScript (or maybe PHP in general) seems to treat empty strings 
typically equivalent to null or false.

armin


On 05/01/2012 10:26, Andrea D' Aversa wrote:
> Hi,
> I'm creatingan applicationin php language.
> I'm usingPHP-MapScript,PostGISforqueryfor aqueryByPointthat Imustreturn
> informationformattedtext/html butthe query resultis 0.
> Thisis the code:
>
> <?php
> require_once("includes/config.inc.php");
> function pixelToGeo($map,$x,$y) {
>      # Convert x,y pixel values to georeferenced coords.  Returns a
> pointObj.
>      $minx = $map->extent->minx;
>      $miny = $map->extent->miny;
>      $maxx = $map->extent->maxx;
>      $maxy = $map->extent->maxy;
>
>      $dx = $maxx - $minx;
>      $dy = $maxy - $miny;
>
>      $width = $map->width;
>      $height = $map->height;
>
>      $dxpp = (double)$dx/$width;
>      $dypp = (double)$dy/$height;
>
>      $geox = $minx + $dxpp*$x;
>      $geoy = $maxy - $dypp*$y;
>
>      $pointObj = ms_newPointObj();
>      $pointObj->setXY($geox,$geoy);
>
>      return $pointObj;
> }
> function FindPoint($point,$map,$radius, $layerName) {
>      $qlayer = $map->getLayerByName($layerName);
>      $check=$qlayer->queryByPoint($point, MS_MULTIPLE, $radius);
>      $image=$map->drawQuery();
>      if($check == MS_FAILURE){
>          echo "<br><font size=4 color=red>There were no results</font><br>";
>      }else {
>          $qlayer->open();
>          $numResults = $qlayer->getNumResults();
>          echo "<br>Result Query= ".$numResults."<br>";
>          if ($numResults != 0) {
>              for ($i = 0; $i < $numResults; $i++) {
>                  $query_result = $qlayer->getResult($i);
>                  $shape = $qlayer->getShape($query_result->tileindex,
> $query_result->shapeindex);
>                  $Lista_ele[]=$shape->values;
>              }
>          } else {
>              $valido =0;
>              $Lista_ele = "";
>          }
>          $qlayer->close();
>          return $Lista_ele;
>      }
> } // end FindPoint
>
> $mapfile_image_width=$_GET["WIDTH"];
> $mapfile_image_height=$_GET["HEIGHT"];
> $folderCache=$_GET["idf"];
> $layerName=$_GET["layer"];
> $SRID=$_GET["SRS"];
>
> $x=$_GET["X"];
> $y=$_GET["Y"];
> $BBOX=explode(",",$_GET["BBOX"]);
> $ms_imagepath=DIR_FOLDER;
> $ms_imageurl=URL_FOLDER;
>
> $map = ms_newMapObj('');
>
> $map->outputformat->set('driver','imagemap');
> $map->outputformat->set('mimetype','text/html; driver=imagemap');
>
> $map->set('width',$mapfile_image_width);
> $map->set('height',$mapfile_image_height);
> $map->web->set('imagepath',$ms_imagepath);
> $map->web->set('imageurl',$ms_imageurl);
>
> $latlongProj = ms_newProjectionObj("EPSG:".$SRID);
> $utmProj = ms_newProjectionObj("EPSG:900913");
> $poPoint = ms_newpointobj();
> $poPoint->setXY($BBOX[0], $BBOX[1]);
> $poPoint->project($utmProj, $latlongProj);
> $xmin=$poPoint->x;
> $ymin=$poPoint->y;
> $poPoint = ms_newpointobj();
> $poPoint->setXY($BBOX[2], $BBOX[3]);
> $poPoint->project($utmProj, $latlongProj);
> $xmax=$poPoint->x;
> $ymax=$poPoint->y;
> $map->setextent($xmin, $ymin, $xmax, $ymax);
>
> echo "<br>BBOX = ".$xmin.", ".$ymin.", ".$xmax.", ".$ymax."<br>";
>
> // Add the only one layer.
> $layer = ms_newLayerObj($map);
> $layer->setConnectionType(MS_POSTGIS);
> $layer->set('name',$layerName);
> $layer->set('type',MS_LAYER_POLYGON);
> $layer->set('status', MS_ON);
> $layer->set('connection', "host=xxx port=xxx dbname=xxx user=xxx
> password=xxx");
> $layer->set('data','the_geom from (select * from '.$layerName.') as foo
> using unique gid using SRID='.$SRID);
> $layer->set("template", "");
> $layer->set("dump", "true");
>
> // Add some colors.
> $class = ms_newClassObj($layer);
> $style = ms_newStyleObj($class);
> $style->color->setRGB(0, 255, 255);
> $style->outlinecolor->setRGB(255, 255, 0);
>
> $image = $map->draw();
> $map->preparequery();
> $point = ms_newpointobj();
> $point->setXY($x,$y);
> $geo_point = pixelToGeo($map,$point->x,$point->y);
>
> echo "POINT TO CLICK = ".$geo_point->x. " , ".$geo_point->y;
>
> print_r( FindPoint($geo_point,$map,-1, $layerName));
> ?>
> My result is:
> BBOX = 2395177.6101675, 4571367.7734744, 2612958.1665649, 4679392.0513538
> POINT TO CLICK = 2470361.0341494 , 4612710.2888501
> There were no results
>
>
> What's wrong?
> Allthe examplesIfound onlineusinga mapfile,but Idon't have amap file.
>
> Thanks
> _________________________________________
> *Andrea D'Aversa*
>
>
> _______________________________________________
> mapserver-users mailing list
> mapserver-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapserver-users


More information about the mapserver-users mailing list