Created
December 5, 2012 02:23
-
-
Save chomy/4211556 to your computer and use it in GitHub Desktop.
Convert JPS format to ascii text.
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
#!/usr/bin/ruby | |
require "rexml/parsers/streamparser" | |
require "rexml/parsers/baseparser" | |
require "rexml/streamlistener" | |
class JPS2TextListener | |
include REXML::StreamListener | |
def initialize | |
@id = -1 | |
@alti = lambda{|val| | |
pos = calcLatLng(@id+=1) | |
print pos[0], " ", pos[1] ," ",val,"\n" | |
} | |
@func = nil | |
end | |
def tag_start(name, attrs) | |
case name | |
when "alti" then | |
@func = @alti | |
when "jps:westBoundLongitude" then | |
@func = lambda{|val| @west = val.to_f} | |
when "jps:eastBoundLongitude" then | |
@func = lambda{|val| @east = val.to_f} | |
when "jps:southBoundLatitude" | |
@func = lambda{|val| @south = val.to_f} | |
when "jps:northBoundLatitude" | |
@func = lambda{|val| @north = val.to_f} | |
when "jps:high" | |
@high = true | |
when "jps:low" | |
@low = true | |
when "jps:coordValues" | |
@func = lambda{|val| | |
if @high == true | |
@high = [val.split[0].to_f, val.split[1].to_f] | |
else | |
@low = [val.split[0].to_f, val.split[1].to_f] | |
end | |
} | |
end | |
end | |
def tag_end(name) | |
@func = nil | |
if name === "jps:extent" | |
@dx = (@east - @west) / (@high[0] - @low[0]) | |
@dy = (@south - @north) / (@high[1] - @low[1]) | |
@nx = @high[0] - @low[0] + 1 | |
end | |
end | |
def text(val) | |
unless @func == nil | |
@func.call val | |
end | |
end | |
def calcLatLng(i) | |
lng = sprintf("%.9f", (@west + @dx * (i % @nx))) | |
lat = sprintf("%.9f", (@north + @dy * ((i / @nx).truncate))) | |
return [lat, lng] | |
end | |
end | |
def main | |
if ARGV.size == 0 | |
puts "Usage: ruby jps2text.rb JPSFileName" | |
return | |
end | |
xml = File.open(ARGV[0]) | |
parser = REXML::Parsers::StreamParser.new(xml, JPS2TextListener.new) | |
puts parser.parse | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment