Skip to content

Commit

Permalink
skyalps-gtfs: fix shapes duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
clezag committed Sep 9, 2024
1 parent 009e52b commit 3c1958e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -174,14 +177,6 @@ public void calculateGtfs() throws Exception {
gtfsRoutes.add(new RoutesValues(flight.id, flight.name, RoutesValues.ROUTE_TYPE_AIR_SERVICE, agencySkyalps.agency_id()));
}

// shapes unique per ID/sequence
gtfsShapes = gtfsShapes.stream()
.collect(Collectors.toMap(
s -> new Object[]{s.shape_id(), s.shape_pt_sequence()},
Function.identity()))
.values().stream()
.toList();

GTFSWriter.writeAgency(gtfsAgencies);
GTFSWriter.writeCalendar_Dates(gtfsCalendarDates);
GTFSWriter.writeStop_Times(gtfsStopTimes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.opendatahub.gtfs.AgencyValues;
import com.opendatahub.gtfs.CalendarValues;
Expand All @@ -26,7 +31,7 @@ public class GTFSWriter {
public static final File FOLDER_FILE = new File("GTFS");

public static String ZIP_FILE_NAME = FOLDER_FILE + ".zip";

public static void makeFolder() throws Exception {
if (!FOLDER_FILE.exists()) {
FOLDER_FILE.mkdirs();
Expand All @@ -51,7 +56,8 @@ private static void writeCsv(String file, List<String[]> csv) throws Exception {

public static void writeStop_Times(List<Stop_TimesValues> stopTimesValues) throws Exception {
var csv = newCsv();
addRow(csv, "trip_id", "arrival_time", "departure_time", "stop_id", "stop_sequence", "timepoint", "shape_dist_traveled");
addRow(csv, "trip_id", "arrival_time", "departure_time", "stop_id", "stop_sequence", "timepoint",
"shape_dist_traveled");
for (var stoptimesvalues : stopTimesValues) {
addRow(csv,
stoptimesvalues.trip_id(),
Expand Down Expand Up @@ -172,14 +178,23 @@ public static void writeShape(List<ShapeValue> shapes) throws Exception {
var csv = newCsv();
addRow(csv, " shape_id", "shape_pt_lat", "shape_pt_lon", "shape_pt_sequence", "shape_dist_traveled");

for (ShapeValue shape : shapes) {
addRow(csv,
shape.shape_id(),
shape.shape_pt_lat(),
shape.shape_pt_lon(),
String.valueOf(shape.shape_pt_sequence()),
shape.shape_dist_traveled());
}
shapes.stream()
// shapes unique per ID/sequence
.collect(Collectors.toConcurrentMap(
s -> Collections.unmodifiableList(Arrays.asList(s.shape_id(), s.shape_pt_sequence())),
Function.identity(),
(l, r) -> l))
.values().stream()
.sorted(Comparator
.comparing(ShapeValue::shape_id)
.thenComparing(ShapeValue::shape_pt_sequence))
.forEach((shape) -> addRow(csv,
shape.shape_id(),
shape.shape_pt_lat(),
shape.shape_pt_lon(),
String.valueOf(shape.shape_pt_sequence()),
shape.shape_dist_traveled()));

writeCsv("shape.txt", csv);
}
}

0 comments on commit 3c1958e

Please sign in to comment.