forked from arpit1094/geocoder_here_maps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CL-25818, Upgraded geocoder version and fixed calculate_route, lookup…
… and autocomplete API (#5) * CL-25743, Upgraded geocoder version and changed code accordingly * Resolved comments and refactored code * Adding Flexible polyline decoding logic to return route results with lat, lng
- Loading branch information
1 parent
8f07ede
commit f78ecb3
Showing
13 changed files
with
220 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Geocoder::Lookup | ||
class HereLookupId < Base | ||
include HereLookupDefaultMethods | ||
|
||
def name | ||
"HereLookupId" | ||
end | ||
|
||
private | ||
|
||
def base_query_url(query) | ||
"#{protocol}://lookup.search.#{domain}/v1/lookup?" | ||
end | ||
|
||
def results(query) | ||
return [] unless item = fetch_data(query) | ||
[item].compact.presence || [] | ||
end | ||
|
||
def query_url_params(query) | ||
params = {id: super[:location_id]} | ||
params.merge(query_url_here_options(query)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
lib/geocoder/results/concerns/flexible_polyline_decoder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
module Geocoder::Result | ||
class FlexiblePolylineDecoder | ||
|
||
DECODING_TABLE = [ | ||
62, -1, -1, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, | ||
22, 23, 24, 25, -1, -1, -1, -1, 63, -1, 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 | ||
] | ||
FORMAT_VERSION = 1 | ||
|
||
# Reserved values 4 and 5 should not be selectable | ||
THIRD_DIM_MAP = {1=> 'level', 2=> 'altitude', 3=> 'elevation', 6=> 'custom1', 7=> 'custom2'} | ||
|
||
def decode(encoded) | ||
# Return an iterator over coordinates. The number of coordinates are 2 or 3 depending on the polyline content. | ||
return unless encoded.present? | ||
last_lat = last_lng = last_z = 0 | ||
|
||
decoder = decode_unsigned_values(encoded) | ||
header = decode_header(decoder) | ||
|
||
factor_degree = 10.0 ** header[:precision] | ||
factor_z = 10.0 ** header[:third_dim_precision] | ||
third_dim = header[:third_dim] | ||
third_dim_key = THIRD_DIM_MAP[third_dim] | ||
|
||
if third_dim.zero? | ||
return decoder.each_slice(2).drop(1).map do |decoded_lat, decoded_lng| | ||
last_lat += to_signed(decoded_lat) | ||
last_lng += to_signed(decoded_lng) | ||
[last_lat / factor_degree, last_lng / factor_degree] | ||
end | ||
else | ||
return decoder.drop(2).each_slice(3).map do |decoded_lat, decoded_lng, decoded_z| | ||
last_lat += to_signed(decoded_lat) | ||
last_lng += to_signed(decoded_lng) | ||
last_z += to_signed(decoded_z) | ||
|
||
{ lat: (last_lat / factor_degree), | ||
lng: (last_lng / factor_degree), | ||
"#{third_dim_key}": (last_z / factor_z) | ||
} | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def decode_header(decoder) | ||
# Decode the polyline header from an encoded_char. Returns a PolylineHeader object. | ||
raise 'Invalid format version' if decoder.next != FORMAT_VERSION | ||
|
||
value = decoder.next | ||
precision = value & 15 | ||
value >>= 4 | ||
third_dim = value & 7 | ||
third_dim_precision = (value >> 3) & 15 | ||
|
||
{ precision: precision, third_dim: third_dim, | ||
third_dim_precision: third_dim_precision | ||
} | ||
end | ||
|
||
def decode_char(char) | ||
# Decode a single char to the corresponding value | ||
char_value = char.ord | ||
DECODING_TABLE[char_value - 45] | ||
end | ||
|
||
def to_signed(value) | ||
# Decode the sign from an unsigned value | ||
value = ~value unless (value & 1).zero? | ||
value >>= 1 | ||
end | ||
|
||
def decode_unsigned_values(encoded) | ||
# Return an iterator over encoded unsigned values part of an encoded polyline | ||
result = shift = 0 | ||
|
||
Enumerator.new do |y| | ||
encoded.each_char do |char| | ||
|
||
value = decode_char(char) | ||
result |= (value & 0x1F) << shift | ||
|
||
if (value & 0x20).zero? | ||
y << result | ||
result = shift = 0 | ||
else | ||
shift += 5 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Geocoder::Result | ||
class HereLookupId < Here | ||
|
||
def label(format = :full) | ||
@data['title'] | ||
end | ||
|
||
def location_id | ||
@data['id'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.