[Mapserver-users] gen2shp + Shape Index query problem + mysql + mapscript

Eduin Yesid Carriillo yecarrillo at hotmail.com
Sun Apr 20 12:46:03 EDT 2003


I developed a routine to convert SQL data to Shape following these steps:

1. First Query:
$query = "SELECT code, coord_x, coord_y FROM ... INTO OUTFILE $genFile ...";
$result = mysql_query($query,$dbi);

2. Shape Generation
$cmd = "$exeFile \"$shpFile\" points < \"$genFile\"";
$execRes = system($cmd, $state);
unlink ($shpFile.".dbf")

3. Query database one more time:
$query = "SELECT code, attrib1, attrib2.. FROM ... ";
$result = mysql_query($query,$dbi);

4. DBF Generation:
  $fields = mysql_num_fields($result);

  $def = Array();
  while ($i < $fields) {
      $type  = mysql_field_type  ($result, $i);
      if ($type == "int" or $type == "real") {$dtype = "N";} else {$dtype =
"C";}
      $name  = mysql_field_name  ($result, $i);
      $len   = mysql_field_len   ($result, $i);
      $flags = mysql_field_flags ($result, $i);

      array_push($def, array("$name", "$dtype", $len, 4));
      $i++;
  }

  $dbfid = dbase_create($dbfFile.".dbf", $def);
  if (!$dbfid) {
    $Result = "Error '".$dbfFile."'\\n";
  } else {

    $i=0;
    while($row = mysql_fetch_row($result)) {
      $i++;
      dbase_add_record ($dbfid, $row);
    }

  }
  dbase_close ($dbfid);


The process works fine and it generates the shp, dbf and shx files. ArcView
is able to open the files and the selections also work.  Mapserver shows the
generated points in a normal way.

However, in MapScript, the selections don't show any result. It probably
breaks up the relationship of the index of the shape with the dbf. The
problem it seems to be in this code piece:


for($iLayer=0; $iLayer < $gpoMap->numlayers; $iLayer++)   {
        $oLayer = $gpoMap->GetLayer($iLayer);
        $numResults = $oLayer->getNumResults();

        if ($numResults == 0)  continue;

        $oLayer->open($gpoMap->shapepath);
        $selFields = explode(" ", $oLayer->getMetaData("RESULT_FIELDS"));

        for ($iRes=0; $iRes < $numResults; $iRes++)   {
                    $oRes = $oLayer->getResult($iRes);
                    $oShape =
$oLayer->getShape($oRes->tileindex,$oRes->shapeindex);

                    for($iField=0; $iField < sizeof($selFields); $iField++)
{
                        echo $oShape->values[$selFields[$iField]];
                    }
        }
}

Notice the following functions
- Layer->getNumResults  : get the number of results OK!
- Layer->getResult :  ...
- Layer->getShape : ...
- Shape->values : Empty array

Does somebody know an alternative way of generation of the shape file?

Thank you

Eduin Yesid Carrillo
Ingeniero Civil
COLOMBIA





More information about the mapserver-users mailing list