Skip to content

Commit

Permalink
Merge pull request #8 from matsim-org/transitLineAttributes
Browse files Browse the repository at this point in the history
Transit line attributes
  • Loading branch information
vsp-gleich authored Sep 18, 2019
2 parents cc5a774 + b28a522 commit 4a6d136
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/matsim/contrib/gtfs/GtfsConverter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.matsim.contrib.gtfs;

import java.text.Normalizer;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -92,6 +93,10 @@ public void convert(){
activeTrips.stream().map(trip -> feed.routes.get(trip.route_id)).distinct().forEach(route -> {
TransitLine tl = ts.getFactory().createTransitLine(getReadableTransitLineId(route));
ts.addTransitLine(tl);
tl.getAttributes().putAttribute("gtfs_agency_id", String.valueOf(route.agency_id));
tl.getAttributes().putAttribute("gtfs_route_type", String.valueOf(route.route_type));
tl.getAttributes().putAttribute("gtfs_route_short_name",
Normalizer.normalize(route.route_short_name, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "")); // replaces non ascii symbols
});

this.convertTrips(activeTrips);
Expand Down Expand Up @@ -218,7 +223,11 @@ private Id<TransitLine> getReadableTransitLineId(Trip trip) {
}

private Id<TransitLine> getReadableTransitLineId(Route route) {
return Id.create(route.route_short_name == null ? "XXX---" + route.route_id : route.route_short_name + "---" + route.route_id, TransitLine.class);
String asciiShortName = "XXX";
if (route.route_short_name != null && route.route_short_name.length() > 0) {
asciiShortName = Normalizer.normalize(route.route_short_name, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
}
return Id.create(asciiShortName + "---" + route.route_id, TransitLine.class);
}

}
11 changes: 8 additions & 3 deletions src/test/java/org/matsim/contrib/gtfs/GtfsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.matsim.pt.transitSchedule.api.TransitRouteStop;
import org.matsim.pt.transitSchedule.api.TransitSchedule;
import org.matsim.pt.transitSchedule.api.TransitScheduleReader;
import org.matsim.pt.transitSchedule.api.TransitStopFacility;

import java.time.LocalDate;

Expand Down Expand Up @@ -44,15 +45,19 @@ private void compareTransitSchedules(MutableScenario sc1, MutableScenario sc2) {
TransitSchedule ts1 = sc1.getTransitSchedule();
TransitSchedule ts2 = sc2.getTransitSchedule();
Assert.assertEquals(ts1.getFacilities().size(), ts2.getFacilities().size());
for(Id stopId: ts1.getFacilities().keySet()){
for(Id<TransitStopFacility> stopId: ts1.getFacilities().keySet()){
Assert.assertEquals(ts1.getFacilities().get(stopId).getName(), ts2.getFacilities().get(stopId).getName());
Assert.assertEquals(ts1.getFacilities().get(stopId).getCoord(), ts2.getFacilities().get(stopId).getCoord());
Assert.assertEquals(ts1.getFacilities().get(stopId).getLinkId(), ts2.getFacilities().get(stopId).getLinkId());
}
Assert.assertEquals(ts1.getTransitLines().size(), ts2.getTransitLines().size());
for(Id lineId: ts1.getTransitLines().keySet()){
for(Id<TransitLine> lineId: ts1.getTransitLines().keySet()){
Assert.assertEquals(ts1.getTransitLines().get(lineId).getRoutes().size(), ts1.getTransitLines().get(lineId).getRoutes().size());
for(Id routeId: ts1.getTransitLines().get(lineId).getRoutes().keySet()){
Assert.assertEquals(ts1.getTransitLines().get(lineId).getAttributes().getAttribute("gtfs_agency_id"), ts2.getTransitLines().get(lineId).getAttributes().getAttribute("gtfs_agency_id"));
Assert.assertEquals(ts1.getTransitLines().get(lineId).getAttributes().getAttribute("gtfs_route_type"), ts2.getTransitLines().get(lineId).getAttributes().getAttribute("gtfs_route_type"));
Assert.assertEquals(ts1.getTransitLines().get(lineId).getAttributes().getAttribute("gtfs_route_short_name"), ts2.getTransitLines().get(lineId).getAttributes().getAttribute("gtfs_route_short_name"));

for(Id<TransitRoute> routeId: ts1.getTransitLines().get(lineId).getRoutes().keySet()){
TransitRoute tr1 = ts1.getTransitLines().get(lineId).getRoutes().get(routeId);
TransitRoute tr2 = ts2.getTransitLines().get(lineId).getRoutes().get(routeId);
Assert.assertEquals(tr1.getStops().size(), tr2.getStops().size());
Expand Down
10 changes: 8 additions & 2 deletions test/input/transitSchedule.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE transitSchedule SYSTEM "http://www.matsim.org/files/dtd/transitSchedule_v1.dtd">
<!DOCTYPE transitSchedule SYSTEM "http://www.matsim.org/files/dtd/transitSchedule_v2.dtd">

<transitSchedule>
<transitStops>
<stopFacility id="TEST_STOP_1" x="-116.784582" y="36.868446" name="GTFS TEST STOP 1"/>
<stopFacility id="TEST_STOP_2" x="-116.751677" y="36.915682" name="GTFS TEST STOP 2"/>
</transitStops>
<transitLine id="Test Route---TEST_ROUTE">
<attributes>
<attribute name="gtfs_agency_id" class="java.lang.String" >Unitrans</attribute>
<attribute name="gtfs_route_short_name" class="java.lang.String" >Test Route</attribute>
<attribute name="gtfs_route_type" class="java.lang.String" >3</attribute>
</attributes>

<transitRoute id="Test Route---TEST_ROUTE_0">
<transportMode>bus</transportMode>
<routeProfile>
Expand Down Expand Up @@ -42,4 +48,4 @@
</departures>
</transitRoute>
</transitLine>
</transitSchedule>
</transitSchedule>

0 comments on commit 4a6d136

Please sign in to comment.