Skip to content

Instantly share code, notes, and snippets.

@manveru
Created March 26, 2020 20:58
Show Gist options
  • Save manveru/a599affce0968aa64e908309e745e30f to your computer and use it in GitHub Desktop.
Save manveru/a599affce0968aa64e908309e745e30f to your computer and use it in GitHub Desktop.
module OpenRoutes
module Geocode
def search_address(text)
search(
api_key: ENV["OPENROUTE_API_KEY"],
text: text,
boundary_country: ["AT"],
sources: %w[openstreetmap openaddresses],
layers: %w[address]
)
end
def autocomplete_address(text)
autocomplete(
api_key: ENV["OPENROUTE_API_KEY"],
text: text,
boundary_country: ["AT"],
sources: %w[openstreetmap openaddresses],
layers: %w[address]
)
end
def autocomplete_town(text)
autocomplete(
api_key: ENV["OPENROUTE_API_KEY"],
text: text,
boundary_country: ["AT"],
sources: %w[whosonfirst geonames],
layers: %w[coarse]
)
end
def autocomplete(
api_key : String,
text : String,
focus_point_lon : Float64? = nil,
focus_point_lat : Float64? = nil,
boundary_rect_min_lon : Float64? = nil,
boundary_rect_min_lat : Float64? = nil,
boundary_rect_max_lon : Float64? = nil,
boundary_rect_max_lat : Float64? = nil,
boundary_country : Array(String) = [] of String,
language : String? = "de-AT",
sources : Array(String) = %w[openstreetmap openaddresses whosonfirst geonames],
layers : Array(String) = %w[address venue neighbourhood locality borough localadmin county region macrocounty macroregion country coarse]
)
url = URI.parse("https://api.openrouteservice.org/geocode/autocomplete")
url.query = HTTP::Params.new({
"api_key" => [api_key],
"text" => [text],
"lang" => [language],
# "focus_point_lon" => [ focus_point_lon.to_s ],
# "focus_point_lat" : [ focus_point_lat.to_s ],
# "boundary_rect_min_lon" : [ boundary_rect_min_lon.to_s ],
# "boundary_rect_min_lat" : [ boundary_rect_min_lat.to_s ],
# "boundary_rect_max_lon" : [ boundary_rect_max_lon.to_s ],
# "boundary_rect_max_lat" : [ boundary_rect_max_lat.to_s ],
"boundary_country": [boundary_country.join(",")],
"sources": [sources.join(",")],
"layers": [layers.join(",")],
}).to_s
HTTP::Client.get(url) do |response|
GeoJSON::FeatureCollection.from_json(response.body_io)
end
end
def search(
api_key : String,
text : String,
focus_point_lon : Float64? = nil,
focus_point_lat : Float64? = nil,
boundary_rect_min_lon : Float64? = nil,
boundary_rect_min_lat : Float64? = nil,
boundary_rect_max_lon : Float64? = nil,
boundary_rect_max_lat : Float64? = nil,
boundary_country : Array(String) = [] of String,
language : String? = "de-AT",
sources : Array(String) = %w[openstreetmap openaddresses whosonfirst geonames],
layers : Array(String) = %w[address venue neighbourhood locality borough localadmin county region macrocounty macroregion country coarse]
)
url = URI.parse("https://api.openrouteservice.org/geocode/search")
url.query = HTTP::Params.new({
"api_key" => [api_key],
"text" => [text],
"lang" => [language],
# "focus_point_lon" => [ focus_point_lon.to_s ],
# "focus_point_lat" : [ focus_point_lat.to_s ],
# "boundary_rect_min_lon" : [ boundary_rect_min_lon.to_s ],
# "boundary_rect_min_lat" : [ boundary_rect_min_lat.to_s ],
# "boundary_rect_max_lon" : [ boundary_rect_max_lon.to_s ],
# "boundary_rect_max_lat" : [ boundary_rect_max_lat.to_s ],
"boundary_country": [boundary_country.join(",")],
"sources": [sources.join(",")],
"layers": [layers.join(",")],
}).to_s
HTTP::Client.get(url) do |response|
GeoJSON::FeatureCollection.from_json(response.body_io)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment