[ka-Map-users] Searching using kaMap
Pēteris Brūns
peteris.bruns at gmail.com
Fri Apr 21 09:09:13 EDT 2006
Here is simple serch code for ka-map!
<?
include_once( '../include/config.php' );
if (!extension_loaded('MapScript'))
{
dl( $szPHPMapScriptModule );
}
$results = array();
$oMap = ms_newMapObj($szMapFile);
//$map=$oMap; //jus for test
$aLayers = $oMap->getAllLayerNames(); //get all layers in map file
$tot = $oMap->numlayers; // total layers-count
//------------------------------------------------------------------------------
foreach($aLayers AS $layer) {
$oLayer = $oMap->getLayerByName($layer);
/* detect if group should be searchable In yor mapfile sercahable layer
will be detected just like "queryable" "true" but her use "searchfield"
"field_name"*/
$szSearchfield = $oLayer->getMetaData('searchfield');
if ($oLayer->getMetaData( "searchfield" ) != "") {
$szSearchfield = $szSearchfield;
//----------------------------------------------------
//postgis connect (NOT TESTED IF is possible to test it culd be nice to know
works it or not)
if($oLayer->connectiontype == MS_POSTGIS){
$searchstring = $szSearchfield . ' ~* \'' . $searchstring
.'\' ';
} else { // Shapefile
$numclasses = $oLayer->numclasses;
for($i = 1 ; $i < $numclasses; $i++){
}
// Second HACK: it work
if($numclasses > 1){
$class = $oLayer->getClass(0);
// Match all
$class->setExpression('/.*/');
}
}
#----------------------------------------------------------------------------------------------
if(@$oLayer->queryByAttributes($szSearchfield, $searchstring,
MS_MULTIPLE) == MS_SUCCESS ){ //MS_SUCCESS
$oLayer->open();
// Add element to results array for each result row
for ($j=0; $j < $oLayer->getNumResults(); $j++)
{
// get next shape row
$result = $oLayer->getResult($j);
$shape = $oLayer->getShape($result->tileindex,
$result->shapeindex);
//var_dump($shape);
// push the row array onto the results array
$aTmp = $shape->values;
//Calculate centroid
$x_c = ($shape->bounds->minx + $shape->bounds->maxx) /
2; //get x average value
$y_c = ($shape->bounds->miny + $shape->bounds->maxy) /
2; //get y average value
//$aTmp = array_merge( $aTmp , array('x' => $x,'y' =>
$y, 'id' => $result->shapeindex));
$xmin = $shape->bounds->minx;
$xmax = $shape->bounds->maxx;
$ymin = $shape->bounds->miny;
$ymax = $shape->bounds->maxy;
$aTmp = array_merge( $aTmp , array('xmin' =>
$xmin,'xmax' => $xmin,'ymin' => $ymin,'ymax' =>
$ymax,'x_c'=>$x_c,'y_c'=>$y_c, 'id' => $result->shapeindex));
$results[$layername][] = $aTmp;
// end for loop
}
//echo implode
printer($results,$szSearchfield,$layer); //printing
}
else{
print('No results returned!<br> Searched string:' . "\n" .
$searchstring);
}
$oLayer->close();
//----------------------------------------------------
$results=flush($results);
}
else{
$szSearchfield = "Not defined searchfield";
}
}
function search($szSearchfield,$searchstring){
//----------------------------------------------------
}
//-------------------------------------create print
-----------------------------
function printer($results,$szSearchfield,$layer){
foreach($results as $key_val => $value) {
echo "<br>Returned results for layer:<b>".$layer."</b><br>"; // top for
each layer
foreach($value as $key_val => $v) {
$m = iconv("latin1", "UTF-8", $v[strtoupper($szSearchfield)]);
$field=$v[strtoupper($szSearchfield)];
//In $m place you can use $v[strtoupper($szSearchfield)] this is used for
iconv etc..
print "<a href=#
onClick=\"myKaMap.zoomToExtents(".$v["xmin"].",".$v["ymin"].",".$v["xmax"].",".$v["ymax"].")\"
onMouseOver=\"toolTip.moveGeo(".$v["x_c"].",".$v["y_c"]."),
toolTip.setText('".$m."')\"
onMouseOut=\"toolTip.move()\">".$m."</a><br>\n";
}
}
}
?>
links to working examples:
http://85.234.167.38/js/ka-map/htdocs/index_tooltip_search.html
and http://85.234.167.38/js/ka-map/htdocs/index_search.html
On 4/21/06, Paul Spencer <pspencer at dmsolutions.ca> wrote:
>
> Ramesh,
>
> the bounds of the shape should be a RectObj. This means you can use
> the project() function to reproject it.
>
> see: http://mapserver.gis.umn.edu/docs/reference/phpmapscript-class/
> classes/rectobj
>
> You would need to do something like:
>
> $shp->bounds->project($oLayer->getProjection(),$oMap->getProjection());
>
> I'm not sure if you can actually do this to a rectObj that is part of
> a shape. To be safe, you can do this:
>
> $rect = ms_newRectObj();
> $rect->setExtent($shp->bounds->minx,$shp->bounds->miny,$shp->bounds-
> >maxx,$shp->bounds->maxy);
>
> $rect->project($oLayer->getProjection(),$oMap->getProjection());
>
> Cheers
>
> Paul
>
> On 20-Apr-06, at 10:36 PM, ramesh karra wrote:
>
> > Hi paul,
> >
> > Hope it is OK to send this mail directly to you. If
> > not, let me know, I will send this to mapservers list.
> >
> > I made some progress in writing in the php script to
> > retrieve data from the dbf file. The data in the dbf
> > file is in lat+long projection and I am different
> > output projection to display the lines/points
> > correctly. How do I get the data retrieved from the
> > dbf file converted to my output projection?
> >
> > $oMap = ms_newMapObj($szMapFile);
> >
> >
> > $oLayer = $oMap->getLayerByName("mylayer");
> > $oLayer->queryByAttributes("Name", "xxxx",
> > MS_SINGLE);
> >
> > if ($oLayer->getNumResults() > 0) {
> > $r = $oLayer->getResult(0);
> > $oLayer->open();
> > $shp = $oLayer->getShape($r->tileindex,
> > $r->shapeindex);
> > $ext = $shp->bounds;
> > $shp->free();
> > $oLayer->close();
> >
> > print $ext->minx;
> > }
> >
> > Basically, the above script works and prints the value
> > but it is in lat+long projection (like 71.231789), I
> > need to convert this to my output projection. I am
> > hoping that there some function to do this because
> > mapserver is already doing it while displaying my map.
> >
> > Thanks
> > Ramesh
> >
> >
> > --- Paul Spencer <pspencer at dmsolutions.ca> wrote:
> >
> >> Ramesh, there are soooo many! I don't even know
> >> where to start. Try
> >> googling php ajax and see what you get :)
> >>
> >> Cheers
> >>
> >> Paul
> >>
> >> On 13-Apr-06, at 7:00 PM, ramesh karra wrote:
> >>
> >>> Thanks for the quick reply paul!
> >>>
> >>> I am relatively new to both PHP and AJAX. To
> >> achieve
> >>> what you suggested, could you also suggest some
> >> online
> >>> tutorial/examples or books?
> >>>
> >>> Thanks again,
> >>> Ramesh
> >>>
> >>> --- Paul Spencer <pspencer at dmsolutions.ca> wrote:
> >>>
> >>>> Ramesh,
> >>>>
> >>>> I would write a small php script to do the
> >>>> interaction with the dbf
> >>>> file and call it from my page using ajax.
> >> Actually,
> >>>> I'd use postgis
> >>>> but that's another discussion ;)
> >>>>
> >>>> Cheers
> >>>>
> >>>> Paul
> >>>>
> >>>> On 13-Apr-06, at 6:47 PM, ramesh karra wrote:
> >>>>
> >>>>> Hi,
> >>>>>
> >>>>> I have 100s of points in shapefiles(.shp,.shx
> >> and
> >>>>> .dbf).
> >>>>> that I use with my maps to show a place and
> >> label
> >>>> it.
> >>>>> Is it possible to access the .dbf file in kaMap?
> >>>>> I would like to implement a feature to search
> >> for
> >>>> a
> >>>>> point and when a point of interest is found,
> >> zoom
> >>>> to
> >>>>> that place. Is it possible to do this in kaMap
> >>>> without
> >>>>> having to create an XML file from .dbf file?
> >>>>>
> >>>>> Thanks
> >>>>> Ramesh
> >>>>>
> >>>>>
> >> __________________________________________________
> >>>>> Do You Yahoo!?
> >>>>> Tired of spam? Yahoo! Mail has the best spam
> >>>> protection around
> >>>>> http://mail.yahoo.com
> >>>>> _______________________________________________
> >>>>> ka-Map-users mailing list
> >>>>> ka-Map-users at lists.maptools.org
> >>>>>
> >>>>
> >>>
> >>
> > http://lists.maptools.org/mailman/listinfo/ka-map-users
> >>>>
> >>>>
> >>>
> >>
> > +-----------------------------------------------------------------+
> >>>> |Paul Spencer
> >>>> pspencer at dmsolutions.ca |
> >>>>
> >>>
> >>
> > +-----------------------------------------------------------------+
> >>>> |Applications & Software Development
> >>>> |
> >>>> |DM Solutions Group Inc
> >>>> http://www.dmsolutions.ca/|
> >>>>
> >>>
> >>
> > +-----------------------------------------------------------------+
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>
> >>>
> >>> __________________________________________________
> >>> Do You Yahoo!?
> >>> Tired of spam? Yahoo! Mail has the best spam
> >> protection around
> >>> http://mail.yahoo.com
> >>
> >>
> > +-----------------------------------------------------------------+
> >> |Paul Spencer
> >> pspencer at dmsolutions.ca |
> >>
> > +-----------------------------------------------------------------+
> >> |Applications & Software Development
> >> |
> >> |DM Solutions Group Inc
> >> http://www.dmsolutions.ca/|
> >>
> > +-----------------------------------------------------------------+
> >>
> >>
> >>
> >>
> >>
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam? Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
>
> +-----------------------------------------------------------------+
> |Paul Spencer pspencer at dmsolutions.ca |
> +-----------------------------------------------------------------+
> |Applications & Software Development |
> |DM Solutions Group Inc http://www.dmsolutions.ca/|
> +-----------------------------------------------------------------+
>
>
>
>
> _______________________________________________
> ka-Map-users mailing list
> ka-Map-users at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/ka-map-users
>
--
pb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maptools.org/pipermail/ka-map-users/attachments/20060421/d6a249a9/attachment-0001.html
More information about the ka-Map-users
mailing list