From c1c69963c8b2a9c8b7ad2538d01da86f9440983a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 27 Mar 2024 21:51:49 +0100 Subject: [PATCH] debut export csv --- .../idfm/CSVStreamSchedulesProvider.java | 65 +++++++------ .../project/idfm/IDFMNetworkExtractor.java | 91 ++++++++++--------- .../u_paris/gla/project/idfm/Transport.java | 19 ++-- 3 files changed, 97 insertions(+), 78 deletions(-) diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamSchedulesProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamSchedulesProvider.java index d41d529..36fe27c 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamSchedulesProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVStreamSchedulesProvider.java @@ -1,44 +1,36 @@ package fr.u_paris.gla.project.idfm; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; - import fr.u_paris.gla.project.io.ScheduleFormat; + import java.text.NumberFormat; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.util.*; public class CSVStreamSchedulesProvider { private static final NumberFormat MINUTES_SECOND_FORMATTER = NumberFormat .getInstance(Locale.ENGLISH); + static { MINUTES_SECOND_FORMATTER.setMinimumIntegerDigits(2); } - private String[] line = new String[ScheduleFormat.NUMBER_COLUMNS]; - - private Iterator currentTrace; + private final String[] line = new String[ScheduleFormat.NUMBER_COLUMNS]; + private final Iterator currentTrace; + private final Map transports; + Map> lineSegments = new HashMap<>(); private Iterator> currentPath = Collections.emptyIterator(); private Iterator currentSegmentStart = Collections.emptyIterator(); private Iterator currentSegmentEnd = Collections.emptyIterator(); - Map> lineSegments = new HashMap<>(); - - private StopEntry start = null; private StopEntry end = null; - private boolean hasNext = false; private boolean onNext = false; - /** Create the stream provider */ - public CSVStreamSchedulesProvider(Iterator traces) { + /** + * Create the stream provider + */ + public CSVStreamSchedulesProvider(Iterator traces, Map t) { this.currentTrace = traces; + this.transports = t; } public boolean hasNext() { @@ -93,15 +85,34 @@ public class CSVStreamSchedulesProvider { TraceEntry trace = this.currentTrace.next(); this.currentPath = trace.getPaths().iterator(); - this.line[ScheduleFormat.LINE_INDEX] = trace.lname; + // Retrieve transports informations + Transport transport = this.transports.get(trace.id); - // Write terminus - List terminus = trace.getTerminus(); - if (!terminus.isEmpty()) { - this.line[ScheduleFormat.TERMINUS_INDEX] = terminus.get(0); - } + // Build bifurcations + transport.buildBifurcation(); - this.lineSegments.clear(); + // Iterate over possibilites + transport.descriptions.forEach(desc -> { + // TODO: On doit ajouter toutes les horaires au lieu de mettre que la première + Arrays.stream(new String[]{desc.first}).forEach(time -> { + // Write line name + this.line[ScheduleFormat.LINE_INDEX] = trace.lname; + + // Write bifurcation + this.line[ScheduleFormat.TRIP_SEQUENCE_INDEX] = desc.bifurcation.toString(); + + // Write terminus + this.line[ScheduleFormat.TERMINUS_INDEX] = desc.from; + + // Write time TODO: Pourquoi ça marche pas ?? + this.line[ScheduleFormat.TIME_INDEX] = time; + + // Test + // System.out.println("TEST CSV: " + Arrays.toString(this.line)); + + this.lineSegments.clear(); + }); + }); } List path = this.currentPath.next(); this.currentSegmentEnd = path.iterator(); 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 6406dd5..83351fb 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 @@ -3,25 +3,19 @@ */ package fr.u_paris.gla.project.idfm; -import java.io.IOException; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Stream; - +import fr.u_paris.gla.project.utils.CSVTools; +import fr.u_paris.gla.project.utils.GPS; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import fr.u_paris.gla.project.utils.CSVTools; -import fr.u_paris.gla.project.utils.GPS; +import java.io.IOException; +import java.text.MessageFormat; +import java.util.*; +import java.util.Map.Entry; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Stream; /** * Code of an extractor for the data from IDF mobilite. @@ -30,7 +24,9 @@ import fr.u_paris.gla.project.utils.GPS; */ public class IDFMNetworkExtractor { - /** The logger for information on the process */ + /** + * The logger for information on the process + */ private static final Logger LOGGER = Logger .getLogger(IDFMNetworkExtractor.class.getName()); @@ -50,7 +46,9 @@ public class IDFMNetworkExtractor { private static final int IDFM_STOPS_LAT_INDEX = 7; // Magically chosen values - /** A number of stops on each line */ + /** + * A number of stops on each line + */ private static final int GUESS_STOPS_BY_LINE = 5; // Well named constants @@ -60,8 +58,7 @@ public class IDFMNetworkExtractor { * Main entry point for the extractor of IDF mobilite data into a network as * defined by this application. * - * @param args - * the arguments (expected one for the destination file) + * @param args the arguments (expected one for the destination file) */ public static void main(String[] args) { @@ -89,7 +86,7 @@ public class IDFMNetworkExtractor { cleanTraces(traces); Map transports = new HashMap<>(); - CSVStreamProvider provider = new CSVStreamProvider(traces.values().iterator(),transports); + CSVStreamProvider provider = new CSVStreamProvider(traces.values().iterator(), transports); // Write into args[0] try { @@ -100,20 +97,20 @@ public class IDFMNetworkExtractor { () -> MessageFormat.format("Could not write in file {0}", args[0])); } - // CSVStreamSchedulesProvider providerschedules = new CSVStreamSchedulesProvider(traces.values().iterator()); + /*CSVStreamSchedulesProvider providerschedules = new CSVStreamSchedulesProvider(traces.values().iterator()); // TraceEntry tmp = traces.values().iterator().next(); // tmp.getTerminus() // .forEach(m -> LOGGER.log(Level.INFO, m)); // Write into args[1] - // try { - // CSVTools.writeCSVToFile(args[1], Stream.iterate(providerschedules.next(), - // t -> providerschedules.hasNext(), t -> providerschedules.next())); - // } catch (IOException e) { - // LOGGER.log(Level.SEVERE, e, - // () -> MessageFormat.format("Could not write in file {0}", args[1])); - // } + try { + CSVTools.writeCSVToFile(args[1], Stream.iterate(providerschedules.next(), + t -> providerschedules.hasNext(), t -> providerschedules.next())); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, e, + () -> MessageFormat.format("Could not write in file {0}", args[1])); + }*/ System.out.println("****** END ******"); System.out.println(transports.size()); System.out.println(transports.get("IDFM:C01371").name); @@ -141,6 +138,15 @@ public class IDFMNetworkExtractor { System.out.println("******************Derniere description ***********************"); System.out.println(rerd.descriptions); + // Write into args[1] + CSVStreamSchedulesProvider providerschedules = new CSVStreamSchedulesProvider(traces.values().iterator(), transports); + try { + CSVTools.writeCSVToFile(args[1], Stream.iterate(providerschedules.next(), + t -> providerschedules.hasNext(), t -> providerschedules.next())); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, e, + () -> MessageFormat.format("Could not write in file {0}", args[1])); + } } private static void cleanTraces(Map traces) { @@ -158,15 +164,16 @@ public class IDFMNetworkExtractor { } } - /** @param path */ + /** + * @param path + */ private static boolean cleanLine(List> stops) { for (List path : stops) { for (int i = 0; i < path.size(); i++) { StopEntry stop = path.get(i); - if (!(stop instanceof UnidentifiedStopEntry)) { + if (!(stop instanceof UnidentifiedStopEntry unidentified)) { continue; } - UnidentifiedStopEntry unidentified = (UnidentifiedStopEntry) stop; StopEntry stopResolution = unidentified.resolve(); if (stopResolution == null) { return false; @@ -178,24 +185,24 @@ public class IDFMNetworkExtractor { } private static void addStop(String[] line, Map traces, - List stops) { + List stops) { StopEntry entry = new StopEntry(line[IDFM_STOPS_NAME_INDEX], Double.parseDouble(line[IDFM_STOPS_LON_INDEX]), Double.parseDouble(line[IDFM_STOPS_LAT_INDEX])); String rid = line[IDFM_STOPS_RID_INDEX]; - + //Add traces description if it's empty - if (traces.keySet().contains(rid)) { + if (traces.containsKey(rid)) { TraceEntry tmp = traces.get(rid); - if (tmp.isDescriptionEmpty()){ + if (tmp.isDescriptionEmpty()) { List descriptions = extractDescription(line[IDFM_STOPS_SCHEDULES_INDEX]); tmp.addDescriptions(descriptions); } } - + // Add terminus to the traces - if (traces.keySet().contains(rid)) { + if (traces.containsKey(rid)) { extractTerminus(line[IDFM_STOPS_SCHEDULES_INDEX]).forEach(t -> traces.get(rid).addTerminus(t)); } @@ -205,7 +212,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]); + TraceEntry entry = new TraceEntry(line[IDFM_TRACE_SNAME_INDEX], line[IDFM_TRACE_ID_INDEX]); List> buildPaths = buildPaths(line[IDFM_TRACE_SHAPE_INDEX]); entry.getPaths().addAll(buildPaths); if (buildPaths.isEmpty()) { @@ -221,8 +228,8 @@ public class IDFMNetworkExtractor { for (StopEntry stopEntry : path) { if (stopEntry instanceof UnidentifiedStopEntry unidentified && GPS.distance(entry.latitude, entry.longitude, - stopEntry.latitude, - stopEntry.longitude) < QUARTER_KILOMETER) { + stopEntry.latitude, + stopEntry.longitude) < QUARTER_KILOMETER) { unidentified.addCandidate(entry); } } @@ -268,7 +275,7 @@ public class IDFMNetworkExtractor { } } catch ( - JSONException e) { + JSONException e) { // Ignoring invalid element! LOGGER.log(Level.FINE, e, () -> MessageFormat.format("Invalid json element {0}", JSON)); //$NON-NLS-1$ @@ -289,7 +296,7 @@ public class IDFMNetworkExtractor { String last = stop.getString("last"); //We skip the lines where from equals to // if(from.compareTo(to) != 0){ - all.add(new TraceDescription(from, to, first, last)); + all.add(new TraceDescription(from, to, first, last)); // } } } catch (JSONException e) { 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 8aabc85..2174f2b 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 @@ -25,11 +25,12 @@ public class Transport { Stop debut = stopsMap.get(d.from); Stop fin = stopsMap.get(d.to); - - SimpleEntry > sol = roadToLast(debut.name, debut.name, fin.name, new ArrayList(), new ArrayList()); - if(sol.getKey()){ - found ++; - d.bifurcation = sol.getValue(); + if (debut != null && fin != null) { + SimpleEntry> sol = roadToLast(debut.name, debut.name, fin.name, new ArrayList(), new ArrayList()); + if (sol.getKey()) { + found++; + d.bifurcation = sol.getValue(); + } } } System.out.println("J'en ai trouvé "+found); @@ -55,7 +56,7 @@ public class Transport { } List visitedCopy = new ArrayList<>(alreadyVisited); visitedCopy.add(currentStop); - + Stop current = stopsMap.get(currentStop); int i = 1; List > > solutions = new ArrayList<>(); @@ -80,7 +81,7 @@ public class Transport { } return new SimpleEntry<>(trouve,bifSol) ; } - + public void addStop(String start,String end,String bifurcation){ Stop start_stop; Stop end_stop; @@ -100,10 +101,10 @@ public class Transport { start_stop.connected.add( end_stop); } - + /** - * + * * @param stopsMap * @param start * @param end