Created
February 3, 2016 04:40
-
-
Save Jylanthas/49cfae606ac60e9df347 to your computer and use it in GitHub Desktop.
lol
This file contains 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
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