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

Eric Bridger eric at gomoos.org
Mon Apr 21 02:06:08 PDT 2003


Eduin,
Your generation of the shape file and dbf file looks fine.  I think the
problem is in your mapscript code as you said.

You need to also open the dbf file. The shapeindex in the shape file is
also the index into the dbf file.
I'm not sure of the PHP mapscript syntax for this,but in perl mapscript:

use Xbase;
my $dbfile = $oLayer->data . '.dbf';
my $hDBF = new XBase "$dbfile";
...

my @record = $hDBF->get_record($oRes->shapeindex);

Eric Bridger
www.gomoos.org


> 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
> 
> 
> _______________________________________________
> Mapserver-users mailing list
> Mapserver-users at lists.gis.umn.edu
> http://lists.gis.umn.edu/mailman/listinfo/mapserver-users
> 





More information about the MapServer-users mailing list