You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Yeah, went I implemented the NTS plugin I implemented everything that was straightforward. The above were more difficult for some reason - mainly because the NetTopologySuite API didn't map out-of-the-box - so I deferred them. As people request the above we'll work on adding them.
This is a good up-for-grabs for anyone wanting to get some practice on EF Core SQL translation - I'll accept PRs for individual translations (you don't have to do everything at once).
Please don't forget about ST_GeographyFromWKT. I really need that one. Now I'm trying to use NetTopologySuite.Geometries.Geometry.DefaultFactory.CreateMultiPolygon() as a workaround.
Note, implemented the following as part of #672: ST_Dimension, ST_EndPoint, ST_Envelope, ST_ExteriorRing, ST_IsRing, ST_NumInteriorRings, ST_PointOnSurface, ST_SRID, ST_StartPoint
Reacted by Austin Drenski, Vyacheslav Napadovsky and Brice Lambson
@EricStG am far from a PostGIS expert: should the <-> operator be used everywhere instead of ST_Distance? If so, why does ST_Distance exist and not make use of indexes? Any more precise info on this would help.
@roji I'm far from being an expert as well, but my understanding is that <-> returns values that gives you relative distances, but doesn't necessarily map to a real distance unit, that's why it only makes sense to use it for ordering.
From this section, they mention that only specific functions/operators can make use of indices, but not sure why this couldn't be applied to something like ST_Distance
@EricStG thanks for the extra info, although I'm still not totally clear on what should happen, and when exactly <-> should be used instead of ST_Distance().
Can you help out by contacting the PostGIS folks and asking for clarification, possibly proposing that they make their documentation clearer on this point?
You would use ST_Distance or ST_Distance_Sphere when you need to know the actual distance between two points. like the kilometers between two geography points, but because it's calculated for every row, performance would suffer over any significant datasets.
I'll try posting on the PostGIS mailing list to see if anyone can point me to better documentation.
ST_Distance existed before we had <->.
If you are using PostgreSQL 9.5+ with PostGIS 2.2, you can just stick with using <->, but note that it doesn't always use and index. <-> only uses and index when it is in the ORDER BY clause and one side is a constant geometry (which you can achieve even when you don't have a constant by using a LATERAL clause).
In the case of geography though <-> will always give the sphere distance, while ST_Distance by default gives the spheroid distance. So ST_Distance is slightly more accurate to use for geography.
+1 for the implementation of ST_DWithin. It must be used instead of ST_Distance if spatial indexes are to be used. I tried using a user-defined function as a proxy for a quick work-around but it does not use the spatial index inside a user-defined function. @roji@bricelam Is there any other work-around I can use until this is implemented?
I took another look, and the provider already translates Geometry.IsWithinDistance to ST_DWithin since version 3.0.0 (see #740). All supported spatial translations are documented here.
I tried using a user-defined function as a proxy for a quick work-around but it does not use the spatial index inside a user-defined function.
That shouldn't be the case - whether the provider translates to ST_DWithin or whether you translate to it via a UDF shouldn't have any bearing on whether it uses a spatial index or not... Review your index and the SQL being generated to find out what's going on.
I took another look, and the provider already translates Geometry.IsWithinDistance to ST_DWithin since version 3.0.0 (see #740). All supported spatial translations are documented here.
I tried using a user-defined function as a proxy for a quick work-around but it does not use the spatial index inside a user-defined function.
That shouldn't be the case - whether the provider translates to ST_DWithin or whether you translate to it via a UDF shouldn't have any bearing on whether it uses a spatial index or not... Review your index and the SQL being generated to find out what's going on.
You have to define your function as SQL (usually, a single select), not plsql, so postgresql can inline it and use indexes:
CREATE OR REPLACEFUNCTIONpublic.fast_distance(geom1 geometry, geom2 geometry)
RETURNS double precision
LANGUAGE sql
IMMUTABLE STRICT
AS $function$
SELECT geom1<->geom2;
$function$
;
BTW, is there any way I can write a query that translates as LATERAL JOIN? AFAIK, it's the only way to get KNN with indexes for a fairly common use case, namely, matching the closest point in table b for all points in table a
FYI I'm adding built-in translation support for the <-> operator for version 6.0.
@faibistes that's a good workaround in the meantime. For LATERAL JOINs, EF Core will automatically produce these as needed, depending on how your query is structured.
@luo007 sure, translations are added based on how much people ask for them (PRs are also always welcome!). For ST_AsMVTGeom, we'd need to decide how to map the required PostGIS box2d type (which doesn't exist in NetTopologySuite).
ST_AsMVT is an aggregate function, so blocked by EF Core support: dotnet/efcore#22957
@luo007 sure, translations are added based on how much people ask for them (PRs are also always welcome!). For ST_AsMVTGeom, we'd need to decide how to map the required PostGIS box2d type (which doesn't exist in NetTopologySuite).
ST_AsMVT is an aggregate function, so blocked by EF Core support: dotnet/efcore#22957
I think the box2d type can be replaced by the geometry type in NTS temporarily, and used with ST_MakeEnvelope.
ST_AsMVT I have no idea how to implement it
Yes, it should now be possible to translate to ST_AsMVT.
I don't think keeping this "additional translations" issue is useful at this point, since most translations are already supported - we can track specific translations via individual issues (opened #3016 for ST_AsMVT specifically).
Activity
roji commentedon Jun 5, 2018
Yeah, went I implemented the NTS plugin I implemented everything that was straightforward. The above were more difficult for some reason - mainly because the NetTopologySuite API didn't map out-of-the-box - so I deferred them. As people request the above we'll work on adding them.
This is a good up-for-grabs for anyone wanting to get some practice on EF Core SQL translation - I'll accept PRs for individual translations (you don't have to do everything at once).
tematim commentedon Jun 26, 2018
Hello,
I'll need these one too :
Point
ST_Equals
ST_Buffer
LineString
ST_Centroid
ST_StartPoint
ST_Equals
ST_Buffer
Polygon
ST_Centroid
ST_Multi
ST_Buffer
ST_Dump
slavanap commentedon Aug 13, 2018
Please don't forget about ST_GeographyFromWKT. I really need that one. Now I'm trying to use
NetTopologySuite.Geometries.Geometry.DefaultFactory.CreateMultiPolygon()as a workaround.slavanap commentedon Aug 13, 2018
More than that, I'm trying to migrate to PostGIS from SqlGeography and haven't found method similar to SqlGeography.ReorientObject() just yet.
roji commentedon Oct 27, 2018
Note, implemented the following as part of #672: ST_Dimension, ST_EndPoint, ST_Envelope, ST_ExteriorRing, ST_IsRing, ST_NumInteriorRings, ST_PointOnSurface, ST_SRID, ST_StartPoint
EricStG commentedon Nov 7, 2018
Are there plans to add the <-> operator to that list? while
Distancewould work, it doesn't use indexesroji commentedon Nov 11, 2018
@EricStG am far from a PostGIS expert: should the <-> operator be used everywhere instead of
ST_Distance? If so, why doesST_Distanceexist and not make use of indexes? Any more precise info on this would help.EricStG commentedon Nov 11, 2018
@roji I'm far from being an expert as well, but my understanding is that
<->returns values that gives you relative distances, but doesn't necessarily map to a real distance unit, that's why it only makes sense to use it for ordering.From this section, they mention that only specific functions/operators can make use of indices, but not sure why this couldn't be applied to something like
ST_Distanceroji commentedon Nov 12, 2018
@EricStG thanks for the extra info, although I'm still not totally clear on what should happen, and when exactly
<->should be used instead ofST_Distance().Can you help out by contacting the PostGIS folks and asking for clarification, possibly proposing that they make their documentation clearer on this point?
EricStG commentedon Nov 12, 2018
To get the nearest neighbors, you would use
<->To search within a distance, you would use
ST_DWithin. More info here: https://postgis.net/2013/08/26/tip_ST_DWithin/You would use
ST_DistanceorST_Distance_Spherewhen you need to know the actual distance between two points. like the kilometers between two geography points, but because it's calculated for every row, performance would suffer over any significant datasets.I'll try posting on the PostGIS mailing list to see if anyone can point me to better documentation.
roji commentedon Nov 13, 2018
Thanks for your help on this @EricStG, we'll wait to see what comes out.
EricStG commentedon Nov 13, 2018
@roji This is the answer I got: https://lists.osgeo.org/pipermail/postgis-users/2018-November/043009.html
11 remaining items
EricStG commentedon Mar 18, 2021
In short, ST_DIstance can't use an index: https://postgis.net/workshops/postgis-intro/knn.html
john-larson commentedon Apr 4, 2021
+1 for the implementation of
ST_DWithin. It must be used instead ofST_Distanceif spatial indexes are to be used. I tried using a user-defined function as a proxy for a quick work-around but it does not use the spatial index inside a user-defined function. @roji @bricelam Is there any other work-around I can use until this is implemented?roji commentedon Apr 4, 2021
I took another look, and the provider already translates
Geometry.IsWithinDistancetoST_DWithinsince version 3.0.0 (see #740). All supported spatial translations are documented here.That shouldn't be the case - whether the provider translates to
ST_DWithinor whether you translate to it via a UDF shouldn't have any bearing on whether it uses a spatial index or not... Review your index and the SQL being generated to find out what's going on.faibistes commentedon Apr 28, 2021
You have to define your function as SQL (usually, a single select), not plsql, so postgresql can inline it and use indexes:
Then:
And in the context:
BTW, is there any way I can write a query that translates as LATERAL JOIN? AFAIK, it's the only way to get KNN with indexes for a fairly common use case, namely, matching the closest point in table b for all points in table a
EDIT: OK, this is brilliant:
translates to:
roji commentedon Apr 28, 2021
FYI I'm adding built-in translation support for the
<->operator for version 6.0.@faibistes that's a good workaround in the meantime. For LATERAL JOINs, EF Core will automatically produce these as needed, depending on how your query is structured.
luo007 commentedon May 8, 2021
Can "ST_AsMVT" and "ST_AsMVTGeom" be supported?
roji commentedon May 8, 2021
@luo007 sure, translations are added based on how much people ask for them (PRs are also always welcome!). For ST_AsMVTGeom, we'd need to decide how to map the required PostGIS box2d type (which doesn't exist in NetTopologySuite).
ST_AsMVT is an aggregate function, so blocked by EF Core support: dotnet/efcore#22957
luo007 commentedon May 10, 2021
I think the box2d type can be replaced by the geometry type in NTS temporarily, and used with ST_MakeEnvelope.
ST_AsMVT I have no idea how to implement it
XYCaptain commentedon Jun 10, 2021
@luo007
Hello, how do you implement these calls now? I would appreciate it if you could give me a hint.
luo007 commentedon Jun 15, 2021
ST_AsMVT Because efcore does not support custom aggregate functions, it cannot be implemented
luo007 commentedon Dec 12, 2023
Hi, I see that efcore7 now supports custom aggregate functions. ST_AsMVT Whether it is supported
roji commentedon Dec 12, 2023
Yes, it should now be possible to translate to ST_AsMVT.
I don't think keeping this "additional translations" issue is useful at this point, since most translations are already supported - we can track specific translations via individual issues (opened #3016 for ST_AsMVT specifically).