Last active
July 24, 2024 15:40
-
-
Save bixb0012/d3d0bc914bc30654b5bedf5da2e98e83 to your computer and use it in GitHub Desktop.
PowerShell: Esri-related
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 5.1 | |
# Reference: 1) https://github.com/Esri/cim-spec | |
# Reference: 2) https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument?view=netframework-4.8.1 | |
# Reference: 3) https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlelement?view=netframework-4.8.1 | |
# Reference: 4) https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-5.1 | |
# Example 1: A function to convert Esri Cartographic Information Model (CIM) XML to a custom PSObject | |
# that has a property for each field in the XML content. | |
function ConvertFrom-EsriXml { | |
<# | |
.Description | |
The ConvertFrom-EsriXML function converts an Esri-based XML Document or | |
XML Element into a custom PSObject that has a property for each XML | |
attribute and node. | |
#> | |
param( | |
[Parameter(Mandatory, ValueFromPipeline)] | |
[ValidateScript({ $_ -is [Xml.XmlElement] -or $_ -is [Xml.XmlDocument] })] | |
$Xml | |
) | |
process { | |
if ($Xml -is [Xml.XmlDocument]) { | |
return (ConvertFrom-EsriXML -Xml $Xml.DocumentElement) | |
} | |
else { | |
$Properties = @{} | |
foreach ($Attribute in $Xml.Attributes) { | |
if ($Attribute.LocalName -ne "type") { continue } | |
$Name = $Attribute.LocalName | |
$Value = $Attribute.('#text').Replace("typens:", "") | |
if ($Value.StartsWith("ArrayOf")) { | |
$Value = $Value.Replace("ArrayOf", "") | |
if ($Value -in "String", "Double", "Int") { | |
$Type = $Value -as [type] | |
[array]$Array = foreach ($Item in $Xml.$Value) { | |
[Convert]::ChangeType($Item, $Type) | |
} | |
} | |
else { | |
[array]$Array = foreach ($Item in $Xml.$Value) { | |
ConvertFrom-EsriXml -Xml $Item | |
} | |
} | |
if (-not $Array) { $Array = @() } | |
return ,$Array | |
} | |
$Properties[$Name] = $Value | |
} | |
foreach ($Node in $Xml.ChildNodes) { | |
$Name = $Node.LocalName | |
$Value = $Node.('#text') | |
if ($Node.HasAttributes) { | |
$Value = ConvertFrom-EsriXml -Xml $Node | |
} | |
else { | |
$Value = switch -regex ($Value) { | |
"^true$" { $true; break } | |
"^false$" { $false; break } | |
"^[+-]?\d+$" { [int64]$_; break } | |
"^[+-]?((\d*\.\d+)|(\d+\.\d*))$" { [double]$_; break } | |
"^[+-]?\d*\.\d+[eE][+-]?\d+$" { [double]$_; break } | |
default { $_ } | |
} | |
} | |
if ($Properties[$Name]) { | |
$Properties[$Name] = $Properties[$Name], $Value | ForEach-Object { $_ } | |
} | |
else { | |
$Properties[$Name] = $Value | |
} | |
} | |
} | |
return [pscustomobject]$Properties | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment