Module: Barton::Find
- Defined in:
- lib/barton/core.rb
Class Method Summary collapse
-
.address(address) ⇒ Object
Geocode lookup to google.
-
.crosses?(a, b, lat, long) ⇒ Boolean
this fails edge cases where lat,long < ax,by & > bx,ay ie when it crosses just in front or behind diagonally.
- .documents(query = {}, type = nil) ⇒ Object
- .electorates(query) ⇒ Object
- .members(query) ⇒ Object
-
.point_in_poly?(geo, boundaries) ⇒ Boolean
Ray casting algorithm to find if point is in polygon.
Class Method Details
.address(address) ⇒ Object
Geocode lookup to google
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/barton/core.rb', line 86 def self.address( address ) # http://maps.googleapis.com/maps/api/geocode/json?address=address&sensor=false®ion=au begin google = RestClient.get "http://maps.googleapis.com/maps/api/geocode/json?address=#{URI.escape( address )}&sensor=false®ion=au" json = JSON.parse( google ) if json['status'] == 'OK' #puts google return "#{json['results'][0]['geometry']['location']['lng']},#{json['results'][0]['geometry']['location']['lat']}" else return nil end rescue => e puts e end end |
.crosses?(a, b, lat, long) ⇒ Boolean
this fails edge cases where lat,long < ax,by & > bx,ay ie when it crosses just in front or behind diagonally
124 125 126 127 128 129 130 |
# File 'lib/barton/core.rb', line 124 def self.crosses?( a, b, lat, long ) ay, ax = a.split( ',' ) by, bx = b.split( ',' ) ax, ay, bx, by = ax.to_f, ay.to_f, bx.to_f, by.to_f return 0 if ay < long and by < long return ( ( ax < lat and lat < bx ) or ( bx < lat and lat < ax ) ) ? 1 : 0 end |
.documents(query = {}, type = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/barton/core.rb', line 74 def self.documents( query={}, type=nil ) results = Array.new terms = ["id:#{query[:id]}"] if query.has_key?( :id ) terms = query[:tags] if query.has_key?( :tags ) geo = query[:geo] if query.has_key?( :geo ) geo = self.address( query[:address] ) if query.has_key?( :address ) and not query.has_key?( :geo ) s = Data.search( terms, geo, type ) s.results.each { |e| results.push( e.to_hash ) } results end |
.electorates(query) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/barton/core.rb', line 40 def self.electorates( query ) docs = Find.documents( query, 'electorate' ) # remove unwanted fields docs.each do |e| e.keys.each { |k| e.delete( k ) if k.match( /_|geobox|boundaries|highlight|sort/ ) } e[:url] = "#{Barton.api_url}/api/electorates/#{e[:id]}" members = Array.new if e.has_key?( :members ) e[:members].each do |m| m = m.to_hash m[:url] = "#{Barton.api_url}/api/members/#{m[:id]}" m.delete( :id ) members.push( m ) end end e[:members] = members if members.length > 0 end docs end |
.members(query) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/barton/core.rb', line 60 def self.members( query ) results = Array.new docs = Find.documents( query, 'member' ) docs.each do |m| m.keys.each { |k| m.delete( k ) if k.match( /_|highlight|sort/ ) } m[:url] = "#{Barton.api_url}/api/members/#{m[:id]}" e = m[:electorate].to_hash e[:url] = "#{Barton.api_url}/api/electorates/#{e[:id]}" e.delete( :id ) m[:electorate] = e end docs end |
.point_in_poly?(geo, boundaries) ⇒ Boolean
Ray casting algorithm to find if point is in polygon
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/barton/core.rb', line 103 def self.point_in_poly?( geo, boundaries ) # get pairs of points of boundaries long, lat = geo.split( ',' ) long, lat = long.to_f, lat.to_f boundaries.each do |b| coords = b.split( ' ' ) length = coords.length first, second = coords.shift, coords.shift cuts = 0 while length > 1 cuts += self.crosses?( first, second, lat, long ) first = second second = coords.shift length -= 1 end return cuts.odd? ? true : false end end |