Skip to content

Commit

Permalink
add bounding box query for stops
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Mar 29, 2018
1 parent f133550 commit d39de8d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.sql.Array;
import java.sql.SQLException;

import static com.conveyal.gtfs.graphql.GraphQLUtil.floatArg;
import static com.conveyal.gtfs.graphql.GraphQLUtil.intArg;
import static com.conveyal.gtfs.graphql.GraphQLUtil.intt;
import static com.conveyal.gtfs.graphql.GraphQLUtil.multiStringArg;
Expand Down Expand Up @@ -633,6 +634,10 @@ public class GraphQLGtfsSchema {
.argument(stringArg("namespace")) // FIXME maybe these nested namespace arguments are not doing anything.
.argument(multiStringArg("stop_id"))
.argument(multiStringArg("pattern_id"))
.argument(floatArg("minLat"))
.argument(floatArg("minLon"))
.argument(floatArg("maxLat"))
.argument(floatArg("maxLon"))
.argument(intArg(ID_ARG))
.argument(intArg(LIMIT_ARG))
.argument(intArg(OFFSET_ARG))
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/conveyal/gtfs/graphql/GraphQLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;

import static graphql.Scalars.GraphQLFloat;
import static graphql.Scalars.GraphQLInt;
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLArgument.newArgument;
Expand Down Expand Up @@ -49,4 +50,11 @@ public static GraphQLArgument intArg (String name) {
.build();
}

public static GraphQLArgument floatArg (String name) {
return newArgument()
.name(name)
.type(GraphQLFloat)
.build();
}

}
25 changes: 22 additions & 3 deletions src/main/java/com/conveyal/gtfs/graphql/fetchers/JDBCFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.conveyal.gtfs.graphql.GraphQLUtil.multiStringArg;
import static com.conveyal.gtfs.graphql.GraphQLUtil.stringArg;
Expand All @@ -39,6 +41,8 @@ public class JDBCFetcher implements DataFetcher<List<Map<String, Object>>> {
public static final String ID_ARG = "id";
public static final String LIMIT_ARG = "limit";
public static final String OFFSET_ARG = "offset";
public static final List<String> boundingBoxArgs = Arrays.asList("minLat", "minLon", "maxLat", "maxLon");
public static final List<String> paginationArgs = Arrays.asList(LIMIT_ARG, OFFSET_ARG);
public final String tableName;
public final String parentJoinField;
private final String sortField;
Expand Down Expand Up @@ -172,11 +176,14 @@ public List<Map<String, Object>> getResults (String namespace, List<String> pare
// FIXME add sort order?
sortBy = String.format(" order by %s", sortField);
}
for (String key : arguments.keySet()) {
Set<String> argumentKeys = arguments.keySet();
for (String key : argumentKeys) {
// Limit and Offset arguments are for pagination. projectStops is used to mutate shape points before
// returning them. All others become "where X in A, B, C" clauses.
String[] argsToSkip = new String[]{LIMIT_ARG, OFFSET_ARG};
if (Arrays.asList(argsToSkip).indexOf(key) != -1) continue;
Set<String> argsToSkip = new HashSet<>();
argsToSkip.addAll(boundingBoxArgs);
argsToSkip.addAll(paginationArgs);
if (argsToSkip.contains(key)) continue;
if (ID_ARG.equals(key)) {
Integer value = (Integer) arguments.get(key);
conditions.add(String.join(" = ", "id", value.toString()));
Expand All @@ -185,6 +192,18 @@ public List<Map<String, Object>> getResults (String namespace, List<String> pare
if (values != null && !values.isEmpty()) conditions.add(makeInClause(key, values));
}
}
if (argumentKeys.containsAll(boundingBoxArgs)) {
// Handle bounding box arguments if all are supplied.
for (String bound : boundingBoxArgs) {
Double value = (Double) arguments.get(bound);
// Determine delimiter/equality operator based on min/max
String delimiter = bound.startsWith("max") ? " <= " : " >= ";
// Determine field based on lat/lon
// FIXME: Currently only works with stops. Add pattern query.
String field = bound.endsWith("Lon") ? "stop_lon" : "stop_lat";
conditions.add(String.join(delimiter, field, value.toString()));
}
}
if ( ! conditions.isEmpty()) {
sqlBuilder.append(" where ");
sqlBuilder.append(String.join(" and ", conditions));
Expand Down

0 comments on commit d39de8d

Please sign in to comment.