Last active
January 11, 2022 23:01
-
-
Save danielfone/b0c53ef0a9c4e32753708d1be6dc57b7 to your computer and use it in GitHub Desktop.
Convert Stats NZ Urban/Rural KML to Geoguessr map regions JSON
This file contains hidden or 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
# 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