Skip to content

FAQ/ PowerShell script to import embankment data into DioVISTA

We are creating a PowerShell script to import the embankment data stored in a CSV file into DioVISTA.
However, the following error message appears, and the process cannot be performed as intended.

Unable to convert argument "0" (value "...") of "AppendChild" to type "System.Xml.XmlNode".

How do I fix this?

response

Try the PowerShell script below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#プロジェクトに盛土を追加するスクリプト
# プロジェクトファイルを指定する(入力)
$fsxproj = ".\sample.fsxproj"

# プロジェクトファイルを指定する(出力)
$newFsxproj = ".\sample_addEmbankment.fsxproj"

# csvファイルを指定する(入力)
$csv = Import-Csv -Encoding UTF8 ".\Embankment.csv"

#プロジェクトの読み込み
$fsxprojInfo = Get-Item $fsxproj
[xml]$xml = (cat -Encoding UTF8 $fsxproj)

# Embankment.csvから読み込んだデータを盛土としてプロジェクトに追加
foreach ($id in ($csv | select -Unique "ID")) {
    $CoordInfos = $csv | where { $_.ID -eq $id.ID }
    $firstCoordInfo = $CoordInfos[0]
    $newFill = $xml.CreateElement("fill")
    [void]$xml.floodSim.conditions.fills.AppendChild($newFill)

    $attrs = @{
        "name"       = "盛土" + $firstCoordInfo.ID.ToString()
        "upperWidth" = $firstCoordInfo.Width.ToString()
        "lowerWidth" = $firstCoordInfo.Width.ToString()
        "valid"      = "True"
        "lineStyle"  = "0 2 255 80 80 255"
        "display3d"  = "False"
    }
    $attrs.GetEnumerator() | % {
        $newAttribute = $xml.CreateAttribute($_.Name)
        $newAttribute.Value = $_.Value
        [void]$newFill.Attributes.Append($newAttribute)
    }

    $newLine = $xml.CreateElement("line")
    [void]$newFill.AppendChild($newLine)
    foreach ($coordInfo  in $CoordInfos) {
        $y = ([double]$coordInfo.Y * [Math]::PI / 180.0)
        $x = ([double]$coordInfo.X * [Math]::PI / 180.0)
        $coordText = "{0:0.000000000000000} {1:0.000000000000000} {2} {3}" -f $y, $x, $coordInfo.Hhigh, $coordInfo.Hlow
        $coordValue = $xml.CreateTextNode($coordText)
        $newCoord = $xml.CreateElement("coord")
        [void]$newCoord.AppendChild($coordValue)
        [void]$newLine.AppendChild($newCoord)
    }
}
$xml.Save((Join-Path $fsxprojInfo.DirectoryName $newFsxproj))

Here is an example of fill data: Paste it into a text editor and save it as "Embankment.csv". The encoding is UTF8.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
ID,Width,X,Y,Hlow,Hhigh
1,20,140.03000000,36.00000000,10,15
1,20,140.02707107,36.00707107,10,15
1,20,140.02000000,36.01000000,10,15
1,20,140.01292893,36.00707107,10,15
1,20,140.01000000,36.00000000,10,15
1,20,140.01292893,35.99292893,10,15
1,20,140.02000000,35.99000000,10,15
1,20,140.02707107,35.99292893,10,15
1,20,140.03000000,36.00000000,10,15
2,15,140.00000000,36.01000000,0,4
2,15,140.00000000,35.99000000,0,4
3,15,140.01000000,36.00000000,0,3
3,15,140.00923880,36.00382683,0,3
3,15,140.00707107,36.00707107,0,3
3,15,140.00382683,36.00923880,0,3
3,15,140.00000000,36.01000000,0,3
3,15,139.99617317,36.00923880,0,3
3,15,139.99292893,36.00707107,0,3
3,15,139.99076120,36.00382683,0,3
3,15,139.99000000,36.00000000,0,3
3,15,139.99076120,35.99617317,0,3
3,15,139.99292893,35.99292893,0,3
3,15,139.99617317,35.99076120,0,3
3,15,140.00000000,35.99000000,0,3
3,15,140.00382683,35.99076120,0,3
3,15,140.00707107,35.99292893,0,3
3,15,140.00923880,35.99617317,0,3
3,15,140.01000000,36.00000000,0,3
column substance
ID Embankment ID
Width Embankment width
X Longitude of the embankment component point (degree)
Y Latitude of the embankment component point (degree)
Hlow Terrain elevation (m)
Hhigh Top elevation (m)

When the script is executed, embankment 1, embankment 2, and embankment 3 are created. The figure on the right (red octagon) in the figure below is embankment 1. The figure on the left (red circle) is embankment 3. The vertical line in the circle is embankment 2.

Indicates the properties of Fill 1.


Last update: 2023-03-17