Skip to content

Commit

Permalink
Merge pull request #335 from HSLdevcom/vilkku-bike-updater
Browse files Browse the repository at this point in the history
Add vilkku city bike updater
  • Loading branch information
TarmoA authored Apr 9, 2020
2 parents 4325c2e + bec4368 commit a8e9b16
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ protected void configurePolling (Graph graph, JsonNode config) throws Exception
source = new SamocatScooterRentalDataSource(networkName);
} else if (sourceType.equals("sharingos")) {
source = new SharingOSBikeRentalDataSource(networkName);
} else if (sourceType.equals("vilkku")) {
source = new VilkkuBikeRentalDataSource(networkName);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.opentripplanner.updater.bike_rental;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;

import com.google.common.base.Strings;
import org.opentripplanner.routing.bike_rental.BikeRentalStation;
import org.opentripplanner.util.NonLocalizedString;

/**
* Vilkku (Kuopio, Finland) bike rental data source.
* url: https://kaupunkipyorat.kuopio.fi/tkhs-export-map.html?format=xml
*/
public class VilkkuBikeRentalDataSource extends GenericXmlBikeRentalDataSource {

private String networkName;

public VilkkuBikeRentalDataSource(String networkName) {
super("//station");
this.networkName = Strings.isNullOrEmpty(networkName) ? "vilkku" : networkName;
}

public BikeRentalStation makeStation(Map<String, String> attributes) {

// some place entries appear to actually be checked-out bikes, not stations
if (attributes.get("bike") != null) {
return null;
}

BikeRentalStation station = new BikeRentalStation();
station.networks = new HashSet<>(Collections.singleton(this.networkName));
station.id = attributes.get("name");
station.x = getCoordinate(attributes.get("longitude"));
station.y = getCoordinate(attributes.get("latitude"));
station.name = new NonLocalizedString(attributes.get("name"));
station.bikesAvailable = getAvailableBikes(attributes);
station.spacesAvailable = getAvailableSpaces(attributes);
station.state = "Station on";
return station;
}

private double getCoordinate(String coordinate) {
// for some reason the API returns coordinates with ',' as decimal separator
if (coordinate.contains(",")) {
return Double.parseDouble(coordinate.replace(",", "."));
}
return Double.parseDouble(coordinate);
}

private int getAvailableBikes(Map<String, String> attributes) {
int bikes = Integer.parseInt(attributes.get("freeBikes"));
int eBikes = Integer.parseInt(attributes.get("freeEBikes"));

return bikes + eBikes;
}

private int getAvailableSpaces(Map<String, String> attributes) {
int locks = Integer.parseInt(attributes.get("freeLocks"));
int eLocks = Integer.parseInt(attributes.get("freeELocks"));

return locks + eLocks;
}
}
1 change: 1 addition & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<logger name="org.opentripplanner.updater.bike_rental.SmooveBikeRentalDataSource" level="warn" />
<logger name="org.opentripplanner.updater.bike_rental.SamocatScooterRentalDataSource" level="warn" />
<logger name="org.opentripplanner.updater.bike_rental.SharingOSBikeRentalDataSource" level="warn" />
<logger name="org.opentripplanner.updater.bike_rental.VilkkuBikeRentalDataSource" level="warn" />
<logger name="org.opentripplanner.updater.bike_rental.NextBikeRentalDataSource" level="warn" />
<logger name="org.opentripplanner.updater.bike_rental.BikeRentalUpdater" level="warn" />
<logger name="org.opentripplanner.updater.stoptime.TimetableSnapshotSource" level="error" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class TestBikeRentalStationSource extends TestCase {

private static final String NEXT_BIKE_TEST_DATA_URL = "file:src/test/resources/bike/next.xml";
private static final String VILKKU_BIKE_TEST_DATA_URL = "file:src/test/resources/bike/vilkku.xml";

public void testKeolisRennes() {

Expand Down Expand Up @@ -231,5 +232,36 @@ private NextBikeRentalDataSource createAndUpdateNextBikeRentalDataSource(String
assertTrue(source.update());
return source;
}


public void testVilkku() {
VilkkuBikeRentalDataSource source = createAndUpdateVilkkuBikeRentalDataSource(VILKKU_BIKE_TEST_DATA_URL, null);
List<BikeRentalStation> rentalStations = source.getStations();
assertEquals(1, rentalStations.size());

Map<String,BikeRentalStation> stationByName = rentalStations.stream()
.peek(System.out::println)
.collect(Collectors.toMap(BikeRentalStation::getName, station -> station));

BikeRentalStation testStation = stationByName.get("test station");
assertEquals("test station", testStation.id);
assertEquals(27.687, testStation.x);
assertEquals(62.887, testStation.y);
assertEquals(2, testStation.bikesAvailable);
assertEquals(3, testStation.spacesAvailable);
assertEquals("Station on", testStation.state);
assertEquals("[vilkku]", testStation.networks.toString());

// Test giving network name to data source
VilkkuBikeRentalDataSource sourceWithCustomNetwork = createAndUpdateVilkkuBikeRentalDataSource(VILKKU_BIKE_TEST_DATA_URL, "kuopio");
List<BikeRentalStation> rentalStationsWithCustomNetwork = sourceWithCustomNetwork.getStations();
BikeRentalStation tempClosedStationWithCustomNetwork = rentalStationsWithCustomNetwork.get(0);
assertEquals("[kuopio]", tempClosedStationWithCustomNetwork.networks.toString());
}

private VilkkuBikeRentalDataSource createAndUpdateVilkkuBikeRentalDataSource(String url, String networkName) {
VilkkuBikeRentalDataSource source = new VilkkuBikeRentalDataSource(networkName);
source.setUrl(url);
assertTrue(source.update());
return source;
}
}
11 changes: 11 additions & 0 deletions src/test/resources/bike/vilkku.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<xml>
<station>
<name>test station</name>
<freeBikes>1</freeBikes>
<freeEBikes>1</freeEBikes>
<freeLocks>1</freeLocks>
<freeELocks>2</freeELocks>
<latitude>62,887</latitude>
<longitude>27,687</longitude>
</station>
</xml>

0 comments on commit a8e9b16

Please sign in to comment.