debut export csv

This commit is contained in:
Mylloon 2024-03-27 21:51:49 +01:00
parent 503b5ce35a
commit c1c69963c8
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 97 additions and 78 deletions

View file

@ -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<TraceEntry> currentTrace;
private final String[] line = new String[ScheduleFormat.NUMBER_COLUMNS];
private final Iterator<TraceEntry> currentTrace;
private final Map<String, Transport> transports;
Map<StopEntry, Set<StopEntry>> lineSegments = new HashMap<>();
private Iterator<List<StopEntry>> currentPath = Collections.emptyIterator();
private Iterator<StopEntry> currentSegmentStart = Collections.emptyIterator();
private Iterator<StopEntry> currentSegmentEnd = Collections.emptyIterator();
Map<StopEntry, Set<StopEntry>> 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<TraceEntry> traces) {
/**
* Create the stream provider
*/
public CSVStreamSchedulesProvider(Iterator<TraceEntry> traces, Map<String, Transport> 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();
// Retrieve transports informations
Transport transport = this.transports.get(trace.id);
// Build bifurcations
transport.buildBifurcation();
// 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
List<String> terminus = trace.getTerminus();
if (!terminus.isEmpty()) {
this.line[ScheduleFormat.TERMINUS_INDEX] = terminus.get(0);
}
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<StopEntry> path = this.currentPath.next();
this.currentSegmentEnd = path.iterator();

View file

@ -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) {
@ -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<String, TraceEntry> traces) {
@ -158,15 +164,16 @@ public class IDFMNetworkExtractor {
}
}
/** @param path */
/**
* @param path
*/
private static boolean cleanLine(List<List<StopEntry>> stops) {
for (List<StopEntry> 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;
@ -185,7 +192,7 @@ public class IDFMNetworkExtractor {
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()) {
List<TraceDescription> descriptions = extractDescription(line[IDFM_STOPS_SCHEDULES_INDEX]);
@ -195,7 +202,7 @@ public class IDFMNetworkExtractor {
// 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));
}

View file

@ -25,13 +25,14 @@ public class Transport {
Stop debut = stopsMap.get(d.from);
Stop fin = stopsMap.get(d.to);
if (debut != null && fin != null) {
SimpleEntry<Boolean, List<Integer>> sol = roadToLast(debut.name, debut.name, fin.name, new ArrayList<String>(), new ArrayList<Integer>());
if (sol.getKey()) {
found++;
d.bifurcation = sol.getValue();
}
}
}
System.out.println("J'en ai trouvé "+found);
}