Class: Underpass::QL::Request

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

Overview

Prepares the full Overpass QL query string from a user query, bounding box, and configuration options.

Supports both bounding-box and named-area query templates.

Constant Summary collapse

QUERY_TEMPLATE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

<<-TEMPLATE
  [out:json][timeout:TIMEOUT]BBOX;
  (
    QUERY
  );
  out body;
  RECURSE
  out skel qt;
TEMPLATE
AREA_QUERY_TEMPLATE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

<<-TEMPLATE
  [out:json][timeout:TIMEOUT];
  area["name"="AREA_NAME"]->.searchArea;
  (
    QUERY(area.searchArea);
  );
  out body;
  RECURSE
  out skel qt;
TEMPLATE

Instance Method Summary collapse

Constructor Details

#initialize(query, bbox = nil, recurse: '>', area_name: nil) ⇒ Request

Creates a new request.

Parameters:

  • query (String)

    the Overpass QL query body

  • bbox (String, nil) (defaults to: nil)

    the bounding box string from BoundingBox

  • recurse (String, nil) (defaults to: '>')

    the recurse operator (default: +“>”+)

  • area_name (String, nil) (defaults to: nil)

    an OSM area name for area-based queries



39
40
41
42
43
44
# File 'lib/underpass/ql/request.rb', line 39

def initialize(query, bbox = nil, recurse: '>', area_name: nil)
  @overpass_query = query
  @global_bbox = bbox ? "[#{bbox}]" : ''
  @recurse = recurse
  @area_name = area_name
end

Instance Method Details

#to_queryString

Converts the request into a complete Overpass QL query string.

Returns:

  • (String)

    the full query string ready for the API



49
50
51
52
53
54
55
56
57
58
# File 'lib/underpass/ql/request.rb', line 49

def to_query
  template = @area_name ? AREA_QUERY_TEMPLATE : QUERY_TEMPLATE
  timeout = Underpass.configuration.timeout.to_s

  result = template.sub('TIMEOUT', timeout)
  result = result.sub('AREA_NAME', @area_name) if @area_name
  result = result.sub('BBOX', @global_bbox) unless @area_name
  result.sub('QUERY', @overpass_query)
        .sub('RECURSE', recurse_statement)
end