Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect order of parameters when calling PostGIS functions #1305

Merged
merged 2 commits into from
Jan 6, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading