[postgis-users] Help with SQL query?

Pierre Racine Pierre.Racine at sbf.ulaval.ca
Mon Nov 23 18:13:44 PST 2015


Good catch Roxanne! Thanks!

From: postgis-users [mailto:postgis-users-bounces at lists.osgeo.org] On Behalf Of Roxanne Reid-Bennett
Sent: Monday, November 23, 2015 7:46 PM
To: postgis-users at lists.osgeo.org
Subject: Re: [postgis-users] Help with SQL query?

On 11/23/2015 12:41 PM, Darrel Maddy wrote:
Dear Pierre,

I was not looking for a total solution and I am grateful for the suggestion.

Although it may not look like it, I did consult the documentation and also I have Regina’s book beside me.  Unfortunately, for me at least, both documents assume some knowledge of SQL – which I do not have.  I am also trying to do this simultaneously with a large number of other things that are new to me.  I do not find the errors reported at all informative and consider the query I am trying to perform to be relatively trivial and hence I had hoped the structure of the query might have been more intuitive.  For others it may be.

FWIW - I don't play with rasters, but this appears to be a pure SQL thing... add "as rast" like below and try again.


SELECT ST_MapAlgebra(deposition.rast, concentrated.rast, 'IF concentrated > 6 THEN deposition ELSE NULL ENDIF ' )
as rast
            FROM mymodel.deposition, mymodel.concentrated
            WHERE ST_UpperleftX(mymodel.deposition.rast) = ST_UpperleftX(mymodel.concentrated.rast) AND
                         ST_UpperleftY(mymodel.deposition.rast) = ST_UpperleftY(mymodel.deposition.rast)

Roxanne



I will try not to bother you again.

Darrel




From: postgis-users [mailto:postgis-users-bounces at lists.osgeo.org] On Behalf Of Pierre Racine
Sent: 23 November 2015 20:30
To: PostGIS Users Discussion <postgis-users at lists.osgeo.org><mailto:postgis-users at lists.osgeo.org>
Subject: Re: [postgis-users] Help with SQL query?

The expression has to stay as it was: 'IF [rast2] > threshold THEN [rast1] ELSE NULL ENDIF '

Just replace the threshold value as you did.

Do not try to replace the [rast2] and [rast1]. They refer to the first and second raster pixel values. Read the ST_Mapalgebra doc…

Don’t expect our suggestions to work blindly. I did not test this query. I’m not in your context. I expect you read the doc about all the mentioned functions and adjust for your specific context. I said “your query should “look like” this”…

Pierre


From: postgis-users [mailto:postgis-users-bounces at lists.osgeo.org] On Behalf Of Darrel Maddy
Sent: Monday, November 23, 2015 2:47 PM
To: PostGIS Users Discussion
Subject: Re: [postgis-users] Help with SQL query?

OK I spoke too soon.

I tried this:

SELECT (ST_SummaryStats(ST_Union(rast))).sum sum
FROM (SELECT ST_MapAlgebra(deposition.rast, concentrated.rast, 'IF concentrated > 6 THEN deposition ELSE NULL ENDIF ' )
            FROM mymodel.deposition, mymodel.concentrated
            WHERE ST_UpperleftX(mymodel.deposition.rast) = ST_UpperleftX(mymodel.concentrated.rast) AND
                         ST_UpperleftY(mymodel.deposition.rast) = ST_UpperleftY(mymodel.deposition.rast) ) foo;

And all I get is rast does not exist.

I’m afraid the penny has not dropped yet ☹

Darrel




From: postgis-users [mailto:postgis-users-bounces at lists.osgeo.org] On Behalf Of Darrel Maddy
Sent: 23 November 2015 16:30
To: PostGIS Users Discussion <postgis-users at lists.osgeo.org<mailto:postgis-users at lists.osgeo.org>>
Subject: Re: [postgis-users] Help with SQL query?

Dear Pierre and Rasmus,

Many thanks for trying to help.

Rasmus: I am aware of GMT but I was looking for a solution in postgis so that I can keep all of the data extraction in one place.

Pierre: That is exactly what I was looking for and very many thanks for including the explanation.  I am a little overwhelmed with the number of functions offered in postgis. It is certainly a remarkable tool.  Watching the queries plough through my datasets is a pleasure – albeit the results do not always please me ☺

Hopefully I can put this to work later tonight.

Best wishes

Darrel




From: postgis-users [mailto:postgis-users-bounces at lists.osgeo.org] On Behalf Of Pierre Racine
Sent: 23 November 2015 16:17
To: PostGIS Users Discussion <postgis-users at lists.osgeo.org<mailto:postgis-users at lists.osgeo.org>>
Subject: Re: [postgis-users] Help with SQL query?

Darrel,


1)      Create a new raster selecting the right pixels with ST_MapAlgebra(raster, raster)

2)      Make sure only intersecting rasters are processed by using their upper left corner X and Y coordinates (with ST_UpperLeftX() and UpperLeftY())

3)      Sum the selected pixels with ST_SummaryStats(rast)

All in all a global query should look like this:

SELECT (ST_SummaryStats(ST_Union(rast))).sum sum
FFOM (SELECT ST_MapAlgebra(tableA.rast, tableB.rast, 'IF [rast2] > threshold THEN [rast1] ELSE NULL ENDIF ' )
            FROM tableA, tableB
            WHERE ST_UpperleftX(tableA.rast) = ST_UpperleftX(tableB.rast) AND
                         ST_UpperleftY(tableA.rast) = ST_UpperleftY(tableB.rast) AND
                         maybe some other condition here if you get time series e.g. tableA.year = tableB.year AND tableA.month = tableB.month) foo

If you have millions of tile you could create indexes on ST_UpperleftX(tableA.rast), ST_UpperleftX(tableB.rast), ST_UpperleftY(tableA.rast) and ST_UpperleftY(tableB.rast) to make the query faster.

You could also just use WHERE ST_Intersects(tableA.rast, tableB.rast) instead…

Pierre

From: postgis-users [mailto:postgis-users-bounces at lists.osgeo.org] On Behalf Of Darrel Maddy
Sent: Monday, November 23, 2015 7:20 AM
To: PostGIS Users Discussion
Subject: [postgis-users] Help with SQL query?

Dear all,

As you know I am relatively new to postgis and SQL and therefore  I have much to learn. However, I am facing a paper deadline and need to do some quick analysis of the data I have and I am struggling to figure out how best to pursue what I need to do.

I have a significant number of rasters which have double precision values.  Without going into detail about what the rasters represent, I need to extract and sum values from one set of rasters in say table A based upon  values in another set of rasters in say table B  where the pixel value in the raster from Table B exceeds a threshold. Both tables are the same size (rasters are tiled) but I also need to figure out how I make sure the correct rasters are compared.  They have filenames like this rastervariable_10.tif, rastervariable_100.tif , presumably I need to use a logical expression to strip the numerical value (in this case this represents the year) and then order on that basis?

I can do this in QGIS one at a time but that is a little clumsy and rather time consuming.

If someone can just point me in the right direction I am sure I can figure out the rest for myself.

Apologies once more for asking what is probably a rather trivial question and yet again demonstrating my ignorance.

Many thanks

Darrel




_______________________________________________

postgis-users mailing list

postgis-users at lists.osgeo.org<mailto:postgis-users at lists.osgeo.org>

http://lists.osgeo.org/mailman/listinfo/postgis-users




--

[At other schools] I think the most common fault in general is to teach students how to pass exams instead of teaching them the science.

Donald Knuth
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20151124/a0b8eeed/attachment.html>


More information about the postgis-users mailing list