From ef61b8dafff5d4a9509b5618a5b7c1488b07554c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 11 Apr 2024 23:03:07 +0200 Subject: [PATCH] [feat] generation of a csv file with line/url --- .../gla/project/idfm/CSVImageProvider.java | 55 +++++++++++++++++++ .../gla/project/idfm/CSVStreamProvider.java | 5 +- .../project/idfm/IDFMNetworkExtractor.java | 13 ++++- .../u_paris/gla/project/idfm/TraceEntry.java | 5 +- .../u_paris/gla/project/idfm/Transport.java | 4 +- .../u_paris/gla/project/io/ImageFormat.java | 24 ++++++++ 6 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 src/main/java/fr/u_paris/gla/project/idfm/CSVImageProvider.java create mode 100644 src/main/java/fr/u_paris/gla/project/io/ImageFormat.java diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVImageProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVImageProvider.java new file mode 100644 index 0000000..2d4d115 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVImageProvider.java @@ -0,0 +1,55 @@ +/** + * + */ +package fr.u_paris.gla.project.idfm; + +import fr.u_paris.gla.project.io.ScheduleFormat; +import java.text.MessageFormat; +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import fr.u_paris.gla.project.io.ImageFormat; +import fr.u_paris.gla.project.utils.GPS; + +public final class CSVImageProvider { + private static final NumberFormat MINUTES_SECOND_FORMATTER = NumberFormat + .getInstance(Locale.ENGLISH); + static { + MINUTES_SECOND_FORMATTER.setMinimumIntegerDigits(2); + } + + private final String[] line = new String[ImageFormat.NUMBER_COLUMNS]; + private final Iterator current; + + /** Create the stream provider */ + public CSVImageProvider(Iterator traces) { + this.current = traces; + } + + /** Check if next exists */ + public boolean hasNext() { + return this.current.hasNext(); + } + + /** Get Next element */ + public String[] next() { + if (!this.hasNext()) { + return null; + } + + Transport element = this.current.next(); + this.line[ImageFormat.LINE_INDEX] = element.name; + this.line[ImageFormat.IMAGE_URL_INDEX] = element.image_url; + + return Arrays.copyOf(this.line, this.line.length); + } +} diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java index 0249eac..6df2b64 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamProvider.java @@ -56,6 +56,7 @@ public final class CSVStreamProvider { private String traceId = ""; private String traceType = ""; + private String url_image = ""; /** Create the stream provider */ public CSVStreamProvider(Iterator traces, Map t) { @@ -114,6 +115,7 @@ public final class CSVStreamProvider { this.traceId = trace.id; this.traceType = trace.type; + this.url_image = trace.url; this.descriptions.clear(); this.descriptions.addAll(trace.descriptions); @@ -189,9 +191,10 @@ public final class CSVStreamProvider { String start_p = this.line[NetworkFormat.START_INDEX]; String end_p = this.line[NetworkFormat.STOP_INDEX]; // String bifurcation = this.line[NetworkFormat.VARIANT_INDEX]; + Transport transp = null; if(!transports.containsKey(traceId)){ - transp = new Transport(nameTransport,traceType); + transp = new Transport(nameTransport,traceType, url_image); transports.put(traceId, transp); }else{ transp = transports.get(traceId); diff --git a/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java b/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java index 55e0bc2..d01efef 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/IDFMNetworkExtractor.java @@ -45,6 +45,7 @@ public class IDFMNetworkExtractor { private static final int IDFM_STOPS_NAME_INDEX = 5; private static final int IDFM_STOPS_LON_INDEX = 6; private static final int IDFM_STOPS_LAT_INDEX = 7; + public static final int IDFM_URL_INDEX = 10; // Magically chosen values /** @@ -179,6 +180,16 @@ public class IDFMNetworkExtractor { LOGGER.log(Level.SEVERE, e, () -> MessageFormat.format("Could not write in file {0}", args[1])); } + + CSVImageProvider providerimage = new CSVImageProvider(transports.values().iterator()); + String imageCSV = "image.csv"; + try { + CSVTools.writeCSVToFile(imageCSV, Stream.iterate(providerimage.next(), + t -> providerimage.hasNext(), t -> providerimage.next())); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, e, + () -> MessageFormat.format("Could not write in file {0}", imageCSV)); + } } private static void cleanTraces(Map traces) { @@ -244,7 +255,7 @@ public class IDFMNetworkExtractor { } private static void addLine(String[] line, Map traces) { - TraceEntry entry = new TraceEntry(line[IDFM_TRACE_SNAME_INDEX], line[IDFM_TRACE_ID_INDEX],line[IDFM_TRACE_TYPE_INDEX]); + TraceEntry entry = new TraceEntry(line[IDFM_TRACE_SNAME_INDEX], line[IDFM_TRACE_ID_INDEX],line[IDFM_TRACE_TYPE_INDEX], line[IDFM_URL_INDEX]); List> buildPaths = buildPaths(line[IDFM_TRACE_SHAPE_INDEX]); entry.getPaths().addAll(buildPaths); if (buildPaths.isEmpty()) { diff --git a/src/main/java/fr/u_paris/gla/project/idfm/TraceEntry.java b/src/main/java/fr/u_paris/gla/project/idfm/TraceEntry.java index c989f53..8891fb8 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/TraceEntry.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/TraceEntry.java @@ -15,7 +15,7 @@ public final class TraceEntry { public final String lname; public final String id; public final String type; - + public final String url; @@ -29,11 +29,12 @@ public final class TraceEntry { * * @param lname the name of the line */ - public TraceEntry(String lname,String ident,String t_type) { + public TraceEntry(String lname,String ident,String t_type, String img_url) { super(); this.lname = lname; this.id = ident; this.type = t_type; + this.url = img_url; } // FIXME list of lists are bad practice in direct access... diff --git a/src/main/java/fr/u_paris/gla/project/idfm/Transport.java b/src/main/java/fr/u_paris/gla/project/idfm/Transport.java index 31fe9ed..1e13dd9 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/Transport.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/Transport.java @@ -17,14 +17,16 @@ public class Transport { Map stopsMap = new HashMap<>(); public String name; public String type; + public String image_url; //All the line descriptions (directions and schedules) List descriptions = new ArrayList<>(); - public Transport(String name,String type){ + public Transport(String name,String type, String url){ this.name = name; this.type = type; + this.image_url = url; } diff --git a/src/main/java/fr/u_paris/gla/project/io/ImageFormat.java b/src/main/java/fr/u_paris/gla/project/io/ImageFormat.java new file mode 100644 index 0000000..7f7f8c8 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/io/ImageFormat.java @@ -0,0 +1,24 @@ +/** + * + */ +package fr.u_paris.gla.project.io; + +import java.time.format.DateTimeFormatter; +import java.time.format.ResolverStyle; +import java.util.ArrayList; +import java.util.List; + +/** + * A tool class for the Image format. + */ +public final class ImageFormat { + public static final int NUMBER_COLUMNS = 2; + + public static final int LINE_INDEX = 0; + public static final int IMAGE_URL_INDEX = 1; + + /** Hidden constructor for tool class */ + private ImageFormat() { + // Tool class + } +}