-
Notifications
You must be signed in to change notification settings - Fork 225
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
NTS: Additional translations #457
Comments
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). |
Hello, I'll need these one too : LineString Polygon |
Please don't forget about ST_GeographyFromWKT. I really need that one. Now I'm trying to use |
More than that, I'm trying to migrate to PostGIS from SqlGeography and haven't found method similar to SqlGeography.ReorientObject() just yet. |
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 |
Are there plans to add the <-> operator to that list? while |
@EricStG am far from a PostGIS expert: should the <-> operator be used everywhere instead of |
@roji I'm far from being an expert as well, but my understanding is that |
@EricStG thanks for the extra info, although I'm still not totally clear on what should happen, and when exactly Can you help out by contacting the PostGIS folks and asking for clarification, possibly proposing that they make their documentation clearer on this point? |
To get the nearest neighbors, you would use You would use I'll try posting on the PostGIS mailing list to see if anyone can point me to better documentation. |
Thanks for your help on this @EricStG, we'll wait to see what comes out. |
@roji This is the answer I got: https://lists.osgeo.org/pipermail/postgis-users/2018-November/043009.html
|
@EricStG OK, thanks for the info... I don't think I mind assuming PostgreSQL 9.5+ with PostGIS 2.2. However, if I'm understanding things correctly, we may want to always use <-> for geometry, but use it for geography only under In any case I'd certainly write back to the PostGIS people to propose adding this important information to their docs... |
Looking at the EF Core spatial data documentation, it looks like the following are still missing: Geometry.Buffer(double, int)Can be implemented using ST_Buffer Geometry.InteriorPointCan be implemented using ST_PointOnSurface Geometry.OgcGeometryTypeCan be implemented using ST_GeometryType Geometry.Union()There's a comment in the code mentioning that ST_Union with a single parameter is an aggregate, but the SQLite translation uses Does this make sense? Anyone mind if I work on it? |
Correct, |
Absolutely! (it's even marked up for grabs). Looking forward to seeing a PR! |
Doing some quick search in the code, it looks like these are the only missing methods based on the original list:
I'll see if I can make those happen in the next few weeks |
I'd run them by the NTS team first. They seemed receptive to adding operations that made sense. Otherwise yes, EF.Functions (or maybe just Geometry extension methods) would be the next best thing. |
Quick question. Operator "<->" hasnt' been implemented yet, has it? In my case the difference is huge. Order by "<->" takes around 4 ms, ST_Distance with spheroid set to true = 250 ms, set to false = 120 ms. |
Isn't this simply because |
BTW not saying it should not be supported - there are definitely situations where 2D distance is useful for geography, just asking. |
Interestingly the operator returns the same result as ST_Distance with spheroid set to false. Does anyone know why then there is such a performance gap? As for my use case - precision isn't that important. BTW. the difference (between operator/spheroid=false and spheroid=true) isn't big though. ~10km for distance around 4500 km. |
In short, ST_DIstance can't use an index: https://postgis.net/workshops/postgis-intro/knn.html |
+1 for the implementation of |
I took another look, and the provider already translates
That shouldn't be the case - whether the provider translates to |
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 |
FYI I'm adding built-in translation support for the @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. |
Can "ST_AsMVT" and "ST_AsMVTGeom" be supported? |
@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. |
@luo007 |
ST_AsMVT Because efcore does not support custom aggregate functions, it cannot be implemented |
Hi, I see that efcore7 now supports custom aggregate functions. ST_AsMVT Whether it is supported |
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). |
Doing my research, I found some additional translations you may want to add:
AsBinaryST_AsBinaryBufferST_BufferConvexHullST_ConvexHullDimensionST_DimensionEndPointST_EndPointEnvelopeST_EnvelopeExteriorRingST_ExteriorRingGetPointNST_PointNIsRingST_IsRingNormalizedST_NormalizeNumInteriorRingsST_NumInteriorRingPointOnSurfaceST_PointOnSurfaceSRIDST_SRIDStartPointST_StartPointThe text was updated successfully, but these errors were encountered: