Skip to content

Commit

Permalink
Merge pull request #527 from entur/bbox-query
Browse files Browse the repository at this point in the history
Add bounding box search functionality to vehicles and stations queries
  • Loading branch information
testower authored Sep 10, 2024
2 parents b6259a2 + 88b8ac8 commit 3866c49
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 33 deletions.
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@
<artifactId>mapbox-sdk-geojson</artifactId>
<version>5.8.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>31.3</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -356,4 +361,21 @@
</build>
</profile>
</profiles>

<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
</project>
132 changes: 114 additions & 18 deletions src/main/java/org/entur/lamassu/controller/GraphQLQueryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.entur.lamassu.model.entities.Station;
import org.entur.lamassu.model.entities.Vehicle;
import org.entur.lamassu.model.provider.FeedProvider;
import org.entur.lamassu.service.BoundingBoxQueryParameters;
import org.entur.lamassu.service.FeedProviderService;
import org.entur.lamassu.service.GeoSearchService;
import org.entur.lamassu.service.RangeQueryParameters;
Expand Down Expand Up @@ -71,6 +72,10 @@ public Collection<Vehicle> getVehicles(
Double lat,
Double lon,
Double range,
Double minimumLatitude,
Double minimumLongitude,
Double maximumLatitude,
Double maximumLongitude,
Integer count,
List<String> codespaces,
List<String> systems,
Expand All @@ -84,17 +89,10 @@ public Collection<Vehicle> getVehicles(
return vehicleCache.getAll(ids);
}

validateRange(range);
validateCount(count);
validateCodespaces(codespaces);
validateSystems(systems);

var queryParams = new RangeQueryParameters();
queryParams.setLat(lat);
queryParams.setLon(lon);
queryParams.setRange(range);
queryParams.setCount(count);

var filterParams = new VehicleFilterParameters();
filterParams.setCodespaces(codespaces);
filterParams.setSystems(systems);
Expand All @@ -103,10 +101,54 @@ public Collection<Vehicle> getVehicles(
filterParams.setPropulsionTypes(propulsionTypes);
filterParams.setIncludeReserved(includeReserved);
filterParams.setIncludeDisabled(includeDisabled);
filterParams.setCount(count);

if (
isBoundingBoxSearch(
minimumLatitude,
minimumLongitude,
maximumLatitude,
maximumLongitude
)
) {
var boundingBoxQueryParameters = new BoundingBoxQueryParameters();
boundingBoxQueryParameters.setMinimumLatitude(minimumLatitude);
boundingBoxQueryParameters.setMinimumLongitude(minimumLongitude);
boundingBoxQueryParameters.setMaximumLatitude(maximumLatitude);
boundingBoxQueryParameters.setMaximumLongitude(maximumLongitude);

logger.debug(
"getVehicles called boundingBoxQueryParameters={} filter={}",
boundingBoxQueryParameters,
filterParams
);

return geoSearchService.getVehiclesInBoundingBox(
boundingBoxQueryParameters,
filterParams
);
} else if (isRangeSearch(range, lat, lon)) {
validateRange(range);

logger.debug("getVehicles called query={} filter={}", queryParams, filterParams);
var rangeQueryParameters = new RangeQueryParameters();
rangeQueryParameters.setLat(lat);
rangeQueryParameters.setLon(lon);
rangeQueryParameters.setRange(range);

return geoSearchService.getVehiclesNearby(queryParams, filterParams);
logger.debug(
"getVehicles called rangeQueryParameters={} filter={}",
rangeQueryParameters,
filterParams
);

return geoSearchService.getVehiclesWithinRange(rangeQueryParameters, filterParams);
} else {
throw new GraphqlErrorException.Builder()
.message(
"You must either specify lat, lon and range OR minimumLatitude, minimumLongitude, maximumLatitude and maximumLongitude"
)
.build();
}
}

public Vehicle getVehicle(String id) {
Expand All @@ -118,6 +160,10 @@ public Collection<Station> getStations(
Double lat,
Double lon,
Double range,
Double minimumLatitude,
Double minimumLongitude,
Double maximumLatitude,
Double maximumLongitude,
Integer count,
List<String> codespaces,
List<String> systems,
Expand All @@ -129,27 +175,77 @@ public Collection<Station> getStations(
return stationCache.getAll(ids);
}

validateRange(range);
validateCount(count);
validateCodespaces(codespaces);
validateSystems(systems);

var queryParams = new RangeQueryParameters();
queryParams.setLat(lat);
queryParams.setLon(lon);
queryParams.setRange(range);
queryParams.setCount(count);

var filterParams = new StationFilterParameters();
filterParams.setCodespaces(codespaces);
filterParams.setSystems(systems);
filterParams.setOperators(operators);
filterParams.setAvailableFormFactors(availableFormFactors);
filterParams.setAvailablePropulsionTypes(availablePropulsionTypes);
filterParams.setCount(count);

logger.debug("getStations called query={} filter={}", queryParams, filterParams);
if (
isBoundingBoxSearch(
minimumLatitude,
minimumLongitude,
maximumLatitude,
maximumLongitude
)
) {
var boundingBoxQueryParameters = new BoundingBoxQueryParameters();
boundingBoxQueryParameters.setMinimumLatitude(minimumLatitude);
boundingBoxQueryParameters.setMinimumLongitude(minimumLongitude);
boundingBoxQueryParameters.setMaximumLatitude(maximumLatitude);
boundingBoxQueryParameters.setMaximumLongitude(maximumLongitude);
logger.debug(
"getStations called boundingBoxQueryParameters={} filter={}",
boundingBoxQueryParameters,
filterParams
);
return geoSearchService.getStationsInBoundingBox(
boundingBoxQueryParameters,
filterParams
);
} else if (isRangeSearch(range, lat, lon)) {
validateRange(range);
var rangeQueryParameters = new RangeQueryParameters();
rangeQueryParameters.setLat(lat);
rangeQueryParameters.setLon(lon);
rangeQueryParameters.setRange(range);
logger.debug(
"getStations called rangeQueryParameters={} filter={}",
rangeQueryParameters,
filterParams
);
return geoSearchService.getStationsWithinRange(rangeQueryParameters, filterParams);
} else {
throw new GraphqlErrorException.Builder()
.message(
"You must either specify lat, lon and range OR minimumLatitude, minimumLongitude, maximumLatitude and maximumLongitude"
)
.build();
}
}

return geoSearchService.getStationsNearby(queryParams, filterParams);
private boolean isRangeSearch(Double range, Double lat, Double lon) {
return range != null && lat != null && lon != null;
}

private boolean isBoundingBoxSearch(
Double minimumLatitude,
Double minimumLongitude,
Double maximumLatitude,
Double maximumLongitude
) {
return (
minimumLatitude != null &&
minimumLongitude != null &&
maximumLatitude != null &&
maximumLongitude != null
);
}

public Station getStation(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
*
*
* * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by
* * the European Commission - subsequent versions of the EUPL (the "Licence");
* * You may not use this work except in compliance with the Licence.
* * You may obtain a copy of the Licence at:
* *
* * https://joinup.ec.europa.eu/software/page/eupl
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the Licence is distributed on an "AS IS" basis,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the Licence for the specific language governing permissions and
* * limitations under the Licence.
*
*/

package org.entur.lamassu.service;

public class BoundingBoxQueryParameters {

private Double minimumLatitude;
private Double minimumLongitude;
private Double maximumLatitude;
private Double maximumLongitude;

public Double getMinimumLatitude() {
return minimumLatitude;
}

public void setMinimumLatitude(Double minimumLatitude) {
this.minimumLatitude = minimumLatitude;
}

public Double getMinimumLongitude() {
return minimumLongitude;
}

public void setMinimumLongitude(Double minimumLongitude) {
this.minimumLongitude = minimumLongitude;
}

public Double getMaximumLatitude() {
return maximumLatitude;
}

public void setMaximumLatitude(Double maximumLatitude) {
this.maximumLatitude = maximumLatitude;
}

public Double getMaximumLongitude() {
return maximumLongitude;
}

public void setMaximumLongitude(Double maximumLongitude) {
this.maximumLongitude = maximumLongitude;
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/entur/lamassu/service/FilterParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class FilterParameters {
private List<String> codespaces;
private List<String> systems;
private List<String> operators;
private Integer count;

public List<String> getCodespaces() {
return codespaces;
Expand All @@ -49,4 +50,12 @@ public List<String> getOperators() {
public void setOperators(List<String> operators) {
this.operators = operators;
}

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}
}
12 changes: 10 additions & 2 deletions src/main/java/org/entur/lamassu/service/GeoSearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@
import org.entur.lamassu.model.entities.Vehicle;

public interface GeoSearchService {
List<Vehicle> getVehiclesNearby(
List<Vehicle> getVehiclesWithinRange(
RangeQueryParameters rangeQueryParameters,
VehicleFilterParameters vehicleFilterParameters
);
List<Station> getStationsNearby(
List<Vehicle> getVehiclesInBoundingBox(
BoundingBoxQueryParameters boundingBoxQueryParameters,
VehicleFilterParameters vehicleFilterParameters
);
List<Station> getStationsWithinRange(
RangeQueryParameters rangeQueryParameters,
StationFilterParameters stationFilterParameters
);
List<Station> getStationsInBoundingBox(
BoundingBoxQueryParameters boundingBoxQueryParameters,
StationFilterParameters stationFilterParameters
);
Collection<String> getVehicleSpatialIndexOrphans();
Collection<String> removeVehicleSpatialIndexOrphans();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public class RangeQueryParameters {
private Double lat;
private Double lon;
private Double range;
private Integer count;

public Double getLat() {
return lat;
Expand All @@ -30,12 +29,4 @@ public Double getRange() {
public void setRange(Double range) {
this.range = range;
}

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}
}
Loading

0 comments on commit 3866c49

Please sign in to comment.