diff --git a/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/map/ProcessedMapRepository.java b/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/map/ProcessedMapRepository.java index 4afb891..ad8f97b 100644 --- a/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/map/ProcessedMapRepository.java +++ b/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/map/ProcessedMapRepository.java @@ -22,4 +22,6 @@ public interface ProcessedMapRepository extends DataLoader getMapBroadcastRates(int intersectionID, Long startTime, Long endTime); List getMapBroadcastRateDistribution(int intersectionID, Long startTime, Long endTime); + + List getIntersectionsContainingPoint(double longitude, double latitude); } \ No newline at end of file diff --git a/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/map/ProcessedMapRepositoryImpl.java b/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/map/ProcessedMapRepositoryImpl.java index 78693f3..31d60cb 100644 --- a/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/map/ProcessedMapRepositoryImpl.java +++ b/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/accessors/map/ProcessedMapRepositoryImpl.java @@ -2,12 +2,14 @@ import java.time.Instant; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import org.bson.Document; import org.bson.conversions.Bson; +import org.locationtech.jts.geom.CoordinateXY; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -39,6 +41,8 @@ import us.dot.its.jpo.ode.api.ConflictMonitorApiProperties; import us.dot.its.jpo.ode.api.models.IDCount; import us.dot.its.jpo.ode.api.models.IntersectionReferenceData; +import us.dot.its.jpo.conflictmonitor.monitor.models.map.MapBoundingBox; +import us.dot.its.jpo.conflictmonitor.monitor.models.map.MapIndex; import us.dot.its.jpo.geojsonconverter.DateJsonMapper; import us.dot.its.jpo.geojsonconverter.pojos.geojson.LineString; @@ -158,6 +162,54 @@ public List getIntersectionIDs() { return referenceDataList; } + public List getIntersectionsContainingPoint(double longitude, double latitude){ + MongoCollection collection = mongoTemplate.getCollection(collectionName); + DistinctIterable docs = collection.distinct("properties.intersectionId", Integer.class); + MongoCursor results = docs.iterator(); + MapIndex index = new MapIndex(); + Map> mapLookup = new HashMap<>(); + while (results.hasNext()) { + Integer intersectionId = results.next(); + if (intersectionId != null){ + + + Query query = getQuery(intersectionId, null, null, true, true); + + List> maps = findProcessedMaps(query); + + if(maps.size() > 0){ + MapBoundingBox box = new MapBoundingBox(maps.getFirst()); + index.insert(box); + mapLookup.put(intersectionId, maps.getFirst()); + } + } + } + + List mapsContainingPoints = index.mapsContainingPoint(new CoordinateXY(longitude, latitude)); + + List result = new ArrayList<>(); + for(MapBoundingBox box: mapsContainingPoints){ + ProcessedMap map = mapLookup.get(box.getIntersectionId()); + IntersectionReferenceData data = new IntersectionReferenceData(); + data.setIntersectionID(map.getProperties().getIntersectionId()); + data.setRoadRegulatorID("-1"); + data.setRsuIP(map.getProperties().getOriginIp()); + + if(map.getProperties().getIntersectionName() != null && map.getProperties().getIntersectionName().isEmpty()){ + data.setIntersectionName(map.getProperties().getIntersectionName()); + } + + if (map.getProperties().getRefPoint() != null) { + data.setLatitude(map.getProperties().getRefPoint().getLatitude().doubleValue()); + data.setLongitude(map.getProperties().getRefPoint().getLongitude().doubleValue()); + } + result.add(data); + } + + return result; + + } + public List getMapBroadcastRates(int intersectionID, Long startTime, Long endTime) { String startTimeString = Instant.ofEpochMilli(0).toString(); @@ -231,6 +283,8 @@ public List getMapBroadcastRateDistribution(int intersectionID, Long st return results; } + + @Override public void add(ProcessedMap item) { mongoTemplate.save(item, collectionName); diff --git a/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/IntersectionController.java b/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/IntersectionController.java index 0f10046..11b160b 100644 --- a/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/IntersectionController.java +++ b/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/IntersectionController.java @@ -43,4 +43,27 @@ public ResponseEntity> getIntersections( } } + + @CrossOrigin(origins = "http://localhost:3000") + @RequestMapping(value = "/intersection/list/location", method = RequestMethod.GET, produces = "application/json") + @PreAuthorize("hasRole('USER') || hasRole('ADMIN')") + public ResponseEntity> getIntersectionsByLocation( + @RequestParam(name = "longitude", required = true, defaultValue = "false") Double longitude, + @RequestParam(name = "latitude", required = true, defaultValue = "false") Double latitude, + @RequestParam(name = "test", required = false, defaultValue = "false") boolean testData) { + + if (testData) { + IntersectionReferenceData ref = new IntersectionReferenceData(); + ref.setRsuIP("10.11.81.12"); + ref.setIntersectionID(12109); + ref.setRoadRegulatorID("0"); + + List refList = new ArrayList<>(); + refList.add(ref); + + return ResponseEntity.ok(refList); + } else { + return ResponseEntity.ok(processedMapRepo.getIntersectionsContainingPoint(longitude, latitude)); + } + } } diff --git a/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/MapController.java b/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/MapController.java index 6116824..18a0bf9 100644 --- a/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/MapController.java +++ b/api/jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/controllers/MapController.java @@ -82,6 +82,4 @@ public ResponseEntity countMaps( } } - - } \ No newline at end of file