Skip to content

Instantly share code, notes, and snippets.

@meysammeisam
Created August 20, 2016 11:22
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 meysammeisam/6deabd488d8d69e608ed7401d71eebcb to your computer and use it in GitHub Desktop.
Save meysammeisam/6deabd488d8d69e608ed7401d71eebcb to your computer and use it in GitHub Desktop.
load "/home/holy-knight/workstation/work-documentation/McDonald/geofence/mds.rb"
#load "/home/hungerstation/workstation/geofence/mds.rb" #### hs-scalling console
geojsons ||= hubs_geojson
class Geofence
attr_accessor :multi_polygon
attr_reader :store_id, :city, :branch, :locals
def initialize(store_id:)
@store_id, @multi_polygon, @city, @branch = store_id
@locals = []
end
def link_locals
find_city
find_branch
intersected_locals
return false if !@city || !@branch || @locals.blank?
factory = RGeo::Geographic.simple_mercator_factory(:srid => 4326)
@locals.each do |local|
intersection_percentage = intersection_percentage(local: local)
next if intersection_percentage < 10.0
geometry = @multi_polygon.intersection local.geom
if geometry.geometry_type.to_s == "Polygon"
geometry = factory.multi_polygon [geometry]
end
create_branchlinklocal(local: local, intersection_percentage: intersection_percentage, geometry: geometry)
end
end
def find_city
cities = City.where("geom is not ?", nil)
cities.each do |city|
if @multi_polygon && @multi_polygon.intersects?(city.geom)
@city = city
return city
end
end
end
def find_branch
return nil unless @city
branches = Branch.where(local_id: @city.locals.ids).where.not(latitude: nil, longitude: nil).where(restaurant_id: 1)
branches.each do |branch|
factory = RGeo::Geographic.simple_mercator_factory(:srid => 4326)
branch_point = factory.point(branch.longitude,branch.latitude)
if @multi_polygon.intersects? branch_point
@branch = branch
return branch
end
end
end
def intersected_locals
return [] if !@city
locals = @city.locals.where.not(geom: nil)
locals.each do |local|
if @multi_polygon.intersects?(local.geom) && !@locals.include?(local)
@locals << local
end
end
end
def intersection_percentage(local:)
return 0 if !local.geom || !@multi_polygon.intersects?(local.geom)
factory = RGeo::Geographic.simple_mercator_factory(:srid => 4326)
intersection = @multi_polygon.intersection local.geom
intersection_area = intersection.area if intersection
geofence_area = @multi_polygon.area
local_area = factory.multi_polygon([local.geom]).area
return ((intersection_area.to_f / local_area) * 100).round(1)
end
def geojson(geom: nil)
geom ||= @multi_polygon
result = {"type"=>"FeatureCollection","features"=>[]}
result["features"].push({
"type" =>"Feature",
"id" => 1111222233334444,
"properties" =>{"id"=> 1123 ,"name"=> "test", "color"=> "#ff33ff" },
"geometry" =>RGeo::GeoJSON.encode(geom)
})
return result
end
private
def create_branchlinklocal(local:, intersection_percentage:, geometry:)
if @branch.blank? || local.blank? || !locals.include?(local) || intersection_percentage < 10.0 || (intersection_percentage < 90.0 && geometry.blank?) || (!geometry.blank? && !geometry.intersects?(local.geom))
return nil
end
branchlinklocal = Branchlinklocal.new(local_id: local.id, branch_id: @branch.id)
if intersection_percentage < 90.0
branchlinklocal.partial_delivery = true
branchlinklocal.partial_geom = geometry
end
branchlinklocal.save
return branchlinklocal
end
def create_branch_area(geometry:)
return nil if @branch.blank? || geometry.blank?
branch_area = BranchArea.new
branch_area.title = ""
branch_area.geom = geometry
branch_area.branch_id = @branch.id
branch_area.save
return branch_area
end
end
######################### Seeding section #########################
#factory = RGeo::Cartesian.factory(:srid => 4326)
factory = RGeo::Geographic.simple_mercator_factory(:srid => 4326)
geofences = []
geojsons[:features].each do |geojson|
coordinates = geojson[:geometry][:coordinates][0]
raise "Errrrrrrrrrrrrrrrrrrr" if(coordinates.first != coordinates.last)
line = factory.linear_ring( coordinates.map{|coord| factory.point(coord[0], coord[1])} )
polygon = factory.polygon line
multi_polygon = factory.multi_polygon [polygon]
str_id = geojson[:properties][:store_id]
str_id = geojson[:properties][:store_id]
geofence = geofences.select{|g| g.store_id == str_id }.first || Geofence.new(store_id: str_id)
geofences << geofence unless geofences.select{|g| g.store_id == str_id }.first
geofence.multi_polygon ||= multi_polygon
geofence.multi_polygon = geofence.multi_polygon + multi_polygon if multi_polygon
end
geofences.each do |geofence|
geofence.find_city
end
geofences.each do |geofence|
geofence.find_branch
end
geofences.each do |geofence|
geofence.link_locals
end
######################### test section #########################
geo = geofences.select{|g| g.city && g.city.id == 1}.first
geo.intersected_locals
intersection_percentages = geo.locals.map{|local| geo.intersection_percentage(local: local) }
intersection_percentages.inject(0){|sum,x| sum + x }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment