Un piccolo script per eseguire le query puntuali con PHP/Mapscript

Lo script riceve alcuni parametri via GET e legge due variabili (il mapfile e il nome dell’estensione mapscript da un file di configurazione), può essere facilmente modificato per adattarlo ad altre esigenze.

Il codice javascript serve a impostare i valori di x e y in un form che ha aperto il webgis

<?php
/**
* Query by point
*/

require_once 'config.php';

// Warning: this override system locale (here it_IT) and avoids ugly DOUBLE errors
setlocale(LC_NUMERIC,'en');


$mapfile = $aszMapFiles[$_REQUEST['map']][1];

if (!extension_loaded('MapScript'))
{
    dl( $szPHPMapScriptModule );
}

$formfield  = $_REQUEST['formfield'];

$x          = floatval($_REQUEST['mX']);
$y          = floatval($_REQUEST['mY']);

$point      = ms_newPointObj();
$point->setXY($x, $y);

$map        = ms_newMapObj($mapfile);

$numlayers  = $map->numlayers;
$html       = '';
$results    = array();


for($i = 0; $i < $numlayers; $i++){
  $layer = $map->getLayer( $i );
  if($layer->type == MS_LAYER_POINT){
    //echo 'Querying ' . $layer->name . "<br />";
    if(@$layer->queryByPoint($point, MS_MULTIPLE, -1) == MS_SUCCESS){
      // get the number of results
      $layer->open();
      $numresults = intval($layer->getNumResults());
      if($numresults){
        // meta data
        $glayer = $layer->getMetaData('DBTABLE');
        $layerdesc = $layer->getMetaData('DESCRIPTION');
        $html .= '<tr><th colspan="4">Punti nel layer "' . $layerdesc . '"</th></tr>' ."\n";
        $html .= '<tr><td>Nome</td><td>x</td><td>y</td><td>&nbsp;</td></tr>' ."\n";
      }
      // add element to results array for each result row
      for ($j=0; $j < $numresults; $j++)
      {
          // get next shape row
          $result = $layer->getResult($j);
          $shape  = $layer->getShape($result->tileindex, $result->shapeindex);
          // push the row array onto the results array
          $aTmp = $shape->values;
          $aTmp = array_merge( $aTmp ,
              array('x' => $shape->bounds->minx,'y' => $shape->bounds->miny, 'glayer' => $glayer, 'layerdesc'=>$layerdesc));
          array_push( $results, $aTmp );
          $html .= "<tr><td>{$aTmp['titolo']}</td><td>{$aTmp['x']}</td><td>{$aTmp['y']}</td><td><button onclick="setPoint({$aTmp['x']}, {$aTmp['y']})">Imposta punto</button></td></tr>" ."\n";
      // end for loop
      }
      $layer->close();
    }
  }
}

$html = "<table border="1"><tr><td>Posizione cliccata</td><td>$x</td><td>$y</td><td><button onclick="setPoint($x, $y)">Imposta punto</button></td></tr>" . $html . '</table>' ."\n";

$html       = < <<EOT
<html>
<head>
<title>Query puntuale</title>
<style type="text/css">
body {
  background:#dedede;
  font-family: verdana, arial, sans-serif;
  font-size: 11px;
}
table {
  background:#fff;
}
</style>
<script language="javascript">
  function setPoint(x, y){
    //alert('setting point ' + x + ', ' + y);
    self.opener.opener.document.forms[0].{$formfield}_x.value = x;
    self.opener.opener.document.forms[0].{$formfield}_y.value = y;
    window.opener.close();
    window.close(this);
  }
</script>
</head>
<body>
  $html
</body>

EOT;
echo $html;
?>