BenMorel
BenMorel

Reputation: 36484

Efficient & compatible way to call several functions on a single, complex SQL result?

I'm developing a geometry library that delegates complex calculations to a GIS-enabled database such as PostgreSQL with PostGIS, SQLite with SpatiaLite, or newer versions of MySQL.

When communicating with the database, I need so send back and forth:

Sending a geometry to the database is an easy one-step process:

ST_GeomFromText(text, srid)

But retrieving the geometry is a two-step process:

Now let's take a real-life example.

Here is the SQL I'm sending to get the centroid of a polygon:

SELECT ST_AsText(ST_Centroid(ST_GeomFromText(geom, srid)));

As you can see, I'm only getting the textual representation of the geometry back, and am missing the SRID.

How can I efficiently get the result of both ST_AsText() and ST_SRID() in a single query?

Here are the possibilities I considered so far:

Note that this must work on MySQL, PostgreSQL, SQLite, and maybe other DBMS in the future.

Feel free to explore other out-of-the-box ideas that I would have missed!

Side note: you may wonder why I'm not always considering that the result geometry has the same SRID as the input geometry; this is probably true, but what happens when a function is called with two geometries with different SRID is not defined by the standard AFAIK, so ideally I'd like the library to stay unopinionated on the subject, and just return whatever SRID the database returns.

Upvotes: 1

Views: 76

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239646

I think all of those databases should support subqueries, which are standard

SELECT
    ST_AsText (centroid),
    ST_SRID   (centroid)
FROM (SELECT ST_Centroid(ST_GeomFromText(geom, srid)) as centroid) as t;

(MySQL)(SQLite)(PostgreSQL)

Upvotes: 1

Related Questions