Esempio n. 1
0
function GetFeatureCount($featuresId, $schemaName, $className, $resourceSrvc, $featureSrvc)
{
    //Try the SelectAggregate shortcut. This is faster than raw spinning a feature reader
    //
    //NOTE: If MapGuide supported scrollable readers like FDO, we'd have also tried
    //that as well.
    $totalEntries = -1;
    $featureName = $schemaName . ":" . $className;
    $canCount = false;
    $gotCount = false;
    $fsBr = $resourceSrvc->GetResourceContent($featuresId);
    $fsXml = $fsBr->ToString();
    $fsDoc = DOMDocument::loadXML($fsXml);
    $providerNodeList = $fsDoc->getElementsByTagName("Provider");
    $providerName = $providerNodeList->item(0)->nodeValue;
    $capsBr = $featureSrvc->GetCapabilities($providerName);
    $capsXml = $capsBr->ToString();
    //This should be good enough to find out if Count() is supported
    $canCount = !(strstr($capsXml, "<Name>Count</Name>") === false);
    if ($canCount) {
        $clsDef = $featureSrvc->GetClassDefinition($featuresId, $schemaName, $className);
        $idProps = $clsDef->GetIdentityProperties();
        if ($idProps->GetCount() > 0) {
            $pd = $idProps->GetItem(0);
            $expr = "COUNT(" . $pd->GetName() . ")";
            $query = new MgFeatureAggregateOptions();
            $query->AddComputedProperty("TotalCount", $expr);
            try {
                $dataReader = $featureSrvc->SelectAggregate($featuresId, $featureName, $query);
                if ($dataReader->ReadNext()) {
                    // When there is no data, the property will be null.
                    if ($dataReader->IsNull("TotalCount")) {
                        $totalEntries = 0;
                        $gotCount = true;
                    } else {
                        $ptype = $dataReader->GetPropertyType("TotalCount");
                        switch ($ptype) {
                            case MgPropertyType::Int32:
                                $totalEntries = $dataReader->GetInt32("TotalCount");
                                $gotCount = true;
                                break;
                            case MgPropertyType::Int64:
                                $totalEntries = $dataReader->GetInt64("TotalCount");
                                $gotCount = true;
                                break;
                        }
                        $dataReader->Close();
                    }
                }
            } catch (MgException $ex) {
                $gotCount = false;
            }
        }
    }
    if ($gotCount == false) {
        $featureReader = null;
        try {
            $featureReader = $featureSrvc->SelectFeatures($featuresId, $featureName, null);
        } catch (MgException $ex) {
            $totalEntries = -1;
            //Can't Count() or raw spin? Oh dear!
        }
        if ($featureReader != null) {
            while ($featureReader->ReadNext()) {
                $totalEntries++;
            }
            $featureReader->Close();
        }
    }
    return $totalEntries;
}
Esempio n. 2
0
 function ApplyTheme()
 {
     $resourceService = $this->site->CreateService(MgServiceType::ResourceService);
     $featureService = $this->site->CreateService(MgServiceType::FeatureService);
     $map = new MgMap();
     $map->Open($resourceService, $this->args['MAPNAME']);
     $layers = $map->GetLayers();
     $layer = $layers->GetItem($this->args['LAYERNAME']);
     $resId = new MgResourceIdentifier($layer->GetFeatureSourceId());
     $layerDefResId = $layer->GetLayerDefinition();
     $byteReader = $resourceService->GetResourceContent($layerDefResId);
     $filter = $layer->GetFilter();
     // Load the Layer Definition and Navigate to the specified <VectorScaleRange>
     $doc = DOMDocument::loadXML($byteReader->ToString());
     $version = $doc->documentElement->getAttribute('version');
     $template = 'templates/arearuletemplate-' . $version . '.xml';
     $layerDefList = $doc->getElementsByTagName('VectorLayerDefinition');
     $layerDef = $layerDefList->item(0);
     $nodeList = $layerDef->getElementsByTagName('VectorScaleRange');
     $vectorScaleRangecElement = $nodeList->item($this->args['SCALERANGEINDEX']);
     $listLength = $nodeList->length;
     for ($index = 0; $index < $listLength; $index++) {
         $layerDef->removeChild($nodeList->item(0));
     }
     $layerDef->appendChild($vectorScaleRangecElement);
     $areaTypeStyle = $vectorScaleRangecElement->getElementsByTagName('AreaTypeStyle')->item(0);
     // Remove any existing <AreaRule> elements.
     if ($areaTypeStyle != null) {
         $areaRuleList = $areaTypeStyle->getElementsByTagName('AreaRule');
         $ruleCount = $areaRuleList->length;
         for ($index = 0; $index < $ruleCount; $index++) {
             $areaTypeStyle->removeChild($areaRuleList->item(0));
         }
         $hasChild = $areaTypeStyle->hasChildNodes();
         if ($hasChild) {
             $element = $areaTypeStyle->childNodes->item(0);
         }
     }
     $CompositeTypeStyles = $vectorScaleRangecElement->getElementsByTagName('CompositeTypeStyle');
     $isEnhancedStyle = $CompositeTypeStyles->length != 0;
     // Remove any existing <CompositeTypeStyle> elements with Polygon <GeometryContext>.
     if ($isEnhancedStyle) {
         $template = 'templates/arearuletemplate-' . $version . '-Enhanced.xml';
         $styleCount = $CompositeTypeStyles->length;
         for ($index = 0; $index < $styleCount; $index++) {
             $CompositeTypeStyle = $CompositeTypeStyles->item($index);
             $GeometryContexts = $CompositeTypeStyle->getElementsByTagName('GeometryContext');
             $contextCount = $GeometryContexts->length;
             for ($i = 0; $i < $contextCount; $i++) {
                 $GeometryContext = $GeometryContexts->item($i);
                 if ($GeometryContext->nodeValue == 'Polygon') {
                     $vectorScaleRangecElement->removeChild($CompositeTypeStyle);
                     $index--;
                     $styleCount--;
                     break;
                 }
             }
         }
     }
     // Now create the new <AreaRule> or <CompositeTypeStyle> elements.
     $areaRuleTemplate = file_get_contents($template);
     $aggregateOptions = new MgFeatureAggregateOptions();
     $portion = 0.0;
     $increment = $this->args['NUMRULES'] > 1 ? $increment = 1.0 / ($this->args['NUMRULES'] - 1) : 1.0;
     if ($this->args['DISTRO'] == 'INDIV_DIST') {
         $values = array();
         $aggregateOptions->AddComputedProperty('THEME_VALUE', 'UNIQUE("' . $this->args['PROPERTYNAME'] . '")');
         if ($filter != '') {
             $aggregateOptions->SetFilter($filter);
         }
         $dataReader = $featureService->SelectAggregate($resId, $layer->GetFeatureClassName(), $aggregateOptions);
         while ($dataReader->ReadNext()) {
             $value = $this->GetFeaturePropertyValue($dataReader, 'THEME_VALUE');
             array_push($values, $value);
         }
         $dataReader->Close();
         if ($this->args['DATATYPE'] == MgPropertyType::String) {
             sort($values, SORT_STRING);
         } else {
             sort($values, SORT_NUMERIC);
         }
         for ($i = 0; $i < count($values); $i++) {
             $filterText = '&quot;' . $this->args['PROPERTYNAME'] . '&quot; = ';
             if ($this->args['DATATYPE'] == MgPropertyType::String) {
                 $filterText .= "'" . $values[$i] . "'";
             } else {
                 $filterText .= $values[$i];
             }
             $areaRuleXML = sprintf($areaRuleTemplate, $this->args['PROPERTYNAME'] . ': ' . $values[$i], $filterText, $this->InterpolateColor($portion, $this->args['FILLFROM'], $this->args['FILLTO'], $this->args['FILLTRANS']), $this->InterpolateColor($portion, $this->args['LINEFROM'], $this->args['LINETO'], 0));
             $areaDoc = DOMDocument::loadXML($areaRuleXML);
             $areaNode = $doc->importNode($areaDoc->documentElement, true);
             if ($areaTypeStyle != null) {
                 if ($hasChild) {
                     $areaTypeStyle->insertBefore($areaNode, $element);
                 } else {
                     $areaTypeStyle->appendChild($areaNode);
                 }
             }
             if ($isEnhancedStyle) {
                 $compositeTypeStyle = $doc->createElement('CompositeTypeStyle');
                 $compositeTypeStyle->appendChild($areaNode);
                 $vectorScaleRangecElement->appendChild($compositeTypeStyle);
             }
             $portion += $increment;
         }
     } else {
         $values = array();
         $aggregateOptions->AddComputedProperty('THEME_VALUE', $this->args['DISTRO'] . '("' . $this->args['PROPERTYNAME'] . '",' . $this->args['NUMRULES'] . ',' . $this->args['MINVALUE'] . ',' . $this->args['MAXVALUE'] . ')');
         if ($filter != '') {
             $aggregateOptions->SetFilter($filter);
         }
         $dataReader = $featureService->SelectAggregate($resId, $layer->GetFeatureClassName(), $aggregateOptions);
         while ($dataReader->ReadNext()) {
             $value = $this->GetFeaturePropertyValue($dataReader, 'THEME_VALUE');
             array_push($values, $value);
         }
         $dataReader->Close();
         for ($i = 0; $i < count($values) - 1; $i++) {
             $filterText = '&quot;' . $this->args['PROPERTYNAME'] . '&quot; &gt;= ' . $values[$i] . ' AND &quot;' . $this->args['PROPERTYNAME'];
             if ($i == count($values) - 2) {
                 $filterText .= '&quot; &lt;= ' . $values[$i + 1];
             } else {
                 $filterText .= '&quot; &lt; ' . $values[$i + 1];
             }
             $areaRuleXML = sprintf($areaRuleTemplate, $this->args['PROPERTYNAME'] . ': ' . $values[$i] . ' - ' . $values[$i + 1], $filterText, $this->InterpolateColor($portion, $this->args['FILLFROM'], $this->args['FILLTO'], $this->args['FILLTRANS']), $this->InterpolateColor($portion, $this->args['LINEFROM'], $this->args['LINETO'], 0));
             $areaDoc = DOMDocument::loadXML($areaRuleXML);
             $areaNode = $doc->importNode($areaDoc->documentElement, true);
             if ($areaTypeStyle != null) {
                 if ($hasChild) {
                     $areaTypeStyle->insertBefore($areaNode, $element);
                 } else {
                     $areaTypeStyle->appendChild($areaNode);
                 }
             }
             if ($isEnhancedStyle) {
                 $compositeTypeStyle = $doc->createElement('CompositeTypeStyle');
                 $compositeTypeStyle->appendChild($areaNode);
                 $vectorScaleRangecElement->appendChild($compositeTypeStyle);
             }
             $portion += $increment;
         }
     }
     // Now save our new layer definition to the session and add it to the map.
     $layerDefinition = $doc->saveXML();
     $uniqueName = $this->MakeUniqueLayerName($map, $this->args['LAYERNAME'], $this->args['THEMENAME']);
     $legendLabel = $layer->GetLegendLabel();
     if (strlen(trim($this->args['THEMENAME'])) > 0) {
         $legendLabel .= ' (' . $this->args['THEMENAME'] . ')';
     }
     $layerResId = new MgResourceIdentifier('Session:' . $this->args['SESSION'] . '//' . $uniqueName . '.LayerDefinition');
     $byteSource = new MgByteSource($layerDefinition, strlen($layerDefinition));
     $resourceService->SetResource($layerResId, $byteSource->GetReader(), null);
     $newLayer = new MgLayer($layerResId, $resourceService);
     $newLayer->SetName($uniqueName);
     $newLayer->SetLegendLabel($legendLabel);
     $newLayer->SetDisplayInLegend($layer->GetDisplayInLegend());
     $newLayer->SetVisible(true);
     $newLayer->SetSelectable($layer->GetLegendLabel());
     $layers->Insert($layers->IndexOf($layer), $newLayer);
     $map->Save($resourceService);
     return $uniqueName;
 }
