function add_layer_resource_to_map($layerResourceID, $resourceService, $layerName, $layerLegendLabel, &$map) { $newLayer = new MgLayer($layerResourceID, $resourceService); // Add the new layer to the map's layer collection $newLayer->SetName($layerName); $newLayer->SetVisible(true); $newLayer->SetLegendLabel($layerLegendLabel); $newLayer->SetDisplayInLegend(true); $layerCollection = $map->GetLayers(); if (!$layerCollection->Contains($layerName)) { // Insert the new layer at position 0 so it is at the top // of the drawing order $layerCollection->Insert(0, $newLayer); } return $newLayer; }
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); // Load the Layer Definition and Navigate to the specified <VectorScaleRange> $doc = DOMDocument::loadXML($byteReader->ToString()); $nodeList = $doc->getElementsByTagName('VectorScaleRange'); $vectorScaleRangecElement = $nodeList->item($this->args['SCALERANGEINDEX']); $areaTypeStyle = $vectorScaleRangecElement->getElementsByTagName('AreaTypeStyle')->item(0); // Remove any existing <AreaRule> elements. $areaRuleList = $areaTypeStyle->getElementsByTagName('AreaRule'); $ruleCount = $areaRuleList->length; for ($index = 0; $index < $ruleCount; $index++) { $areaTypeStyle->removeChild($areaRuleList->item(0)); } // Now create the new <AreaRule> elements. $areaRuleTemplate = file_get_contents("templates/arearuletemplate.xml"); $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') { $aggregateOptions->AddFeatureProperty($this->args['PROPERTYNAME']); $aggregateOptions->SelectDistinct(true); $dataReader = $featureService->SelectAggregate($resId, $layer->GetFeatureClassName(), $aggregateOptions); while ($dataReader->ReadNext()) { $value = $this->GetFeaturePropertyValue($dataReader, $this->args['PROPERTYNAME']); $filterText = '"' . $this->args['PROPERTYNAME'] . '" = '; if ($this->args['DATATYPE'] == MgPropertyType::String) { $filterText .= "'" . $value . "'"; } else { $filterText .= $value; } $areaRuleXML = sprintf($areaRuleTemplate, $this->args['PROPERTYNAME'] . ': ' . $value, $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); $areaTypeStyle->appendChild($areaNode); $portion += $increment; } $dataReader->Close(); } else { $values = array(); $aggregateOptions->AddComputedProperty('THEME_VALUE', $this->args['DISTRO'] . '("' . $this->args['PROPERTYNAME'] . '",' . $this->args['NUMRULES'] . ',' . $this->args['MINVALUE'] . ',' . $this->args['MAXVALUE'] . ')'); $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 = '"' . $this->args['PROPERTYNAME'] . '" >= ' . $values[$i] . ' AND "' . $this->args['PROPERTYNAME']; if ($i == count($values) - 1) { $filterText .= '" <= ' . $values[$i + 1]; } else { $filterText .= '" < ' . $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); $areaTypeStyle->appendChild($areaNode); $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; }
function ApplyTheme() { $resourceService = $this->site->CreateService(MgServiceType::ResourceService); $featureService = $this->site->CreateService(MgServiceType::FeatureService); $map = new MgMap($this->site); $map->Open($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 = new DOMDocument(); $doc->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 = $layer->SelectAggregate($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 = '"' . $this->args['PROPERTYNAME'] . '" = '; 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 = new DOMDocument(); $areaDoc->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 = $layer->SelectAggregate($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 = '"' . $this->args['PROPERTYNAME'] . '" >= ' . $values[$i] . ' AND "' . $this->args['PROPERTYNAME']; if ($i == count($values) - 2) $filterText .= '" <= ' . $values[$i + 1]; else $filterText .= '" < ' . $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 = new DOMDocument(); $areaDoc->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(); return $uniqueName; }