diff --git a/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityServiceQueriesTests.kt b/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityServiceQueriesTests.kt index 486e31a5f..4b6396bd9 100644 --- a/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityServiceQueriesTests.kt +++ b/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityServiceQueriesTests.kt @@ -328,8 +328,9 @@ class EntityServiceQueriesTests : WithTimescaleContainer, WithKafkaContainer { @CsvSource( "near;minDistance==1600000, Polygon, '[[[100.0, 0.0], [101.0, 0.0], [101.0, -1.0], [100.0, 0.0]]]', 0", "near;maxDistance==1000, Polygon, '[[[100.0, 0.0], [101.0, 0.0], [101.0, -1.0], [100.0, 0.0]]]', 1", - "contains, Polygon, '[[[90.0, 0.0], [100.0, 10.0], [110.0, 0.0], [100.0, -10.0], [90.0, 0.0]]]', 1", - "contains, Polygon, '[[[80.0, 0.0], [90.0, 5.0], [90.0, 0.0], [80.0, 0.0]]]', 0", + "within, Polygon, '[[[90.0, 0.0], [100.0, 10.0], [110.0, 0.0], [100.0, -10.0], [90.0, 0.0]]]', 1", + "within, Polygon, '[[[80.0, 0.0], [90.0, 5.0], [90.0, 0.0], [80.0, 0.0]]]', 0", + "contains, Polygon, '[[[90.0, 0.0], [100.0, 10.0], [110.0, 0.0], [100.0, -10.0], [90.0, 0.0]]]', 0", "equals, Point, '[100.0, 0.0]', 1", "equals, Point, '[101.0, 0.0]', 0", "intersects, Polygon, '[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]', 1", diff --git a/shared/src/main/kotlin/com/egm/stellio/shared/queryparameter/GeoQuery.kt b/shared/src/main/kotlin/com/egm/stellio/shared/queryparameter/GeoQuery.kt index bc505c7c7..ceaa8356f 100644 --- a/shared/src/main/kotlin/com/egm/stellio/shared/queryparameter/GeoQuery.kt +++ b/shared/src/main/kotlin/com/egm/stellio/shared/queryparameter/GeoQuery.kt @@ -9,7 +9,6 @@ import com.egm.stellio.shared.model.BadRequestDataException import com.egm.stellio.shared.model.ExpandedEntity import com.egm.stellio.shared.model.ExpandedTerm import com.egm.stellio.shared.model.WKTCoordinates -import com.egm.stellio.shared.util.JsonLdUtils import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_VALUE import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_GEOPROPERTY_VALUE import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_LOCATION_PROPERTY @@ -46,25 +45,25 @@ data class GeoQuery( fun buildSqlFilter(target: ExpandedEntity? = null): String { val targetWKTCoordinates = """ - (select jsonb_path_query_first(#{TARGET}#, '$."$geoproperty"."$NGSILD_GEOPROPERTY_VALUE"[0]')->>'$JSONLD_VALUE') + (select jsonb_path_query_first(#{TARGET}#, '$."$geoproperty"."$NGSILD_GEOPROPERTY_VALUE"[0]')->>'$JSONLD_VALUE') """.trimIndent() val georelQuery = Georel.prepareQuery(georel) return ( if (georelQuery.first == Georel.NEAR_DISTANCE_MODIFIER) """ - public.ST_Distance( - cast('SRID=4326;${wktCoordinates.value}' as public.geography), - cast('SRID=4326;' || $targetWKTCoordinates as public.geography), - false - ) ${georelQuery.second} ${georelQuery.third} + public.ST_Distance( + cast('SRID=4326;${wktCoordinates.value}' as public.geography), + cast('SRID=4326;' || $targetWKTCoordinates as public.geography), + false + ) ${georelQuery.second} ${georelQuery.third} """.trimIndent() else """ - public.ST_${georelQuery.first}( - public.ST_GeomFromText('${wktCoordinates.value}'), - public.ST_GeomFromText($targetWKTCoordinates) - ) + public.ST_${georelQuery.first}( + public.ST_GeomFromText($targetWKTCoordinates), + public.ST_GeomFromText('${wktCoordinates.value}') + ) """.trimIndent() ) .let { @@ -95,7 +94,7 @@ data class GeoQuery( } val geoproperty = requestParams[QueryParameter.GEOPROPERTY.key]?.let { expandJsonLdTerm(it, contexts) - } ?: JsonLdUtils.NGSILD_LOCATION_PROPERTY + } ?: NGSILD_LOCATION_PROPERTY // if at least one parameter is provided, the three must be provided for the geoquery to be valid val notNullGeoParameters = listOfNotNull(georel, geometry, coordinates) diff --git a/shared/src/test/kotlin/com/egm/stellio/shared/util/GeoQueryUtilsTests.kt b/shared/src/test/kotlin/com/egm/stellio/shared/util/GeoQueryUtilsTests.kt index 69340a16e..ee6be06c5 100644 --- a/shared/src/test/kotlin/com/egm/stellio/shared/util/GeoQueryUtilsTests.kt +++ b/shared/src/test/kotlin/com/egm/stellio/shared/util/GeoQueryUtilsTests.kt @@ -145,9 +145,9 @@ class GeoQueryUtilsTests { assertEqualsIgnoringNoise( """ public.ST_disjoint( - public.ST_GeomFromText('POLYGON ((0 1, 1 1, 0 1))'), public.ST_GeomFromText((select jsonb_path_query_first('{"@id":"urn:ngsi-ld:Entity:01","@type":["https://uri.etsi.org/ngsi-ld/default-context/Entity"],"https://uri.etsi.org/ngsi-ld/location":[{"@type":["https://uri.etsi.org/ngsi-ld/GeoProperty"],"https://uri.etsi.org/ngsi-ld/hasValue":[{"@value":"POINT (24.30623 60.07966)"}]}]}', - '$."https://uri.etsi.org/ngsi-ld/location"."https://uri.etsi.org/ngsi-ld/hasValue"[0]')->>'@value')) + '$."https://uri.etsi.org/ngsi-ld/location"."https://uri.etsi.org/ngsi-ld/hasValue"[0]')->>'@value')), + public.ST_GeomFromText('POLYGON ((0 1, 1 1, 0 1))') ) """, queryStatement diff --git a/subscription-service/src/test/kotlin/com/egm/stellio/subscription/service/SubscriptionServiceTests.kt b/subscription-service/src/test/kotlin/com/egm/stellio/subscription/service/SubscriptionServiceTests.kt index 2c952d7bf..2c2ca9709 100644 --- a/subscription-service/src/test/kotlin/com/egm/stellio/subscription/service/SubscriptionServiceTests.kt +++ b/subscription-service/src/test/kotlin/com/egm/stellio/subscription/service/SubscriptionServiceTests.kt @@ -1307,8 +1307,8 @@ class SubscriptionServiceTests : WithTimescaleContainer, WithKafkaContainer { @CsvSource( "near;minDistance==1000, Polygon, '[[[100.0, 0.0], [101.0, 0.0], [101.0, -1.0], [100.0, 0.0]]]', 0", "near;maxDistance==1000, Polygon, '[[[100.0, 0.0], [101.0, 0.0], [101.0, -1.0], [100.0, 0.0]]]', 1", - "contains, Polygon, '[[[90.0, 0.0], [100.0, 10.0], [110.0, 0.0], [100.0, -10.0], [90.0, 0.0]]]', 1", - "contains, Polygon, '[[[80.0, 0.0], [90.0, 5.0], [90.0, 0.0], [80.0, 0.0]]]', 0", + "within, Polygon, '[[[90.0, 0.0], [100.0, 10.0], [110.0, 0.0], [100.0, -10.0], [90.0, 0.0]]]', 1", + "within, Polygon, '[[[80.0, 0.0], [90.0, 5.0], [90.0, 0.0], [80.0, 0.0]]]', 0", "equals, Point, '[100.0, 0.0]', 1", "equals, Point, '[101.0, 0.0]', 0", "intersects, Polygon, '[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]', 1",