Skip to content

Instantly share code, notes, and snippets.

@danielfone
Last active January 11, 2022 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielfone/b0c53ef0a9c4e32753708d1be6dc57b7 to your computer and use it in GitHub Desktop.
Save danielfone/b0c53ef0a9c4e32753708d1be6dc57b7 to your computer and use it in GitHub Desktop.
Convert Stats NZ Urban/Rural KML to Geoguessr map regions JSON
# Get the file from https://datafinder.stats.govt.nz/layer/105158-urban-rural-2021-generalised/
# USAGE: ruby kml2geoguessr.rb urban-rural-2021-generalised.kml > rural-nz.geoguesser.json
require "nokogiri"
require "json"
TYPES = [
# "Major urban area",
# "Large urban area",
"Medium urban area",
"Small urban area",
# "Rural settlement"
]
doc = Nokogiri::XML(ARGF)
places = doc.css("Placemark").select { |e| TYPES.include?(e.at("SimpleData[name=IUR2021_V1_00_NAME]").content) }
warn places.count.to_s + " places selected."
# don't judge me, i'm actually a professional i promise
coordinates = places.map { |p|
{
coordinates: p.at("MultiGeometry Polygon outerBoundaryIs LinearRing coordinates").text.strip.split.each_with_index.map { |c, i| lng, lat = c.split(","); { lat: lat.to_f, lng: lng.to_f } }
}
}
warn "Coordinates: " + coordinates.sum { |c| c[:coordinates].size }.to_s
puts JSON.generate(coordinates)
# From here, I use a HTTP client to POST the map JSON based on requests I captured from the browser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment