[postgis-users] Help with SQL query?
Birgit Laggner
birgit.laggner at ti.bund.de
Tue Nov 24 04:18:38 PST 2015
Hi Darrel,
my PostGIS version is too old for testing, but if I read the
documentation right, then your expression has to be SQL. And IF THEN
ELSE etc. is not SQL as far as I know - SQL has CASE WHEN.
So, I would assume, you would need to write your expression like this:
'CASE WHEN [rast2] > 0.0 THEN [rast1] ELSE NULL END'
I am curious if this helps with your error...
Regards,
Birgit
Am 24.11.2015 um 10:09 schrieb Darrel Maddy:
>
> Dear Roxanne,
>
> Many thanks. I did pickup that issue shortly after my last post and it
> cured the first problem.
>
> There is still an SQL issue with the expression however.
>
> I tried ‘IF [rast2]>0.0 THEN [rast1] ELSE NULL ENDIF’
>
> This produces an error at $2. I can remove that problem by doing this
>
> ‘IF ([rast2]>0.0) THEN [rast1] ELSE NULL ENDIF’
>
> But then I get the error at THEN
>
> Curiously ‘[rast2]+[rast1]’ works as intended so somehow the
> conditional is being specified incorrectly.
>
> Unfortunately there is nothing in the documentation which helps me
> with this variant.
>
> I will keep trying.
>
> Darrel
>
> *From:*postgis-users [mailto:postgis-users-bounces at lists.osgeo.org]
> *On Behalf Of *Roxanne Reid-Bennett
> *Sent:* 24 November, 2015 12:46 AM
> *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 L
>
> 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 J
>
> 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 NULLENDIF ' )
>
> 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
>
>
> _______________________________________________
> postgis-users mailing list
> postgis-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/postgis-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20151124/9d1cce18/attachment.html>
More information about the postgis-users
mailing list