[mapserver-users] Can't figure out queries with Mapscript.

Hankley, Chip Chip.Hankley at GASAI.Com
Mon Jul 8 06:57:59 PDT 2002


Ben...

There's probably a number of ways to do this, but here's how I do it (see
below). There's a Mapfile component, and a mapscript component. FWIW, this
is essentially exactly the way it's done in the GMAP demo.

Cheers!

Chip

----------------------------------------------------------------

...in the mapfile, this would be the layer definition for the layer I want
to query:

LAYER
  NAME "DePere_Parcels"
  METADATA
    "DESCRIPTION"   "PARCEL_INFO"
    "RESULT_FIELDS" "PARCEL_NUM AREA" 
      # These are the two fields (PARCEL_NUM and 
      # AREA) that I want to return
  END
  DATA parcels
  STATUS DEFAULT
  #minscale 5000
  TYPE polygon
  CLASS
    TEMPLATE "ttt_query.html"
    Name Parcels
  END  # CLASS
END  # LAYER

... in the PHP code, I have a function that handles the query...

function query_map($map, $real_pts) {

  //$map is the Map Object
  //$real_pts is an array with two members:
  //  $real_pts[0] x-coordinate in real world terms of the user click
  //  $real_pts[1] y-coordinate in real world terms of the user click
  //
  //NOTE that this function is set up to query only ONE layer, and return
  // the first HIT from a query... so it wouldn't handle multiple layers
  // or return results from numerous objects, although it could easily be
  // modified to do both.

  $click_pt = ms_newPointObj();
  $click_pt->setXY($real_pts[0], $real_pts[1]);

  @$map->queryByPoint($click_pt, MS_SINGLE, -1);
  $Layer = $map->GetLayerByName("DePere_Parcels");
  $count_results = $Layer->getNumResults();

  if ($count_results > 0) {
    $Layer->open($map->shapepath);
    $selFields = explode(" ", $Layer->getMetaData("RESULT_FIELDS"));

    //Take the first record returned, you could modify this to 
    // loop through a bunch of returned records
    $oRes = $Layer->getResult(0);
    $oShape = $Layer->getShape($oRes->tileindex,$oRes->shapeindex);
    //Save the bounding box to an array
    $aResBoundBox = array($oShape->bounds->minx, $oShape->bounds->miny,
                    $oShape->bounds->maxx, $oShape->bounds->maxy);

    //Retrieve the PIN into a variable
    $PIN = $oShape->values[PARCEL_NUM];
    $prcAREA = $oShape->values[AREA];
    $oShape->free();

    $query_result = array($PIN, $prcAREA);
    $return $query_result;
}



More information about the MapServer-users mailing list