Skip to content

Commit

Permalink
update opencv
Browse files Browse the repository at this point in the history
also fix some resource leaks with
try-with-resources
  • Loading branch information
markusstraub authored and polettif committed Nov 21, 2022
1 parent 470c8a0 commit 484f96b
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 63 deletions.
48 changes: 24 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

<distributionManagement>
<repository>
<id>pt2matsim-releases</id>
<name>pt2matsim Releases Maven Repository</name>
<url>https://repo.matsim.org/repository/matsim-releases/</url>
</repository>
<id>pt2matsim-releases</id>
<name>pt2matsim Releases Maven Repository</name>
<url>https://repo.matsim.org/repository/matsim-releases/</url>
</repository>
</distributionManagement>

<repositories>
Expand All @@ -23,14 +23,14 @@
<url>https://repo.matsim.org/repository/matsim</url>
</repository>
<repository>
<id>osgeo</id>
<url>https://repo.osgeo.org/repository/release</url>
<id>osgeo</id>
<url>https://repo.osgeo.org/repository/release</url>
</repository>
</repositories>

<properties>
<geotools.version>24.2</geotools.version>
</properties>
<properties>
<geotools.version>24.2</geotools.version>
</properties>

<dependencies>
<dependency>
Expand All @@ -47,13 +47,13 @@
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.10</version>
<version>5.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>de.grundid.opendatalab</groupId>
<artifactId>geojson-jackson</artifactId>
Expand Down Expand Up @@ -117,16 +117,16 @@
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/matsim/pt2matsim/editor/BasicScheduleEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

package org.matsim.pt2matsim.editor;

import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;

