Skip to content

Instantly share code, notes, and snippets.

@stupidpupil
Created December 19, 2015 12:06
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 stupidpupil/188af501fb99a62d6ae3 to your computer and use it in GitHub Desktop.
Save stupidpupil/188af501fb99a62d6ae3 to your computer and use it in GitHub Desktop.
Reduces UA boundaries from http://idea.ed.ac.uk/data/kmz/ to only Wales and only Polygons
require 'nokogiri'
wales = Nokogiri::XML(File.read('GB.kml')) ; 1
las = {'6B1' => 'Anglesey',
'6C2' => 'Blaenau Gwent',
'6B3' => 'Bridgend',
'6B2' => 'Caerphilly',
'6A8' => 'Cardiff',
'6B7' => 'Carmarthenshire',
'6A4' => 'Ceredigion',
'6A7' => 'Conwy',
'6C1' => 'Denbighshire',
'6B5' => 'Flintshire',
'6A2' => 'Gwynedd',
'6B8' => 'Merthyr Tydfil',
'6A1' => 'Monmouthshire',
'6A5' => 'Neath Port Talbot',
'6B9' => 'Newport',
'6A3' => 'Pembrokeshire',
'6C4' => 'Powys',
'6A9' => 'Rhondda, Cynon, Taf',
'6A6' => 'Swansea',
'6C3' => 'Torfaen',
'6B6' => 'Vale of Glamorgan',
'6B4' => 'Wrexham'}
placemarks = wales.xpath('//xmlns:Placemark') ;1
placemarks.each do |p|
name = p.at_xpath('./xmlns:name/text()').text
if not (mat = las.to_a.find {|l| name.match l[-1]}).nil?
p.at_xpath('./xmlns:name').content = mat[0]
p.at_xpath('./xmlns:MultiGeometry/xmlns:Polygon').parent = p
p.at_xpath('./xmlns:MultiGeometry').remove
else
p.remove
end
end ; 1
File.write('Wales.kml', wales.to_xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment