0

I have a GIS input file that contains hundreds of the following lines (objects):

{
    POSITION="[950.28174, 797.89899]";
    TYPE="Saltlake";
    AZIMUT="-25.722504";
    PARENT="";
};

    {
    POSITION="[900.71307, 777.2226, -3.8146973]";
    TYPE="Oak";
    AZIMUT="-168.15758";
    PARENT="";
};

POSITION means length, width, height [x,y,z]. Currently I use this powershell script to convert the input for the next tool:

$d=gc input.txt   
$rows=@()   
for ($i=0; $i -le $d.count -2; ++$i) {                                          
if( $d[$i] -match "POSITION" ) { 
 $pos = $d[$i].Replace('POSITION="[', '').Replace(',',';').Replace(']";','').Replace(' ', '').trim()  
 $typ = $d[$i+1].Replace('TYPE=', '').Replace(';', '').Replace(' ', '').trim() 
 $ori = $d[$i+2].Replace('AZIMUT="', '').Replace('"', '').Replace('PARENT=;', '').Replace(' ', '').trim() 
 $rows += $("{0};{1};{2}" -f $typ,$pos,$ori)            
} 
}

sc -path output.csv -value $rows

The output.txt looks like this:

"Saltlake";950.28174;797.89899;-25.722504;
"Oak";900.71307;777.2226;-3.8146973e-006;-168.15758;

What happens is that the next conversion tool mistakes the AZIMUT value of "Saltlake" for its Z-Position, as there was no input given after the y-value in POSITION of input.txt - unlike for "Oak".

Now, is it possible to replace that missing Z-input space with a "0" and of course, don't replace it if a Z-Position is given?

The aim is to make output.txt look like this:

 "Saltlake";950.28174;797.89899;0;-25.722504;
 "Oak";900.71307;777.2226;-3.8146973e-006;-168.15758;
0

1 Answer 1

1

I added one line to make this work. I use the -split operator to check how many parts there are in the POSITION (by splitting on the separator ;). If that equals two, it means the zero has to be added, and otherwise not.

$d=gc .\tmp.txt
$rows=@()   
for ($i=0; $i -le $d.count -2; ++$i) {                                          
    if( $d[$i] -match "POSITION" ) { 
        $pos = $d[$i].Replace('POSITION="[', '').Replace(',',';').Replace(']";','').Replace(' ', '').trim()
        if (($pos -split ';').count -eq 2) {$pos += ";0"}
        $typ = $d[$i+1].Replace('TYPE=', '').Replace(';', '').Replace(' ', '').trim() 
        $ori = $d[$i+2].Replace('AZIMUT="', '').Replace('"', '').Replace('PARENT=;', '').Replace(' ', '').trim() 
        $rows += $("{0};{1};{2}" -f $typ,$pos,$ori)            
    } 
}
$rows
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.