Dipole Consulting SPRL

Querying dynamic point layer with php/mapscript PDF Print E-mail

php/mapscript allows to use mapserver very efficiently inside a php application.

Some parts of the behaviour is still a bit undocumented.

Querying dynamically created point layer is one of those missing parts.

This article is based on MapServer version 4.10 

 

To be able to query a dynamic layer, you need to have a layer definition in your mapfile.

This definition MUST have a TEMPLATE attribute (either if it points on nothing).

The following example should work :

 

    LAYER
        NAME "my_layer"
        STATUS ON 
        TYPE POINT
        TOLERANCE 10
        LABELCACHE ON
        LABELMAXSCALE 400000  
        TRANSPARENCY ALPHA
        TEMPLATE nofile.html
        CLASS
            NAME "Points"
            COLOR 255 0 0
            OUTLINECOLOR 255 0 0 
            SIZE 10 
            SYMBOL "circle"
            LABEL
                POSITION AUTO
                ANGLE AUTO
                SIZE 12
                COLOR 0 0 0 
                TYPE truetype
                FONT DejaVuSans
                ANTIALIAS true
                OUTLINECOLOR 255 255 255
                PARTIALS false
                ENCODING 'utf8'
            END          
        END 
     
        PROJECTION
            "+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 +lat_0=90 +lon_0=4.367486666666666 
               +x_0=150000.013 +y_0=5400088.438 +ellps=intl +units=m +no_defs"
        END       
    END

 

Then you need to fill in your layer with points. This is a bit tricky because you cannot add pointObj to mapeserver shapeObj.

Thus, you need to add a lineObj composed by only one point ;-) :

 

    $shp = ms_newShapeObj(MS_SHAPE_POINT);
    $pt = ms_newLineObj();
    $pt->addXY($point->x,$point->y);
    $shp->add($pt);
    $shp->set('classindex', $obj->get_classindex());
    $shp->set('text', $obj->get_id());
    $shp->set('index', $obj->get_internal_id());
    $lyr->addFeature($shp);

 

Finally, you can query your layer using a geographical point (not a pixel one !) :

 

    // transform pixel to geo coords :
    $cx = Pix2Geo($_POST["mapa_x"], 0, $map->height, $map_extent[0], $map_extent[2], 0);
    $cy = Pix2Geo($_POST["mapa_y"], 0, $map->width , $map_extent[1], $map_extent[3], 1);
    $mpoint = ms_newPointObj();
    $mpoint->SetXY($cx, $cy);
    // find into layer the most closed point
    $layer->set("toleranceunits",MS_METERS);
    $res = $layer->queryByPoint($mpoint, MS_SINGLE, 100);
    if ($res == MS_SUCCESS){
        $layer->open();
        $rslt  =  $layer->getResult(0);
        echo "Foudn point $rslt->shapeindex";   
    } else {
        echo "No point found"; 
     }
 


 ! You cannot use the $layer->getShape() method on dynamically created layer.

 

The key to solve this problem is to use the index of the shapeObj as key to find out which point has been selected.

Last Updated ( mardi, 09 octobre 2007 )
 
< Prev   Next >
Main Menu
Home
Services
Products
References
Technical
Jobs
Downloads
Links
Contact Us
Login Form





Lost Password?
No account yet? Register
Newsflash
The JWaveLib 1.1.0 has been released on the 31th of July. A heavy refactoring work has been done on the existing code base to improve the use of the JWavelib by the different tools and products build on top of it (JWaveLib for eWON , WaveSniffer ).