Skip to content

NTS: Additional translations #457

Closed as not planned
Closed as not planned

Description

@bricelam

Doing my research, I found some additional translations you may want to add:

NTS PostGIS
AsBinary ST_AsBinary
Buffer ST_Buffer
ConvexHull ST_ConvexHull
Dimension ST_Dimension
EndPoint ST_EndPoint
Envelope ST_Envelope
ExteriorRing ST_ExteriorRing
GetInteriorRingN ST_InteriorRingN
GetPointN ST_PointN
IsCCW ST_IsPolygonCCW
IsRing ST_IsRing
Normalized ST_Normalize
NumInteriorRings ST_NumInteriorRing
PointOnSurface ST_PointOnSurface
SRID ST_SRID
StartPoint ST_StartPoint
ToGMLFeature ST_AsGML
WKBReader.Read ST_GeomFromWKB
WKTReader.Read ST_GeomFromText

Activity

  1. roji commented on Jun 5, 2018

    @roji
    Member

    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).

  2. added this to the Backlog milestone on Jun 10, 2018
  3. tematim commented on Jun 26, 2018

    @tematim

    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

  4. slavanap commented on Aug 13, 2018

    @slavanap
    Contributor

    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.

  5. slavanap commented on Aug 13, 2018

    @slavanap
    Contributor

    More than that, I'm trying to migrate to PostGIS from SqlGeography and haven't found method similar to SqlGeography.ReorientObject() just yet.

  6. roji commented on Oct 27, 2018

    @roji
    Member

    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

  7. EricStG commented on Nov 7, 2018

    @EricStG
    Contributor

    Are there plans to add the <-> operator to that list? while Distance would work, it doesn't use indexes

  8. roji commented on Nov 11, 2018

    @roji
    Member

    @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.

  9. EricStG commented on Nov 11, 2018

    @EricStG
    Contributor

    @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

  10. roji commented on Nov 12, 2018

    @roji
    Member

    @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?

  11. EricStG commented on Nov 12, 2018

    @EricStG
    Contributor

    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_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.

  12. roji commented on Nov 13, 2018

    @roji
    Member

    Thanks for your help on this @EricStG, we'll wait to see what comes out.

  13. EricStG commented on Nov 13, 2018

    @EricStG
    Contributor

    @roji This is the answer I got: https://lists.osgeo.org/pipermail/postgis-users/2018-November/043009.html

    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.

  14. 11 remaining items

  15. EricStG commented on Mar 18, 2021

    @EricStG
    Contributor
  16. john-larson commented on Apr 4, 2021

    @john-larson

    +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?

  17. roji commented on Apr 4, 2021

    @roji
    Member

    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.

  18. faibistes commented on Apr 28, 2021

    @faibistes

    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 REPLACE FUNCTION public.fast_distance(geom1 geometry, geom2 geometry)
     RETURNS double precision
     LANGUAGE sql
     IMMUTABLE STRICT
    AS $function$
    	SELECT geom1<->geom2;
    $function$
    ;

    Then:

    public static class Extensiones
    {
        public static double FastDistance(this Geometry a,this Geometry b) => throw new InvalidOperationException("Server only");
    }

    And in the context:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var gMethodInfo = typeof(Extensiones.Extensiones)
            .GetRuntimeMethod(nameof(Extensiones.Extensiones.FastDistance), new[] { typeof(Geometry), typeof(Geometry) });
        modelBuilder.HasDbFunction(gMethodInfo)
            .HasName("public.fast_distance");
    }

    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:

    q = Ctos.Select(x => new {
         Cto = x,
         Finca = Fincas.OrderBy(y => y.Area.FastDistance(x.Area)).First()
    });

    translates to:

    SELECT c.gid, c.geom, c.migac, c.cobertura, c.cto, c.tipo, t.gid, t.accesos, t.baftth, t.geom, t.estado, t.fuente, t.finca, t.fxliberacion, t.modifpor, t.cnucl, t.fxsenda, t.acctrac, t.uuii_norm
    FROM nac.cto AS c
    LEFT JOIN LATERAL (
        SELECT f.gid, f.accesos, f.baftth, f.geom, f.estado, f.fuente, f.finca, f.fxliberacion, f.modifpor, f.cnucl, f.fxsenda, f.acctrac, f.uuii_norm
        FROM nac.fincadesp AS f
        ORDER BY nac.fast_distance(f.geom, c.geom)
        LIMIT 1
    ) AS t ON TRUE
  19. roji commented on Apr 28, 2021

    @roji
    Member

    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.

  20. luo007 commented on May 8, 2021

    @luo007

    Can "ST_AsMVT" and "ST_AsMVTGeom" be supported?

  21. roji commented on May 8, 2021

    @roji
    Member

    @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

  22. luo007 commented on May 10, 2021

    @luo007

    @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

  23. XYCaptain commented on Jun 10, 2021

    @XYCaptain

    Can "ST_AsMVT" and "ST_AsMVTGeom" be supported?

    @luo007
    Hello, how do you implement these calls now? I would appreciate it if you could give me a hint.

  24. luo007 commented on Jun 15, 2021

    @luo007

    可以支持“ST_AsMVT”和“ST_AsMVTGeom”吗?

    @luo007
    你好,你现在如何实现这些调用?如果你能给我一个提示,我将不胜感激。

    ST_AsMVT Because efcore does not support custom aggregate functions, it cannot be implemented

  25. luo007 commented on Dec 12, 2023

    @luo007

    Hi, I see that efcore7 now supports custom aggregate functions. ST_AsMVT Whether it is supported

  26. roji commented on Dec 12, 2023

    @roji
    Member

    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).

  27. removed this from the Backlog milestone on Dec 12, 2023
  28. removed
    good first issue
    on Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions