Skip to content

Commit

Permalink
Merge pull request #135 from ibi-group/fix-1.x-scooter-rentals
Browse files Browse the repository at this point in the history
Exclude MultiPolygons from single-geometry return.
  • Loading branch information
demory authored Nov 8, 2022
2 parents f4d844d + 7c06e4b commit 20c9020
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;

import static org.opentripplanner.util.GeoJsonUtils.parsePolygonOrMultiPolygonFromJsonNode;
Expand Down Expand Up @@ -114,9 +116,11 @@ private void setRegionsFromConfig(JsonNode config) {
if (regionsGeoJson != null) {
regions = parseRegionJson(regionsGeoJson);
} else if (regionsUrl != null) {
String protocol = null;
try {
protocol = new URL(regionsUrl).getProtocol();
// fetch regions
InputStream data = fetchFromUrl(regionsUrl);
InputStream data = fetchFromUrlWithException(regionsUrl);

if (data == null) {
LOG.warn("Failed to get data from url " + regionsUrl);
Expand All @@ -127,24 +131,39 @@ private void setRegionsFromConfig(JsonNode config) {
JsonNode rootNode = mapper.readTree(data);
regions = parseRegionJson(rootNode);
data.close();
} catch (FileNotFoundException fnf) {
LOG.warn("Error reading vehicle rental regions from {} (not found)", regionsUrl, fnf);
if (protocol.equals("http") || protocol.equals("https")) {
// For http(s) regionsUrl, the file could be available later,
// so don't create default regions at this time.
return;
} else {
// Treat FileNotFoundException in the case of a local (not http(s)) file
// the same as if no regionsUrl parameter was specified.
regions = createDefaultRegions();
}
} catch (IOException e) {
LOG.warn("Error reading vehicle rental regions from " + regionsUrl, e);
return;
}
} else {
LOG.warn("region GeoJson not found in configuration for Vehicle Rental Datasource." +
"Dropoffs are assumed to be allowed in full extent of graph.");
GeometryFactory geometryFactory = new GeometryFactory();
VehicleRentalRegion entireEarthRegion = new VehicleRentalRegion();
entireEarthRegion.network = networkName;
entireEarthRegion.geometry = geometryFactory.toGeometry(new Envelope(-180, 180, -90, 90));
regions = Arrays.asList(entireEarthRegion);
regions = createDefaultRegions();
}
if (regions != null) {
regionsLoadedFromConfig = true;
}
}

private List<VehicleRentalRegion> createDefaultRegions() {
GeometryFactory geometryFactory = new GeometryFactory();
VehicleRentalRegion entireEarthRegion = new VehicleRentalRegion();
entireEarthRegion.network = networkName;
entireEarthRegion.geometry = geometryFactory.toGeometry(new Envelope(-180, 180, -90, 90));
return Arrays.asList(entireEarthRegion);
}

@Override public List<RentalUpdaterError> getErrors() {
return errors;
}
Expand Down Expand Up @@ -614,6 +633,16 @@ private InputStream fetchFromUrl(String url, boolean fetchRequired) {
return data;
}

/**
* Helper to fetch data from a URL, or file or something.
*
* @param url The URL or file or something to fetch from
* @return An inputStream if successful or null if not successful
*/
private InputStream fetchFromUrlWithException(String url) throws IOException {
return getDataFromUrlOrFile(url, headerName, headerValue);
}

@Override
public String toString() {
return getClass().getName() + "(" + networkName + " " + rootUrl + ")";
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/opentripplanner/util/GeoJsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.geom.TopologyException;
import org.locationtech.jts.precision.GeometryPrecisionReducer;
Expand Down Expand Up @@ -52,9 +53,13 @@ public static Geometry parsePolygonOrMultiPolygonFromJsonNode(
geometries.add(GeometryUtils.convertGeoJsonToJtsGeometry(feature.getGeometry()));
}

// if there is just one geometry item, return that immediately, otherwise a ClassCastException will occur
// If there is just one geometry item, return that immediately, otherwise a ClassCastException will occur,
// except if that is a MultiPolygon, which has nested geometries that we still want to union.
if (geometries.size() == 1) {
return geometries.get(0);
Geometry firstGeometry = geometries.get(0);
if (!(firstGeometry instanceof MultiPolygon)) {
return firstGeometry;
}
}

// union all geometries into a single geometry
Expand Down

0 comments on commit 20c9020

Please sign in to comment.