Skip to content

Instantly share code, notes, and snippets.

@dbreunig
Created January 20, 2019 03:22
Show Gist options
  • Save dbreunig/acbb6706ee37244a8b07bf8798382597 to your computer and use it in GitHub Desktop.
Save dbreunig/acbb6706ee37244a8b07bf8798382597 to your computer and use it in GitHub Desktop.
A Ruby script to download elevation data and write it to a simple CSV.
# A script for obtaining elevation data.
# Using the docker image here: https://github.com/racemap/elevation-service
# Steps:
# $ docker run --rm -eTILE_SET_PATH=s3:// -p3000:3000 normanrz/elevation-service
# ruby elevationDownloader -s 35.035443,-111.040346 -f 35.016741,-111.005449 -p 0.001
require 'optparse'
require 'uri'
require 'net/http'
require 'json'
# Helper to parse command line argument strings
def stringToCoordinatePair(coordinateString)
components = coordinateString.split(',')
components.map! { |s| s.to_f }
return components
end
# Options to pass in: start and finish coordinates, decimal point precision
# Usage Example:
# $ ruby elevationDownloader.rb -s 35.035443,-111.040346 -f 35.016741,-111.005449 -p 0.001
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: bulkPointQueryTool.rb -s [lat,lon] -e [lat,lon] -p [0.001]"
opts.on("-s", "--start [COORDINATES]", String, "Your start coordinates") do |start|
options[:start] = stringToCoordinatePair(start)
end
opts.on("-f", "--finish [COORDINATES]", String, "Your finish coordinates") do |finish|
options[:finish] = stringToCoordinatePair(finish)
end
opts.on("-p", "--precision [ZEROS]", Float, "Your decimal precision, espressed like '0.001'") do |precision|
options[:precision] = precision
end
end.parse!
# We want to move like a typewriter, across once, step down back to zero, across again
# Let's generate all the lat steps and lon steps for our pairs
latSteps = [options[:start][0]]
if ( options[:start][0] > options[:finish][0] ) # We need two conditions to account for N/S hemispheres
while ( latSteps.last > options[:finish][0] ) # We've walked the whole way
latSteps << (latSteps.last - options[:precision])
end
else
while ( latSteps.last < options[:finish][0] ) # We've walked the whole way
latSteps << latSteps.last + options[:precision]
end
end
lonSteps = [options[:start][1]]
if ( options[:start][1] > options[:finish][1] ) # We need two conditions to account for E/W hemispheres
while ( lonSteps.last < options[:finish][1] ) # We've walked the whole way
lonSteps << lonSteps.last - options[:precision]
end
else
while ( lonSteps.last < options[:finish][1] ) # We've walked the whole way
lonSteps << lonSteps.last + options[:precision]
end
end
# Now let's zip them together in all combinations
coordinates = []
latSteps.each { |lat| lonSteps.each { |lon| coordinates << [lat.round(6), lon.round(6)] } }
# Post JSON array of 900 points
fileCursor = 1
csv_string_rows = []
coordinates.each_slice(500) do | coords |
uri = URI.parse('http://localhost:3000')
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req.body = JSON.generate(coords)
http = Net::HTTP.new(uri.host, uri.port)
resp = http.request(req)
elevations = JSON.parse(resp.body)
coords.each_with_index do | c, i |
csv_string_rows << "#{c.join","}, #{elevations[i]}"
end
# Write the file
# File.open("coordinates_#{fileCursor}.txt", "w+") do |f|
# puts "Writing file: #{fileCursor}..."
# coords.each_with_index do | c, i |
# f.puts("#{c.join","}, #{elevations[i]}")
# end
# fileCursor += 1
# end
end
csv_string_rows.each do | row |
puts row
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment