Skip to content

Commit

Permalink
Feature/add distance map (#26)
Browse files Browse the repository at this point in the history
* add distance map function

* add readme update

* fix links

* add tests for distance map

* remove leftover code
  • Loading branch information
MockusTravelTime authored Apr 9, 2024
1 parent 5b4bc08 commit 1539b31
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
78 changes: 67 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ You may specify an optional rate limit when initializing your client. The `rate_
client = TravelTime::Client.new(rate_limit = 60)
```

### [Isochrones (Time Map)](https://traveltime.com/docs/api/reference/isochrones)
### [Isochrones (Time Map)](https://docs.traveltime.com/api/reference/isochrones)
Given origin coordinates, find shapes of zones reachable within corresponding travel time.
Find unions/intersections between different searches.

Expand Down Expand Up @@ -135,6 +135,62 @@ response = client.time_map(
puts response.body
```

### [Distance Map](https://docs.traveltime.com/api/reference/distance-map)
Given origin coordinates, find shapes of zones reachable within corresponding travel distance.
Find unions/intersections between different searches.

Body attributes:
* departure_searches: Searches based on departure times. Leave departure location at no earlier than given time. You can define a maximum of 10 searches.
* arrival_searches: Searches based on arrival times. Arrive at destination location at no later than given time. You can define a maximum of 10 searches.
* unions: Define unions of shapes that are results of previously defined searches.
* intersections: Define intersections of shapes that are results of previously defined searches.

```ruby
require 'time'

departure_search = {
id: "driving from Trafalgar Square",
coords: {
lat: 51.506756,
lng: -0.128050
},
transportation: { type: "driving" },
departure_time: Time.now.iso8601,
travel_distance: 1800,
}

arrival_search = {
id: "cycling to Trafalgar Square",
coords: {
lat: 51.506756,
lng: -0.128050
},
transportation: { type: "cycling" },
arrival_time: Time.now.iso8601,
travel_distance: 1800,
range: { enabled: true, width: 3600 }
}

union = {
id: 'union of driving and cycling',
search_ids: ['driving from Trafalgar Square', 'cycling to Trafalgar Square']
}

intersection = {
id: 'intersection of driving and cycling',
search_ids: ['driving from Trafalgar Square', 'cycling to Trafalgar Square']
}

response = client.distance_map(
departure_searches: [departure_search],
arrival_searches: [arrival_search],
unions: [union],
intersections: [intersection]
)

puts response.body
```

### [Isochrones (Time Map) Fast](https://docs.traveltime.com/api/reference/isochrones-fast)
A very fast version of Isochrone API. However, the request parameters are much more limited.

Expand All @@ -161,7 +217,7 @@ response = client.time_map_fast(
puts response.body
```

### [Distance Matrix (Time Filter)](https://traveltime.com/docs/api/reference/distance-matrix)
### [Distance Matrix (Time Filter)](https://docs.traveltime.com/api/reference/distance-matrix)
Given origin and destination points filter out points that cannot be reached within specified time limit.
Find out travel times, distances and costs between an origin and up to 2,000 destination points.

Expand Down Expand Up @@ -227,7 +283,7 @@ response = client.time_filter(
puts response.body
```

### [Time Filter (Fast)](https://traveltime.com/docs/api/reference/time-filter-fast)
### [Time Filter (Fast)](https://docs.traveltime.com/api/reference/time-filter-fast)
A very fast version of `time_filter()`.
However, the request parameters are much more limited.

Expand Down Expand Up @@ -326,7 +382,7 @@ puts(response.body)

The responses are in the form of a list where each position denotes either a travel time (in seconds) of a journey, or if negative that the journey from the origin to the destination point is impossible.

### [Routes](https://traveltime.com/docs/api/reference/routes)
### [Routes](https://docs.traveltime.com/api/reference/routes)
Returns routing information between source and destinations.

Body attributes:
Expand Down Expand Up @@ -397,23 +453,23 @@ response = client.routes(
puts response.body
```

### [Geocoding (Search)](https://traveltime.com/docs/api/reference/geocoding-search)
### [Geocoding (Search)](https://docs.traveltime.com/api/reference/geocoding-search)
Match a query string to geographic coordinates.

```ruby
response = client.geocoding(query: 'London', within_country: 'GB')
puts response.body
```

### [Reverse Geocoding](https://traveltime.com/docs/api/reference/geocoding-reverse)
### [Reverse Geocoding](https://docs.traveltime.com/api/reference/geocoding-reverse)
Attempt to match a latitude, longitude pair to an address.

```ruby
response = client.reverse_geocoding(lat: 51.506756, lng: -0.128050)
puts response.body
```

### [Time Filter (Postcodes)](https://traveltime.com/docs/api/reference/postcode-search)
### [Time Filter (Postcodes)](https://docs.traveltime.com/api/reference/postcode-search)
Find reachable postcodes from origin (or to destination) and get statistics about such postcodes.
Currently only supports United Kingdom.

Expand Down Expand Up @@ -446,7 +502,7 @@ response = client.time_filter_postcodes(
puts response.body
```

### [Time Filter (Postcode Districts)](https://traveltime.com/docs/api/reference/postcode-district-filter)
### [Time Filter (Postcode Districts)](https://docs.traveltime.com/api/reference/postcode-district-filter)
Find districts that have a certain coverage from origin (or to destination) and get statistics about postcodes within such districts.
Currently only supports United Kingdom.

Expand Down Expand Up @@ -481,7 +537,7 @@ response = client.time_filter_postcode_districts(
puts response.body
```

### [Time Filter (Postcode Sectors)](https://traveltime.com/docs/api/reference/postcode-sector-filter)
### [Time Filter (Postcode Sectors)](https://docs.traveltime.com/api/reference/postcode-sector-filter)
Find sectors that have a certain coverage from origin (or to destination) and get statistics about postcodes within such sectors.
Currently only supports United Kingdom.

Expand Down Expand Up @@ -516,15 +572,15 @@ response = client.time_filter_postcode_sectors(
puts response.body
```

### [Map Info](https://traveltime.com/docs/api/reference/map-info)
### [Map Info](https://docs.traveltime.com/api/reference/map-info)
Get information about currently supported countries.

```ruby
response = client.map_info
puts response.body
```

### [Supported Locations](https://traveltime.com/docs/api/reference/supported-locations)
### [Supported Locations](https://docs.traveltime.com/api/reference/supported-locations)
Find out what points are supported by the api.

```ruby
Expand Down
10 changes: 10 additions & 0 deletions lib/travel_time/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ def time_map(departure_searches: nil, arrival_searches: nil, unions: nil, inters
perform_request { connection.post('time-map', payload, { 'Accept' => format }) }
end

def distance_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil, format: nil)
payload = {
departure_searches: departure_searches,
arrival_searches: arrival_searches,
unions: unions,
intersections: intersections
}.compact
perform_request { connection.post('distance-map', payload, { 'Accept' => format }) }
end

def time_map_fast(arrival_searches:, format: nil)
payload = {
arrival_searches: arrival_searches
Expand Down
9 changes: 9 additions & 0 deletions spec/travel_time/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@
it_behaves_like 'an endpoint method'
end

describe '#distance_map' do
subject(:response) { client.distance_map }

let(:url) { "#{described_class::API_BASE_URL}distance-map" }
let(:stub) { stub_request(:post, url) }

it_behaves_like 'an endpoint method'
end

describe '#time_map_fast' do
subject(:response) { client.time_map_fast(arrival_searches: []) }

Expand Down

0 comments on commit 1539b31

Please sign in to comment.