import org.apache.log4j.Logger;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
Expand Down Expand Up @@ -93,15 +97,20 @@ public TransitSchedule getSchedule() {
*/
@Override
public void parseCommandCsv(String filePath) throws IOException {
CSVReader reader = new CSVReader(new FileReader(filePath), ';');

String[] line = reader.readNext();
while(line != null) {
log.info(CollectionUtils.arrayToString(line));
executeCmdLine(line);
line = reader.readNext();
try (CSVReader reader = new CSVReaderBuilder(new FileReader(filePath))
.withCSVParser(new CSVParserBuilder()
.withSeparator(';')
.build())
.build()) {
String[] line = reader.readNext();
while (line != null) {
log.info(CollectionUtils.arrayToString(line));
executeCmdLine(line);
line = reader.readNext();
}
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
reader.close();
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/matsim/pt2matsim/gtfs/GtfsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.core.utils.misc.OptionalTime;
import org.matsim.core.utils.misc.Time;
import org.matsim.pt.transitSchedule.api.*;
import org.matsim.pt2matsim.gtfs.lib.*;
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/org/matsim/pt2matsim/gtfs/GtfsFeedImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.matsim.pt2matsim.gtfs;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.io.input.BOMInputStream;
Expand Down Expand Up @@ -225,6 +227,8 @@ protected void loadStops() throws IOException {
reader.close();
} catch (ArrayIndexOutOfBoundsException e) {
throw new RuntimeException("Line " + l + " in stops.txt is empty or malformed.");
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
log.info("... stops.txt loaded");
}
Expand Down Expand Up @@ -271,6 +275,8 @@ protected boolean loadCalendar() {
return false;
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Line " + l + " in calendar.txt is empty or malformed.");
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
log.info("... calendar.txt loaded");
return true;
Expand Down Expand Up @@ -324,6 +330,8 @@ protected boolean loadCalendarDates() {
return false;
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Line " + l + " in calendar_dates.txt is empty or malformed.");
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
return true;
}
Expand Down Expand Up @@ -367,6 +375,8 @@ protected void loadShapes() {
log.info("... no shapes file found.");
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Line " + l + " in shapes.txt is empty or malformed.");
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -410,6 +420,8 @@ protected void loadRoutes() throws IOException {
reader.close();
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Line " + l + " in routes.txt is empty or malformed.");
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
log.info("... routes.txt loaded");
}
Expand Down Expand Up @@ -470,6 +482,8 @@ protected void loadTrips() throws IOException {
reader.close();
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Line " + l + " in trips.txt is empty or malformed.");
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
log.info("... trips.txt loaded");
}
Expand Down Expand Up @@ -555,6 +569,8 @@ protected void loadStopTimes() throws IOException {
reader.close();
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Line " + l + " in stop_times.txt is empty or malformed.");
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
log.info("... stop_times.txt loaded");
}
Expand Down Expand Up @@ -609,6 +625,8 @@ protected void loadFrequencies() {
throw new RuntimeException("Line " + l + " in frequencies.txt is empty or malformed.");
} catch (IOException e) {
e.printStackTrace();
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -652,6 +670,8 @@ protected void loadTransfers() {
transfersFileFound = false;
} catch (IOException e) {
e.printStackTrace();
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
if(transfersFileFound) {
log.info("... transfers.txt loaded");
Expand All @@ -662,14 +682,15 @@ protected String unzip(String compressedZip) {
String unzippedFolder = compressedZip.substring(0, compressedZip.length() - 4) + "/";
log.info("Unzipping " + compressedZip + " to " + unzippedFolder);

try {
ZipFile zipFile = new ZipFile(compressedZip);
try (ZipFile zipFile = new ZipFile(compressedZip)) {
if(zipFile.isEncrypted()) {
throw new RuntimeException("Zip file is encrypted");
}
zipFile.extractAll(unzippedFolder);
} catch (ZipException e) {
e.printStackTrace();
} catch(IOException e) {
throw new RuntimeException(e);
}
return unzippedFolder;
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/matsim/pt2matsim/hafas/lib/OperatorReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ public class OperatorReader {

public static Map<String, String> readOperators(String BETRIEB_DE) throws IOException {
Map<String, String> operators = new HashMap<>();
BufferedReader readsLines = new BufferedReader(new InputStreamReader(new FileInputStream(BETRIEB_DE), "utf-8"));
String newLine = readsLines.readLine();
while (newLine != null) {
String abbrevationOperator = newLine.split("\"")[1].replace(" ","");
newLine = readsLines.readLine();
if (newLine == null) break;
String operatorId = newLine.substring(8, 14).trim();
operators.put(operatorId, abbrevationOperator);
// read the next operator:
newLine = readsLines.readLine();
try(BufferedReader readsLines = new BufferedReader(new InputStreamReader(new FileInputStream(BETRIEB_DE), "utf-8"))) {
String newLine = readsLines.readLine();
while (newLine != null) {
String abbrevationOperator = newLine.split("\"")[1].replace(" ","");
newLine = readsLines.readLine();
if (newLine == null) break;
String operatorId = newLine.substring(8, 14).trim();
operators.put(operatorId, abbrevationOperator);
// read the next operator:
newLine = readsLines.readLine();
}
}
return operators;
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/org/matsim/pt2matsim/tools/CsvTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.matsim.pt2matsim.tools;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;

import org.matsim.core.utils.collections.MapUtils;
import org.matsim.core.utils.collections.Tuple;

Expand Down Expand Up @@ -142,15 +144,16 @@ public static <K, E, V> void writeNestedMapToFile(Object[] header, Map<K, Map<E,

public static Map<String, Map<String, String>> readNestedMapFromFile(String fileName, boolean ignoreFirstLine) throws IOException {
Map<String, Map<String, String>> map = new HashMap<>();

CSVReader reader = new CSVReader(new FileReader(fileName));
if(ignoreFirstLine) reader.readNext();
String[] line = reader.readNext();
while(line != null) {
MapUtils.getMap(line[0], map).put(line[1], line[2]);
line = reader.readNext();
try(CSVReader reader = new CSVReader(new FileReader(fileName))) {
if(ignoreFirstLine) reader.readNext();
String[] line = reader.readNext();
while(line != null) {
MapUtils.getMap(line[0], map).put(line[1], line[2]);
line = reader.readNext();
}
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
reader.close();
return map;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/matsim/pt2matsim/tools/GtfsTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static Map<LocalDate, Set<Trip>> getTripsOndates(GtfsFeed feed) {
* usually the largest file.
*/
public static void writeStopTimes(Collection<Trip> trips, String folder) throws IOException {
CSVWriter stopTimesWriter = new CSVWriter(new FileWriter(folder + GtfsDefinitions.Files.STOP_TIMES.fileName), ',');
CSVWriter stopTimesWriter = new CSVWriter(new FileWriter(folder + GtfsDefinitions.Files.STOP_TIMES.fileName));
String[] header = GtfsDefinitions.Files.STOP_TIMES.columns;
stopTimesWriter.writeNext(header, true);

Expand All @@ -161,7 +161,7 @@ public static void writeStopTimes(Collection<Trip> trips, String folder) throws
* Experimental class to write stops.txt (i.e. after filtering for one date)
*/
public static void writeStops(Collection<Stop> stops, String path) throws IOException {
CSVWriter stopsWriter = new CSVWriter(new FileWriter(path + GtfsDefinitions.Files.STOPS.fileName), ',');
CSVWriter stopsWriter = new CSVWriter(new FileWriter(path + GtfsDefinitions.Files.STOPS.fileName));
String[] header = GtfsDefinitions.Files.STOPS.columns;
stopsWriter.writeNext(header, true);
for(Stop stop : stops) {
Expand All @@ -180,7 +180,7 @@ public static void writeStops(Collection<Stop> stops, String path) throws IOExce
* Experimental class to write trips.txt (i.e. after filtering for one date)
*/
public static void writeTrips(Collection<Trip> trips, String path) throws IOException {
CSVWriter tripsWriter = new CSVWriter(new FileWriter(path + GtfsDefinitions.Files.TRIPS.fileName), ',');
CSVWriter tripsWriter = new CSVWriter(new FileWriter(path + GtfsDefinitions.Files.TRIPS.fileName));
String[] header = GtfsDefinitions.Files.TRIPS.columns;
tripsWriter.writeNext(header, true);
for(Trip trip : trips) {
Expand All @@ -198,7 +198,7 @@ public static void writeTrips(Collection<Trip> trips, String path) throws IOExce
* Experimental class to write transfers.txt (i.e. after creating additional walk transfer)
*/
public static void writeTransfers(Collection<Transfer> transfers, String path) throws IOException {
CSVWriter transfersWiter = new CSVWriter(new FileWriter(path + GtfsDefinitions.Files.TRANSFERS.fileName), ',');
CSVWriter transfersWiter = new CSVWriter(new FileWriter(path + GtfsDefinitions.Files.TRANSFERS.fileName));
String[] columns = GtfsDefinitions.Files.TRANSFERS.columns;
String[] optionalColumns = GtfsDefinitions.Files.TRANSFERS.optionalColumns;
String[] header = Stream.concat(Arrays.stream(columns), Arrays.stream(optionalColumns)).toArray(String[]::new);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/matsim/pt2matsim/tools/ShapeTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.matsim.pt2matsim.tools;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;

import org.locationtech.jts.geom.Coordinate;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
Expand Down Expand Up @@ -263,9 +265,7 @@ public static Map<Id<RouteShape>, RouteShape> readShapesFile(String shapeFile, S
Map<Id<RouteShape>, RouteShape> shapes = new HashMap<>();
CoordinateTransformation ct = TransformationFactory.getCoordinateTransformation("WGS84", outputCoordinateSystem);

CSVReader reader;
try {
reader = new CSVReader(new FileReader(shapeFile));
try (CSVReader reader = new CSVReader(new FileReader(shapeFile))) {
String[] header = reader.readNext();
Map<String, Integer> col = getIndices(header, GtfsDefinitions.Files.SHAPES.columns);
String[] line = reader.readNext();
Expand All @@ -280,14 +280,15 @@ public static Map<Id<RouteShape>, RouteShape> readShapesFile(String shapeFile, S
currentShape.addPoint(ct.transform(point), Integer.parseInt(line[col.get(GtfsDefinitions.SHAPE_PT_SEQUENCE)]));
line = reader.readNext();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("File not found!");
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Emtpy line found file!");
} catch (IOException e) {
e.printStackTrace();
} catch (CsvValidationException e) {
throw new RuntimeException(e);
}
return shapes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.matsim.pt.transitSchedule.api.TransitSchedule;
import org.matsim.pt.transitSchedule.api.TransitStopFacility;
import org.matsim.pt.utils.TransitScheduleValidator;
import org.matsim.pt2matsim.config.OsmConverterConfigGroup;
import org.matsim.pt2matsim.config.PublicTransitMappingConfigGroup;
import org.matsim.pt2matsim.config.PublicTransitMappingStrings;
import org.matsim.pt2matsim.run.CreateDefaultPTMapperConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.matsim.pt2matsim.osm.lib.OsmDataImpl;
import org.matsim.pt2matsim.osm.lib.OsmFileReader;
import org.matsim.pt2matsim.run.CreateDefaultOsmConfig;
import org.matsim.pt2matsim.run.CreateDefaultPTMapperConfig;

/**
* @author polettif
Expand Down

0 comments on commit 484f96b

Please sign in to comment.