working with selections ...

Paul Spencer pspencer at dmsolutions.ca
Mon Aug 14 17:46:21 EDT 2006


All,

I've been pulling what little hair I have left out of my head over this!

I would like to create a selection in the map using the PHP API.

I have the following code:

$map = new MgMap();
$map->Open($resourceService, $mapName);
$layerObj = $map->GetLayers()->GetItem($layerName);
$featureResId = new MgResourceIdentifier($layerObj->GetFeatureSourceId 
());
$featureGeometryName = $layerObj->GetFeatureGeometryName();
$class = $layerObj->GetFeatureClassName();
$queryOptions = new MgFeatureQueryOptions();
$queryOptions->SetFilter($filter);
$wktRW = new MgWktReaderWriter();
$geom = $wktRW->Read($spatialFilter);
$queryOptions->SetSpatialFilter($featureGeometryName, $geom,  
MgFeatureSpatialOperations::Inside);
$featureReader = $featureService->SelectFeatures($featureResId,  
$class, $queryOptions);
$selection = new MgSelection($map);
$selection->AddFeatures($layerObj, $featureReader, 0);
$selection->Save($resourceService, $mapName);

If I add this ...

echo countFeatures($featureReader);
function countFeatures($featureReader) {
     $i = 0;
     while ($featureReader->ReadNext()) {
         $i++;
     }

     return $i;
}

it tells me that I have the appropriate number of features in the  
feature reader (for the geom and attribute filter chosen) ... in my  
test cases, it was over 7000 features so I limited it spatially to  
about 200 ... but I don't leave this in because it consumes the  
features and nothing gets added to the selection :)

The problem is that after the selection is saved, I don't get  
anything highlighted on the map.  I have another PHP script that  
retrieves the properties of selected features.  It works if I use the  
mapagent to select the features but it doesn't work if I use this php  
script ... well, it reports that there is one layer with a selection  
but no features on it.

The problem occurs if I use just a basic filter, just a spatial  
filter, or both combined.  It also occurs regardless of the number of  
features selected from 1 to the full 7000+.

I also tried using my second php script inline to read the selection  
info and no joy.  The script that reads the selection info looks like:

$layers = $selection->GetLayers();
$layer = $layers->GetItem(0); //yes there is one layer in the result
$layerClassName = $layer->GetFeatureClassName();
$selectionString = $selection->GenerateFilter($layer, $layerClassName);
$layerFeatureId = $layer->GetFeatureSourceId();
$layerFeatureResource = new MgResourceIdentifier($layerFeatureId);
$queryOptions->SetFilter($selectionString);
$featureReader = $featureService->SelectFeatures 
($layerFeatureResource, $layerClassName, $queryOptions);
//... etc etc etc

The feature class name is the same in both cases.  The filter seems  
appropriate.

I am obviously missing something here but I cannot see what it is.  I  
did try saving the $map as well, with no obvious change.  Or is it a  
bug?  Oh, I also tried saving the selection as XML and creating a new  
selection and initializing it from the XML but that didn't seem to  
change anything either.

Cheers

Paul

+-----------------------------------------------------------------+
|Paul Spencer                           pspencer at dmsolutions.ca   |
+-----------------------------------------------------------------+
|Applications & Software Development                              |
|DM Solutions Group Inc                 http://www.dmsolutions.ca/|
+-----------------------------------------------------------------+








More information about the Mapguide-users mailing list