Created
August 20, 2019 07:04
-
-
Save meggart/0465ebba8eb279a6f80784dda8459ba7 to your computer and use it in GitHub Desktop.
Parse prj from Shapefile
This file contains 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
import Tokenize.Tokens: IDENTIFIER, LSQUARE, COMMA, RSQUARE, STRING, ENDMARKER | |
function parsetokens(t,ar) | |
if t[1].kind==Tokenize.Tokens.IDENTIFIER | |
@assert t[2].kind==LSQUARE | |
brackcount=1 | |
i=3 | |
while true | |
t[i].kind==LSQUARE && (brackcount=brackcount+1) | |
t[i].kind==RSQUARE && (brackcount=brackcount-1) | |
brackcount==0 && break | |
i+=1 | |
end | |
newar = [] | |
push!(ar,t[1].val => newar) | |
parsetokens(view(t,3:i-1),newar) | |
if length(t)>i && t[i+1].kind == COMMA | |
parsetokens(view(t,i+2:length(t)),ar) | |
end | |
else | |
v = t[1].kind==STRING ? strip(t[1].val,'\"') : t[1].val | |
push!(ar,v) | |
if length(t) > 1 && t[2].kind==COMMA | |
parsetokens(view(t,3:length(t)),ar) | |
end | |
end | |
ar | |
end | |
function parseprj(filename) | |
s = String(read(filename)) | |
t = tokenize(s) |> collect | |
parsetokens(t,[]) | |
end | |
parseprj("/home/fgans/Downloads/TM_WORLD_BORDERS_SIMPL-0.3.prj") | |
## Returns | |
# 1-element Array{Any,1}: | |
# "GEOGCS" => Any["GCS_WGS_1984", "DATUM"=>Any["D_WGS_1984", "SPHEROID"=>Any["WGS_1984", "6378137.0", "298.257223563"]], | |
# "PRIMEM"=>Any["Greenwich", "0.0"], "UNIT"=>Any["Degree", "0.0174532925199433"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment