Skip to content

Instantly share code, notes, and snippets.

@maphew
Last active October 18, 2017 20:41
Show Gist options
  • Save maphew/8d97e7b68ce3df6f281e7bf3100a964e to your computer and use it in GitHub Desktop.
Save maphew/8d97e7b68ce3df6f281e7bf3100a964e to your computer and use it in GitHub Desktop.
Convert Arcinfo Ascii Grid file to JSON
# asc2json
# Convert Arcinfo Ascii Grid file to JSON
# RegEx replace:
# - strip leading whitespace
# - strip line not beginning with a number
# - add opening `[` to beginning of line
# - space to comma
# - end of line to `],`
# - strip empty list `[],`
$infile = $args[0]
$outfile = (Get-Item $infile).Basename + '.json'
(Get-Content $infile) |
Foreach-Object {$_ -replace "^\s*", ""} |
Foreach-Object {$_ -replace "^\D.*", ""} |
Foreach-Object {$_ -replace "^", "["} |
Foreach-Object {$_ -replace "\ ", ","} |
Foreach-Object {$_ -replace "$", "],"} |
Foreach-Object {$_ -replace "^\[\],$", ""} |
Set-Content $outfile
# Remove empty lines
(Get-Content $outfile) |
? {$_.trim() -ne "" } |
Set-Content $outfile
@maphew
Copy link
Author

maphew commented Oct 18, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment