Class: GPSTools::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/gps_tools/geometry.rb

Instance Method Summary collapse

Instance Method Details

#filter_by_polygon(polygon, coords) ⇒ Object

Returns a new array containing all coordinates within the given polygon



44
45
46
# File 'lib/gps_tools/geometry.rb', line 44

def filter_by_polygon(polygon, coords)
    coords.select { |coord| in_polygon?(polygon, coord) }
end

#filter_by_radius(radius, center, coords) ⇒ Object

Returns a new array containing all coordinates within the given radius



58
59
60
# File 'lib/gps_tools/geometry.rb', line 58

def filter_by_radius(radius, center, coords)
    coords.select{ |coord| in_radius?(radius, center, coord) }
end

#in_polygon?(polygon, coord) ⇒ Boolean

Returns a boolean that identifies if a given gps coordinate, coord, is within a given polygon (array of [lat, lng])

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gps_tools/geometry.rb', line 5

def in_polygon?(polygon, coord)
    n = polygon.length

    # Polygon must have at least 3 points
    if n < 3
        return false
    end
    
    extreme_point = [10000, coord[1]]

    intersections = 0
    i = 0

    while i < n do
        if i == n -1
            next_point = polygon[0]
        else
            next_point = polygon[i+1]
        end

        if intersect?(polygon[i], next_point, coord, extreme_point)

            # Special case - catches case where point is on an edge
            # if lies on an edge, return true right away
            if orientation(polygon[i], coord, next_point) == 0 && on_segment(polygon[i], coord, next_point)
                return true
            end

            intersections += 1
        end
        i += 1
    end

    # If odd number of intersections, we're good, if even, outside of polygon
    intersections % 2 == 1
end

#in_radius?(radius, center, coord) ⇒ Boolean

Returns a boolean that identifies if a given gps coordinate, coord, is within a given radius of a center point, center

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/gps_tools/geometry.rb', line 51

def in_radius?(radius, center, coord)
    distance = GPSTools::Distance.new.get_distance(center, coord)
    distance <= radius
end

#max_lat(polygon) ⇒ Object

Accepts polygon (array of [lat, lng]) and returns greatest lat



64
65
66
67
# File 'lib/gps_tools/geometry.rb', line 64

def max_lat(polygon)
    lats = polygon.map { |coord| coord[0]}
    lats.max
end

#max_lng(polygon) ⇒ Object

Accepts polygon (array of [lat, lng]) and returns greatest lng



71
72
73
74
# File 'lib/gps_tools/geometry.rb', line 71

def max_lng(polygon)
    lngs = polygon.map { |coord| coord[1]}
    lngs.max
end

#min_lat(polygon) ⇒ Object

Accepts polygon (array of [lat, lng]) and returns smallest lat



78
79
80
81
# File 'lib/gps_tools/geometry.rb', line 78

def min_lat(polygon)
    lats = polygon.map { |coord| coord[0]}
    lats.min
end

#min_lng(polygon) ⇒ Object

Accepts polygon (array of [lat, lng]) and returns greatest lat



85
86
87
88
# File 'lib/gps_tools/geometry.rb', line 85

def min_lng(polygon)
    lngs = polygon.map { |coord| coord[1]}
    lngs.min
end