Skip to content

Instantly share code, notes, and snippets.

@Jylanthas
Created February 3, 2016 04:40
Show Gist options
  • Save Jylanthas/49cfae606ac60e9df347 to your computer and use it in GitHub Desktop.
Save Jylanthas/49cfae606ac60e9df347 to your computer and use it in GitHub Desktop.
lol
file = open(kml_path)
doc = Nokogiri::XML(file.read)
corridors = doc.css('kml Document Placemark').map { |placemark|
next unless corridor?(placemark)
create_polygon(placemark)
}
def factory
@factory ||= Locality.new.latlng.factory
end
def create_polygon(kml)
coords_str = kml.at_css('Polygon outerBoundaryIs LinearRing coordinates').text
coords_3d = coords_str.tr(' ', ',').split(',') # originally "x,y,z x,y,z"
coords_3d = coords_3d.map &:to_f # remove z axis
coords = coords_3d.reject { |flt| flt == 0.0 } # remove z axis
points = coords.each_slice(2).map { |lng_lat| factory.point *lng_lat }
linear_ring = factory.linear_ring points
factory.polygon(linear_ring)
end
# migration
class AddLatlngToLocality < ActiveRecord::Migration
def change
add_column :localities, :latlng, :point, geographic: true, default: 'POINT(0 0)'
add_index :localities, :latlng, using: :gist
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment