[mapserver-users] Querying an external database

Michael Schulz mschulz at webgis.de
Thu Jul 18 09:23:06 EDT 2002


Hi,

i am not sure, but i think the only way of connecting an external
database to specific shapes in a shapefile is only possible via the
shp-id. If your query to the db results in a list of shp-ids then you
could loop through the shapes in your shapefile and reclassify the
matching shapes. It is not really a query anymore (in mapserver terms).
Something like this:

$map = ms_newMapObj('mapfile');
$link = mysql_connect( $host, $user, $pwd );  // connect to the db
mysql_select_db("db_name");                   //
$qstring = "select blabla from blablabla";    // and query it,
$qresult = mysql_query($qstring);             // the result should give
you a list of shp-ids
$shplayer = $map->GetLayerByName("layer");    // get the layer
$shplayer->open($map->shapepath);             // open the layer,
necessary for getshape()
while ($line = mysql_fetch_array($qresult, MYSQL_ASSOC))
{
  $oShape = $shplayer->getShape(-1, (int) $line["field_with_shpid"]);
// get the shape with shapeindex from db
  $oShape->set("classindex", 1);          // assign this shape a new
classindex (that must exist in the mapfile)
  $oShape->draw($map, $shplayer, $image, "d"); // draw this shape on the
map
  $oShape->free();
}
mysql_free_result($qresult);
$shplayer->close();

Well, this just a sketch, i used similiar code to reclassify a layer
based on values from an external db.
Hope it helps.

Cheers, Michael



Alessandro Cadeddu schrieb:

> Hi all, I'd like to build an application which allow the user to query
> an external database, for example querying the attribute table in
> MySQl or Access
> database, and gets a list of ID. Then I need to query the shapefile to
> highlight all the returned ID.How can I do this?How can I link the
> returned ID from the external database with the shapefile ID?
> How can I query the shapefile and highlight the results in
> PHP/MapScript ?Can anyone send me an example?
>
>
> Thanks very
> much ____________________________________________________________________
> Alessandro Cadeddu            E-mail: acadeddu at crs4.it
>   CRS4 - GIS
>   Sesta Strada Ovest              Phone: +39 070 2796.293
>   Z.I. Macchiareddu
>   C.P. 94 - 09010 Uta (CA), Italy
> ____________________________________________________________________

--
-----------------------------------------------------------
Michael Schulz                                in medias res
Dipl.-Geologe                              Gesellschaft für
                                Informationstechnologie mbH
                             Sautierstr. 38, 79104 Freiburg
                                     0761 55695-95 (Fax 96)
mschulz at webgis.de                             www.webgis.de







More information about the mapserver-users mailing list