Class: Underpass::QL::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/underpass/ql/builder.rb

Overview

DSL for building Overpass QL queries programmatically.

Examples:

Query for restaurants

builder = Underpass::QL::Builder.new
builder.node('amenity' => 'restaurant')
Underpass::QL::Query.perform(bbox, builder)

Proximity search with around

builder = Underpass::QL::Builder.new
builder.node('amenity' => 'cafe').around(500, 44.4268, 26.1025)

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Creates a new empty builder.



17
18
19
20
# File 'lib/underpass/ql/builder.rb', line 17

def initialize
  @statements = []
  @around = nil
end

Instance Method Details

#around(radius, lat_or_point, lon = nil) ⇒ self

Sets a proximity filter for all statements.

Parameters:

  • radius (Numeric)

    search radius in meters

  • lat_or_point (Numeric, RGeo::Feature::Point)

    latitude or an RGeo point

  • lon (Numeric, nil) (defaults to: nil)

    longitude (required when lat_or_point is numeric)

Returns:

  • (self)

    for method chaining



64
65
66
67
68
69
70
71
# File 'lib/underpass/ql/builder.rb', line 64

def around(radius, lat_or_point, lon = nil)
  @around = if lat_or_point.respond_to?(:y)
              { radius: radius, lat: lat_or_point.y, lon: lat_or_point.x }
            else
              { radius: radius, lat: lat_or_point, lon: lon }
            end
  self
end

#node(tags = {}) ⇒ self

Adds a node query statement.

Parameters:

  • tags (Hash{String => String}) (defaults to: {})

    tag filters

Returns:

  • (self)

    for method chaining



26
27
28
29
# File 'lib/underpass/ql/builder.rb', line 26

def node(tags = {})
  @statements << build_statement('node', tags)
  self
end

#nwr(tags = {}) ⇒ self

Adds a node/way/relation (nwr) query statement.

Parameters:

  • tags (Hash{String => String}) (defaults to: {})

    tag filters

Returns:

  • (self)

    for method chaining



53
54
55
56
# File 'lib/underpass/ql/builder.rb', line 53

def nwr(tags = {})
  @statements << build_statement('nwr', tags)
  self
end

#relation(tags = {}) ⇒ self

Adds a relation query statement.

Parameters:

  • tags (Hash{String => String}) (defaults to: {})

    tag filters

Returns:

  • (self)

    for method chaining



44
45
46
47
# File 'lib/underpass/ql/builder.rb', line 44

def relation(tags = {})
  @statements << build_statement('relation', tags)
  self
end

#to_qlString

Converts the builder into an Overpass QL query string.

Returns:

  • (String)

    the Overpass QL query



76
77
78
79
80
81
82
# File 'lib/underpass/ql/builder.rb', line 76

def to_ql
  if @around
    @statements.map { |s| append_around(s) }.join("\n")
  else
    @statements.join("\n")
  end
end

#way(tags = {}) ⇒ self

Adds a way query statement.

Parameters:

  • tags (Hash{String => String}) (defaults to: {})

    tag filters

Returns:

  • (self)

    for method chaining



35
36
37
38
# File 'lib/underpass/ql/builder.rb', line 35

def way(tags = {})
  @statements << build_statement('way', tags)
  self
end