Skip to content

Commit

Permalink
refactor(Added new public methods to GeoJsonUtil to get locations and…
Browse files Browse the repository at this point in the history
… location shapes from CSV file)
  • Loading branch information
br648 committed Nov 3, 2023
1 parent 11990bc commit 87c7a3d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/conveyal/gtfs/model/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Location that = (Location) o;
return stop_name == that.stop_name &&
zone_id == that.zone_id &&
stop_desc == that.stop_desc &&
return Objects.equals(stop_name, that.stop_name) &&
Objects.equals(zone_id, that.zone_id) &&
Objects.equals(stop_desc, that.stop_desc) &&
Objects.equals(stop_url, that.stop_url) &&
Objects.equals(location_id, that.location_id) &&
geometry_type == that.geometry_type;
Objects.equals(geometry_type, that.geometry_type);
}

@Override
Expand Down
50 changes: 41 additions & 9 deletions src/main/java/com/conveyal/gtfs/util/GeoJsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -61,20 +62,18 @@ private GeoJsonUtil() {
* Takes the content of a zip file entry and converts it into a {@link FeatureCollection} which is a class
* representation of features held in the locations file.
*/
public static FeatureCollection getLocations(ZipFile zipFile, ZipEntry entry) {
public static FeatureCollection getFeatureCollection(ZipFile zipFile, ZipEntry entry) {
try (InputStream zipInputStream = zipFile.getInputStream(entry)) {
String content;
try (InputStream bomInputStream = new BOMInputStream(zipInputStream)) {
content = new BufferedReader(
new InputStreamReader(bomInputStream, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n")
);
.collect(Collectors.joining("\n"));
}
return FeatureConverter.toFeatureCollection(content);
} catch (IOException e) {
LOG.error("Exception while opening zip entry: ", e);
e.printStackTrace();
return null;
}
}
Expand Down Expand Up @@ -275,11 +274,8 @@ public static CsvReader getCsvReaderFromGeoJson(
ZipEntry entry,
List<String> errors
) {
FeatureCollection features = GeoJsonUtil.getLocations(zipFile, entry);
if (features == null || features.numFeatures() == 0) {
String message = "Unable to extract GeoJson features (or none are available) from " + entry.getName();
LOG.warn(message);
if (errors != null) errors.add(message);
FeatureCollection features = getFeaturesFromGeoJson(zipFile, entry, errors);
if (features == null) {
return null;
}
StringBuilder csvContent = new StringBuilder();
Expand All @@ -295,6 +291,42 @@ public static CsvReader getCsvReaderFromGeoJson(
return new CsvReader(new StringReader(csvContent.toString()));
}

/**
* Extract the locations from Geo Json.
*/
public static List<Location> getLocationsFromGeoJson(ZipFile zipFile, ZipEntry entry, List<String> errors) {
FeatureCollection features = getFeaturesFromGeoJson(zipFile, entry, errors);
if (features == null) {
return Collections.emptyList();
}
return GeoJsonUtil.unpackLocations(features, errors);
}

/**
* Extract the location shapes from Geo Json.
*/
public static List<LocationShape> getLocationShapesFromGeoJson(ZipFile zipFile, ZipEntry entry, List<String> errors) {
FeatureCollection features = getFeaturesFromGeoJson(zipFile, entry, errors);
if (features == null) {
return Collections.emptyList();
}
return GeoJsonUtil.unpackLocationShapes(features, errors);
}

/**
* Extract the Geo Json features from file.
*/
private static FeatureCollection getFeaturesFromGeoJson(ZipFile zipFile, ZipEntry entry, List<String> errors) {
FeatureCollection features = GeoJsonUtil.getFeatureCollection(zipFile, entry);
if (features == null || features.numFeatures() == 0) {
String message = "Unable to extract GeoJson features (or none are available) from " + entry.getName();
LOG.warn(message);
if (errors != null) errors.add(message);
return null;
}
return features;
}

/**
* Convert {@link Location} and {@link LocationShape} lists to a serialized String conforming to the GeoJson
* standard.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/conveyal/gtfs/loader/GtfsFlexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void canLoadAndWriteToFlexContentZipFile() throws IOException {
// assert that rows of data were written to files within the zipfile
try (ZipFile zip = new ZipFile(outZip)) {
ZipEntry entry = zip.getEntry("locations.geojson");
FeatureCollection featureCollection = GeoJsonUtil.getLocations(zip, entry);
FeatureCollection featureCollection = GeoJsonUtil.getFeatureCollection(zip, entry);
List<Feature> features = featureCollection.getFeatures();
assertEquals(features.get(0).getId(),"area_275");
assertEquals(features.get(1).getId(),"area_276");
Expand Down

0 comments on commit 87c7a3d

Please sign in to comment.