Skip to content

Commit

Permalink
Added simple implementation of a progress indicator, activated when `…
Browse files Browse the repository at this point in the history
…verbose = FALSE` (Issue #150).

Small tweaks in the documentation.
  • Loading branch information
mvpsaraiva committed May 27, 2021
1 parent 965fa2d commit 2ae36c5
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 42 deletions.
13 changes: 11 additions & 2 deletions java-r5rcore/src/org/ipea/r5r/R5Process.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.ipea.r5r;

import com.conveyal.r5.analyst.cluster.RegionalTask;
import com.conveyal.r5.analyst.decay.DecayFunction;
import com.conveyal.r5.analyst.scenario.Scenario;
import com.conveyal.r5.api.util.LegMode;
import com.conveyal.r5.api.util.TransitModes;
Expand All @@ -13,6 +12,7 @@
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public abstract class R5Process {
Expand Down Expand Up @@ -73,18 +73,27 @@ public List<LinkedHashMap<String, ArrayList<Object>>> run() throws ExecutionExce
int[] requestIndices = new int[nOrigins];
for (int i = 0; i < nOrigins; i++) requestIndices[i] = i;

return r5rThreadPool.submit(() ->
AtomicInteger totalProcessed = new AtomicInteger(1);

List<LinkedHashMap<String, ArrayList<Object>>> processResults = r5rThreadPool.submit(() ->
Arrays.stream(requestIndices).parallel()
.mapToObj(index -> {
LinkedHashMap<String, ArrayList<Object>> results = null;
try {
results = runProcess(index);

if (!Utils.verbose) {
System.out.print("\rOrigin " + totalProcessed.getAndIncrement() + " of " + nOrigins + " processed. ");
}
} catch (ParseException e) {
e.printStackTrace();
}
return results;
}).
collect(Collectors.toList())).get();

System.out.println("\n");
return processResults;
}

protected abstract LinkedHashMap<String, ArrayList<Object>> runProcess(int index) throws ParseException;
Expand Down
58 changes: 34 additions & 24 deletions java-r5rcore/src/org/ipea/r5r/R5RCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.IntStream;

public class R5RCore {

Expand Down Expand Up @@ -120,11 +121,11 @@ public void setNumberOfThreadsToMax() {
}

public void silentMode() {
Utils.setLogMode("ERROR");
Utils.setLogMode("ERROR", false);
}

public void verboseMode() {
Utils.setLogMode("ALL");
Utils.setLogMode("ALL", true);
}

private final TransportNetwork transportNetwork;
Expand Down Expand Up @@ -412,33 +413,42 @@ public LinkedHashMap<String, Object> findSnapPoints(String[] fromId, double[] fr
return snapFinder.run();
}

public LinkedHashMap<String, ArrayList<Object>> getGrid(int resolution) {
public LinkedHashMap<String, Object> getGrid(int resolution) {
return getGrid(resolution, true);
}

public LinkedHashMap<String, Object> getGrid(int resolution, boolean dropGeometry) {
Grid gridPointSet = new Grid(resolution, this.transportNetwork.getEnvelope());

RDataFrame gridTable = new RDataFrame();
gridTable.addStringColumn("id", "");
gridTable.addDoubleColumn("lat", 0.0);
gridTable.addDoubleColumn("lon", 0.0);
gridTable.addStringColumn("geometry", "");

int i = 0;
for (int x = 0; x < gridPointSet.width; x++) {
for (int y = 0; y < gridPointSet.height; y++) {
i++;

double lat = Grid.pixelToCenterLat(y + gridPointSet.north, resolution);
double lon = Grid.pixelToCenterLon(x + gridPointSet.west, resolution);
Polygon pixelPolygon = Grid.getPixelGeometry(x + gridPointSet.west, y + gridPointSet.north, resolution);

gridTable.append();
gridTable.set("id", String.valueOf(i));
gridTable.set("lat", lat);
gridTable.set("lon", lon);
gridTable.set("geometry", pixelPolygon.toString());
String[] id = new String[gridPointSet.featureCount()];
double[] lat = new double[gridPointSet.featureCount()];
double[] lon = new double[gridPointSet.featureCount()];
String[] geometry = new String[gridPointSet.featureCount()];

IntStream.range(0, gridPointSet.featureCount()).parallel().forEach(index -> {
int x = index % gridPointSet.width;
int y = index / gridPointSet.width;

id[index] = String.valueOf(index);
lat[index] = Grid.pixelToCenterLat(y + gridPointSet.north, resolution);
lon[index] = Grid.pixelToCenterLon(x + gridPointSet.west, resolution);

if (!dropGeometry) {
geometry[index] = Grid.getPixelGeometry(x + gridPointSet.west, y + gridPointSet.north, resolution).toString();
}
});

// Build edges return table
LinkedHashMap<String, Object> gridTable = new LinkedHashMap<>();
gridTable.put("id", id);
gridTable.put("lat", lat);
gridTable.put("lon", lon);

if (!dropGeometry) {
gridTable.put("geometry", geometry);
}

return gridTable.getDataFrame();
return gridTable;
}


Expand Down
6 changes: 5 additions & 1 deletion java-r5rcore/src/org/ipea/r5r/Utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

public class Utils {

static public boolean verbose = true;

public static EnumSet<LegMode> setLegModes(String modes) {
EnumSet<LegMode> legModes = EnumSet.noneOf(LegMode.class);

Expand Down Expand Up @@ -48,7 +50,9 @@ public static int getSecondsFromMidnight(String departureTime) throws ParseExcep
return (int) ((date.getTime() - reference.getTime()) / 1000L);
}

public static void setLogMode(String mode) {
public static void setLogMode(String mode, boolean verbose) {
Utils.verbose = verbose;

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

Logger logger = loggerContext.getLogger("com.conveyal.r5");
Expand Down
4 changes: 2 additions & 2 deletions r-package/R/accessibility.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#' decay function. See `details` for the available decay functions.
#'
#' @param r5r_core a rJava object to connect with R5 routing engine
#' @param origins,destinations a spatial sf POINT object, or a data.frame
#' containing the columns 'id', 'lon', 'lat'
#' @param opportunities_colname string. The column name in the `destinations`
#' input that tells the number of opportunities in each location.
#' Defaults to "opportunities".
Expand Down Expand Up @@ -60,8 +62,6 @@
#' Defaults to use all available threads (Inf).
#' @param verbose logical. TRUE to show detailed output messages (the default)
#' or FALSE to show only eventual ERROR messages.
#' @param origins
#' @param destinations
#'
#' @return A data.table with accessibility estimates for all origin points, by
#' a given transport mode, and per travel time cutoff and percentile.
Expand Down
6 changes: 3 additions & 3 deletions r-package/R/elevation_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ tobler_hiking <- function(slope) {
#' the terrain's slopes
#'
#' @param r5r_core a rJava object to connect with R5 routing engine
#' @param raster_file string. Path to raster files containing the study area's
#' topography. If a list is provided, all the rasters are
#' automatically merged.
#' @param raster_files string. Path to raster files containing the study area's
#' topography. If a list is provided, all the rasters are
#' automatically merged.
#'
#' @return No return value, called for side effects.
#' @family elevation support functions
Expand Down
2 changes: 1 addition & 1 deletion r-package/R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if(getRversion() >= "2.15.1") utils::globalVariables(
'route', 'temp_duration', 'temp_route', 'route', 'temp_sign', '.I',
'segment_duration', 'total_duration', 'wait', 'release_date', 'con',
"start_lon", "start_lat", "end_lon", "end_lat", "slope",
"walk_multiplier", "bike_multiplier"))
"walk_multiplier", "bike_multiplier", "found"))



Expand Down
Binary file modified r-package/inst/jar/r5r_0_5_0.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion r-package/man/accessibility.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion r-package/man/apply_elevation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions r-package/tests_marcus/test_snap.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ data_path <- system.file("extdata/poa", package = "r5r")
r5r_core <- setup_r5(data_path, verbose = FALSE, use_elevation = TRUE)

# get regular grid at resolution 8
grid_df <- r5r_core$getGrid(8L)
grid_df <- jdx::convertToR(grid_df)
grid_df$geometry <- st_as_sfc(grid_df$geometry)
grid_df <- st_as_sf(grid_df, crs = 4326)
grid_df <- r5r_core$getGrid(8L, TRUE)
grid_df <- jdx::convertToR(grid_df)


# grid_df$geometry <- st_as_sfc(grid_df$geometry)
# grid_df <- st_as_sf(grid_df, crs = 4326)

grid_df %>%
mapview(xcol="lon", ycol="lat", crs=4326)

grid_df %>% mutate(id = as.integer(id)) %>% mapview::mapview(alpha.regions = 0)
mapview::mapview(snap_df, xcol="lon", ycol="lat")
mapview::mapview(snap_df %>% filter(point_id=="4"), xcol="snap_lon", ycol="snap_lat")

r5r_core$silentMode()
system.time(
snap_df <- find_snap(r5r_core, grid_df, "WALK")
)

snap_df <- find_snap(r5r_core, grid_df, "BICYCLE") %>% arrange(distance)

mapview::mapview(snap_df, xcol="lon", ycol="lat", crs=4326)
mapview::mapview(snap_df %>% drop_na(), xcol="snap_lon", ycol="snap_lat", zcol="distance", crs=4326)
Expand All @@ -32,8 +40,10 @@ leafsync::sync(mv1, mv2)
original <- snap_df %>% select(id = point_id, lat, lon)
snapped <- snap_df %>% select(id = point_id, lat = snap_lat, lon = snap_lon)

ttm_orig <- travel_time_matrix(r5r_core, origins = original, destinations = original,
verbose = FALSE)
system.time(
ttm_orig <- travel_time_matrix(r5r_core, origins = original, destinations = original,
verbose = FALSE)
)

ttm_snap <- travel_time_matrix(r5r_core, origins = snapped, destinations = snapped,
verbose = FALSE)
Expand Down

0 comments on commit 2ae36c5

Please sign in to comment.