Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions app/org/maproulette/framework/model/Challenge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
* @param boundingPolygons List of bounding polygon GeoJSON features
* @return true if any part of the geometry is within any bounding polygon
*/
private def checkGeometryWithinBounds(

Check failure on line 292 in app/org/maproulette/framework/model/Challenge.scala

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=maproulette_maproulette2&issues=AZ1oDQGo2cMG-a9l0k9T&open=AZ1oDQGo2cMG-a9l0k9T&pullRequest=1223
geometry: JsValue,
boundingPolygons: List[JsValue]
): Boolean = {
Expand All @@ -297,24 +297,44 @@
(geometry \ "type").asOpt[String] match {
case Some("Point") =>
val coordinates = (geometry \ "coordinates").as[List[Double]]
if (coordinates.length == 2) {
if (coordinates.length >= 2) {
val x = coordinates(0) // longitude
val y = coordinates(1) // latitude
boundingPolygons.exists(polygon => isPointInPolygon(x, y, polygon))
} else {
false
}
case Some("MultiPoint") =>
val points = (geometry \ "coordinates").as[List[List[Double]]]
points.exists(coord => {
if (coord.length >= 2) {
boundingPolygons.exists(polygon => isPointInPolygon(coord(0), coord(1), polygon))
} else {
false
}
})
case Some("LineString") =>
val coordinates = (geometry \ "coordinates").as[List[List[Double]]]
coordinates.exists(coord => {
if (coord.length == 2) {
if (coord.length >= 2) {
val x = coord(0) // longitude
val y = coord(1) // latitude
boundingPolygons.exists(polygon => isPointInPolygon(x, y, polygon))
} else {
false
}
})
case Some("MultiLineString") =>
val lines = (geometry \ "coordinates").as[List[List[List[Double]]]]
lines.exists(line =>
line.exists(coord => {
if (coord.length >= 2) {
boundingPolygons.exists(polygon => isPointInPolygon(coord(0), coord(1), polygon))
} else {
false
}
})
)
case Some("Polygon") =>
// Check if any vertex of the polygon's exterior ring is within bounds
val rings = (geometry \ "coordinates").as[List[List[List[Double]]]]
Expand All @@ -327,6 +347,19 @@
}
})
)
case Some("MultiPolygon") =>
val polygons = (geometry \ "coordinates").as[List[List[List[List[Double]]]]]
polygons.exists(polygon =>
polygon.headOption.exists(ring =>
ring.exists(coord => {
if (coord.length >= 2) {
boundingPolygons.exists(bp => isPointInPolygon(coord(0), coord(1), bp))
} else {
false
}
})
)
)
case Some("GeometryCollection") =>
val geometries = (geometry \ "geometries").as[List[JsValue]]
geometries.exists(subGeometry => checkGeometryWithinBounds(subGeometry, boundingPolygons))
Expand Down
3 changes: 2 additions & 1 deletion app/org/maproulette/models/dal/ChallengeDAL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,8 @@ class ChallengeDAL @Inject() (
)(implicit id: Long, c: Option[Connection] = None): Unit = {
this.permission.hasWriteAccess(ChallengeType(), user)
this.withMRConnection { implicit c =>
val challenge = this.retrieveById(id) match {
// Bypass cache to ensure we get the latest bounds from the DB
val challenge = this._retrieveById(caching = false) match {
case Some(c) => c
case None =>
throw new NotFoundException(
Expand Down
Loading