Skip to content

Instantly share code, notes, and snippets.

@fancyremarker
Created December 26, 2014 02:11
Show Gist options
  • Save fancyremarker/8b07c2b6cb36a1312ab7 to your computer and use it in GitHub Desktop.
Save fancyremarker/8b07c2b6cb36a1312ab7 to your computer and use it in GitHub Desktop.
Filter NYS DEC trail maps to a given map circle (e.g., Catskills Region)
#!/usr/bin/env ruby
# Run on http://www.dec.ny.gov/maps/hikingtrailslink.kmz
# Also: to get rid of waypoints, leaving only routes
# sed -i 's/<MultiGeometry><Point>.*/<MultiGeometry>/' OUTFILE
require 'openssl'
require 'geokit'
INPUT = ARGV[0]
OUTPUT = ARGV[1]
CATSKILLS_CENTER = Geokit::GeoLoc.new(lat: 42.10417700, lng: -74.32525600)
CATSKILLS_RADIUS = 20
infile = File.open(INPUT)
File.open(OUTPUT, 'w') do |outfile|
until (line = infile.take(1).first) == "<Placemark>\n"
outfile << line
end
loop do
break unless line == "<Placemark>\n"
linebuf = [line]
until (line = infile.take(1).first) == "</MultiGeometry></Placemark>\n"
linebuf << line
end
linebuf << line
match = linebuf.grep(/<Point><coordinates>/).first
coordinates = match[/<coordinates>(.*,.*),.*/, 1].split(',').map(&:to_f)
geoloc = Geokit::GeoLoc.new(lat: coordinates[1], lng: coordinates[0])
if geoloc.distance_to(CATSKILLS_CENTER) < CATSKILLS_RADIUS
name = linebuf.grep(/KMLName/).first[/>(.+)</, 1]
puts "Copying #{name}..."
linebuf.each { |l| outfile << l }
end
line = infile.take(1).first
end
outfile << line
outfile.write(infile.read)
end
infile.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment