Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BCycle Layer #38

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'dotenv'
gem 'sinatra-contrib'
gem 'sinatra-cors'
gem 'sinatra'
gem 'thin'
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ GEM
rack-protection (= 2.1.0)
sinatra (= 2.1.0)
tilt (~> 2.0)
sinatra-cors (1.2.0)
thin (1.8.0)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
Expand All @@ -35,7 +36,8 @@ DEPENDENCIES
dotenv
sinatra
sinatra-contrib
sinatra-cors
thin

BUNDLED WITH
2.1.4
2.2.29
3 changes: 3 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'uri'
require 'fileutils'
require 'date'
require 'sinatra/cors'

def api_request(path)
uri = URI.parse("#{ENV['GTFS_BASE_URL']}#{path}")
Expand All @@ -30,6 +31,8 @@ class WeGoBusMap < Sinatra::Base
]
}

set :allow_origin, 'https://gtfs.transitnownash.org'

configure :development, :test do
set :force_ssl, (ENV['FORCE_SSL'] == '1')
end
Expand Down
20 changes: 20 additions & 0 deletions public/assets/images/bcycle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions public/assets/javascript/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x
const vehiclesLayer = L.layerGroup().addTo(map)
const routesLayer = L.layerGroup().addTo(map)
const stopsLayer = L.layerGroup().addTo(map)
const bcycleLayer = L.layerGroup().addTo(map)

L.control.layers(
null,
{
'Vehicles': vehiclesLayer, // eslint-disable-line
'Routes': routesLayer, // eslint-disable-line
'Stops': stopsLayer // eslint-disable-line
'Stops': stopsLayer, // eslint-disable-line
'BCycle': bcycleLayer // eslint-disable-line
}
).addTo(map)

