Method: OverpassGraph.get_roads

Defined in:
lib/overpass_graph/get_roads.rb

.get_roads(north, east, south, west, allowed_values, disallowed_values) ⇒ Object



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/overpass_graph/get_roads.rb', line 8

def self.get_roads(north, east, south, west, allowed_values, disallowed_values)
  '''
  Gets roads by querying the Overpass API.
  :return: a list of hashes with information about all the roads in the bounding box
  '''

  if 90 < north || 90 < south || north < -90 || south < -90
    raise "Latitudes in bounding boxes must be between -90.0 and 90.0"
  end

  if 180 < east || 180 < west || east < -180 || west < -180
    raise "Longitudes in bounding boxes must be between -180.0 and 180.0"
  end

  if north < south
    raise "Northern latitude is less than southern latitude.\nDid you mean 'overpass_graph(#{south}, #{east}, #{north}, #{west}...)"
  end

  if east < west
    puts "OVERPASS_GRAPH WARNING: Eastern longitude is less than western longitude.\n"\
         "In most cases this is not intended by the developer.\n"\
         "Perhaps you meant 'overpass_graph(#{north}, #{west}, #{south}, #{east}...)'?\n"\
         "Find out more here: https://dev.overpass-api.de/overpass-doc/en/full_data/bbox.html"
  end

  options = {
    bbox: {
      s: south,
      n: north,
      w: west,
      e: east
    },
    timeout: TIMEOUT,
    maxsize: MAXSIZE
  }

  allowed_string = allowed_values.map{|allowed_value| "[highway=#{allowed_value}]" }.join()
  disallowed_string = disallowed_values.map{|disallowed_value| "[highway!=#{disallowed_value}]" }.join()

  # query for all highways within the bounding box
  overpass = OverpassAPI::QL.new(options)
  query = "way[highway]#{allowed_string}#{disallowed_string};(._;>;);out geom;"
  
  begin
    response = overpass.query(query)
  rescue
    
    # try again if first request is denied, if second request is denied, throw the exception
    response = overpass.query(query)

  end

  # filter out all partial roads and return the filtered hash
  elements = response[:elements]
  return elements.filter { |element| element[:type] == "way" }

end