Esempio n. 3
0
 private function GetFeatureClassMBR($featuresId, $className, $geomProp, $featureSrvc)
 {
     $extentGeometryAgg = null;
     $extentGeometrySc = null;
     $extentByteReader = null;
     $mbr = new stdClass();
     $geomName = $geomProp->GetName();
     $spatialContext = $geomProp->GetSpatialContextAssociation();
     // Finds the coordinate system
     $agfReaderWriter = new MgAgfReaderWriter();
     $spatialcontextReader = $featureSrvc->GetSpatialContexts($featuresId, false);
     while ($spatialcontextReader->ReadNext()) {
         if ($spatialcontextReader->GetName() == $spatialContext) {
             $mbr->coordinateSystem = $spatialcontextReader->GetCoordinateSystemWkt();
             // Finds the extent
             $extentByteReader = $spatialcontextReader->GetExtent();
             break;
         }
     }
     $spatialcontextReader->Close();
     if ($extentByteReader != null) {
         // Get the extent geometry from the spatial context
         $extentGeometrySc = $agfReaderWriter->Read($extentByteReader);
     }
     // Try to get the extents using the selectaggregate as sometimes the spatial context
     // information is not set
     $aggregateOptions = new MgFeatureAggregateOptions();
     $featureProp = 'SPATIALEXTENTS("' . $geomName . '")';
     $aggregateOptions->AddComputedProperty('EXTENTS', $featureProp);
     try {
         $dataReader = $featureSrvc->SelectAggregate($featuresId, $className, $aggregateOptions);
         if ($dataReader->ReadNext()) {
             // Get the extents information
             $byteReader = $dataReader->GetGeometry('EXTENTS');
             $extentGeometryAgg = $agfReaderWriter->Read($byteReader);
         }
         $dataReader->Close();
     } catch (MgException $e) {
         if ($extentGeometryAgg == null) {
             //We do have one last hope. EXTENT() is an internal MapGuide custom function that's universally supported
             //as it operates against an underlying select query result. This raw-spins the reader server-side so there
             //is no server -> web tier transmission overhead involved.
             try {
                 $aggregateOptions = new MgFeatureAggregateOptions();
                 $aggregateOptions->AddComputedProperty("COMP_EXTENT", "EXTENT(" . $geomName . ")");
                 $dataReader = $featureSrvc->SelectAggregate($featuresId, $className, $aggregateOptions);
                 if ($dataReader->ReadNext()) {
                     // Get the extents information
                     $byteReader = $dataReader->GetGeometry('COMP_EXTENT');
                     $extentGeometryAgg = $agfReaderWriter->Read($byteReader);
                 }
                 $dataReader->Close();
             } catch (MgException $e2) {
             }
         }
     }
     $mbr->extentGeometry = null;
     // Prefer SpatialExtents() of EXTENT() result over spatial context extent
     if ($extentGeometryAgg != null) {
         $mbr->extentGeometry = $extentGeometryAgg;
     }
     if ($mbr->extentGeometry == null) {
         //Stil null? Now try spatial context
         if ($extentGeometrySc != null) {
             $mbr->extentGeometry = $extentGeometrySc;
         }
     }
     return $mbr;
 }
Esempio n. 4
0
 public static function GetDistinctValues($featSvc, $fsId, $schemaName, $className, $distinctPropName)
 {
     $values = array();
     $query = new MgFeatureAggregateOptions();
     $query->AddComputedProperty("RESULT", "UNIQUE({$distinctPropName})");
     $rdr = $featSvc->SelectAggregate($fsId, "{$schemaName}:{$className}", $query);
     while ($rdr->ReadNext()) {
         array_push($values, self::GetBasicValueFromReader($rdr, "RESULT"));
     }
     $rdr->Close();
     return $values;
 }