Expand Down Expand Up @@ -202,7 +204,7 @@ const formatVehicleSpeed = function (speed) {

const updateMap = function () {
// Delete very outdated markers (likely no longer in the feed)
$.each(markers, function (i, marker) {
$.each(markers, function (i) {
if (Math.round(((Date.now() / 1000) - markers[i].data.updated) / 60) >= 10) {
map.removeLayer(markers[i])
markers[i].remove()
Expand Down Expand Up @@ -305,7 +307,7 @@ const checkForAlerts = function () {
plural: data.length !== 1 ? 's' : ''
}
))
alertIndicator.on('click', function (e) {
alertIndicator.on('click', function () {
displayAlerts(data)
})
})
Expand Down Expand Up @@ -393,7 +395,7 @@ const displayLocationButton = function () {
const locationButton = $(L.Util.template(
$('#location_button_template').html()
))
$(locationButton).on('click', function (e) {
$(locationButton).on('click', function () {
map.locate()
})
$(mapToolsContainer).append(locationButton)
Expand Down Expand Up @@ -438,7 +440,7 @@ const displayRoute = function (tripData) {
if (!L.Browser.mobile) {
routeShapes[shapeId].bindTooltip('Route ' + routeData.route_short_name + ' (click to remove)')
}
routeShapes[shapeId].on('click', function (e) {
routeShapes[shapeId].on('click', function () {
stopsLayer.removeLayer(stopLayer)
routesLayer.removeLayer(routeLayer)
// Allows the shape to be redrawn
Expand Down
69 changes: 69 additions & 0 deletions public/assets/javascript/bcycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* global $, L, bcycleLayer, moment */

const GBFS_REFRESH_TTL = 20 * 1000
const GBFS_BASE_URL = 'https://gbfs.bcycle.com/bcycle_nashville'

var BCycleIcon = L.Icon.extend({
options: {
iconUrl: 'assets/images/bcycle.svg',
iconSize: [32, 32],
shadowUrl: null,
}
})

let stationInfo = {}
let gbfsMarkers = {}

// Main Loop
$.get(GBFS_BASE_URL + '/station_information.json', function (station_information) {
$(station_information.data.stations).each(function(i) {
var station = station_information.data.stations[i]
stationInfo[station.station_id] = station
gbfsMarkers[station.station_id] = L.marker([station.lat, station.lon], {icon: new BCycleIcon()}).addTo(bcycleLayer)
})
updateBCycleMarkers()
})

// Format BCycle Tooltip
const formatBCyclePopup = function (station) {
return L.Util.template(
$('#bcycle_popup_template').html(),
{
station_name: station.name || 'Not Set',
station_address: station.address || 'Not Set',
lat: station.lat || null,
lon: station.lon || null,
is_renting: station.status.is_renting == '1' ? 'Yes' : 'No',
is_returning: station.status.is_returning == '1' ? 'Yes' : 'No',
num_bikes_available: station.status.num_bikes_available || 'Unknown',
num_docks_available: station.status.num_docks_available || 'Unknown',
last_reported: moment.unix(station.status.last_reported).format('h:mm a')
}
)
}

// Format BCycle Tooltip
const formatBCycleTooltip = function (station) {
return L.Util.template(
$('#bcycle_tooltip_template').html(),
{
station_name: station.name || 'Not Set',
num_bikes_available: station.status.num_bikes_available || 'Unknown',
num_docks_available: station.status.num_docks_available || 'Unknown'
}
)
}

// Update BCycle Markers
const updateBCycleMarkers = function () {
$.get(GBFS_BASE_URL + '/station_status.json', function (station_status) {
$(station_status.data.stations).each(function (i) {
var status = station_status.data.stations[i]
stationInfo[status.station_id]['status'] = status
gbfsMarkers[status.station_id].bindPopup(formatBCyclePopup(stationInfo[status.station_id]))
gbfsMarkers[status.station_id].bindTooltip(formatBCycleTooltip(stationInfo[status.station_id]))
})
})
console.log('refresh called')
setTimeout(updateBCycleMarkers, GBFS_REFRESH_TTL)
}
31 changes: 31 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,37 @@
</div>
</template>

<template id="bcycle_popup_template">
<div style="width: 300px;">
<div class="stop-label">{station_name}</div>
<p><i class="fas fa-map-marked-alt fa-fw" aria-hidden="true"></i> <a href="https://www.google.com/maps/dir/?api=1&travelmode=transit&destination={lat}%2C{lon}" target="_blank">{station_address}</a></p>
<dl class="row">
<dt class="col-7"><i class="fas fa-exchange-alt fa-fw" aria-hidden="true"></i> Renting?</dt>
<dd class="col-5">{is_renting}</dd>
<dt class="col-7"><i class="fas fa-inbox fa-fw" aria-hidden="true"></i> Allows Returns?</dt>
<dd class="col-5">{is_returning}</dd>
<dt class="col-7"><i class="fas fa-biking fa-fw" aria-hidden="true"></i> Bikes Available</dt>
<dd class="col-5">{num_bikes_available}</dd>
<dt class="col-7"><i class="fas fa-bicycle fa-fw" aria-hidden="true"></i> Docks Available</dt>
<dd class="col-5">{num_docks_available}</dd>
<dt class="col-7"><i class="fas fa-clock fa-fw" aria-hidden="true"></i> Last Updated</dt>
<dd class="col-5">{last_reported}</dd>
</dl>
</div>
</template>

<template id="bcycle_tooltip_template">
<div style="width: 300px;">
<div class="stop-label">{station_name}</div>
<dl class="row">
<dt class="col-7"><i class="fas fa-biking fa-fw" aria-hidden="true"></i> Bikes Available</dt>
<dd class="col-5">{num_bikes_available}</dd>
<dt class="col-7"><i class="fas fa-bicycle fa-fw" aria-hidden="true"></i> Docks Available</dt>
<dd class="col-5">{num_docks_available}</dd>
</dl>
</div>
</template>

<%= erb 'partials/alert'.to_sym %>

<template id="alert_empty_template">
Expand Down
1 change: 1 addition & 0 deletions views/layouts/app.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<script src="/assets/javascript/leaflet.rotatedMarker.js"></script>
<script src="/assets/javascript/leaflet.textpath.js"></script>
<script src="/assets/javascript/app.js"></script>
<script src="/assets/javascript/bcycle.js"></script>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
Expand Down