Changes between Version 206 and Version 207 of WKTRaster/SpecificationWorking03


Ignore:
Timestamp:
Jul 24, 2012, 1:41:23 PM (12 years ago)
Author:
pracine
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • WKTRaster/SpecificationWorking03

    v206 v207  
    1818== ''Note that accomplished objectives have been moved at the bottom of the page.'' ==
    1919
     20----
     21== '''Objective 2.0.04 - Implement better support for NULL, Empty, !HasNoBand(rast), !HasNoBand(rast, band) and !BandIsNoData rasters in all functions.''' ==
     22
     23 Each function should better handle NULL rasters, empty rasterm rasters with no band and bands only filled with nodata values.
     24
     25  1) Generally, when NULL rasters are provided, return NULL. If the function involves a second non NULL raster and something can be done with it, do it.
     26
     27  2) Generally, when empty rasters are provided (ST_IsEmpty, width=0 or height=0), return an empty raster. If the function involves a second non empty raster and something can be done with it, do it.
     28
     29  2) Generally, when a !HasNoBand(rast) or a !HasNoBand(rast, band) raster is provided return a raster with no band but with the right extent. If the function involves a second raster having a band or the band, treat the first missing band like a !BandIsNoData.
     30
     31  4) A !BandIsNoData raster is a normal raster but many functions can be optimized with this type of raster.
     32
     33  5) All functions having missing the requested information (about its extent when it is a NULL or an empty raster or about its band when it is a !HasNoBand(rast) or a !HasNoBand(rast, band) raster) should return NULL or a documented value.
     34
     35  6) Try as less as possible to return EXCEPTION (or ERROR).
     36
     37 See discussion in http://trac.osgeo.org/postgis/ticket/594
     38
     39'''ST_IsEmpty(rast) -> boolean'''
     40
     41 Returns TRUE if the raster width or the raster height is 0.
     42
     43'''ST_HasNoBand'''
     44
     45 '''Variants'''
     46
     47  1) ST_HasNoBand(rast, band) -> boolean
     48
     49  2) ST_HasNoBand(rast) -> boolean
     50
     51 Returns TRUE if the the raster does not have this band.
     52
     53 Variant 2 returns TRUE if the the raster does not have any band.
     54
     55'''ST_BandIsNoData'''
     56
     57 '''Variants'''
     58
     59  1) ST_BandIsNoData(rast, band) -> boolean
     60
     61  2) ST_BandIsNoData(rast) -> boolean
     62
     63 Returns TRUE if the specified raster band is only filled with no data value.
     64
     65 Variant 2 default to band 1.
     66
     67 '''Implementation details'''
     68
     69 This require a new flag to be set in the core at import and at edition. See discussion in http://trac.osgeo.org/postgis/ticket/593
    2070
    2171----
     
    472522== Accomplished objectives ==
    473523
    474 ----
    475 == '''Objective FV.01 - Being able to return a JPEG, a TIFF, a PNG or any image format supported by GDAL - ''Done''''' ==
    476  
    477 '''ST_bytea(raster, integer) -> raster''' -- the integer parameters is the band number of the raster.[[BR]]
    478 What is does?
    479 
    480 ----
    481 ~~'''Open Question:''' When exporting a multiband raster to JPEG, TIFF, PNG, SVG or KML, how should we specify the band number in the exporting function.~~
    482 
    483 ~~There is two options to select the band to convert from a multiband raster in all the ST_AsFormat functions. [[BR]]~~
    484 [[BR]]
    485 ~~ 1. Precede each call with ST_Band() to return a selected band.[[BR]]~~
    486 ~~  Pros: This is a general function that can be called before any function that would otherwise require a band parameter.[[BR]]~~
    487 ~~  Cons: This implies creating a temporary raster. This might be more elegant and general but is this too much overhead comparing with having a band parameter?~~
    488 
    489 ~~ 2. Add a band parameter to each ST_AsFormat function.[[BR]]~~
    490 ~~  Pros: Hypothetically less overhead.[[BR]]~~
    491 ~~  Cons: Every functions implying access to a band should then have this parameter when in most case it would be equal to 1. In many cases it makes no sence to have to specify a band parameter since it is the whole raster that we want to export, including all the bands.~~
    492 
    493 ~~Pierre: More I think about it more I think that the first option is the best one...~~
    494 
    495 ~~mloskot: Perhaps there is a compromise in form of two sets of functions: 1) ST_As* which always burn the whole raster (all bands) 2) ST_BandAs* which takes number of band as a parameter and return only this requested band.~~
    496 
    497 ----
    498 
    499 '''ST_Band(raster, integer) -> raster''' -- the integer parameters are the band number of the rasters.[[BR]]
    500 Return a single band from a multiband raster. If "band" is greater than the value returned by ST_GetNumBands(), the function returns the last band. This function should be used to select a band before converting it to JPEG, TIFF, PNG, SVG or KML with the corresponding function. e.g. '''ST_AsTIFF(ST_Band(raster, band))
    501 
    502 A complete implementation of ST_Band should include the following:
    503 
    504 1. ST_Band(rast raster, nbands int[]) -> raster
    505 
    506     nbands is an array of 1-based band indices of the bands to copy into the output raster
    507 
    508     For a raster rast with 3 bands:
    509 {{{
    510 ST_Band(rast, ARRAY[1,3,2])
    511 
    512 ST_Band(rast, ARRAY[3,2,1])
    513 }}}
    514     You can rearrange the bands as above. You can also duplicate the bands:
    515 {{{
    516 ST_Band(rast, ARRAY[1,2,3,2,1])
    517 }}}
    518 
    519 2. ST_Band(rast raster, nband int) -> raster
    520 
    521     nband is a single integer of the 1-based band index of the band to copy into the output raster
    522 
    523 {{{
    524 ST_Band(rast, 1)
    525 
    526 ST_Band(rast, 3)
    527 }}}
    528 
    529 3. ST_Band(rast raster, nbands text) -> raster
    530 
    531     nbands is a comma separated string of 1-based band indices indicating the bands to copy into the output raster
    532 
    533 {{{
    534 ST_Band(rast, '1,2')
    535 
    536 ST_Band(rast, '1,2,3, 1, 1 , 2')
    537 }}}
    538 
    539 4. ST_Band(rast raster, nbands text, delimiter char) -> raster
    540 
    541     nbands is a user-specified delimiter separated string of 1-based band indices indicating the bands to copy into the output raster
    542 
    543 {{{
    544 ST_Band(rast, '1,2', ',')
    545 
    546 ST_Band(rast, '1,2,3, 1, 1 , 2', ',')
    547 }}}
    548 
    549 5. ST_Band(rast raster) -> raster
    550 
    551     the band to extract is automatically assumed to be one.
    552 
    553 {{{
    554 ST_Band(rast)
    555 }}}
    556 
    557 If an index is outside the valid range of band indices for a raster (less than 1 or greater than the value returned by ST_NumBands), the function will fail and return.
    558 
    559 ~~''' Open Question: ''' Should the function fail if an index is invalid?  How should this work when providing more than one indices to the function?~~
    560 
    561 ----
    562 
    563 '''ST_AsJPEG(raster, quality) -> JPEG as "bytea"'''[[BR]]
    564 
    565 The JPEG format has several limitations:
    566 
    567   1. JPEG only allows 1 (greyscale) or 3 (RGB) bands of data
    568 
    569   2. JPEG only supports 8BUI pixeltype
    570 
    571   3. JPEG cannot embed spatial reference information within the file but can have an associated world file
    572 
    573 To address the limitations:
    574 
    575   1. Use ST_Band to specify which band(s) should be passed to the ST_AsJPEG function. Variations of ST_AsJPEG are made available that allows specifying a band index. If a raster whose number of specified bands does not equal 1 or 3 is provided, a warning is raised and the first or the first three bands are used.
    576 
    577   2. Throw an exception if any of the specified bands is not 8BUI. The user should use ST_Reclass to convert any non-8BUI bands to 8BUI.
    578 
    579   3. Nothing can be done.
    580 
    581 A proposed set of variations of the ST_AsJPEG function:
    582 
    583 1. ST_AsJPEG(rast raster, options text[])
    584 
    585     rast: the raster with one or three bands in 8BUI pixel type to generate a JPEG image from
    586 
    587     options: array of creation options to pass to the GDAL JPEG driver
    588 
    589 {{{
    590 ST_AsJPEG(rast, ARRAY['QUALITY=90', 'PROGRESSIVE=ON'])
    591 }}}
    592 
    593 2. ST_AsJPEG(rast raster)
    594 
    595     Like !#1 above but use the driver's default creation options
    596 
    597 3. ST_AsJPEG(rast raster, nbands int[], options text[])
    598 
    599     nbands: an integer array specifying the band indices of the raster to include in the JPEG file
    600 
    601 {{{
    602 ST_AsJPEG(rast, ARRAY[1,3,6], ARRAY['QUALITY=50'])
    603 }}}
    604 
    605 4. ST_AsJPEG(rast raster, nbands int[])
    606 
    607     Like !#3, but use the default creation options
    608 
    609 {{{
    610 ST_AsJPEG(rast, ARRAY[1,3,6])
    611 }}}
    612 
    613 5. ST_AsJPEG(rast raster, nbands int[], quality int)
    614 
    615     quality: number between 10 and 100 indicating image quality
    616 
    617 {{{
    618 ST_AsJPEG(rast, ARRAY[1,2,3], 90)
    619 }}}
    620 
    621 6. ST_AsJPEG(rast raster, nband int, options text[])
    622 
    623     nband: index of the band to include
    624 
    625 {{{
    626 ST_AsJPEG(rast, 2, ARRAY['QUALITY=25'])
    627 }}}
    628 
    629 7. ST_AsJPEG(rast raster, nband int, quality int)
    630 
    631 {{{
    632 ST_AsJPEG(rast, 5, 75)
    633 }}}
    634 
    635 8. ST_AsJPEG(rast raster, nband int)
    636 
    637 {{{
    638 ST_AsJPEG(rast, 4)
    639 }}}
    640 
    641 ''OLD NOTES''
    642 
    643 ~~Return the raster as a JPEG encoded as a PostgreSQL bytea. By default quality is set to 75, but this option can be used to select other values. Values must be in the range 10-100. Low values result in higher compression ratios, but poorer image quality. Values above 95 are not meaningfully better quality but can but substantially larger. (copied from http://www.gdal.org/frmt_jpeg.html)~~
    644 
    645 
    646 ~~'''Open Question:''' Is JPEG export limited to raster having 8 bit unsigned integer pixeltype (8BUI)?~~
    647 
    648 ~~[http://www.gdal.org/frmt_jpeg.html See how GDAL do it]. It converts only 8 bits rasters. Should we do the same?~~
    649 
    650 ~~Otherwise, how do we convert other types to 8BUI? e.g. 16BUI or 8BSI?~~
    651 
    652 ~~Pierre: It might be more simple to ignore pixeltypes other than 8BUI but it would be very convenient to have a way to quickly export elevation data for example as a JPEG. It would be nice to have an elegant solution to this. Maybe something inspired from !MapServer.~~
    653 
    654 ~~Proposition one (Pierre): ST_AsJPEG could simply (optionally when the pixeltype is not 8BUI) map the ST_Maximum() and ST_Minimum() value to 0-255. ST_Maximum() and ST_Minimum() are not in the spec yet but this could be on nice usage of it. They will imply caching the min and max when importing and editing. Both function should ignore the !NoDataValues. They could also be two parameters passed to ST_AsJPEG(raster, quality, min, max).~~
    655 
    656 ~~Proposition two: There could also be just one parameter (string) defining a mapping method:~~
    657 
    658 ~~ * Method "None": No mapping. This is possible only for 8BUI.~~
    659 
    660 ~~ * Method "!MaxMinValue": Use the Max and the Min cached in the raster. e.g. for 16BSI (min, max) -> (-2033, 2456) -> (round((-2033 - -2033)/(2456 - -2033)*255), round((2456 - -2033)/(2456 - -2033)*255)) -> (0, 255).[[BR]]~~
    661 ~~[[BR]]This is equivalent to ST_AsJPEG(raster, quality, ST_Minimum(rast), ST_Maximum(rast))~~
    662 
    663 ~~ * Method "!MaxMinType": Use the Max and the Min allowed by the type. e.g. for 16BSI (min, max) -> (-2033, 2456) -> (round((-2033 - -32768)/(32767 - -32768)*255), round((2456 - -32768)/(32767 - -32768)*255)) -> (120, 137)[[BR]]~~
    664 ~~[[BR]]This would be equivalent to ST_AsJPEG(raster, quality, ST_BandPixelTypeMin(rast), ST_BandPixelTypeMax(rast)). Both functions (ST_BandPixelTypeMin & ST_BandPixelTypeMax) are not yet planned and I could not find an SQL query that returns the equivalent range for a type. [http://groups.google.nl/group/microsoft.public.sqlserver.programming/browse_thread/thread/46512c2691da4607/6743f4aea485c6d1 One possible solution.]~~
    665 
    666 
    667 ~~mloskot: ATM, I have no thoughts on this issue.~~
    668 
    669 ~~'''Open Question:''' Is JPEG export limited to raster having 1 or 3 bands?~~
    670 
    671 ~~[http://www.gdal.org/frmt_jpeg.html See how GDAL do it]. It converts only 1 or 3 band rasters. Should we do the same? In this case 1 band rasters would be exported as a greyscale JPEG having R G and B identical and 3 band rasters would be interpreted as R, G and B.~~
    672 
    673 ~~Pierre: I think the answer should be yes. I don't see how we could have a 2 band raster fit into RGB.~~
    674 
    675 ~~mloskot: I agree, the answer should be yes.~~
    676 
    677 ~~'''Here is an attempt to define the different versions of the function:'''~~
    678 
    679 ~~The most minimalistic versions of the function should assume band 1, 2 and 3 as being r, g, b and the quality equal to 75:~~
    680 
    681 ~~ ST_AsJPEG(raster) -quality = 75~~
    682 
    683 ~~A variant allow specifying the quality:~~
    684 
    685 ~~ ST_AsJPEG(raster, integer)~~
    686 
    687 ~~Another variant should enable us to specify which band correspond to the r, the g and the b:~~
    688 
    689 ~~ ST_AsJPEG(raster, integer, integer, integer) - raster, rband, gband, bband, quality=75~~
    690 
    691 ~~ ST_AsJPEG(raster, integer, integer, integer, integer) - raster, rband, gband, bband, quality~~
    692 
    693 ~~Another version should be designed to be used with a future ST_Band(raster) function. In this case there is no attempt to extract r, g or b band from any passed raster:~~
    694 
    695 ~~ ST_AsJPEG(raster, raster, raster)~~
    696 
    697 ~~ ST_AsJPEG(raster, raster, raster, integer) -with the quality param~~
    698 
    699 ~~Another series should allow converting 1 band raster with pixel of type 8BUI to a grayscale JPEG (Carefull study of the GDAL behavior when converting a single band to JPEG should be done before confirming these functions):~~
    700 
    701 ~~ ST_AsJPEG(raster, "GRAYSCALE") - convert only band 1 with quality = 75~~
    702 
    703 ~~ ST_AsJPEG(raster, "GRAYSCALE", integer) - convert only band 1 with specified quality~~
    704 
    705 ~~ ST_AsJPEG(raster, integer, "GRAYSCALE") - allow specifying the band number to convert~~
    706 
    707 ~~ ST_AsJPEG(raster, integer, "GRAYSCALE", integer) - allow specifying the band number to convert and the quality~~
    708 
    709 ~~Another series should allow converting 1 band raster of ANY pixel type to a grayscale JPEG. Pixel types different than 8BUI should be mapped according to specified min, max values and a mapping mode: "!MaxMinValue" (default) or "!MaxMinType".~~
    710 
    711 ~~ ST_AsJPEG(raster, "GRAYSCALE", min, max, text) - convert only band 1 with quality = 75~~
    712 
    713 ~~ ST_AsJPEG(raster, "GRAYSCALE", integer, min, max, text) - convert only band 1 with specified quality~~
    714 
    715 ~~ ST_AsJPEG(raster, integer, "GRAYSCALE", min, max, text) - allow specifying the band number to convert~~
    716 
    717 ~~ ST_AsJPEG(raster, integer, "GRAYSCALE", integer, min, max, text) - allow specifying the band number to convert and the quality~~
    718 
    719 ----
    720 
    721 '''ST_AsTIFF(raster, compression) -> TIFF as "bytea"'''[[BR]]
    722 Return the raster as a TIFF encoded as a PostgreSQL bytea. If raster is a multiband raster and no band were selected with ST_Band() every band are written to the resulting TIFF.
    723 
    724 compression=[JPEG/LZW/PACKBITS/DEFLATE/CCITTRLE/CCITTFAX3/CCITTFAX4/NONE]: Set the type of compression to use. None is the default. The CCITT compression should only be used with 1bit (NBITS=1) data. JPEG should only be used with Byte data. When using JPEG add a number specifying the quality. 75 is the default. e.g. ST_AsTIFF(raster, "JPEG60") (copied from http://www.gdal.org/frmt_gtiff.html)
    725 
    726 A proposed implementation of the ST_AsTIFF functions.
    727 
    728 The TIFF format is probably the most robust available for converting rasters to GDAL rasters. Not only does it support all PostGIS Raster pixel types, it also provides plenty of creation options and possibly no issues with the number of bands. The only limitation found is that there can only be one NODATA value for all bands.
    729 
    730 ''If the compression parameter/option is specified to JPEG, all bands must be of pixel type 8BUI.  If the compression parameter/option is specified to one of the CCITT options, all bands must be of pixel type 1BB.  If any band violates the restriction, an exception is raised.''
    731 
    732 The next three functions are the most basic of the ST_AsTIFF functions.
    733 
    734 1. ST_AsTIFF(rast raster, options text[], srs text) -> bytea
    735 
    736     The most generic version of this function. All other ST_AsTIFF functions call this function.
    737 
    738     This function will check that all bands of the raster to be converted has the same NODATA value. If there are more than one possible NODATA values, a WARNING will be raised and the output TIFF will use the NODATA value of the first band with a NODATA value.
    739 
    740         options: the GDAL creation options found in the Creation Options section of the GDAL TIFF driver
    741 
    742         srs: the user-specified OGC WKT or the proj4 text for a spatial reference to embed in the GDAL raster. TIFF is one of the formats that supports embedding the spatial reference within the image file.
    743 
    744 {{{
    745 ST_AsTIFF(rast, ARRAY['COMPRESS=DEFLATE', 'ZLEVEL=9'], '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    746 
    747 ST_AsTIFF(rast, ARRAY['COMPRESS=DEFLATE', 'ZLEVEL=9'], 'PROJCS["NAD83 / California Albers",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["standard_parallel_1",34],PARAMETER["standard_parallel_2",40.5],PARAMETER["latitude_of_center",0],PARAMETER["longitude_of_center",-120],PARAMETER["false_easting",0],PARAMETER["false_northing",-4000000],AUTHORITY["EPSG","3310"],AXIS["X",EAST],AXIS["Y",NORTH]]')
    748 }}}
    749 
    750 2. ST_AsTIFF(rast raster, options text[]) -> bytea
    751 
    752 This one removes the user-specified srs argument. The output TIFF's spatial reference will be set to the same as the input raster, if possible.
    753 
    754 {{{
    755 ST_AsTIFF(rast, ARRAY['COMPRESS=DEFLATE', 'ZLEVEL=9'])
    756 }}}
    757 
    758 3. ST_AsTIFF(rast raster) -> bytea
    759 
    760 The simplest implementation of this function. Since the options argument has been removed, the output TIFF will be created with default options. Like the prior function, the spatial reference of the TIFF will be set to the same as the input raster.
    761 
    762 {{{
    763 ST_AsTIFF(rast)
    764 }}}
    765 
    766 
    767 The next three functions add a band index argument to filter the raster's bands before generating the output TIFF.
    768 
    769 4. ST_AsTIFF(rast raster, nbands int[], options text[], srs text) -> bytea
    770 
    771 {{{
    772 ST_AsTIFF(rast, ARRAY[3,1,2], ARRAY['COMPRESS=DEFLATE', 'ZLEVEL=9'], '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    773 }}}
    774 
    775 5. ST_AsTIFF(rast raster, nbands int[], options text[]) -> bytea
    776 
    777 This one removes the user-specified srs argument. The output TIFF's spatial reference will be set to the same as the input raster, if possible.
    778 
    779 {{{
    780 ST_AsTIFF(rast, ARRAY[3,1,2], ARRAY['COMPRESS=DEFLATE', 'ZLEVEL=9'])
    781 }}}
    782 
    783 6. ST_AsTIFF(rast raster, nbands int[]) -> bytea
    784 
    785 Since the options argument has been removed, the output TIFF will be created with default options. Like the prior function, the spatial reference of the TIFF will be set to the same as the input raster.
    786 
    787 {{{
    788 ST_AsTIFF(rast, ARRAY[3,1,2])
    789 }}}
    790 
    791 
    792 The next two functions add a compression argument. If the compression desired is JPEG or DEFLATE, the user can specify a quality as part of the compression string.
    793 
    794 Examples are:
    795 
    796 {{{
    797 JPEG90
    798 
    799 JPEG
    800 
    801 DEFLATE8
    802 
    803 DEFLATE
    804 }}}
    805 
    806 7. ST_AsTIFF(rast raster, compression text, srs text) -> bytea
    807 
    808 This function will parse the compression string for the compression type and the compression quality. It will also inspect to make sure that the pixel types of the raster's bands are appropriate for the compression type. This is primarily for JPEG and CCITT compression types, which only support 8BUI and 1BB respectively.
    809 
    810 {{{
    811 ST_AsTIFF(rast, 'JPEG90', '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    812 
    813 ST_AsTIFF(rast, 'JPEG', '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    814 
    815 ST_AsTIFF(rast, 'LZMA', '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    816 }}}
    817 
    818 8. ST_AsTIFF(rast raster, compression text) -> bytea
    819 
    820 The output TIFF will be created with default options. Like the prior function, the spatial reference of the TIFF will be set to the same as the input raster.
    821 
    822 {{{
    823 ST_AsTIFF(rast, 'LZMA')
    824 }}}
    825 
    826 
    827 The next two functions include band index and compression arguments
    828 
    829 9. ST_AsTIFF(rast raster, nbands int[], compression text, srs text) -> bytea
    830 
    831 {{{
    832 ST_AsTIFF(rast, ARRAY[2], 'JPEG90', ARRAY['BIGTIFF=IF_NEEDED'], '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    833 
    834 ST_AsTIFF(rast, ARRAY[1,3], 'JPEG', ARRAY['BIGTIFF=IF_NEEDED'], '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    835 
    836 ST_AsTIFF(rast, ARRAY[3,1,2], 'LZMA', ARRAY['BIGTIFF=IF_NEEDED'], '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    837 }}}
    838 
    839 10. ST_AsTIFF(rast raster, nbands int[], compression text) -> bytea
    840 
    841 {{{
    842 ST_AsTIFF(rast, ARRAY[3,2], 'DEFLATE9')
    843 }}}
    844 
    845 The output TIFF will be created with default options. The spatial reference of the TIFF will be set to the same as the input raster.
    846 
    847 ----
    848 ~~'''Open Question:''' What if we want to export only the first two band of a three band layer?~~
    849 
    850 ~~Maybe we need a ST_RasterFromBands(band1, band2, etc...) to reconstitute a multiband raster from multiple sources (having the same width, height, pixelsize, etc...)~~
    851 
    852 ~~mloskot: or ST_RasterFromBands(bands) where bands is ARRAY[int]. For instance, ST_RasterFromBands(ARRAY[1,3]) will burn new raster from 1 and 3 bands of input raster.~~
    853 
    854 ----
    855 '''ST_AsPNG(raster, band) -> PNG as "bytea"'''
    856 
    857 Like the JPEG raster format, the PNG format has limitations:
    858 
    859   1. PNG only allows 1 (greyscale) or 3 (RGB) bands of data
    860 
    861   2. PNG only supports 8BUI and 16BUI pixeltypes. Any other pixeltype will be written as 8BUI, though the results are probably useless
    862 
    863   3. PNG cannot embed spatial reference information within the file but can have an associated world file
    864 
    865 Like JPEG, the limitations can be resolved:
    866 
    867   1. Use ST_Band to specify which band(s) should be passed to the ST_AsPNG function. If a raster whose number of specified bands does not equal 1 or 3 is provided, a warning is raised and the first or the first three bands are used.
    868 
    869   2. Throw an exception if any of the specified bands is not 8BUI or 16BUI. The user should use ST_Reclass to convert any non-8BUI or 16BUI bands to 8BUI or 16BUI.
    870 
    871   3. Nothing can be done within this function. ST_Georeference() can be used to the contents of the associated world file
    872 
    873 A proposed set of variations of the ST_AsPNG function:
    874 
    875 1. ST_AsPNG(rast raster, options text[])
    876 
    877     rast: the raster with one or three bands in 8BUI or 16BUI pixel type to generate a PNG image from
    878 
    879     options: array of creation options to pass to the GDAL PNG driver
    880 
    881 {{{
    882 ST_AsPNG(rast, ARRAY['ZLEVEL=9'])
    883 }}}
    884 
    885 2. ST_AsPNG(rast raster)
    886 
    887     Like !#1 above but use the driver's default creation options
    888 
    889 3. ST_AsPNG(rast raster, nbands int[], options text[])
    890 
    891     nbands: an integer array specifying the band indices of the raster to include in the PNG file
    892 
    893 {{{
    894 ST_AsPNG(rast, ARRAY[3,1,2], ARRAY['ZLEVEL=9'])
    895 }}}
    896 
    897 4. ST_AsPNG(rast raster, nbands int[])
    898 
    899     Like !#3, but use the default creation options
    900 
    901 {{{
    902 ST_AsPNG(rast, ARRAY[3])
    903 }}}
    904 
    905 5. ST_AsPNG(rast raster, nbands int[], compression int)
    906 
    907     compression: number between 1 and 9 indicating the amount of time to spend on compression. 1 is fastest with least compression. 9 is slowest with best compression
    908 
    909 {{{
    910 ST_AsPNG(rast, ARRAY[2,1,3], 3)
    911 }}}
    912 
    913 6. ST_AsPNG(rast raster, nband int, options text[])
    914 
    915     nband: index of the band to include
    916 
    917 {{{
    918 ST_AsPNG(rast, 2, ARRAY['ZLEVEL=5'])
    919 }}}
    920 
    921 7. ST_AsPNG(rast raster, nband int, compression int)
    922 
    923 {{{
    924 ST_AsPNG(rast, 1, 8)
    925 }}}
    926 
    927 8. ST_AsPNG(rast raster, nband int)
    928 
    929 {{{
    930 ST_AsPNG(rast, 1)
    931 }}}
    932 
    933 ----
    934 
    935 '''ST_AsGDALRaster(raster, band int, type text, options text) -> bytea'''
    936 
    937 Use GDAL to convert the raster into one of the format suported by GDAL.
    938 
    939 This is a generic interface to outputting a supported and installed GDAL raster:
    940 
    941 1. ST_AsGDALRaster(rast raster, format text, options text[], srs text) -> bytea
    942 
    943   This is the most generic and GDAL-specific method to convert a raster to a GDAL raster.  All other version of ST_AsGDALRaster and other format specific functions (ST_AsJPEG, ST_AsTIFF and ST_AsPNG) are all wrappers around this function.  Reference information for the format and options arguments of a particular format are specified at: http://gdal.org/formats_list.html.  The arguments specified are:
    944 
    945   format: the GDAL format code.  e.g. GTiff, JPEG, PNG
    946 
    947   options: the GDAL creation options found in the '''Creation Options''' section of a specified format.  e.g. COMPRESS=JPEG, JPEG_QUALITY=90
    948 
    949   srs: the user-specified OGC WKT or the proj4 text for a spatial reference to embed in the GDAL raster.  Not all formats support embedding this information.  e.g. the non-empty value for the srtext or proj4text column from the spatial_ref_sys table.
    950 
    951 {{{
    952 ST_AsGDALRaster(rast, 'GTiff', ARRAY['COMPRESS=JPEG', 'JPEG_QUALITY=90'], '+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
    953 
    954 ST_AsGDALRaster(rast, 'GTiff', ARRAY['COMPRESS=JPEG', 'JPEG_QUALITY=90'], 'PROJCS["NAD83 / California Albers",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["standard_parallel_1",34],PARAMETER["standard_parallel_2",40.5],PARAMETER["latitude_of_center",0],PARAMETER["longitude_of_center",-120],PARAMETER["false_easting",0],PARAMETER["false_northing",-4000000],AUTHORITY["EPSG","3310"],AXIS["X",EAST],AXIS["Y",NORTH]]')
    955 }}}
    956 
    957 
    958 2. ST_AsGDALRaster(rast raster, format text, options text[]) -> bytea
    959 
    960   This one removes the user-specified srs argument.  The output GDAL raster's spatial reference will be set to the same as the input raster, if possible.
    961 
    962 {{{
    963 ST_AsGDALRaster(rast, 'JPEG', ARRAY['QUALITY=50'])
    964 
    965 ST_AsGDALRaster(rast, 'PNG', ARRAY['ZLEVEL=7'])
    966 }}}
    967 
    968 3. ST_AsGDALRaster(rast raster, format text) -> bytea
    969 
    970   The simplest implementation of this function.  Since the options argument has been removed, the output GDAL raster will be created with default options.  Like the prior function, the spatial reference of the GDAL raster will be set to the same as the input raster.
    971 
    972 {{{
    973 ST_AsGDALRaster(rast, 'JPEG')
    974 }}}
    975 
    976 ----
    977 
    978 '''ST_GDALDrivers() -> set of record'''
    979 
    980 As each GDAL installation may be different and ST_AsGDALRaster can be used to support formats other than GTiff, JPEG and PNG, a method is needed to expose to the end user the possible GDAL formats capable of being exported.  This function will output the following columns.
    981 
    982   idx: the internal GDAL index number
    983 
    984   short_name: the GDAL format code.  This is the value to pass to the format paramenter of ST_AsGDALRaster
    985 
    986   long_name: the full name of the GDAL format
    987 
    988   create_options: the creation options available for the format as an XML string.
    989 
    990 The formats outputted from ST_getGDALDrivers have been filtered to only those that the GDAL capabilities !CreateCopy and Virtual IO support.
    991 
    992 '''Open Question:''' Should the GDAL raster process be capable of supporting the GDAL capability Create?  As the GDAL raster process writes nothing to a file in the filesystem (via Virtual IO), should there be support for writing the output GDAL raster temporarily to the filesystem?  If so, how is it done in other PostgreSQL extensions in a secure manner?
    993 
    994 ----
    995 
    996 ~~'''ST_srtext(rast raster) -> text'''
    997 
    998 ~~A helper function to get the value of column srtext or proj4text for a raster with an SRID.  By default, the srtext is returned.  If srtext is not available but proj4text is, the proj4text is returned.
    999 
    1000 ~~This function may be removed based upon the capabilities of SPI.  It may not be possible to remove this function as the srs function argument of ST_AsGDALRaster can be NULL, thereby instructing the function to not embed any spatial reference information into the output GDAL raster.
    1001 
    1002 ----
    1003 
    1004 '''ST_Reclass(rast raster, VARIADIC argset reclassarg[]) -> raster'''
    1005 
    1006 Due to limitations in the JPEG (8BUI) and PNG (8BUI and 16BUI) raster formats regarding supported pixel/data types, a method must be provided that can convert a band of a larger data type to 8BUI, amongst other uses.  ST_Reclass allows raster's band pixel values to be remapped from one range of numbers to another as well as between pixel types, e.g. 32BF to 8BUI.
    1007 
    1008 ST_Reclass returns a duplicate of the submitted raster with the bands specified to be reclassed being processed.  This means that if a raster with 5 bands are submitted and band 1 is to be reclassed, the output raster will have 5 bands with band 1 reclassified.  The other four bands will not be touched.
    1009 
    1010 1. ST_Reclass(rast raster, VARIADIC argset reclassarg[]) -> raster
    1011 
    1012   rast: the raster whose specified bands are to be reclassified
    1013 
    1014   reclassarg: a new custom type defining the parameters required for reclassifying a band's pixel values.
    1015 
    1016 {{{
    1017 CREATE TYPE reclassarg AS (
    1018 
    1019   nband int,
    1020 
    1021   reclassexpr text,
    1022 
    1023   pixeltype text,
    1024 
    1025   nodata double
    1026 
    1027 );
    1028 }}}
    1029 
    1030     nband: index of the band to reclass (1-based)
    1031 
    1032     reclassexpr: reclassification expression indicating the ranges to convert from and to.  More than one expression can be provided by separating the expression with a comma (,).  The values provided can be of any valid numeric type.
    1033 
    1034       ''rangefrom:rangeto[, rangefrom:rangeto]''
    1035 
    1036 {{{
    1037 0-100:0-10
    1038 
    1039 0-100:0-10, 101-1000:11-100
    1040 
    1041 0-100:0-10, 101-1000:11-100, 1001-10000:101-1000
    1042 }}}
    1043 
    1044       In the last example above, the default evaluation of the ranges is
    1045 
    1046 {{{
    1047         0 <= x < 100 reclassified to 0 <= y <= 10
    1048 
    1049         101 <= x < 1000 reclassified to 11 <= y <= 100
    1050 
    1051         1001 <= x < 10000 reclassified to 101 <= y <= 1000
    1052 }}}
    1053 
    1054       To change the evaluation of rangefrom, use square brackets and parentheses.
    1055 
    1056 {{{
    1057 1. [a-b] = a <= x <= b
    1058 
    1059 2. (a-b] = a < x <= b
    1060 
    1061 3. [a-b) = a <= x < b
    1062 
    1063 4. (a-b) = a < x < b
    1064 }}}
    1065 
    1066       !#3 above is the default evaluation of x in the range a-b. The use of square brackets and parentheses are optional, so the examples below would be permitted. Missing notations substitute the appropriate notation from #3 above.
    1067 
    1068 {{{
    1069 [a-b = a <= x < b
    1070 
    1071 (a-b = a < x < b
    1072 
    1073 a-b] = a <= x <= b
    1074 
    1075 a-b) = a <= x < b
    1076 }}}
    1077 
    1078       Two special cases are also available for use when x may be outside the range of a-b.  This situation is possible if your range is based upon an approximation, such as from ST_ApproxMinMax.
    1079 
    1080 {{{
    1081 ]a-b or )a-b = x < a, rule matches
    1082 
    1083 a-b[ or a-b( = x >= b, rule matches
    1084 }}}
    1085 
    1086     pixeltype: the reclassified band's pixel type, e.g. 8BUI, 16BUI, 32BF
    1087 
    1088     nodata: the nodata value of the reclassified band.  If the source band has a nodata value, all source pixel value equal to the source nodata value will be converted to the reclassified band's nodata value.  If set to NULL, the reclassified band will NOT have a nodata value specified.
    1089 
    1090 {{{
    1091 ST_Reclass(rast, ROW(1, '0-100:0-10, 101-1000:11-100, 1001-10000:101-1000', '16BUI', NULL));
    1092 
    1093 ST_Reclass(rast, ROW(1, '0-100:0-10, 101-1000:11-100, 1001-10000:101-1000', '16BUI', 1001));
    1094 
    1095 ST_Reclass(rast,
    1096   ROW(1, '0-100:0-10, 101-1000:11-100, 1001-10000:101-1000', '16BUI', 1001),
    1097   ROW(2, '0-100:0-10, 101-1000:11-100, 1001-10000:101-1000', '16BUI', 1001),
    1098   ROW(3, '0-100:0-10, 101-1000:11-100, 1001-10000:101-1000', '16BUI', 1001),
    1099   ROW(5, '0-100:0-10, 101-1000:11-100, 1001-10000:101-1000', '16BUI', 1001)
    1100 )
    1101 }}}
    1102 
    1103   An expanded example
    1104 
    1105 {{{
    1106 SELECT ST_Reclass(
    1107   ST_Band(rast, ARRAY[1,1,1]),
    1108   ROW(1, LEAST(covmin, 0)::text || '-0:0,0-' || GREATEST(covmax, 0)::text || ':0-255', '8BUI'),
    1109   ROW(2, LEAST(covmin, 0)::text || '-0:200,0-' || GREATEST(covmax, 0)::text' || ':0-255','8BUI'),
    1110   ROW(3, LEAST(covmin, 0)::text || '-0:255,0-' || (GREATEST(covmax, 0)/2)::text' || ':0,' || (GREATEST(covmax, 0)/2)::text' || ':' || GREATEST(covmax, 0)::text || ':0-255', '8BUI')
    1111 )
    1112 FROM mycoverage
    1113 }}}
    1114 
    1115 2. ST_Reclass(rast raster, nband int, reclassexpr text, pixeltype text, nodata double) -> raster
    1116 
    1117   provides a method to process just one band of a raster
    1118 
    1119 {{{
    1120 ST_Reclass(rast, 1, '0-100:0-10', '8BUI', 11)
    1121 }}}
    1122 
    1123 3. ST_Reclass(rast raster, nband int, reclassexpr text, pixeltype text) -> raster
    1124 
    1125   nodata parameter removed so reclassified band will NOT have a nodata value set
    1126 
    1127 {{{
    1128 ST_Reclass(rast, 1, '0-100:0-10', '8BUI')
    1129 }}}
    1130 
    1131 4. ST_Reclass(rast raster, reclassexpr text, pixeltype text) -> raster
    1132 
    1133   nband parameter removed so reclassified band is assumed to be 1.  nodata parameter removed so reclassified band has NO nodata value.
    1134 
    1135 {{{
    1136 ST_Reclass(rast, '0-100:0-10', '8BUI')
    1137 }}}
    1138 
    1139 5. ST_Reclass(rast raster, reclassexpr text, pixeltype text, nodata double) -> raster
    1140 
    1141   nband parameter removed so reclassified band is assumed to be 1
    1142 
    1143 {{{
    1144 ST_Reclass(rast, '0-100:0-10', '8BUI', 11)
    1145 }}}
    1146 
    1147 ----
    1148 
    1149 == '''Objective FV.02 - Being able to intersect vector and raster to produce raster.''' ==
    1150 
    1151 '''ST_Intersects(raster, raster) -> boolean'''[[BR]]
    1152 
    1153 This function uses the same tests as the two geometry version of ST_Intersects where tests see if two rasters overlap, touches or has one within the other.  If any condition is true, the two rasters intersect.
    1154 
    1155 For this description, the rasters are called A and B and the bands tested are assumed to be 1.
    1156 
    1157 Preliminary criteria before the real work begins:
    1158 
    1159 1. make sure that rasters A and B have the same SRID.  if not, return false.
    1160 
    1161 2. make sure that the convex hulls of A and B intersect.  if not, return false.
    1162 
    1163 Special case where a raster may be fully contained within another raster's cell (the entirety of A within a cell of B):
    1164 
    1165 1. using every third pixel by row and column, test each selected cell's geopoint (excluding no data by default unless exclude_nodata_value = FALSE) of A to that of B for overlap.
    1166 
    1167 2. if a cell of A does overlap a cell of B, return true.
    1168 
    1169 3. if no cell of A overlaps with B, continue to additional testing
    1170 
    1171 Actual testing involves the use of calculating the intersection of grid lines between A and B
    1172 
    1173 1. Using every third A's column and B's row, calculate the intersection point of each pair of grid lines.
    1174 
    1175 2. If intersection point doesn't exist or is outside the bounds of the end points comprising each grid line used, go back to step 1.
    1176 
    1177 3. If intersection point exists and within bounds, sample the area around the intersection point by offset the intersection point by a small amount (1/10th of the smaller scale of A and B) for 360 degrees starting from 00:00.  sampled points are 0, 45, 90, 135, 180, 225, 270 and 315 degrees.
    1178 
    1179 4. At each sample point, test to see if the geopoint has non-nodata (unless including nodata) values in both A and B.  if so, return true.
    1180 
    1181 5. At the same time as step 4, build an adjacency matrix for the intersection to see if two non-overlapping pixels from A and B touch.
    1182 
    1183 6. Once all sample points have been tested and no overlapping pixels found, the adjacency matrix is checked to see if any sampled pixel of A touched a sampled pixel of B.  If two pixels touched, return true.
    1184 
    1185 7. If after all the searching and testing, nothing intersects or touches, return false.
    1186 
    1187 A set of ST_Intersects functions for rasters:
    1188 
    1189 1. ST_Intersects(raster rastA, raster rastB, integer bandA DEFAULT NULL, integer bandB DEFAULT NULL) -> boolean
    1190 
    1191 If bandA and bandB are NULL, only the convex hulls of the rasters will be tested.  If bandA or bandB are provided, both parameters must be provided and not NULL.
    1192 
    1193 2. ST_Intersects(raster rastA, integer bandA, raster rastB, integer bandB) -> boolean
    1194 
    1195 A refactored set of St_Intersects() for testing a raster and a geometry.  The first set converts the geometry to a raster to see if the two rasters intersect.
    1196 
    1197 3. ST_Intersects(rast raster, geom geometry, nband integer DEFAULT NULL) -> boolean
    1198 
    1199 4. ST_Intersects(rast raster, nband integer, geom geometry) -> boolean
    1200 
    1201 The second set of ST_Intersects() for testing a raster and a geometry converts the raster to a geometry and sees if the geometries intersect.
    1202 
    1203 5. ST_Intersects(geom geometry, rast raster, nband DEFAULT NULL) -> boolean
    1204 
    1205 The order in which the geometry and raster are passed into ST_Intersects determines which method of testing is used.  If geometry is first, the raster is converted to a set of geometries.  If raster is first, the geometry is converted to a raster.
    1206 
    1207 These set of functions are required because there are cases where ST_Intersects(raster, geometry) != ST_Intersects(geometry, raster).
    1208 
    1209 [[Image(st_intersects_triangle.png)]]
    1210 
    1211 In the image above, the black border denotes the boundary of a triangle polygon and the red within is the raster version of the geometry.  The polygon touches the raster in blue while the red raster does not.
    1212 
    1213 '''ST_AsRaster(geometry, pixeltype, val, nodataval, ulx, uly, width, height, pixelsizex, pixelsizey, skewx, skewy) -> raster'''
    1214 
    1215 ST_AsRaster provides the ability convert a geometry into a raster. To create the raster, the X and Y scale or the width and height of the raster must be provided.
    1216 
    1217 The output raster will be in the same coordinate system as the source geometry. The only exception is for ST_AsRaster variations with a raster input parameter.
    1218 
    1219 1. ST_AsRaster([[BR]]
    1220         geom geometry,[[BR]]
    1221         scalex double precision, scaley double precision,[[BR]]
    1222         gridx double precision DEFAULT NULL, gridy double precision DEFAULT NULL,[[BR]]
    1223         pixeltype text[] DEFAULT ARRAY!['8BUI']::text[],[[BR]]
    1224         value double precision[] DEFAULT ARRAY![1]::double precision[],[[BR]]
    1225         nodataval double precision[] DEFAULT ARRAY![0]::double precision[],[[BR]]
    1226         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,[[BR]]
    1227         touched boolean DEFAULT FALSE
    1228 ) -> raster
    1229 
    1230     geom: the geometry to convert to a raster. Can be any valid PostGIS geometry.
    1231 
    1232     scalex: the scale/pixel size in the X axis of the output raster. If scalex and scaley are zero (0), the output raster's scale will be autocomputed.
    1233 
    1234     scaley: the scale/pixel size in the Y axis of the output raster. If scalex and scaley are zero (0), the output raster's scale will be autocomputed.
    1235 
    1236     pixeltype: array of pixel types for each band. Each array element is a band.
    1237 
    1238     value: array of values to burn into the raster for the geometry. Each array element is a band.
    1239 
    1240     nodataval: array of nodata values to burn into the raster. Each array element is a band. If an array element is null, that band will not have a nodata value.
    1241 
    1242     upperleftx: the X value of the upper-left corner of the output raster
    1243 
    1244     upperlefty: the Y value of the upper-left corner of the output raster
    1245 
    1246     gridx: the X coordinate of a point on the grid to which the raster will be aligned. Value is in the raster's world coordinates.
    1247 
    1248     gridy: the Y coordinate of a point on the grid to which the raster will be aligned. Value is in the raster's world coordinates.
    1249 
    1250     skewx: the skew along the X axis of the raster. by default, the skew along the X axis is zero.
    1251 
    1252     skewy: the skew along the Y axis of the raster. by default, the skew along the X axis is zero.
    1253 
    1254     touched: from the GDAL man page for gdal_rasterize: "Enables the ALL_TOUCHED rasterization option so that all pixels touched by lines or polygons will be updated not just those one the line render path, or whose center point is within the polygon. Defaults to disabled for normal rendering rules."
    1255 
    1256 ''If the number of elements for pixeltype, value and nodataval do not match, the minimum number of elements will be used.''
    1257 
    1258 2. ST_AsRaster(
    1259         geom geometry,[[BR]]
    1260         scalex double precision, scaley double precision,[[BR]]
    1261         pixeltype text[],[[BR]]
    1262         value double precision[] DEFAULT ARRAY![1]::double precision[],[[BR]]
    1263         nodataval double precision[] DEFAULT ARRAY![0]::double precision[],[[BR]]
    1264         upperleftx double precision DEFAULT NULL, upperlefty double precision DEFAULT NULL,[[BR]]
    1265         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,[[BR]]
    1266         touched boolean DEFAULT FALSE
    1267 ) -> raster
    1268 
    1269 3. ST_AsRaster(
    1270         geom geometry,[[BR]]
    1271         width integer, height integer,[[BR]]
    1272         gridx double precision DEFAULT NULL, gridy double precision DEFAULT NULL,[[BR]]
    1273         pixeltype text[] DEFAULT ARRAY!['8BUI']::text[],[[BR]]
    1274         value double precision[] DEFAULT ARRAY![1]::double precision[],[[BR]]
    1275         nodataval double precision[] DEFAULT ARRAY![0]::double precision[],[[BR]]
    1276         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,[[BR]]
    1277         touched boolean DEFAULT FALSE
    1278 ) -> raster
    1279 
    1280 4. ST_AsRaster(
    1281         geom geometry,[[BR]]
    1282         width integer, height integer,[[BR]]
    1283         pixeltype text[],[[BR]]
    1284         value double precision[] DEFAULT ARRAY![1]::double precision[],[[BR]]
    1285         nodataval double precision[] DEFAULT ARRAY![0]::double precision[],[[BR]]
    1286         upperleftx double precision DEFAULT NULL, upperlefty double precision DEFAULT NULL,[[BR]]
    1287         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,[[BR]]
    1288         touched boolean DEFAULT FALSE
    1289 ) -> raster
    1290 
    1291 5. ST_AsRaster(
    1292         geom geometry,[[BR]]
    1293         scalex double precision, scaley double precision,[[BR]]
    1294         gridx double precision, gridy double precision,[[BR]]
    1295         pixeltype text,[[BR]]
    1296         value double precision DEFAULT 1,[[BR]]
    1297         nodataval double precision DEFAULT 0,[[BR]]
    1298         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,[[BR]]
    1299         touched boolean DEFAULT FALSE
    1300 ) -> raster
    1301 
    1302 6. ST_AsRaster(
    1303         geom geometry,[[BR]]
    1304         scalex double precision, scaley double precision,[[BR]]
    1305         pixeltype text,[[BR]]
    1306         value double precision DEFAULT 1,[[BR]]
    1307         nodataval double precision DEFAULT 0,[[BR]]
    1308         upperleftx double precision DEFAULT NULL, upperlefty double precision DEFAULT NULL,[[BR]]
    1309         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,[[BR]]
    1310         touched boolean DEFAULT FALSE
    1311 ) -> raster
    1312 
    1313 7. ST_AsRaster(
    1314         geom geometry,[[BR]]
    1315         width integer, height integer,[[BR]]
    1316         gridx double precision, gridy double precision,[[BR]]
    1317         pixeltype text,[[BR]]
    1318         value double precision DEFAULT 1,[[BR]]
    1319         nodataval double precision DEFAULT 0,[[BR]]
    1320         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,[[BR]]
    1321         touched boolean DEFAULT FALSE
    1322 ) -> raster
    1323 
    1324 8. ST_AsRaster(
    1325         geom geometry,[[BR]]
    1326         width integer, height integer,[[BR]]
    1327         pixeltype text,[[BR]]
    1328         value double precision DEFAULT 1,[[BR]]
    1329         nodataval double precision DEFAULT 0,[[BR]]
    1330         upperleftx double precision DEFAULT NULL, upperlefty double precision DEFAULT NULL,[[BR]]
    1331         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,[[BR]]
    1332         touched boolean DEFAULT FALSE
    1333 ) -> raster
    1334 
    1335 9. ST_AsRaster(
    1336         geom geometry,[[BR]]
    1337         ref raster,[[BR]]
    1338         pixeltype text[] DEFAULT ARRAY!['8BUI']::text[],[[BR]]
    1339         value double precision[] DEFAULT ARRAY![1]::double precision[],[[BR]]
    1340         nodataval double precision[] DEFAULT ARRAY![0]::double precision[],[[BR]]
    1341         touched boolean DEFAULT FALSE
    1342 ) -> raster
    1343 
    1344 10. ST_AsRaster(
    1345         geom geometry,[[BR]]
    1346         ref raster,[[BR]]
    1347         pixeltype text,[[BR]]
    1348         value double precision DEFAULT 1,[[BR]]
    1349         nodataval double precision DEFAULT 0,[[BR]]
    1350         touched boolean DEFAULT FALSE
    1351 ) -> raster
    1352 
    1353 
    1354 ~~ * Rasterize the provided geometry to a raster created using the specified parameters.~~
    1355 ~~ * Implemented as a wrapper around GDAL like ST_DumpAsPolygons() does.~~
    1356 ~~ * ST_AsRaster is necessary to implement ST_Intersection(geometry, raster, band) -> raster and an eventual ST_BurnToRaster(raster, geometry)  -> raster where the provided geometry is first rasterized to the same alignment as the other raster involved.~~
    1357 ~~ * Each geometry of a table is rasterized as one raster. To produce a global unique raster including all the geometries of a table (loosing overlaps by the way), a user must use the planned ST_Union aggregate function to merge all the rasters together optionally in a GROUP BY query grouping some rows together.~~
    1358 
    1359 ~~ * The raster is burned with the specified value converted (or truncated) with a warning to the provided pixeltype. The hasnodatavalue flag of the resulting raster must be set and the nodata value is set to the provided value (expect in variant 10 and 12).~~
    1360 
    1361 ~~ * Alignment, width, height and pixelsize are optionally computed:~~[[BR]]
    1362 ~~ -From the vector extent of the geometry,~~[[BR]]
    1363 ~~ -Imposed with parameters,~~[[BR]]
    1364 ~~ -From another provided raster.~~
    1365 
    1366 ~~ * Alignment can be optionally specified as:~~[[BR]]
    1367 ~~ -A x and a y world coordinates specifying an arbitrary pixel corner. Although it can be, it IS NOT necessarily the coordinates of the upper left corner pixel.~~[[BR]]
    1368 ~~ -A ulx and a uly world coordinates specifying the upperleft corner of the raster. This IS NECESSARILY the upper left corner of the upperleft pixel. In this case a width and a height must also be provided.~~[[BR]]
    1369 ~~ -An existing raster. The x and y are derived from the provided raster and the resulting raster has the same ulx, uly, width and height as the provided raster. A 'CROP' option  allows cropping the resulting raster to the minimal extent of the geometry keeping the x and y alignment of the provided raster.~~[[BR]]
    1370 ~~ -Default alignment is the upper left corner of the envelope of the geometry. This might result in table where all rasters are misaligned, but this is useful when reconverting to raster a set of polygons vectorized from rasters (with ST_DumpAsPolygon() or ST_Intersection()).~~
    1371 
    1372 ~~ * Pixelsize can be optionally specified as:~~[[BR]]
    1373 ~~ -One or two floating point numbers. If only one is provided, both x and y pixelsizes are assigned the same value.~~[[BR]]
    1374 ~~ -A width and a height (integers). In this case the x pixelsize is the x extent divided by the provided width and the y pixelsize is the y extent divided by the provided height. This is useful only when the alignment is specified as the upper left corner of the raster.~~[[BR]]
    1375 ~~ -“FIRST_SEGMENT_LENGTH”. The pixelsize is set to the length of the first line segment encountered in the geometry. This is useful when reconverting to raster polygons vectorized from rasters (with ST_DumpAsPolygon() or ST_Intersection()). In this case, all segments are of the same length which is the original raster pixel size. This is useful only when alignment is not specified. If the geometry is a point, return an error.~~[[BR]]
    1376 ~~ -Default is the smallest of the width or height of the extent of the source geometry divided by 250. If the smallest of the width or height of the extent of the source geometry is zero then a warning is reported an no NULL is returned.~~
    1377 
    1378 ~~ * It would be interesting to have a 'ADD_WEIGHTING_INFO' option to create a second band with the length of the line or the area of polygon (or the distance to the center of points) intersecting each pixel. This band could then be used in a ST_Union(rast, 'MAX_LENGTH') or a ST_Union(rast, 'MAX_AREA') function burning the value of the line having the longest intersection with the pixel or the value of the polygon having the biggest intersecting area. For this to be useful ST_Union should work with a ST_MapAlgebra(rast1, rast2) able to refer to pixel value in any band (e.g. rast1.2 referring to the pixel value in the second band). The ST_Union(rast, 'MAX_LENGTH') aggregate could then be implemented with the following state expression: 'CASE WHEN rast1.2 > rast2.2 THEN rast1.1 ELSE rast2.1 ENDIF'. Otherwise we would have to imbricate four Mapalgebra functions. ST_Union(rast, 'MAX_COMBINED_LENGTH') could use a ST_MapAlgebra(rast1, rast2) able to take a custom user function. The state function would accumulate, in a temporary table, the total intersecting length or area for a same value over every overlapping pixels and the final function would determine which of the values from the temporary table would be the right one to burn in the final raster pixel. Variations of ST_Union which could then be easily implemented could be: ST_Union(rast, 'WEIGHTED') to weight the value with the length, the area or the count, ST_Union(rast, 'MOST_FREQUENT') to burn the value for the most frequent points values.~~
    1379 
    1380 ~~ '''Variants'''~~
    1381 
    1382 ~~ * There are many variants because we are creating a raster from scratch. We want to make it easy (serie one and four) to convert geometries to raster and at the same time we want to have flexibility to control the resulting raster properties (series two and three).~~
    1383 ~~ * Variant 3, 5 and 13 are the most useful.~~
    1384 ~~ * Variant 6, 7, 8, 9, 10, 11, 12 are also useful.~~
    1385 ~~ * Variant 1, 2, 4 are useful for quick conversion.~~
    1386 ~~ * All variant should be pl/pgsql variant of variant 9 which is the only one needing to be implemented as a rt_pg functions.~~
    1387 
    1388 ~~ * The '''first series of variant''' get their alignment from the extent of the geometry.~~
    1389 
    1390 ~~ 1) ST_AsRaster(geometry, pixeltype, val, nodataval) – 0, alignment is computed from the geometry envelope, pixel size is computed by dividing the extent by 250. This variant is optional and discouraged as it results in many unaligned rasters for a table with many geometries.~~
    1391 
    1392 ~~ 2) ST_AsRaster(geometry, pixeltype, val, nodataval, pixelsize float8) – 1, x and y are computed from upper left corner it the geometry envelope. This variant is optional and discouraged as it results in many unaligned rasters for a table with many geometries.~~
    1393 
    1394 ~~ 3) ST_AsRaster(geometry, pixeltype, val, nodataval, ‘FIRST_SEGMENT_LENGTH’) – 0, ulx and uly are get from the envelope, pixel size from the length of the first segment. This variant is useful only to rasterize geometries produced by ST_DumpAsPolygon or ST_Intersection. Otherwise it discouraged as it would result in many unaligned rasters for a table with many geometries.~~
    1395 
    1396 ~~ * The '''second series of variant''' have their alignment specified by an arbitrary pixel corner of the desired raster.~~
    1397 
    1398 ~~ 4) ST_AsRaster(geometry, pixeltype, val, nodataval, x float8, y float8) – 2, pixel size is computed by dividing the extent by 250. This variant is optional and discouraged as it results in many unaligned rasters for a table with many geometries.~~
    1399 
    1400 ~~ 5) ST_AsRaster(geometry, pixeltype, val, nodataval, x float8, y float8, pixelsize) – 3, this is one of the preferred variant when the geometry is not the result of ST_DumpAsPolygon or ST_Intersection.~~
    1401 
    1402 ~~ 6) ST_AsRaster(geometry, pixeltype, val, nodataval, x float8, y float8, pixelsizex, pixelsizey) - 4, this is one of the preferred variant when the geometry is not the result of ST_DumpAsPolygon or ST_Intersection.~~
    1403  
    1404 ~~ 7) ST_AsRaster(geometry, pixeltype, val, nodataval, x float8, y float8, pixelsizex, pixelsizey, skewx, skewy) – 6, this is one of the preferred variant when the geometry is not the result of ST_DumpAsPolygon or ST_Intersection.~~
    1405 
    1406 ~~ * The '''third series of variant''' have their alignment specified by the upper left corner of the upper left pixel of the desired raster.~~
    1407 
    1408 ~~ 8) ST_AsRaster(geometry, pixeltype, val, nodataval, ulx, uly, width, height, pixelsize) – 2,2,1, this is useful to rasterize geometries to a common raster extent. Some geometries might be outside the extent of the desired raster extent resulting in a nodata value raster.~~
    1409 
    1410 ~~ 9) ST_AsRaster(geometry, pixeltype, val, nodataval, ulx, uly, width, height, pixelsizex, pixelsizey, skewx, skewy) – 2,2,4, this is useful to rasterize geometries to a common raster extent. Some geometries might be outside the extent of the desired raster extent resulting in a nodata value raster. This is the only variant which must be implemented in the tr_pg layer. All other variant must be implemented in pl/PgSQL and derive the argument to call this variant.~~
    1411 
    1412 ~~ * The '''fourth series of variant''' get their alignment (and size for variant 10 and 12) from an existing raster. Variant 10 and 11 keep the same extent as the provided raster and variant 12 and 13 have their extent cropped to the extent of the geometry.~~
    1413 
    1414 ~~ 10) ST_AsRaster(geometry, val, raster) – 0, the target raster metadata (including pixeltype, hasnodatavalue and nodatavalue) are copied from the provided raster~~
    1415 
    1416 ~~ 11) ST_AsRaster(geometry, pixeltype, val, nodataval, raster) – 0, the target raster metadata are copied from the provided raster.~~
    1417 
    1418 ~~ 12) ST_AsRaster(geometry, val, raster, “CROP”) – 0, the target raster alignment and pixel size are copied from the provided raster (including pixeltype, hasnodatavalue and nodatavalue) but the extent is reduced to the geometry extent. We should not create a (too big) raster and crop at the end. The final extent should be computed BEFORE burning.~~
    1419 
    1420 ~~ 13) ST_AsRaster(geometry, pixeltype, val, nodataval, raster, “CROP”) – 0, the target raster alignment and pixel size are copied from the provided raster but the extent is reduced to the geometry extent. We should not create a (too big) raster and crop at the end. The final extent should be computed BEFORE burning.~~
    1421 
    1422 ~~ '''Questions'''~~
    1423 
    1424 ~~ -How does GDAL/ArcGIS choose the pixel size and the ul of the final raster?~~
    1425 
    1426 ~~ -How does GDAL/ArcGIS allow selecting the value column -What if it is a text column?~~
    1427 
    1428 ~~ -How does GDAL/ArcGIS allow giving a thickness to points and lines -No thickness is given. All pixels intersecting the geometry are burned following a selected method.~~
    1429 
    1430 ~~ -How does GDAL/ArcGIS select which value to assign to the pixel when two or more features intersect with the pixel?~~
    1431 
    1432 '''ST_Intersection(raster, band, geometry) -> raster'''
    1433 
    1434 ST_Intersection is plpgsql function wrapping the two-raster ST_MapAlgebra functions.  Performance should be about the same as calling ST_MapAlgebra directly except for the case where returnband is BOTH as that results in two separate ST_MapAlgebra calls.
    1435 
    1436 A set of ST_Intersection functions for raster, raster.
    1437 
    1438 {{{
    1439 1. ST_Intersection(
    1440     rast1 raster, nband1 int,
    1441     rast2 raster, nband2 int,
    1442     returnband text DEFAULT 'BOTH',
    1443     otheruserfunc regprocedure DEFAULT NULL
    1444 );
    1445 }}}
    1446 
    1447   returnband: can be FIRST, SECOND, BOTH, OTHER
    1448 
    1449     * FIRST returns the band of rast1 in the intersecting extent. returning raster will have one band.
    1450     * SECOND returns the band of rast2 in the intersecting extent. returning raster will have one band.
    1451     * BOTH returns the bands of rast1 and rast2 in the intersection extent. returning raster will have two bands.
    1452     * OTHER returns the computed band based upon rast1 and rast2 in the intersecting extent. returning raster will have one band. If OTHER, must provide a regprocedure to otherfunc
    1453 
    1454   otheruserfunc: function to call when returnband = OTHER. Function format must be identical to tworastuserfunc of 2-raster ST_MapAlgebraFct.
    1455 
    1456 {{{
    1457 2. ST_Intersection(
    1458     rast1 raster, nband1 int,
    1459     rast2 raster, nband2 int,
    1460     otheruserfunc regprocedure
    1461 );
    1462 }}}
    1463 
    1464 {{{
    1465 3. ST_Intersection(
    1466     rast1 raster,
    1467     rast2 raster,
    1468     returnband text DEFAULT 'BOTH',
    1469     otheruserfunc regprocedure DEFAULT NULL
    1470 );
    1471 }}}
    1472 
    1473 {{{
    1474 4. ST_Intersection(
    1475     rast1 raster,
    1476     rast2 raster,
    1477     otheruserfunc regprocedure
    1478 );
    1479 }}}
    1480 
    1481 A set of ST_Intersection functions for raster, geometry (converted to raster).
    1482 
    1483 {{{
    1484 1. ST_Intersection(
    1485     rast raster, nband int,
    1486     geom geometry,
    1487     extent text DEFAULT 'INTERSECTION',
    1488     otheruserfunc regprocedure DEFAULT NULL
    1489 )
    1490 }}}
    1491 
    1492   extent: can be INTERSECTION, FIRST, SECOND, UNION though FIRST and INTERSECTION will probably be the most commonly used
    1493 
    1494 {{{
    1495 2. ST_Intersection(
    1496     rast raster, nband int,
    1497     geom geometry,
    1498     otheruserfunc regprocedure
    1499 );
    1500 }}}
    1501 
    1502 {{{
    1503 3. ST_Intersection(
    1504     rast raster,
    1505     geom geometry,
    1506     extent text DEFAULT 'INTERSECTON',
    1507     otheruserfunc regprocedure DEFAULT NULL
    1508 );
    1509 }}}
    1510 
    1511 {{{
    1512 4. ST_Intersection(
    1513     rast raster,
    1514     geom geometry,
    1515     otheruserfunc regprocedure
    1516 );
    1517 }}}
    1518 
    1519 ~~ The first series of variant return a raster having the same extent as the provided raster.
    1520 
    1521 ~~  Variant 1: ST_Intersection(geometry, val, raster, band, pixeltype, nodatavalue) -> raster
    1522 
    1523 ~~  Variant 2: ST_Intersection(raster, band, geometry, val, pixeltype, nodatavalue) -> raster
    1524 
    1525 ~~  Variant 3: ST_Intersection(geometry, val, raster, pixeltype, nodatavalue) -> raster
    1526 
    1527 ~~  Variant 4: ST_Intersection(raster, geometry, val, pixeltype, nodatavalue) -> raster
    1528 
    1529 ~~ The second series of variant return a raster having the minimal extent.
    1530 
    1531 ~~  Variant 5: ST_Intersection(geometry, val, raster, band, pixeltype, nodatavalue, 'TRIM') -> raster
    1532 
    1533 ~~  Variant 6: ST_Intersection(raster, band, geometry, val, pixeltype, nodatavalue, 'TRIM') -> raster
    1534 
    1535 ~~  Variant 7: ST_Intersection(geometry, val, raster, pixeltype, nodatavalue, 'TRIM') -> raster
    1536 
    1537 ~~  Variant 8: ST_Intersection(raster, geometry, val, pixeltype, nodatavalue, 'TRIM') -> raster
    1538 
    1539 ~~ Returns a two bands raster the first band containing only the pixels from the provided raster intersecting with the geometry and the second band containing the same area filled with the provided value.
    1540 
    1541 ~~ The second band gets its pixeltype and nodatavalue from the parameters.
    1542 
    1543 ~~ Non intersecting pixels are filled with nodata values.
    1544 
    1545 ~~ Variant 1 return a raster having the same extent as the provided raster.
    1546 
    1547 ~~ Variant 3, 4, 7 and 8 defaults the band number to 1.
    1548 
    1549 ~~ Variant 5 to 8 "trim" or "crop" the raster to the withvalue extent (removing extra nodata value pixels surrounding the extent of the resulting withvalue extent).
    1550 
    1551  
    1552 ~~ '''Open questions'''
    1553 
    1554 ~~ PR: Shoud we return one raster per raster/geometry couple or split the raster into as many small rasters as there are areas sharing a same value? The second behavior seems more coherent with the present behavior of ST_Intersection(raster, geometry) -> geometry even if this would produce tons of small two bands rasters.
    1555 
    1556 ~~ '''Implementation details'''
    1557 
    1558 ~~ Rasterize the geometry as a new raster (ST_AsRaster(geometry, pixeltype, val, nodataval, raster)) and then copy only pixels for which both raster bands have a value.
    1559 ~~ Should be implemented as a wrapper around ST_MapAlgebra after rasterizing the geometry to a raster having the same alignment as the raster.
    1560 
    1561 ----
    1562 
    1563 == '''Objective FV.03 - ST_MapAlgebra (1 and 2 raster variants)''' ==
    1564 
    1565 '''Two raster ST_MapAlgebra'''
    1566 
    1567 These set of functions take two input rasters and create a 1-band output raster with the extent defined by extenttype.
    1568 
    1569 
    1570 1. ST_MapAlgebraExpr(
    1571         rast1 raster, band1 integer,[[BR]]
    1572         rast2 raster, band2 integer,[[BR]]
    1573         expression text,[[BR]]
    1574         pixeltype text DEFAULT NULL, extenttype text DEFAULT 'INTERSECTION',[[BR]]
    1575         nodata1expr text DEFAULT NULL, nodata2expr text DEFAULT NULL,[[BR]]
    1576         nodatanodataval double precision DEFAULT NULL
    1577 ) -> raster
    1578 
    1579     rast1: the FIRST raster upon which a map algebra operation is to be done
    1580 
    1581     band1: the band index (1-based) of the FIRST raster upon whose pixels a map algebra operation is to be done
    1582 
    1583     rast2: the SECOND raster upon which a map algebra operation is to be done
    1584 
    1585     band2: the band index (1-based) of the SECOND raster upon whose pixels a map algebra operation is to be done
    1586 
    1587     expression: valid SQL expression resulting in a double precision value or NULL.  called when band 1 pixel AND band 2 pixel are NOT NODATA
    1588 
    1589     pixeltype: the datatype of the output raster's one band
    1590 
    1591     extenttype: one of the following (INTERSECTION, UNION, FIRST, SECOND)
    1592 
    1593     nodata1expr: valid SQL expression resulting in a double precision value or NULL.  called when band 1 pixel IS NODATA and band 2 pixel IS NOT NODATA
    1594 
    1595     nodata2expr: valid SQL expression resulting in a double precision value or NULL.  called when band 1 pixel IS NOT NODATA and band 2 pixel IS NODATA
    1596 
    1597     nodatanodataval: double precision value used when band1 pixel and band 2 pixel are NODATA
    1598 
    1599 {{{
    1600 ST_MapAlgebraExpr(r1.rast, 1, r2.rast, 2, 'rast1', '32BF', 'INTERSECTION')
    1601 
    1602 ST_MapAlgebraExpr(r1.rast, 1, r2.rast, 2, '((rast1 + rast2)/2.)::numeric', '32BF', 'UNION', 'rast2', 'rast1', NULL)
    1603 
    1604 ST_MapAlgebraExpr(r1.rast, 1, r2.rast, 2, 'CASE WHEN rast2 IS NOT NULL THEN NULL ELSE rast1 END', '32BF', 'FIRST', NULL, 'rast1', NULL)
    1605 
    1606 ST_MapAlgebraExpr(r1.rast, 1, r2.rast, 2, 'CASE WHEN rast1 IS NOT NULL THEN NULL ELSE rast2 END', '32BF', 'SECOND', 'rast2', NULL, NULL)
    1607 }}}
    1608 
    1609 2. ST_MapAlgebraExpr(
    1610         rast1 raster,[[BR]]
    1611         rast2 raster,[[BR]]
    1612         expression text,[[BR]]
    1613         pixeltype text DEFAULT NULL, extenttype text DEFAULT 'INTERSECTION',[[BR]]
    1614         nodata1expr text DEFAULT NULL, nodata2expr text DEFAULT NULL,[[BR]]
    1615         nodatanodataval double precision DEFAULT NULL
    1616 ) -> raster
    1617 
    1618 {{{
    1619 ST_MapAlgebraExpr(r1.rast, r2.rast, 'rast1', '32BF', 'INTERSECTION')
    1620 
    1621 ST_MapAlgebraExpr(r1.rast, r2.rast, '((rast1 + rast2)/2.)::numeric', '32BF', 'UNION', 'rast2', 'rast1', NULL)
    1622 
    1623 ST_MapAlgebraExpr(r1.rast, r2.rast, 'CASE WHEN rast2 IS NOT NULL THEN NULL ELSE rast1 END', '32BF', 'FIRST', NULL, 'rast1', NULL)
    1624 
    1625 ST_MapAlgebraExpr(r1.rast, r2.rast, 'CASE WHEN rast1 IS NOT NULL THEN NULL ELSE rast2 END', '32BF', 'SECOND', 'rast2', NULL, NULL)
    1626 }}}
    1627 
    1628 This set of 2-raster ST_MapAlgebra functions require a user-provided function instead of expressions.  The user-provided function must receive three parameters (two double precision and one VARIADIC text array) and returns one double precision value.  A template for this user-provided function would be:
    1629 
    1630 {{{
    1631 CREATE OR REPLACE FUNCTION userfunction(rast1 double precision, rast2 double precision, VARIADIC arg text[])
    1632         RETURNS double precision
    1633         AS $$
    1634         DECLARE
    1635         BEGIN
    1636                 -- your code here
    1637         END;
    1638         $$
    1639         LANGUAGE 'plpgsql';
    1640 }}}
    1641 
    1642 The function should be able to support a NULL input parameter.  The function may also be STRICT.
    1643 
    1644 1. ST_MapAlgebraFct(
    1645         rast1 raster, band1 integer,[[BR]]
    1646         rast2 raster, band2 integer,[[BR]]
    1647         userfunction regprocedure,[[BR]]
    1648         pixeltype text DEFAULT NULL, extenttype text DEFAULT 'INTERSECTION',[[BR]]
    1649         VARIADIC userargs text[] DEFAULT NULL
    1650 )
    1651 
    1652 {{{
    1653 ST_MapAlgebraFct(r1.rast, 1, r2.rast, 1, 'raster_mapalgebra_intersection(double precision, double precision, text[])'::regprocedure, '32BF', 'INTERSECTION')
    1654 }}}
    1655 
    1656 2. ST_MapAlgebraFct(
    1657         rast1 raster,[[BR]]
    1658         rast2 raster,[[BR]]
    1659         userfunction regprocedure,[[BR]]
    1660         pixeltype text DEFAULT NULL, extenttype text DEFAULT 'INTERSECTION',[[BR]]
    1661         VARIADIC userargs text[] DEFAULT NULL
    1662 )
    1663 
    1664 {{{
    1665 ST_MapAlgebraFct(r1.rast, r2.rast, 'raster_mapalgebra_intersection(double precision, double precision, text[])'::regprocedure, '32BF', 'INTERSECTION')
    1666 }}}
    1667 
    1668 ----
    1669 == '''Objective FV.14 - Being able to intersect two rasters to get a raster.''' ==
    1670  
    1671 '''ST_Intersection(raster, integer, raster, integer) -> raster''' - Returns a two bands raster with values only in the intersecting areas of both rasters. Integer parameters are the band number of the raster.
    1672 
    1673 Please look at ST_Intersection(raster, geometry) in FV.01 for complete specs.
    1674 
    1675 ~~ '''Variants'''
    1676 
    1677 ~~  1) ST_Intersection(raster, integer, raster, integer) -> raster -- the integer parameters are the band number of the rasters[[BR]]
    1678 ~~  2) ST_Intersection(raster, raster, integer) -> raster -- default first raster to band # 1[[BR]]
    1679 ~~  3) ST_Intersection(raster, integer, raster) -> raster -- default second raster to band # 1[[BR]]
    1680 ~~  4) ST_Intersection(raster, raster) -> raster -- default both rasters to band # 1
    1681 
    1682 ----
    1683 == '''Objective FV.16 - Being able to quickly get raster statistics. - ''Done''''' ==
    1684 
    1685 '''Add cached basic raster statistic to the base raster WKB format.
    1686 
    1687 Statistics to be cached should include:
    1688 
    1689   min/max[[BR]]
    1690   mean[[BR]]
    1691   standard deviation[[BR]]
    1692   histogram[[BR]]
    1693   build parameters of stats (sample rate, method used to determine # of bins in histogram?)[[BR]]
    1694 
    1695 How are the statistics to be kept fresh?  Automatically using some method to see how much of the raster has changed since the last stats calculation?  Or let the user decide?
    1696 
    1697 ----
    1698 
    1699 '''ST_SummaryStats(raster, nband) -> record'''[[BR]]
    1700 This is the core function that gets the summary statistics (# of values, mean, standard deviation, minimum value, maximum value) of a specified raster's band.  It is this function that ST_Mean, ST_StdDev and ST_MinMax calls for their appropriate values.
    1701 
    1702 1. ST_SummaryStats(rast raster, nband int, exclude_nodata_value boolean) -> record
    1703 
    1704   returns one record of five columns (count, mean, stddev, min, max)
    1705 
    1706   nband: index of band
    1707 
    1708   exclude_nodata_value: if FALSE, nodata values in band are included in the stats. if TRUE, nodata values are not included
    1709 
    1710 {{{
    1711 ST_SummaryStats(rast, 1, FALSE)
    1712 }}}
    1713 
    1714 2. ST_SummaryStats(rast raster, nband int) -> record
    1715 
    1716   assumes exclude_nodata_value = TRUE
    1717 
    1718 {{{
    1719 ST_SummaryStats(rast, 2)
    1720 }}}
    1721 
    1722 3. ST_SummaryStats(rast raster, exclude_nodata_value boolean) -> record
    1723 
    1724   assumes nband = 1
    1725 
    1726 {{{
    1727 ST_SummaryStats(rast, TRUE)
    1728 }}}
    1729 
    1730 4. ST_SummaryStats(rast raster) -> record
    1731 
    1732   assumes nband = 1 and exclude_nodata_value = TRUE
    1733 
    1734 {{{
    1735 ST_SummaryStats(rast)
    1736 }}}
    1737 
    1738 Due to the time it may take to do on-the-fly calculation of summary stats for large rasters (say 10000 x 10000), an alternative that sacrifices accuracy for speed is required.  The following functions sample a percentage of the raster in a methodical randomized manner.  The algorithm used for sampling is...
    1739 
    1740 1. select the larger dimension of the width and height.  compute the number of pixels to sample in each "row" of the larger dimension
    1741 
    1742 2. pick pixels from each "row" of the larger dimension in an incremental rolling manner where each increment is randomly determined.
    1743 
    1744 The set of ST_ApproxSummaryStats functions are:
    1745 
    1746 1. ST_ApproxSummaryStats(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision) -> record
    1747 
    1748   sample_percent: a value between 0 and 1 indicating the percentage of the raster band's pixels to consider
    1749 
    1750 {{{
    1751 ST_ApproxSummaryStats(rast, 3, FALSE, 0.1)
    1752 
    1753 ST_ApproxSummaryStats(rast, 1, TRUE, 0.5)
    1754 }}}
    1755 
    1756 2. ST_ApproxSummaryStats(rast raster, nband int, sample_percent double precision) -> record
    1757 
    1758   assumes that nband = 1
    1759 
    1760 {{{
    1761 ST_ApproxSummaryStats(rast, 2 0.01)
    1762 
    1763 ST_ApproxSummaryStats(rast, 4, 0.025)
    1764 }}}
    1765 
    1766 3. ST_ApproxSummaryStats(rast raster, exclude_nodata_value boolean, sample_percent double precision) -> record
    1767 
    1768   assumes that nband = 1
    1769 
    1770 {{{
    1771 ST_ApproxSummaryStats(rast, FALSE, 0.01)
    1772 
    1773 ST_ApproxSummaryStats(rast, TRUE, 0.025)
    1774 }}}
    1775 
    1776 4. ST_ApproxSummaryStats(rast raster, sample_percent double precision) -> record
    1777 
    1778   assumes that nband = 1 and exclude_nodata_value = TRUE
    1779 
    1780 {{{
    1781 ST_ApproxSummaryStats(rast, 0.25)
    1782 }}}
    1783 
    1784 5. ST_ApproxSummaryStats(rast raster) -> record
    1785 
    1786   assumes that nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    1787 
    1788 {{{
    1789 ST_ApproxSummaryStats(rast)
    1790 }}}
    1791 
    1792 The situation arises where the summary statistics of a coverage table is required.  As the coverage may be large (tens of gigabytes of memory or larger), the following functions are provided to permit an incremental computation of the summary statistics.
    1793 
    1794 1. ST_SummaryStats(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean) -> record
    1795 
    1796   rastertable: name of table with raster column
    1797 
    1798   rastercolumn: name of column of data type raster
    1799 
    1800 {{{
    1801 ST_SummaryStats('tmax_2010', 'rast', 1, FALSE)
    1802 
    1803 ST_SummaryStats('precip_2011', 'rast', 1, TRUE)
    1804 }}}
    1805 
    1806 2. ST_SummaryStats(rastertable text, rastercolumn text, nband int) -> record
    1807 
    1808     exclude_nodata_value = TRUE
    1809 
    1810 {{{
    1811 ST_SummaryStats('tmax_2010', 'rast', 1)
    1812 }}}
    1813 
    1814 3. ST_SummaryStats(rastertable text, rastercolumn text, exclude_nodata_value boolean) -> record
    1815 
    1816     nband = 1
    1817 
    1818 {{{
    1819 ST_SummaryStats('precip_2011', 'rast', TRUE)
    1820 }}}
    1821 
    1822 4. ST_SummaryStats(rastertable text, rastercolumn text) -> record
    1823 
    1824     nband = 1 and exclude_nodata_value = TRUE
    1825 
    1826 {{{
    1827 ST_SummaryStats('tmin_2009', 'rast')
    1828 }}}
    1829 
    1830 Variations for ST_ApproxSummaryStats are:
    1831 
    1832 1. ST_ApproxSummaryStats(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean, sample_percent double precision) -> record
    1833 
    1834 {{{
    1835 ST_ApproxSummaryStats('tmax_2010', 'rast', 1, FALSE, 0.5)
    1836 
    1837 ST_ApproxSummaryStats('precip_2011', 'rast', 1, TRUE, 0.2)
    1838 }}}
    1839 
    1840 2. ST_ApproxSummaryStats(rastertable text, rastercolumn text, nband int, sample_percent double precision) -> record
    1841 
    1842     exclude_nodata_value = TRUE
    1843 
    1844 {{{
    1845 ST_ApproxSummaryStats('tmax_2010', 'rast', 1, 0.5)
    1846 
    1847 ST_ApproxSummaryStats('precip_2011', 'rast', 1, 0.2)
    1848 }}}
    1849 
    1850 3. ST_ApproxSummaryStats(rastertable text, rastercolumn text, exclude_nodata_value boolean, sample_percent double precision) -> record
    1851 
    1852     nband = 1
    1853 
    1854 {{{
    1855 ST_ApproxSummaryStats('tmax_2010', 'rast', FALSE, 0.5)
    1856 
    1857 ST_ApproxSummaryStats('precip_2011', 'rast', TRUE, 0.2)
    1858 }}}
    1859 
    1860 4. ST_ApproxSummaryStats(rastertable text, rastercolumn text, sample_percent double precision) -> record
    1861 
    1862     nband = 1 and exclude_nodata_value = TRUE
    1863 
    1864 {{{
    1865 ST_ApproxSummaryStats('tmax_2010', 'rast', 0.5)
    1866 
    1867 ST_ApproxSummaryStats('precip_2011', 'rast', 0.2)
    1868 }}}
    1869 
    1870 5. ST_ApproxSummaryStats(rastertable text, rastercolumn text) -> record
    1871 
    1872     nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    1873 
    1874 {{{
    1875 ST_ApproxSummaryStats('tmax_2010', 'rast')
    1876 
    1877 ST_ApproxSummaryStats('precip_2011', 'rast')
    1878 }}}
    1879 
    1880 The mean returned in the coverage functions (has rastertable and rastercolumn arguments) is the true mean of the raster tiles. The standard deviation returned is the standard deviation of all raster tiles.
    1881 
    1882 ----
    1883 
    1884 '''ST_Count(raster, nband) -> bigint'''[[BR]]
    1885 
    1886 This function calls ST_SummaryStats and only returns the count from that function.
    1887 
    1888 1. ST_Count(rast raster, nband int, exclude_nodata_value boolean) -> bigint
    1889 
    1890     returns the count as an integer
    1891 
    1892     nband: index of band
    1893 
    1894     exclude_nodata_value: if FALSE, nodata values in band are included in the stats. if TRUE, nodata values are not included
    1895 
    1896 {{{
    1897 ST_Count(rast, 1, FALSE)
    1898 }}}
    1899 
    1900 2. ST_Count(rast raster, nband int) -> bigint
    1901 
    1902     assumes exclude_nodata_value = TRUE
    1903 
    1904 {{{
    1905 ST_Count(rast, 2)
    1906 }}}
    1907 
    1908 3. ST_Count(rast raster, exclude_nodata_value boolean) -> bigint
    1909 
    1910     assumes nband = 1
    1911 
    1912 {{{
    1913 ST_Count(rast, TRUE)
    1914 }}}
    1915 
    1916 4. ST_Count(rast raster) -> bigint
    1917 
    1918     assumes nband = 1 and exclude_nodata_value = TRUE
    1919 
    1920 {{{
    1921 ST_Count(rast)
    1922 }}}
    1923 
    1924 The set of ST_ApproxCount functions are:
    1925 
    1926 1. ST_ApproxCount(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision) -> bigint
    1927 
    1928     sample_percent: a value between 0 and 1 indicating the percentage of the raster band's pixels to consider
    1929 
    1930 {{{
    1931 ST_ApproxCount(rast, 3, FALSE, 0.1)
    1932 
    1933 ST_ApproxCount(rast, 1, TRUE, 0.5)
    1934 }}}
    1935 
    1936 2. ST_ApproxCount(rast raster, nband int, sample_percent double precision) -> bigint
    1937 
    1938     assumes that nband = 1
    1939 
    1940 {{{
    1941 ST_ApproxCount(rast, 2 0.01)
    1942 
    1943 ST_ApproxCount(rast, 4, 0.025)
    1944 }}}
    1945 
    1946 3. ST_ApproxCount(rast raster, exclude_nodata_value boolean, sample_percent double precision) -> bigint
    1947 
    1948     assumes that nband = 1
    1949 
    1950 {{{
    1951 ST_ApproxCount(rast, FALSE, 0.01)
    1952 
    1953 ST_ApproxCount(rast, TRUE, 0.025)
    1954 }}}
    1955 
    1956 4. ST_ApproxCount(rast raster, sample_percent double precision) -> bigint
    1957 
    1958     assumes that nband = 1 and exclude_nodata_value = TRUE
    1959 
    1960 {{{
    1961 ST_ApproxCount(rast, 0.25)
    1962 }}}
    1963 
    1964 5. ST_ApproxCount(rast raster) -> bigint
    1965 
    1966     assumes that nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    1967 
    1968 {{{
    1969 ST_ApproxCount(rast)
    1970 }}}
    1971 
    1972 The following functions are provided for coverage tables.
    1973 
    1974 1. ST_Count(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean) -> bigint
    1975 
    1976     rastertable: name of table with raster column
    1977 
    1978     rastercolumn: name of column of data type raster
    1979 
    1980 {{{
    1981 ST_Count('tmax_2010', 'rast', 1, FALSE)
    1982 
    1983 ST_Count('precip_2011', 'rast', 1, TRUE)
    1984 }}}
    1985 
    1986 2. ST_Count(rastertable text, rastercolumn text, nband int) -> bigint
    1987 
    1988     exclude_nodata_value = TRUE
    1989 
    1990 {{{
    1991 ST_Count('tmax_2010', 'rast', 1)
    1992 }}}
    1993 
    1994 3. ST_Count(rastertable text, rastercolumn text, exclude_nodata_value boolean) -> bigint
    1995 
    1996     nband = 1
    1997 
    1998 {{{
    1999 ST_Count('precip_2011', 'rast', TRUE)
    2000 }}}
    2001 
    2002 4. ST_Count(rastertable text, rastercolumn text) -> bigint
    2003 
    2004     nband = 1 and exclude_nodata_value = TRUE
    2005 
    2006 {{{
    2007 ST_Count('tmin_2009', 'rast')
    2008 }}}
    2009 
    2010 Variations for ST_ApproxCount are:
    2011 
    2012 1. ST_ApproxCount(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean, sample_percent double precision) -> bigint
    2013 
    2014 {{{
    2015 ST_ApproxCount('tmax_2010', 'rast', 1, FALSE, 0.5)
    2016 
    2017 ST_ApproxCount('precip_2011', 'rast', 1, TRUE, 0.2)
    2018 }}}
    2019 
    2020 2. ST_ApproxCount(rastertable text, rastercolumn text, nband int, sample_percent double precision) -> bigint
    2021 
    2022     exclude_nodata_value = TRUE
    2023 
    2024 {{{
    2025 ST_ApproxCount('tmax_2010', 'rast', 1, 0.5)
    2026 
    2027 ST_ApproxCount('precip_2011', 'rast', 1, 0.2)
    2028 }}}
    2029 
    2030 3. ST_ApproxCount(rastertable text, rastercolumn text, exclude_nodata_value boolean, sample_percent double precision) -> bigint
    2031 
    2032     nband = 1
    2033 
    2034 {{{
    2035 ST_ApproxCount('tmax_2010', 'rast', FALSE, 0.5)
    2036 
    2037 ST_ApproxCount('precip_2011', 'rast', TRUE, 0.2)
    2038 }}}
    2039 
    2040 4. ST_ApproxCount(rastertable text, rastercolumn text, sample_percent double precision) -> bigint
    2041 
    2042     nband = 1 and exclude_nodata_value = TRUE
    2043 
    2044 {{{
    2045 ST_ApproxCount('tmax_2010', 'rast', 0.5)
    2046 
    2047 ST_ApproxCount('precip_2011', 'rast', 0.2)
    2048 }}}
    2049 
    2050 5. ST_ApproxCount(rastertable text, rastercolumn text) -> bigint
    2051 
    2052     nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2053 
    2054 {{{
    2055 ST_ApproxCount('tmax_2010', 'rast')
    2056 
    2057 ST_ApproxCount('precip_2011', 'rast')
    2058 }}}
    2059 
    2060 ----
    2061 
    2062 '''ST_Sum(raster, nband) -> double precision'''[[BR]]
    2063 
    2064 This function calls ST_SummaryStats and only returns the sum from that function.
    2065 
    2066 1. ST_Sum(rast raster, nband int, exclude_nodata_value boolean) -> double precision
    2067 
    2068     returns the sum as an integer
    2069 
    2070     nband: index of band
    2071 
    2072     exclude_nodata_value: if FALSE, nodata values in band are included in the stats. if TRUE, nodata values are not included
    2073 
    2074 {{{
    2075 ST_Sum(rast, 1, FALSE)
    2076 }}}
    2077 
    2078 2. ST_Sum(rast raster, nband int) -> double precision
    2079 
    2080     assumes exclude_nodata_value = TRUE
    2081 
    2082 {{{
    2083 ST_Sum(rast, 2)
    2084 }}}
    2085 
    2086 3. ST_Sum(rast raster, exclude_nodata_value boolean) -> double precision
    2087 
    2088     assumes nband = 1
    2089 
    2090 {{{
    2091 ST_Sum(rast, TRUE)
    2092 }}}
    2093 
    2094 4. ST_Sum(rast raster) -> double precision
    2095 
    2096     assumes nband = 1 and exclude_nodata_value = TRUE
    2097 
    2098 {{{
    2099 ST_Sum(rast)
    2100 }}}
    2101 
    2102 The set of ST_ApproxSum functions are:
    2103 
    2104 1. ST_ApproxSum(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2105 
    2106     sample_percent: a value between 0 and 1 indicating the percentage of the raster band's pixels to consider
    2107 
    2108 {{{
    2109 ST_ApproxSum(rast, 3, FALSE, 0.1)
    2110 
    2111 ST_ApproxSum(rast, 1, TRUE, 0.5)
    2112 }}}
    2113 
    2114 2. ST_ApproxSum(rast raster, nband int, sample_percent double precision) -> double precision
    2115 
    2116     assumes that nband = 1
    2117 
    2118 {{{
    2119 ST_ApproxSum(rast, 2 0.01)
    2120 
    2121 ST_ApproxSum(rast, 4, 0.025)
    2122 }}}
    2123 
    2124 3. ST_ApproxSum(rast raster, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2125 
    2126     assumes that nband = 1
    2127 
    2128 {{{
    2129 ST_ApproxSum(rast, FALSE, 0.01)
    2130 
    2131 ST_ApproxSum(rast, TRUE, 0.025)
    2132 }}}
    2133 
    2134 4. ST_ApproxSum(rast raster, sample_percent double precision) -> double precision
    2135 
    2136     assumes that nband = 1 and exclude_nodata_value = TRUE
    2137 
    2138 {{{
    2139 ST_ApproxSum(rast, 0.25)
    2140 }}}
    2141 
    2142 5. ST_ApproxSum(rast raster) -> double precision
    2143 
    2144     assumes that nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2145 
    2146 {{{
    2147 ST_ApproxSum(rast)
    2148 }}}
    2149 
    2150 The following functions are provided for coverage tables.
    2151 
    2152 1. ST_Sum(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean) -> double precision
    2153 
    2154     rastertable: name of table with raster column
    2155 
    2156     rastercolumn: name of column of data type raster
    2157 
    2158 {{{
    2159 ST_Sum('tmax_2010', 'rast', 1, FALSE)
    2160 
    2161 ST_Sum('precip_2011', 'rast', 1, TRUE)
    2162 }}}
    2163 
    2164 2. ST_Sum(rastertable text, rastercolumn text, nband int) -> double precision
    2165 
    2166     exclude_nodata_value = TRUE
    2167 
    2168 {{{
    2169 ST_Sum('tmax_2010', 'rast', 1)
    2170 }}}
    2171 
    2172 3. ST_Sum(rastertable text, rastercolumn text, exclude_nodata_value boolean) -> double precision
    2173 
    2174     nband = 1
    2175 
    2176 {{{
    2177 ST_Sum('precip_2011', 'rast', TRUE)
    2178 }}}
    2179 
    2180 4. ST_Sum(rastertable text, rastercolumn text) -> double precision
    2181 
    2182     nband = 1 and exclude_nodata_value = TRUE
    2183 
    2184 {{{
    2185 ST_Sum('tmin_2009', 'rast')
    2186 }}}
    2187 
    2188 Variations for ST_ApproxSum are:
    2189 
    2190 1. ST_ApproxSum(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2191 
    2192 {{{
    2193 ST_ApproxSum('tmax_2010', 'rast', 1, FALSE, 0.5)
    2194 
    2195 ST_ApproxSum('precip_2011', 'rast', 1, TRUE, 0.2)
    2196 }}}
    2197 
    2198 2. ST_ApproxSum(rastertable text, rastercolumn text, nband int, sample_percent double precision) -> double precision
    2199 
    2200     exclude_nodata_value = TRUE
    2201 
    2202 {{{
    2203 ST_ApproxSum('tmax_2010', 'rast', 1, 0.5)
    2204 
    2205 ST_ApproxSum('precip_2011', 'rast', 1, 0.2)
    2206 }}}
    2207 
    2208 3. ST_ApproxSum(rastertable text, rastercolumn text, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2209 
    2210     nband = 1
    2211 
    2212 {{{
    2213 ST_ApproxSum('tmax_2010', 'rast', FALSE, 0.5)
    2214 
    2215 ST_ApproxSum('precip_2011', 'rast', TRUE, 0.2)
    2216 }}}
    2217 
    2218 4. ST_ApproxSum(rastertable text, rastercolumn text, sample_percent double precision) -> double precision
    2219 
    2220     nband = 1 and exclude_nodata_value = TRUE
    2221 
    2222 {{{
    2223 ST_ApproxSum('tmax_2010', 'rast', 0.5)
    2224 
    2225 ST_ApproxSum('precip_2011', 'rast', 0.2)
    2226 }}}
    2227 
    2228 5. ST_ApproxSum(rastertable text, rastercolumn text) -> double precision
    2229 
    2230     nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2231 
    2232 {{{
    2233 ST_ApproxSum('tmax_2010', 'rast')
    2234 
    2235 ST_ApproxSum('precip_2011', 'rast')
    2236 }}}
    2237 
    2238 ----
    2239 
    2240 '''ST_Mean(raster, nband) -> double precision'''[[BR]]
    2241 This function calls ST_SummaryStats and only returns the mean from that function.
    2242 
    2243 1. ST_Mean(rast raster, nband int, exclude_nodata_value boolean) -> double precision
    2244 
    2245   returns the mean as a double precision
    2246 
    2247   nband: index of band
    2248 
    2249   exclude_nodata_value: if FALSE, nodata values in band are included. if TRUE, nodata values are not included.
    2250 
    2251 {{{
    2252 ST_Mean(rast, 1, FALSE)
    2253 }}}
    2254 
    2255 2. ST_Mean(rast raster, nband int) -> double precision
    2256 
    2257   assumes exclude_nodata_value = TRUE
    2258 
    2259 {{{
    2260 ST_Mean(rast, 2)
    2261 }}}
    2262 
    2263 3. ST_Mean(rast raster, exclude_nodata_value boolean) -> double precision
    2264 
    2265   assumes nband = 1
    2266 
    2267 {{{
    2268 ST_Mean(rast, TRUE)
    2269 }}}
    2270 
    2271 4. ST_Mean(rast raster) -> double precision
    2272 
    2273   assumes nband = 1 and exclude_nodata_value = TRUE
    2274 
    2275 {{{
    2276 ST_Mean(rast)
    2277 }}}
    2278 
    2279 The set of ST_ApproxMean functions are:
    2280 
    2281 1. ST_ApproxMean(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2282 
    2283   sample_percent: a value between 0 and 1 indicating the percentage of the raster band's pixels to consider
    2284 
    2285 {{{
    2286 ST_ApproxMean(rast, 3, FALSE, 0.1)
    2287 
    2288 ST_ApproxMean(rast, 1, TRUE, 0.5)
    2289 }}}
    2290 
    2291 2. ST_ApproxMean(rast raster, nband int, sample_percent double precision) -> double precision
    2292 
    2293   assumes that nband = 1
    2294 
    2295 {{{
    2296 ST_ApproxMean(rast, 2 0.01)
    2297 
    2298 ST_ApproxMean(rast, 4, 0.025)
    2299 }}}
    2300 
    2301 3. ST_ApproxMean(rast raster, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2302 
    2303   assumes that nband = 1
    2304 
    2305 {{{
    2306 ST_ApproxMean(rast, FALSE, 0.01)
    2307 
    2308 ST_ApproxMean(rast, TRUE, 0.025)
    2309 }}}
    2310 
    2311 4. ST_ApproxMean(rast raster, sample_percent double precision) -> double precision
    2312 
    2313   assumes that nband = 1 and exclude_nodata_value = TRUE
    2314 
    2315 {{{
    2316 ST_ApproxMean(rast, 0.25)
    2317 }}}
    2318 
    2319 5. ST_ApproxMean(rast raster) -> double precision
    2320 
    2321   assumes that nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2322 
    2323 {{{
    2324 ST_ApproxMean(rast)
    2325 }}}
    2326 
    2327 The following functions are provided for coverage tables.
    2328 
    2329 1. ST_Mean(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean) -> double precision
    2330 
    2331   rastertable: name of table with raster column
    2332 
    2333   rastercolumn: name of column of data type raster
    2334 
    2335 {{{
    2336 ST_Mean('tmax_2010', 'rast', 1, FALSE)
    2337 
    2338 ST_Mean('precip_2011', 'rast', 1, TRUE)
    2339 }}}
    2340 
    2341 2. ST_Mean(rastertable text, rastercolumn text, nband int) -> double precision
    2342 
    2343     exclude_nodata_value = TRUE
    2344 
    2345 {{{
    2346 ST_Mean('tmax_2010', 'rast', 1)
    2347 }}}
    2348 
    2349 3. ST_Mean(rastertable text, rastercolumn text, exclude_nodata_value boolean) -> double precision
    2350 
    2351     nband = 1
    2352 
    2353 {{{
    2354 ST_Mean('precip_2011', 'rast', TRUE)
    2355 }}}
    2356 
    2357 4. ST_Mean(rastertable text, rastercolumn text) -> double precision
    2358 
    2359     nband = 1 and exclude_nodata_value = TRUE
    2360 
    2361 {{{
    2362 ST_Mean('tmin_2009', 'rast')
    2363 }}}
    2364 
    2365 Variations for ST_ApproxMean are:
    2366 
    2367 1. ST_ApproxMean(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2368 
    2369 {{{
    2370 ST_ApproxMean('tmax_2010', 'rast', 1, FALSE, 0.5)
    2371 
    2372 ST_ApproxMean('precip_2011', 'rast', 1, TRUE, 0.2)
    2373 }}}
    2374 
    2375 2. ST_ApproxMean(rastertable text, rastercolumn text, nband int, sample_percent double precision) -> double precision
    2376 
    2377     exclude_nodata_value = TRUE
    2378 
    2379 {{{
    2380 ST_ApproxMean('tmax_2010', 'rast', 1, 0.5)
    2381 
    2382 ST_ApproxMean('precip_2011', 'rast', 1, 0.2)
    2383 }}}
    2384 
    2385 3. ST_ApproxMean(rastertable text, rastercolumn text, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2386 
    2387     nband = 1
    2388 
    2389 {{{
    2390 ST_ApproxMean('tmax_2010', 'rast', FALSE, 0.5)
    2391 
    2392 ST_ApproxMean('precip_2011', 'rast', TRUE, 0.2)
    2393 }}}
    2394 
    2395 4. ST_ApproxMean(rastertable text, rastercolumn text, sample_percent double precision) -> double precision
    2396 
    2397     nband = 1 and exclude_nodata_value = TRUE
    2398 
    2399 {{{
    2400 ST_ApproxMean('tmax_2010', 'rast', 0.5)
    2401 
    2402 ST_ApproxMean('precip_2011', 'rast', 0.2)
    2403 }}}
    2404 
    2405 5. ST_ApproxMean(rastertable text, rastercolumn text) -> double precision
    2406 
    2407     nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2408 
    2409 {{{
    2410 ST_ApproxMean('tmax_2010', 'rast')
    2411 
    2412 ST_ApproxMean('precip_2011', 'rast')
    2413 }}}
    2414 
    2415 The mean returned in the coverage functions (has rastertable and rastercolumn arguments) is the true mean of the raster tiles.
    2416 
    2417 ----
    2418 
    2419 '''ST_StdDev(raster, nband) -> double precision'''[[BR]]
    2420 This function calls ST_SummaryStats and only returns the standard deviation from that function.
    2421 
    2422 1. ST_StdDev(rast raster, nband int, exclude_nodata_value boolean) -> double precision
    2423 
    2424   returns the standard deviation as a double precision
    2425 
    2426   nband: index of band
    2427 
    2428   exclude_nodata_value: if FALSE, nodata values in band are included. if TRUE, nodata values are not included.
    2429 
    2430 {{{
    2431 ST_StdDev(rast, 1, FALSE)
    2432 }}}
    2433 
    2434 2. ST_StdDev(rast raster, nband int) -> double precision
    2435 
    2436   assumes exclude_nodata_value = TRUE
    2437 
    2438 {{{
    2439 ST_StdDev(rast, 2)
    2440 }}}
    2441 
    2442 3. ST_StdDev(rast raster, exclude_nodata_value boolean) -> double precision
    2443 
    2444   assumes nband = 1
    2445 
    2446 {{{
    2447 ST_StdDev(rast, TRUE)
    2448 }}}
    2449 
    2450 4. ST_StdDev(rast raster) -> double precision
    2451 
    2452   assumes nband = 1 and exclude_nodata_value = TRUE
    2453 
    2454 {{{
    2455 ST_StdDev(rast)
    2456 }}}
    2457 
    2458 The set of ST_ApproxStdDev functions are:
    2459 
    2460 1. ST_ApproxStdDev(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2461 
    2462   sample_percent: a value between 0 and 1 indicating the percentage of the raster band's pixels to consider
    2463 
    2464 {{{
    2465 ST_ApproxStdDev(rast, 3, FALSE, 0.1)
    2466 
    2467 ST_ApproxStdDev(rast, 1, TRUE, 0.5)
    2468 }}}
    2469 
    2470 2. ST_ApproxStdDev(rast raster, nband int, sample_percent double precision) -> double precision
    2471 
    2472   assumes that nband = 1
    2473 
    2474 {{{
    2475 ST_ApproxStdDev(rast, 2 0.01)
    2476 
    2477 ST_ApproxStdDev(rast, 4, 0.025)
    2478 }}}
    2479 
    2480 3. ST_ApproxStdDev(rast raster, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2481 
    2482   assumes that nband = 1
    2483 
    2484 {{{
    2485 ST_ApproxStdDev(rast, FALSE, 0.01)
    2486 
    2487 ST_ApproxStdDev(rast, TRUE, 0.025)
    2488 }}}
    2489 
    2490 4. ST_ApproxStdDev(rast raster, sample_percent double precision) -> double precision
    2491 
    2492   assumes that nband = 1 and exclude_nodata_value = TRUE
    2493 
    2494 {{{
    2495 ST_ApproxStdDev(rast, 0.25)
    2496 }}}
    2497 
    2498 5. ST_ApproxStdDev(rast raster) -> double precision
    2499 
    2500   assumes that nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2501 
    2502 {{{
    2503 ST_ApproxStdDev(rast)
    2504 }}}
    2505 
    2506 The following functions are provided for coverage tables.
    2507 
    2508 1. ST_StdDev(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean) -> double precision
    2509 
    2510   rastertable: name of table with raster column
    2511 
    2512   rastercolumn: name of column of data type raster
    2513 
    2514 {{{
    2515 ST_StdDev('tmax_2010', 'rast', 1, FALSE)
    2516 
    2517 ST_StdDev('precip_2011', 'rast', 1, TRUE)
    2518 }}}
    2519 
    2520 2. ST_StdDev(rastertable text, rastercolumn text, nband int) -> double precision
    2521 
    2522     exclude_nodata_value = TRUE
    2523 
    2524 {{{
    2525 ST_StdDev('tmax_2010', 'rast', 1)
    2526 }}}
    2527 
    2528 3. ST_StdDev(rastertable text, rastercolumn text, exclude_nodata_value boolean) -> double precision
    2529 
    2530     nband = 1
    2531 
    2532 {{{
    2533 ST_StdDev('precip_2011', 'rast', TRUE)
    2534 }}}
    2535 
    2536 4. ST_StdDev(rastertable text, rastercolumn text) -> double precision
    2537 
    2538     nband = 1 and exclude_nodata_value = TRUE
    2539 
    2540 {{{
    2541 ST_StdDev('tmin_2009', 'rast')
    2542 }}}
    2543 
    2544 Variations for ST_ApproxStdDev are:
    2545 
    2546 1. ST_ApproxStdDev(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2547 
    2548 {{{
    2549 ST_ApproxStdDev('tmax_2010', 'rast', 1, FALSE, 0.5)
    2550 
    2551 ST_ApproxStdDev('precip_2011', 'rast', 1, TRUE, 0.2)
    2552 }}}
    2553 
    2554 2. ST_ApproxStdDev(rastertable text, rastercolumn text, nband int, sample_percent double precision) -> double precision
    2555 
    2556     exclude_nodata_value = TRUE
    2557 
    2558 {{{
    2559 ST_ApproxStdDev('tmax_2010', 'rast', 1, 0.5)
    2560 
    2561 ST_ApproxStdDev('precip_2011', 'rast', 1, 0.2)
    2562 }}}
    2563 
    2564 3. ST_ApproxStdDev(rastertable text, rastercolumn text, exclude_nodata_value boolean, sample_percent double precision) -> double precision
    2565 
    2566     nband = 1
    2567 
    2568 {{{
    2569 ST_ApproxStdDev('tmax_2010', 'rast', FALSE, 0.5)
    2570 
    2571 ST_ApproxStdDev('precip_2011', 'rast', TRUE, 0.2)
    2572 }}}
    2573 
    2574 4. ST_ApproxStdDev(rastertable text, rastercolumn text, sample_percent double precision) -> double precision
    2575 
    2576     nband = 1 and exclude_nodata_value = TRUE
    2577 
    2578 {{{
    2579 ST_ApproxStdDev('tmax_2010', 'rast', 0.5)
    2580 
    2581 ST_ApproxStdDev('precip_2011', 'rast', 0.2)
    2582 }}}
    2583 
    2584 5. ST_ApproxStdDev(rastertable text, rastercolumn text) -> double precision
    2585 
    2586     nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2587 
    2588 {{{
    2589 ST_ApproxStdDev('tmax_2010', 'rast')
    2590 
    2591 ST_ApproxStdDev('precip_2011', 'rast')
    2592 }}}
    2593 
    2594 The standard deviation returned in the coverage functions (has rastertable and rastercolumn arguments) is the standard deviation of all raster tiles.
    2595 
    2596 ----
    2597 
    2598 '''ST_MinMax(raster, nband) -> record'''[[BR]]
    2599 This function calls ST_SummaryStats and only returns the min and max values from that function.
    2600 
    2601 1. ST_MinMax(rast raster, nband int, exclude_nodata_value boolean) -> record
    2602 
    2603   returns the record (min double precision, max double precision)
    2604 
    2605   nband: index of band
    2606 
    2607   exclude_nodata_value: if FALSE, nodata values in band are included. if TRUE, nodata values are not included.
    2608 
    2609 {{{
    2610 ST_MinMax(rast, 1, FALSE)
    2611 }}}
    2612 
    2613 2. ST_MinMax(rast raster, nband int) -> record
    2614 
    2615   assumes exclude_nodata_value = TRUE
    2616 
    2617 {{{
    2618 ST_MinMax(rast, 2)
    2619 }}}
    2620 
    2621 3. ST_MinMax(rast raster, exclude_nodata_value boolean) -> record
    2622 
    2623   assumes nband = 1
    2624 
    2625 {{{
    2626 ST_MinMax(rast, TRUE)
    2627 }}}
    2628 
    2629 4. ST_MinMax(rast raster) -> record
    2630 
    2631   assumes nband = 1 and exclude_nodata_value = TRUE
    2632 
    2633 {{{
    2634 ST_MinMax(rast)
    2635 }}}
    2636 
    2637 The set of ST_ApproxMinMax functions are:
    2638 
    2639 1. ST_ApproxMinMax(rast raster, nband int, exclude_nodata_value boolean, sample_percent record) -> record
    2640 
    2641   sample_percent: a value between 0 and 1 indicating the percentage of the raster band's pixels to consider
    2642 
    2643 {{{
    2644 ST_ApproxMinMax(rast, 3, FALSE, 0.1)
    2645 
    2646 ST_ApproxMinMax(rast, 1, TRUE, 0.5)
    2647 }}}
    2648 
    2649 2. ST_ApproxMinMax(rast raster, nband int, sample_percent double precision) -> record
    2650 
    2651   assumes that nband = 1
    2652 
    2653 {{{
    2654 ST_ApproxMinMax(rast, 2 0.01)
    2655 
    2656 ST_ApproxMinMax(rast, 4, 0.025)
    2657 }}}
    2658 
    2659 3. ST_ApproxMinMax(rast raster, exclude_nodata_value boolean, sample_percent double precision) -> record
    2660 
    2661   assumes that nband = 1
    2662 
    2663 {{{
    2664 ST_ApproxMinMax(rast, FALSE, 0.01)
    2665 
    2666 ST_ApproxMinMax(rast, TRUE, 0.025)
    2667 }}}
    2668 
    2669 4. ST_ApproxMinMax(rast raster, sample_percent double precision) -> record
    2670 
    2671   assumes that nband = 1 and exclude_nodata_value = TRUE
    2672 
    2673 {{{
    2674 ST_ApproxMinMax(rast, 0.25)
    2675 }}}
    2676 
    2677 5. ST_ApproxMinMax(rast raster) -> record
    2678 
    2679   assumes that nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2680 
    2681 {{{
    2682 ST_ApproxMinMax(rast)
    2683 }}}
    2684 
    2685 The following functions are provided for coverage tables.
    2686 
    2687 1. ST_MinMax(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean) -> record
    2688 
    2689   rastertable: name of table with raster column
    2690 
    2691   rastercolumn: name of column of data type raster
    2692 
    2693 {{{
    2694 ST_MinMax('tmax_2010', 'rast', 1, FALSE)
    2695 
    2696 ST_MinMax('precip_2011', 'rast', 1, TRUE)
    2697 }}}
    2698 
    2699 2. ST_MinMax(rastertable text, rastercolumn text, nband int) -> record
    2700 
    2701     exclude_nodata_value = TRUE
    2702 
    2703 {{{
    2704 ST_MinMax('tmax_2010', 'rast', 1)
    2705 }}}
    2706 
    2707 3. ST_MinMax(rastertable text, rastercolumn text, exclude_nodata_value boolean) -> record
    2708 
    2709     nband = 1
    2710 
    2711 {{{
    2712 ST_MinMax('precip_2011', 'rast', TRUE)
    2713 }}}
    2714 
    2715 4. ST_MinMax(rastertable text, rastercolumn text) -> record
    2716 
    2717     nband = 1 and exclude_nodata_value = TRUE
    2718 
    2719 {{{
    2720 ST_MinMax('tmin_2009', 'rast')
    2721 }}}
    2722 
    2723 Variations for ST_ApproxMinMax are:
    2724 
    2725 1. ST_ApproxMinMax(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean, sample_percent double precision) -> record
    2726 
    2727 {{{
    2728 ST_ApproxMinMax('tmax_2010', 'rast', 1, FALSE, 0.5)
    2729 
    2730 ST_ApproxMinMax('precip_2011', 'rast', 1, TRUE, 0.2)
    2731 }}}
    2732 
    2733 2. ST_ApproxMinMax(rastertable text, rastercolumn text, nband int, sample_percent double precision) -> record
    2734 
    2735     exclude_nodata_value = TRUE
    2736 
    2737 {{{
    2738 ST_ApproxMinMax('tmax_2010', 'rast', 1, 0.5)
    2739 
    2740 ST_ApproxMinMax('precip_2011', 'rast', 1, 0.2)
    2741 }}}
    2742 
    2743 3. ST_ApproxMinMax(rastertable text, rastercolumn text, exclude_nodata_value boolean, sample_percent double precision) -> record
    2744 
    2745     nband = 1
    2746 
    2747 {{{
    2748 ST_ApproxMinMax('tmax_2010', 'rast', FALSE, 0.5)
    2749 
    2750 ST_ApproxMinMax('precip_2011', 'rast', TRUE, 0.2)
    2751 }}}
    2752 
    2753 4. ST_ApproxMinMax(rastertable text, rastercolumn text, sample_percent double precision) -> record
    2754 
    2755     nband = 1 and exclude_nodata_value = TRUE
    2756 
    2757 {{{
    2758 ST_ApproxMinMax('tmax_2010', 'rast', 0.5)
    2759 
    2760 ST_ApproxMinMax('precip_2011', 'rast', 0.2)
    2761 }}}
    2762 
    2763 5. ST_ApproxMinMax(rastertable text, rastercolumn text) -> record
    2764 
    2765     nband = 1, exclude_nodata_value = TRUE and sample_percent = 0.1
    2766 
    2767 {{{
    2768 ST_ApproxMinMax('tmax_2010', 'rast')
    2769 
    2770 ST_ApproxMinMax('precip_2011', 'rast')
    2771 }}}
    2772 
    2773 ----
    2774 
    2775 '''ST_Histogram(raster, nband) -> set of records'''[[BR]]
    2776 ST_Histogram and ST_ApproxHistogram provide methods to determine a raster's data distribution.
    2777 
    2778 The return of ST_Histogram and ST_ApproxHistogram is a set of records where each record is (min, max, count, percent).
    2779 
    2780 ST_Histogram has the following variations.
    2781 
    2782 1. ST_Histogram(rast raster, nband int, exclude_nodata_value boolean, bins int, width double precision[], right boolean) -> set of records
    2783 
    2784   returns set of records of four columns (min, max, count, percent)
    2785 
    2786   nband: index of band to process on
    2787 
    2788   exclude_nodata_value: if FALSE, nodata values in band are included. if TRUE, nodata values are not included.
    2789 
    2790   bins: the number of categories/bins to have in the histogram. If NULL or value less than one, the number of categories will be auto-computed using Sturges' formula if the number of values >= 30 or Square-root choice if number of values < 30.
    2791 
    2792     http://en.wikipedia.org/wiki/Histogram#Mathematical_definition
    2793 
    2794   width: an array indicating the width of each category/bin. If the number of bins is greater than the number of widths, the widths are repeated. Example: 9 bins, widths are [a, b, c] will have the output be [a, b, c, a, b, c, a, b, c].
    2795 
    2796   right: compute the histogram from the right rather than from the left (default). This changes the criteria for evaluating a value x from [a, b) to (a, b].
    2797 
    2798 {{{
    2799 ST_Histogram(rast, 2, FALSE, NULL, NULL, FALSE)
    2800 
    2801 ST_Histogram(rast, 1, TRUE, 100, NULL, FALSE)
    2802 
    2803 ST_Histogram(rast, 2, FALSE, NULL, ARRAY[100, 50], FALSE)
    2804 
    2805 ST_Histogram(rast, 2, FALSE, 9, ARRAY[1000], TRUE)
    2806 
    2807 ST_Histogram(rast, 2, FALSE, 20, ARRAY[100, 200, 300], TRUE)
    2808 }}}
    2809 
    2810 2. ST_Histogram(rast raster, nband int, exclude_nodata_value boolean, bins int, right boolean) -> set of records
    2811 
    2812   parameter "width" is not specified thus resulting in all bins having the same widths
    2813 
    2814 {{{
    2815 ST_Histogram(rast, 2, FALSE, 5, FALSE)
    2816 }}}
    2817 
    2818 3. ST_Histogram(rast raster, nband int, exclude_nodata_value boolean, bins int) -> set of records
    2819 
    2820   the parameter "right" is removed and assumed to be FALSE
    2821 
    2822 {{{
    2823 ST_Histogram(rast, 2, FALSE, 5)
    2824 }}}
    2825 
    2826 4. ST_Histogram(rast raster, nband int, exclude_nodata_value boolean) -> set of records
    2827 
    2828   the parameter "bins" is removed and set to NULL.  The function will compute the number of bins to use
    2829 
    2830 5. ST_Histogram(rast raster, nband int) -> set of records
    2831 
    2832   exclude_nodata_value is assumed to be TRUE
    2833 
    2834 6. ST_Histogram(rast raster) -> set of records
    2835 
    2836   assumes that nband is 1.
    2837 
    2838 7. ST_Histogram(rast raster, nband int, bins int, width double precision[], right boolean) -> set of records
    2839 
    2840   exclude_nodata_value is assumed to be TRUE
    2841 
    2842 8. ST_Histogram(rast raster, nband int, bins int, right boolean) -> set of records
    2843 
    2844   all bins will have equal widths
    2845 
    2846 9. ST_Histogram(rast raster, nband int, bins int) -> set of records
    2847 
    2848   right is assumed to be FALSE
    2849 
    2850 ST_ApproxHistogram should have the following variations.
    2851 
    2852 1. ST_ApproxHistogram(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision, bins int, width double precision[], right boolean) -> set of record
    2853 
    2854     sample_percent: a value between 0 and 1 indicating the percentage of the raster band's pixels to consider when generating the histogram.
    2855 
    2856 {{{
    2857 ST_Histogram(rast, 2, FALSE, 0.1, NULL, NULL, FALSE)
    2858 
    2859 ST_Histogram(rast, 1, TRUE, 1, 100, NULL, FALSE)
    2860 
    2861 ST_Histogram(rast, 2, FALSE, 0.2, NULL, ARRAY[100, 50], FALSE)
    2862 
    2863 ST_Histogram(rast, 2, FALSE, 0.25, 9, ARRAY[1000], TRUE)
    2864 
    2865 ST_Histogram(rast, 2, FALSE, 0.05, 20, ARRAY[100, 200, 300], TRUE)
    2866 }}}
    2867 
    2868 2. ST_ApproxHistogram(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision, bins int, right boolean) -> set of records
    2869 
    2870   parameter "width" is not specified thus resulting in all bins having the same widths
    2871 
    2872 3. ST_ApproxHistogram(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision, bins int) -> set of records
    2873 
    2874   the parameter "right" is removed and assumed to be FALSE
    2875 
    2876 {{{
    2877 ST_ApproxHistogram(rast, 2, FALSE, 5)
    2878 }}}
    2879 
    2880 4. ST_ApproxHistogram(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision) -> set of records
    2881 
    2882   the parameter "bins" is removed and set to NULL so that function can compute the number of bins to use
    2883 
    2884 5. ST_ApproxHistogram(rast raster, nband int, sample_percent double precision) -> set of records
    2885 
    2886   exclude_nodata_value is assumed to be TRUE
    2887 
    2888 6. ST_ApproxHistogram(rast raster, nband int) -> set of records
    2889 
    2890   assumes that sample_percent is 0.1
    2891 
    2892 7. ST_ApproxHistogram(rast raster, sample_percent double_precision) -> set of records
    2893 
    2894   assumes that nband is 1
    2895 
    2896 8. ST_ApproxHistogram(rast raster) -> set of records
    2897 
    2898   assumes that nband is 1 and sample_percent is 0.1
    2899 
    2900 9. ST_ApproxHistogram(rast raster, nband int, sample_percent double precision, bins int, width double precision[], right boolean) -> set of records
    2901 
    2902   exclude_nodata_value is assumed to be TRUE
    2903 
    2904 10. ST_ApproxHistogram(rast raster, nband int, sample_percent double precision, bins int, right boolean) -> set of records
    2905 
    2906   all bins will have equal widths
    2907 
    2908 11. ST_ApproxHistogram(rast raster, nband int, sample_percent double precision, bins int) -> set of records
    2909 
    2910   right is assumed to be FALSE
    2911 
    2912 The following set of function are for coverages.
    2913 
    2914 1. ST_Histogram(rastertable text, rastercolumn text, nband int DEFAULT 1, exclude_nodata_value boolean DEFAULT TRUE, bins int default 0, width double precision[] DEFAULT NULL, right boolean DEFAULT FALSE) -> set of records
    2915 
    2916   rastertable: name of table with raster column
    2917 
    2918   rastercolumn: name of column of data type raster
    2919 
    2920 {{{
    2921 ST_Histogram('tmax_2010', 'rast')
    2922 
    2923 ST_Histogram('precip_2011', 'rast', 1)
    2924 
    2925 ST_Histogram('precip_2011', 'rast', 1, FALSE)
    2926 
    2927 ST_Histogram('precip_2011', 'rast', 1, FALSE, 5)
    2928 
    2929 ST_Histogram('precip_2011', 'rast', 1, FALSE, 10, NULL)
    2930 
    2931 ST_Histogram('precip_2011', 'rast', 1, FALSE, 10, NULL, TRUE)
    2932 }}}
    2933 
    2934 2. ST_Histogram(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean, bins int, right boolean) -> set of records
    2935 
    2936 {{{
    2937 ST_Histogram('tmin_2010', 'rast', 2, FALSE, 5, FALSE)
    2938 }}}
    2939 
    2940 3. ST_Histogram(rastertable text, rastercolumn text, nband int, bins int, width double precision[] DEFAULT NULL, right boolean DEFAULT FALSE) -> set of records
    2941 
    2942 {{{
    2943 ST_Histogram('ndvi_2010', 'rast', 1, 5)
    2944 
    2945 ST_Histogram('ndvi_2010', 'rast', 1, 0, ARRAY[0.5]::double precision[])
    2946 
    2947 ST_Histogram('ndvi_2010', 'rast', 1, 5, NULL, TRUE)
    2948 }}}
    2949 
    2950 4. ST_Histogram(rastertable text, rastercolumn text, nband int, bins int, right boolean) -> set of records
    2951 
    2952 {{{
    2953 ST_Histogram('veg_2009', 'rast', 2, 3, FALSE)
    2954 
    2955 ST_Histogram('veg_2009', 'rast', 2, 3, TRUE)
    2956 }}}
    2957 
    2958 A set of functions of ST_ApproxHistogram for coverage tables:
    2959 
    2960 1. ST_ApproxHistogram(rastertable text, rastercolumn text, nband int DEFAULT 1, exclude_nodata_value boolean DEFAULT TRUE, sample_percent double precision DEFAULT 0.1, bins int DEFAULT 0, width double precision[] DEFAULT NULL, right boolean DEFAULT FALSE) -> set of records
    2961 
    2962 {{{
    2963 ST_ApproxHistogram('precip_2010', 'rast')
    2964 
    2965 ST_ApproxHistogram('precip_2010', 'rast', 1)
    2966 
    2967 ST_ApproxHistogram('precip_2010', 'rast', 2, FALSE)
    2968 
    2969 ST_ApproxHistogram('precip_2010', 'rast', 1, TRUE, 0.25)
    2970 
    2971 ST_ApproxHistogram('precip_2010', 'rast', 3, FALSE, 0.2, 10)
    2972 
    2973 ST_ApproxHistogram('precip_2010', 'rast', 1, TRUE, 0.1, 0, ARRAY[1]::double precision[])
    2974 
    2975 ST_ApproxHistogram('precip_2010', 'rast', 1, TRUE, 0.1, 0, ARRAY[1]::double precision[], FALSE)
    2976 }}}
    2977 
    2978 2. ST_ApproxHistogram(rastertable text, rastercolumn text, nband int, exclude_nodata_value boolean, sample_percent double precision, bins int, right boolean)
    2979 
    2980 3. ST_ApproxHistogram(rastertable text, rastercolumn text, nband int, sample_percent double precision)
    2981 
    2982 4. ST_ApproxHistogram(rastertable text, rastercolumn text, sample_percent double precision)
    2983 
    2984 5. ST_ApproxHistogram(rastertable text, rastercolumn text, nband int, sample_percent double precision, bins int, width double precision[] DEFAULT NULL, right boolean DEFAULT FALSE)
    2985 
    2986 6. ST_ApproxHistogram(rastertable text, rastercolumn text, nband int, sample_percent double precision, bins int, right boolean)
    2987 
    2988 ----
    2989 '''ST_Quantile(raster, nband) -> set of records'''[[BR]]
    2990 In addition to determining the histogram of a raster, providing the ability to compute quantiles permits a user to reference a value in the context of the sample or population. Thus, a value could be examined to be at the raster's 25%, 50%, 75% percentile.
    2991 
    2992 http://en.wikipedia.org/wiki/Quantile
    2993 
    2994 ST_Quantile variations:
    2995 
    2996 1. ST_Quantile(rast raster, nband int, exclude_nodata_value boolean, quantiles double precision[]) -> set of records
    2997 
    2998   each row returned is of (quantile double precision, value double precision)
    2999 
    3000   nband: index of band to process on
    3001 
    3002   exclude_nodata_value: if FALSE, nodata values in band are included. if TRUE, nodata values are not included.
    3003 
    3004   quantiles: array of percentages to compute values for
    3005 
    3006 {{{
    3007 ST_Quantile(rast, 1, FALSE, ARRAY[0.1, 0.3, 0.7])
    3008 
    3009 ST_Quantile(rast, 1, TRUE, ARRAY[0.2])
    3010 
    3011 ST_Quantile(rast, 1, FALSE, ARRAY[0, 1])
    3012 }}}
    3013 
    3014 2. ST_Quantile(rast raster, nband int, quantiles double precision[]) -> set of records
    3015 
    3016   "exclude_nodata_value" is assumed to be TRUE
    3017 
    3018 {{{
    3019 ST_Quantile(rast, 1, ARRAY[0.1, 0.3, 0.7])
    3020 }}}
    3021 
    3022 3. ST_Quantile(rast raster, nband int, exclude_nodata_value boolean) -> set of records
    3023 
    3024   "quantiles" assumed to be ARRAY[0, 0.25, 0.5, 0.75, 1]
    3025 
    3026 4. ST_Quantile(rast raster, nband int) -> set of records
    3027 
    3028   "exclude_nodata_value" is assumed to be TRUE and "quantiles" assumed to be ARRAY[0, 0.25, 0.5, 0.75, 1]
    3029 
    3030 5. ST_Quantile(rast raster, quantiles double precision[]) -> set of records
    3031 
    3032   "nband" is assumed to be 1 and "exclude_nodata_value" is TRUE
    3033 
    3034 6. ST_Quantile(rast raster) -> set of records
    3035 
    3036   "nband" is assumed to be 1 and "quantiles" assumed to be ARRAY[0, 0.25, 0.5, 0.75, 1]
    3037 
    3038 7. ST_Quantile(rast raster, nband int, exclude_nodata_value boolean, quantile double precision) -> record
    3039 
    3040   quantile: the single percentile to compute
    3041 
    3042 8. ST_Quantile(rast raster, nband int, quantile double precision) -> record
    3043 
    3044   "exclude_nodata_value" is assumed to be TRUE
    3045 
    3046 9. ST_Quantile(rast raster, exclude_nodata_value boolean, quantile double precision) -> record
    3047 
    3048   "nband" is assumed to be 1
    3049 
    3050 10. ST_Quantile(rast raster, quantile double precision) -> record
    3051 
    3052   "nband" is assumed to be 1  and "exclude_nodata_value" is assumed to be TRUE
    3053 
    3054 ST_ApproxQuantile adds a "sample_percent" indicating the percentage of the raster to sample
    3055 
    3056 1. ST_ApproxQuantile(rast raster, nband int, exclude_nodata_value boolean, sample_percent double precision, quantiles double precision[]) -> set of records
    3057 
    3058   nband: index of band to process on
    3059 
    3060   exclude_nodata_value: if FALSE, nodata values in band are included. if TRUE, nodata values are not included.
    3061 
    3062   sample_percent: a value between 0 and 1 indicating the percentage of the raster band's pixels to consider when computing the quantiles
    3063 
    3064   quantiles: array of percentages to compute values for
    3065 
    3066 {{{
    3067 ST_ApproxQuantile(rast, 1, FALSE, 0.1, ARRAY[0.1, 0.3, 0.7])
    3068 
    3069 ST_ApproxQuantile(rast, 1, TRUE, .2, ARRAY[0.2])
    3070 
    3071 ST_ApproxQuantile(rast, 1, FALSE, 0.3, ARRAY[0, 1])
    3072 }}}
    3073 
    3074 2. ST_ApproxQuantile(rast raster, nband int, sample_percent double precision, quantiles double precision[]) -> set of records
    3075 
    3076   "exclude_nodata_value" is assumed to be TRUE
    3077 
    3078 {{{
    3079 ST_ApproxQuantile(rast, 1, .05, ARRAY[0.1, 0.3, 0.7])
    3080 }}}
    3081 
    3082 3. ST_ApproxQuantile(rast raster, nband int, sample_percent double precision) -> set of records
    3083 
    3084   "exclude_nodata_value" is assumed to be TRUE and "quantiles" assumed to be ARRAY[0, 0.25, 0.5, 0.75, 1]
    3085 
    3086 4. ST_ApproxQuantile(rast raster, sample_percent double precision, quantiles double precision[]) -> set of records
    3087 
    3088   "nband" is assumed to be 1
    3089 
    3090 5. ST_ApproxQuantile(rast raster, nband int, sample_percent double precision, quantile double precision) -> record
    3091 
    3092   quantile: the single percentile to compute
    3093 
    3094 6. ST_ApproxQuantile(rast raster, sample_percent double precision, quantile double precision) -> record
    3095 
    3096   "nband" is assumed to be 2
    3097 
    3098 7. ST_ApproxQuantile(rast raster, sample_percent double precision) -> set of records
    3099 
    3100   "nband" is assumed to be 1 and "quantiles" assumed to be ARRAY[0, 0.25, 0.5, 0.75, 1]
    3101 
    3102 8. ST_ApproxQuantile(rast raster, nband int, quantile double precision) -> record
    3103 
    3104   "sample_percent" assumed to be 0.1
    3105 
    3106 9. ST_ApproxQuantile(rast raster, quantiles double precision[]) -> set of records
    3107 
    3108   "nband" is assumed to be 1 and "sample_percent" assumed to be 0.1
    3109 
    3110 10. ST_ApproxQuantile(rast raster, quantile double precision) -> record
    3111 
    3112   "nband" assumed to be 1 and "sample_percent" assumed to be 0.1
    3113 
    3114 11. ST_ApproxQuantile(rast raster, nband int) -> set of records
    3115 
    3116   "quantiles" assumed to be ARRAY[0, 0.25, 0.5, 0.75, 1] and "sample_percent" assumed to be 0.1
    3117 
    3118 12. ST_ApproxQuantile(rast raster) -> set of records
    3119 
    3120   "nband" is assumed to be 1, "quantiles" assumed to be ARRAY[0, 0.25, 0.5, 0.75, 1] and "sample_percent" assumed to be 0.1
    3121 
    3122 ----
    3123 == '''Objective FV.03 - Implement all the necessary versions of ST_MapAlgebra''' ==
    3124 
    3125 '''ST_SameAlignment(raster, raster) -> boolean'''
    3126 
    3127 This function returns true if both rasters' grids are aligned.
    3128 
    3129 Two rasters grid are aligned if:
    3130 
    3131 * They share the same pixel scales and skews
    3132 
    3133 * At least one of any of the four corner of any pixel of one raster fall on any corner of the grid of the other raster.
    3134 
    3135 Alignment is not the same concept as georeference. Two rasters can be aligned but not have the same georeference.
    3136 
    3137 Rotation is important here since two rasters grid might look perfectly aligned but are not if their rotated are different.
    3138 
    3139 Knowing if two rasters share the same alignment is useful when it is time to decide if one of them must be resampled before doing other operations (like ST_MapAlgebra on two rasters).
    3140 
    3141 The variants of ST_SameAlignment are:
    3142 
    3143 1. ST_SameAlignment(rast1 raster, rast2 raster)
    3144 
    3145 2. ST_SameAlignment(ulx1, uly1, scalex1, scaley1, skewx1, skewy1, ulx2, uly2, scalex2, scaley2, skewx2, skewy2)
    3146 
    3147 ~~The first variant is useful to PL/pgSQL function which have already get the values of the parameters.
    3148 
    3149 ~~'''Implementation Details'''
    3150 
    3151 ~~Only the first variant should be implemented in C. The second one is a PL/pgSQL variants. The C implementation should follow the PL/pgSQL version implemented in http://trac.osgeo.org/postgis/browser/trunk/raster/scripts/plpgsql/st_mapalgebra.sql
    3152 
    3153 ~~It is not clear if this PL/pgSQL implementation works when raster are rotated. To verify.
    3154 
    3155 ~~See discussion in http://trac.osgeo.org/postgis/ticket/589
    3156 
    3157 ~~Bborie:
    3158 
    3159 ~~The variant that should be developed in C is (2) as it is more likely that someone will have two rasters instead of that large set of raster elements.  Instead, (1) should be a pl/pgsql function calling (2).
    3160 
    3161 ----
    3162 == '''Objective FV.05 - Being able to reproject a raster. - ''Done''''' ==
    3163  
    3164 
    3165 '''ST_Transform(raster|geometry, SRID) -> same type as input'''
    3166 
    3167 ST_Transform enables the end-user to reproject a raster to a new projection.  Unlike the ST_Transform function for geometries, ST_Transform for rasters depends upon the specificiation of a resampling algorithm and an error tolerance.  In addition, reprojecting a raster will probably change the scale of the pixels.  Therefore, ST_Transform for rasters can be more involved than the ST_Transform for geometries.
    3168 
    3169 ''If a skewed raster is provided to ST_Transform, the output raster will be deskewed (zero skew in X and Y axes thus "north up").  To preserve the skew, use ST_Resample.''
    3170 
    3171 1. ST_Transform(rast raster, srid integer, algorithm text DEFAULT '!NearestNeighbour', maxerr double precision DEFAULT 0.125, scalex double precision DEFAULT 0, scaley double precision DEFAULT 0)
    3172 
    3173   returns a new raster in the projection specified by "srid"
    3174 
    3175   srid: the SRID of the projection to use when reprojecting the raster
    3176 
    3177   algorithm: the resampling algorithm to use when reprojecting the raster.  default is '!NearestNeighbour'.  possible algorithms are:
    3178 
    3179 {{{
    3180 NearestNeighbour (default.  fastest performance but worst interpolation)
    3181 
    3182 NearestNeighbor (for those wanting to use the American spelling)
    3183 
    3184 Bilinear
    3185 
    3186 Cubic
    3187 
    3188 CubicSpline
    3189 
    3190 Lanczos
    3191 }}}
    3192 
    3193   maxerr: the threshold for transformation approximation by the resampling algorithm (in pixel units).  default is 0.125, which is the same value used in GDAL gdalwarp utility.  if set to zero, no approximation takes place.
    3194 
    3195   scalex: the reprojected raster's scale in the X axis.  default is 0 indicating that the user isn't specifying the reprojected raster's scale
    3196 
    3197   scaley: the reprojected raster's scale in the Y axis.  default is 0 indicating that the user isn't specifying the reprojected raster's scale
    3198 
    3199 {{{
    3200 ST_Transform(rast, 3310)
    3201 
    3202 ST_Transform(rast, 3310, 'Bilinear')
    3203 
    3204 ST_Transform(rast, 3310, 'Lanczos', 0)
    3205 
    3206 ST_Transform(rast, 3310, 'Lanczos', 0.5)
    3207 
    3208 ST_Transform(rast, 3310, 'Lanczos', 0.125, 1000)
    3209 
    3210 ST_Transform(rast, 3310, 'Lanczos', 0.125, 1000, 1000)
    3211 }}}
    3212 
    3213 2. ST_Transform(rast raster, srid integer, scalex double precision, scaley double precision, algorithm text DEFAULT '!NearestNeighbour', maxerr double precision DEFAULT 0.125)
    3214 
    3215 {{{
    3216 ST_Transform(rast, 4326, 500, 500)
    3217 
    3218 ST_Transform(rast, 4326, 500, 500, 'Cubic')
    3219 
    3220 ST_Transform(rast, 4326, 500, 500, 'CubicSpline', 0)
    3221 }}}
    3222 
    3223 3. ST_Transform(rast raster, srid integer, scalexy double precision, algorithm text DEFAULT '!NearestNeighbour', maxerr double precision DEFAULT 0.125)
    3224 
    3225   scalexy: the reprojected raster's scale in the X and Y axes.  default is 0 indicating that the user isn't specifying the reprojected raster's scale
    3226 
    3227 {{{
    3228 ST_Transform(rast, 4326, 250)
    3229 
    3230 ST_Transform(rast, 4326, 250, 'Cubic')
    3231 
    3232 ST_Transform(rast, 4326, 100, 'CubicSpline', 0)
    3233 }}}
    3234 
    3235 ----
    3236 == '''Objective FV.06 - Being able to do some base raster operations. - ''Done Partially''''' ==
    3237  
    3238 
    3239 '''ST_ValueCount(raster, value) -> integer'''[[BR]]
    3240 
    3241 ST_ValueCount provides the ability to count the number of times that a user-provided value is present in a raster. To handle floating point values, a rounding argument is provided.
    3242 
    3243 A set of functions for one or more search values:
    3244 
    3245 ''If NULL is passed for "searchvalues" to any of the ST_ValueCount variations with "searchvalues", the function returns the counts for all unique values''
    3246 
    3247 1. ST_ValueCount(rast raster, nband integer, exclude_nodata_value boolean, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, count)
    3248 
    3249     returns the number of times that each value in searchvalues is present in the raster
    3250 
    3251     exclude_nodata_value: if FALSE, nodata values in band are considered in the count. if TRUE, nodata values are not considered
    3252 
    3253     searchvalues: the set of values to count in the raster
    3254 
    3255     roundto: the decimal position to round a pixel value to. Originally intended for use with 32BF and 64BF pixel types, it can also be used with integers when round to the tens, hundreds or higher place.
    3256 
    3257         examples are...
    3258 
    3259 {{{
    3260         roundto < 0: no rounding
    3261 
    3262         0: no rounding
    3263 
    3264         0.1: round to the tenths place
    3265 
    3266         0.01: round to the hundredths place
    3267 
    3268         0.001: round to the thousandths place
    3269 
    3270         1: round to the ones place
    3271 
    3272         10: round to the tens place
    3273 
    3274         100: round to the hundreds place
    3275 }}}
    3276 
    3277 {{{
    3278 ST_ValueCount(rast, 1, TRUE, ARRAY[23], 0)
    3279 
    3280 ST_ValueCount(rast, 5, FALSE, ARRAY[3.14], 0.01)
    3281 
    3282 ST_ValueCount(rast, 2, TRUE, ARRAY[100], 100)
    3283 
    3284 ST_ValueCount(rast, 1, FALSE, ARRAY[-9999, 0], 1)
    3285 
    3286 ST_ValueCount(rast, 1, FALSE, NULL::double precision[], 1)
    3287 }}}
    3288 
    3289 2. ST_ValueCount(rast raster, nband integer, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, count)
    3290 
    3291     exclude_nodata_value is assumed to be TRUE
    3292 
    3293 {{{
    3294 ST_ValueCount(rast, 5, ARRAY[3.14], 0.01)
    3295 
    3296 ST_ValueCount(rast, 2, NULL::double precision[], 100)
    3297 }}}
    3298 
    3299 3. ST_ValueCount(rast raster, nband integer, searchvalues double precision[]) -> setof record (searchvalue, count)
    3300 
    3301     roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE
    3302 
    3303 {{{
    3304 ST_ValueCount(rast, 1, ARRAY[-9999])
    3305 
    3306 ST_ValueCount(rast, 1, NULL::double precision[])
    3307 }}}
    3308 
    3309 4. ST_ValueCount(rast raster, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, count)
    3310 
    3311     nband is assumed to be 1. exclude_nodata_value is assumed to be TRUE.
    3312 
    3313 5. ST_ValueCount(rast raster, searchvalues double precision[]) -> setof record (searchvalue, count)
    3314 
    3315     nband is assumed to be 1. roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE.
    3316 
    3317 A set of functions for a single search value:
    3318 
    3319 1. ST_ValueCount(rast raster, nband integer, exclude_nodata_value boolean, searchvalue double precision, roundto double precision) -> integer
    3320 
    3321     returns the number of times that searchvalue is present in the raster
    3322 
    3323     searchvalue: the value to count in the raster
    3324 
    3325 {{{
    3326 ST_ValueCount(rast, 1, TRUE, 23, 0)
    3327 
    3328 ST_ValueCount(rast, 5, FALSE, 3.14, 0.01)
    3329 
    3330 ST_ValueCount(rast, 2, TRUE, 100, 100)
    3331 
    3332 ST_ValueCount(rast, 1, FALSE, -9999, 1)
    3333 }}}
    3334 
    3335 2. ST_ValueCount(rast raster, nband integer, searchvalue double precision, roundto double precision) -> integer
    3336 
    3337     exclude_nodata_value is assumed to be TRUE
    3338 
    3339 {{{
    3340 ST_ValueCount(rast, 5, 3.14, 0.01)
    3341 
    3342 ST_ValueCount(rast, 2, 100, 100)
    3343 }}}
    3344 
    3345 3. ST_ValueCount(rast raster, nband integer, searchvalue double precision) -> integer
    3346 
    3347     roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE
    3348 
    3349 {{{
    3350 ST_ValueCount(rast, 1, -9999)
    3351 }}}
    3352 
    3353 4. ST_ValueCount(rast raster, searchvalue double precision, roundto double precision) -> integer
    3354 
    3355     nband is assumed to be 1. exclude_nodata_value is assumed to be TRUE.
    3356 
    3357 5. ST_ValueCount(rast raster, searchvalue double precision) -> integer
    3358 
    3359     nband is assumed to be 1. roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE.
    3360 
    3361 The set of functions for processing coverages return "bigint" instead of "integer".
    3362 
    3363 A set of functions for one or more search values:
    3364 
    3365 1. ST_ValueCount(rastertable text, rastercolumn text, nband integer, exclude_nodata_value boolean, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, count)
    3366 
    3367     rastertable: name of the table with a raster column
    3368 
    3369     rastercolumn: name of the raster column
    3370 
    3371 {{{
    3372 ST_ValueCount('test', 'rast', 1, TRUE, ARRAY[23], 0)
    3373 
    3374 ST_ValueCount('test', 'rast', 5, FALSE, ARRAY[3.14], 0.01)
    3375 
    3376 ST_ValueCount('test', 'rast', 2, TRUE, ARRAY[100], 100)
    3377 
    3378 ST_ValueCount('test', 'rast', 1, FALSE, ARRAY[-9999, 0], 1)
    3379 
    3380 ST_ValueCount('test', 'rast', 1, FALSE, NULL::double precision[], 1)
    3381 }}}
    3382 
    3383 2. ST_ValueCount(rastertable text, rastercolumn text, nband integer, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, count)
    3384 
    3385     exclude_nodata_value is assumed to be TRUE
    3386 
    3387 {{{
    3388 ST_ValueCount('test', 'rast', 5, ARRAY[3.14], 0.01)
    3389 
    3390 ST_ValueCount('test', 'rast', 2, NULL::double precision[], 100)
    3391 }}}
    3392 
    3393 3. ST_ValueCount(rastertable text, rastercolumn text, nband integer, searchvalues double precision[]) -> setof record (searchvalue, count)
    3394 
    3395     roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE
    3396 
    3397 {{{
    3398 ST_ValueCount('test', 'rast', 1, ARRAY[-9999])
    3399 
    3400 ST_ValueCount('test', 'rast', 1, NULL::double precision[])
    3401 }}}
    3402 
    3403 4. ST_ValueCount(rastertable text, rastercolumn text, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, count)
    3404 
    3405     nband is assumed to be 1. exclude_nodata_value is assumed to be TRUE.
    3406 
    3407 5. ST_ValueCount(rastertable text, rastercolumn text, searchvalues double precision[]) -> setof record (searchvalue, count)
    3408 
    3409     nband is assumed to be 1. roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE.
    3410 
    3411 A set of functions for a single search value:
    3412 
    3413 1. ST_ValueCount(rastertable text, rastercolumn text, nband integer, exclude_nodata_value boolean, searchvalue double precision, roundto double precision) -> bigint
    3414 
    3415     searchvalue: the value to count in the raster
    3416 
    3417 {{{
    3418 ST_ValueCount('test', 'rast', 1, TRUE, 23, 0)
    3419 
    3420 ST_ValueCount('test', 'rast', 5, FALSE, 3.14, 0.01)
    3421 
    3422 ST_ValueCount('test', 'rast', 2, TRUE, 100, 100)
    3423 
    3424 ST_ValueCount('test', 'rast', 1, FALSE, -9999, 1)
    3425 }}}
    3426 
    3427 2. ST_ValueCount(rastertable text, rastercolumn text, nband integer, searchvalue double precision, roundto double precision) -> bigint
    3428 
    3429     exclude_nodata_value is assumed to be TRUE
    3430 
    3431 {{{
    3432 ST_ValueCount('test', 'rast', 5, 3.14, 0.01)
    3433 
    3434 ST_ValueCount('test', 'rast', 2, 100, 100)
    3435 }}}
    3436 
    3437 3. ST_ValueCount(rastertable text, rastercolumn text, nband integer, searchvalue double precision) -> bigint
    3438 
    3439     roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE
    3440 
    3441 {{{
    3442 ST_ValueCount('test', 'rast', 1, -9999)
    3443 }}}
    3444 
    3445 4. ST_ValueCount(rastertable text, rastercolumn text, searchvalue double precision, roundto double precision) -> bigint
    3446 
    3447     nband is assumed to be 1. exclude_nodata_value is assumed to be TRUE.
    3448 
    3449 5. ST_ValueCount(rastertable text, rastercolumn text, searchvalue double precision) -> bigint
    3450 
    3451     nband is assumed to be 1. roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE.
    3452 
    3453 ----
    3454 
    3455 '''ST_ValuePercent(raster, value) -> double precision'''[[BR]]
    3456 
    3457 ST_ValuePercent is the sibling of ST_ValueCount and returns the percentage of a raster's band that is of a specified value. To handle floating point values, a rounding argument is provided.
    3458 
    3459 A set of functions for one or more search values:
    3460 
    3461 ''If NULL is passed for "searchvalues" to any of the ST_ValuePercent variations with "searchvalues", the function returns the percents for all unique values''
    3462 
    3463 1. ST_ValuePercent(rast raster, nband integer, exclude_nodata_value boolean, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, percent)
    3464 
    3465     returns the percentage of a raster's band that each value in searchvalues is found
    3466 
    3467     exclude_nodata_value: if FALSE, nodata values in band are considered in the percents. if TRUE, nodata values are not considered
    3468 
    3469     searchvalues: the set of values to get percents for in the raster
    3470 
    3471     roundto: the decimal position to round a pixel value to. Originally intended for use with 32BF and 64BF pixel types, it can also be used with integers when round to the tens, hundreds or higher place.
    3472 
    3473         examples are...
    3474 
    3475 {{{
    3476         roundto < 0: no rounding
    3477 
    3478         0: no rounding
    3479 
    3480         0.1: round to the tenths place
    3481 
    3482         0.01: round to the hundredths place
    3483 
    3484         0.001: round to the thousandths place
    3485 
    3486         1: round to the ones place
    3487 
    3488         10: round to the tens place
    3489 
    3490         100: round to the hundreds place
    3491 }}}
    3492 
    3493 {{{
    3494 ST_ValuePercent(rast, 1, TRUE, ARRAY[23], 0)
    3495 
    3496 ST_ValuePercent(rast, 5, FALSE, ARRAY[3.14], 0.01)
    3497 
    3498 ST_ValuePercent(rast, 2, TRUE, ARRAY[100], 100)
    3499 
    3500 ST_ValuePercent(rast, 1, FALSE, ARRAY[-9999, 0], 1)
    3501 
    3502 ST_ValuePercent(rast, 1, FALSE, NULL::double precision[], 1)
    3503 }}}
    3504 
    3505 2. ST_ValuePercent(rast raster, nband integer, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, percent)
    3506 
    3507     exclude_nodata_value is assumed to be TRUE
    3508 
    3509 {{{
    3510 ST_ValuePercent(rast, 5, ARRAY[3.14], 0.01)
    3511 
    3512 ST_ValuePercent(rast, 2, NULL::double precision[], 100)
    3513 }}}
    3514 
    3515 3. ST_ValuePercent(rast raster, nband integer, searchvalues double precision[]) -> setof record (searchvalue, percent)
    3516 
    3517     roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE
    3518 
    3519 {{{
    3520 ST_ValuePercent(rast, 1, ARRAY[-9999])
    3521 
    3522 ST_ValuePercent(rast, 1, NULL::double precision[])
    3523 }}}
    3524 
    3525 4. ST_ValuePercent(rast raster, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, percent)
    3526 
    3527     nband is assumed to be 1. exclude_nodata_value is assumed to be TRUE.
    3528 
    3529 5. ST_ValuePercent(rast raster, searchvalues double precision[]) -> setof record (searchvalue, percent)
    3530 
    3531     nband is assumed to be 1. roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE.
    3532 
    3533 A set of functions for a single search value:
    3534 
    3535 1. ST_ValuePercent(rast raster, nband integer, exclude_nodata_value boolean, searchvalue double precision, roundto double precision) -> integer
    3536 
    3537     searchvalue: the value to get a percent for in the raster
    3538 
    3539 {{{
    3540 ST_ValuePercent(rast, 1, TRUE, 23, 0)
    3541 
    3542 ST_ValuePercent(rast, 5, FALSE, 3.14, 0.01)
    3543 
    3544 ST_ValuePercent(rast, 2, TRUE, 100, 100)
    3545 
    3546 ST_ValuePercent(rast, 1, FALSE, -9999, 1)
    3547 }}}
    3548 
    3549 2. ST_ValuePercent(rast raster, nband integer, searchvalue double precision, roundto double precision) -> integer
    3550 
    3551     exclude_nodata_value is assumed to be TRUE
    3552 
    3553 {{{
    3554 ST_ValuePercent(rast, 5, 3.14, 0.01)
    3555 
    3556 ST_ValuePercent(rast, 2, 100, 100)
    3557 }}}
    3558 
    3559 3. ST_ValuePercent(rast raster, nband integer, searchvalue double precision) -> integer
    3560 
    3561     roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE
    3562 
    3563 {{{
    3564 ST_ValuePercent(rast, 1, -9999)
    3565 }}}
    3566 
    3567 4. ST_ValuePercent(rast raster, searchvalue double precision, roundto double precision) -> integer
    3568 
    3569     nband is assumed to be 1. exclude_nodata_value is assumed to be TRUE.
    3570 
    3571 5. ST_ValuePercent(rast raster, searchvalue double precision) -> integer
    3572 
    3573     nband is assumed to be 1. roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE.
    3574 
    3575 The set of functions for processing coverages return "bigint" instead of "integer".
    3576 
    3577 A set of functions for one or more search values:
    3578 
    3579 1. ST_ValuePercent(rastertable text, rastercolumn text, nband integer, exclude_nodata_value boolean, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, percent)
    3580 
    3581     rastertable: name of the table with a raster column
    3582 
    3583     rastercolumn: name of the raster column
    3584 
    3585 {{{
    3586 ST_ValuePercent('test', 'rast', 1, TRUE, ARRAY[23], 0)
    3587 
    3588 ST_ValuePercent('test', 'rast', 5, FALSE, ARRAY[3.14], 0.01)
    3589 
    3590 ST_ValuePercent('test', 'rast', 2, TRUE, ARRAY[100], 100)
    3591 
    3592 ST_ValuePercent('test', 'rast', 1, FALSE, ARRAY[-9999, 0], 1)
    3593 
    3594 ST_ValuePercent('test', 'rast', 1, FALSE, NULL::double precision[], 1)
    3595 }}}
    3596 
    3597 2. ST_ValuePercent(rastertable text, rastercolumn text, nband integer, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, percent)
    3598 
    3599     exclude_nodata_value is assumed to be TRUE
    3600 
    3601 {{{
    3602 ST_ValuePercent('test', 'rast', 5, ARRAY[3.14], 0.01)
    3603 
    3604 ST_ValuePercent('test', 'rast', 2, NULL::double precision[], 100)
    3605 }}}
    3606 
    3607 3. ST_ValuePercent(rastertable text, rastercolumn text, nband integer, searchvalues double precision[]) -> setof record (searchvalue, percent)
    3608 
    3609     roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE
    3610 
    3611 {{{
    3612 ST_ValuePercent('test', 'rast', 1, ARRAY[-9999])
    3613 
    3614 ST_ValuePercent('test', 'rast', 1, NULL::double precision[])
    3615 }}}
    3616 
    3617 4. ST_ValuePercent(rastertable text, rastercolumn text, searchvalues double precision[], roundto double precision) -> setof record (searchvalue, percent)
    3618 
    3619     nband is assumed to be 1. exclude_nodata_value is assumed to be TRUE.
    3620 
    3621 5. ST_ValuePercent(rastertable text, rastercolumn text, searchvalues double precision[]) -> setof record (searchvalue, percent)
    3622 
    3623     nband is assumed to be 1. roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE.
    3624 
    3625 A set of functions for a single search value:
    3626 
    3627 1. ST_ValuePercent(rastertable text, rastercolumn text, nband integer, exclude_nodata_value boolean, searchvalue double precision, roundto double precision) -> bigint
    3628 
    3629     searchvalue: the value to get a percent for in the raster
    3630 
    3631 {{{
    3632 ST_ValuePercent('test', 'rast', 1, TRUE, 23, 0)
    3633 
    3634 ST_ValuePercent('test', 'rast', 5, FALSE, 3.14, 0.01)
    3635 
    3636 ST_ValuePercent('test', 'rast', 2, TRUE, 100, 100)
    3637 
    3638 ST_ValuePercent('test', 'rast', 1, FALSE, -9999, 1)
    3639 }}}
    3640 
    3641 2. ST_ValuePercent(rastertable text, rastercolumn text, nband integer, searchvalue double precision, roundto double precision) -> bigint
    3642 
    3643     exclude_nodata_value is assumed to be TRUE
    3644 
    3645 {{{
    3646 ST_ValuePercent('test', 'rast', 5, 3.14, 0.01)
    3647 
    3648 ST_ValuePercent('test', 'rast', 2, 100, 100)
    3649 }}}
    3650 
    3651 3. ST_ValuePercent(rastertable text, rastercolumn text, nband integer, searchvalue double precision) -> bigint
    3652 
    3653     roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE
    3654 
    3655 {{{
    3656 ST_ValuePercent('test', 'rast', 1, -9999)
    3657 }}}
    3658 
    3659 4. ST_ValuePercent(rastertable text, rastercolumn text, searchvalue double precision, roundto double precision) -> bigint
    3660 
    3661     nband is assumed to be 1. exclude_nodata_value is assumed to be TRUE.
    3662 
    3663 5. ST_ValuePercent(rastertable text, rastercolumn text, searchvalue double precision) -> bigint
    3664 
    3665     nband is assumed to be 1. roundto is assumed to be 0, so no rounding takes place. exclude_nodata_value is assumed to be TRUE.
    3666 
    3667 ----
    3668 
    3669 '''ST_Resample(raster, method, originx, originy, pixelsizex, pixelsizey) -> raster'''
    3670 
    3671 ST_Resample is the main function underlying the ST_Transform, ST_Rescale, ST_Reskew and ST_SnapToGrid functions.  ST_Resample provides the ability to change a raster's projection, adjust the scale of the raster within its extent, change the skew (or deskew) and "dissolve" the raster to a grid.
    3672 
    3673 ''If a skewed raster is provided to ST_Resample and skewx and/or skewy are not provided, the output raster will be deskewed (zero skew in X and Y axes thus "north up")''
    3674 
    3675 1. ST_Resample(
    3676         rast raster,
    3677         srid integer DEFAULT NULL,
    3678         scalex double precision DEFAULT 0, scaley double precision DEFAULT 0,
    3679         gridx double precision DEFAULT NULL, gridy double precision DEFAULT NULL,
    3680         skewx double precision DEFAULT 0, skewy double precision DEFAULT 0,
    3681         algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125
    3682 ) -> raster
    3683 
    3684   srid: the SRID that the function will use to reproject the raster to.  If NULL, the raster's SRID will be used.
    3685 
    3686   scalex: the scale/pixel size in the X axis of the resampled raster.  If scalex and scaley are zero (0), the resampled raster's scale will be autocomputed.  If provided, scaley must also be provided.
    3687 
    3688   scaley: the scale/pixel size in the Y axis of the resampled raster.  If scalex and scaley are zero (0), the resampled raster's scale will be autocomputed.  If provided, scalex must also be provided.
    3689 
    3690   gridx: the X coordinate of a point on the grid to which the raster will be aligned.  Value is in the resampled raster's world coordinates.  If provided, gridy must also be provided.
    3691 
    3692   gridy: the Y coordinate of a point on the grid to which the raster will be aligned.  Value is in the resampled raster's world coordinates.  If provided, gridx must also be provided.
    3693 
    3694   skewx: the skew along the X axis of the resampled raster.  If skewx and skewy are zero (0), the resampled raster will be "north up".
    3695 
    3696   skewy: the skew along the Y axis of the resampled raster.  If skewx and skewy are zero (0), the resampled raster will be "north up".
    3697 
    3698   algorithm: the algorithm to use when resampling the raster. default is 'NearestNeighbour'. possible algorithms are:
    3699 
    3700 {{{
    3701 NearestNeighbour (default.  fastest performance but worst interpolation)
    3702 
    3703 NearestNeighbor (for those wanting to use the American spelling)
    3704 
    3705 Bilinear
    3706 
    3707 Cubic
    3708 
    3709 CubicSpline
    3710 
    3711 Lanczos
    3712 }}}
    3713 
    3714   maxerr: the threshold for approximation by the resampling algorithm (in pixel units). default is 0.125, which is the same value used in GDAL gdalwarp utility. if set to zero, no approximation takes place.
    3715 
    3716 2. ST_Resample(
    3717         rast raster,
    3718         ref raster,
    3719         algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125
    3720 ) -> raster
    3721 
    3722   ref: the reference raster to which rast will copy the scalex, scaley, skewx, skewy and srid.  In addition, the upperleft corner of ref will be used as a grid point to align rast.
    3723 
    3724 ----
    3725 
    3726 '''ST_Rescale(raster, scalex, scaley) -> raster'''
    3727 
    3728 ST_Rescale is a focused function of ST_Resample for changing the X and Y scale without affecting the extent of the raster.
    3729 
    3730 ''If a skewed raster is provided to ST_Rescale, the output raster will be deskewed (zero skew in X and Y axes thus "north up").  To preserve the skew, use ST_Resample.''
    3731 
    3732 1. ST_Rescale(rast raster, scalex double precision, scaley double precision, algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125) -> raster
    3733 
    3734   scalex: the scale/pixel size in the X axis of the resampled raster
    3735 
    3736   scaley: the scale/pixel size in the Y axis of the resampled raster
    3737 
    3738 2. ST_Rescale(rast raster, scalexy double precision, algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125) -> raster
    3739 
    3740   scalexy: the scale/pixel size in the X and Y axes of the resampled raster.
    3741 
    3742 ----
    3743 
    3744 '''ST_Reskew(raster, skewx, skewy) -> raster'''
    3745 
    3746 ST_Reskew is a focused function of ST_Resample for changing the skew along the X and Y axes.
    3747 
    3748 1. ST_Reskew(rast raster, skewx double precision, skewy double precision, algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125) -> raster
    3749 
    3750   skewx: the skew along the X axis of the resampled raster.  If skewx and skewy are zero (0), the resampled raster will be "north up".
    3751 
    3752   skewy: the skew along the Y axis of the resampled raster.  If skewx and skewy are zero (0), the resampled raster will be "north up".
    3753 
    3754 2. ST_Reskew(rast raster, skewxy double precision, algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125) -> raster
    3755 
    3756   skewxy: the skew along both X and Y axes of the resampled raster.  If skewxy is zero (0), the resampled raster will be "north up".
    3757 
    3758 ----
    3759 
    3760 '''ST_SnapToGrid(raster, gridx, gridy)'''
    3761 
    3762 ST_SnapToGrid provides the ability to align a raster to grid (possibly that of a coverage).  The returned raster has its upper-left corner placed at the closest valid grid point to the source raster's upper-left corner without losing a data.  The same is done for the lower-right corner.
    3763 
    3764 [[Image(snaptogrid.png)]]
    3765 
    3766 ''If a skewed raster is provided to ST_SnapToGrid, the output raster will be deskewed (zero skew in X and Y axes thus "north up").  To preserve the skew, use ST_Resample.''
    3767 
    3768 1. ST_SnapToGrid(
    3769         rast raster,
    3770         gridx double precision, gridy double precision,
    3771         algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125,
    3772         scalex double precision DEFAULT 0, scaley double precision DEFAULT 0
    3773 ) -> raster
    3774 
    3775   gridx: the X coordinate of a point on the grid to which the raster will be aligned.  Value is in the raster's world coordinates.
    3776 
    3777   gridy: the Y coordinate of a point on the grid to which the raster will be aligned.  Value is in the raster's world coordinates.
    3778 
    3779   scalex: the scale/pixel size in the X axis of the resampled raster.  This is also the scale of the grid to which the raster is being "snapped".  If provided, scaley must be provided.  If scalex and scaley are zero, the input raster's scale will be used.
    3780 
    3781   scaley: the scale/pixel size in the Y axis of the resampled raster.  This is also the scale of the grid to which the raster is being "snapped"  If provided, scalex must be provided.  If scalex and scaley are zero, the input raster's scale will be used.
    3782 
    3783 2. ST_SnapToGrid(
    3784         rast raster,
    3785         gridx double precision, gridy double precision,
    3786         scalex double precision, scaley double precision,
    3787         algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125
    3788 ) -> raster
    3789 
    3790 3. ST_SnapToGrid(
    3791         rast raster,
    3792         gridx double precision, gridy double precision,
    3793         scalexy double precision,
    3794         algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125
    3795 ) -> raster
    3796 
    3797   scalexy: the scale/pixel size in the X and Y axes of the resampled raster.
    3798 
    3799 ~~ * Implemented as a wrapper around GDAL like done for ST_DumpAsPolygons()~~
    3800 ~~ * Resampling methods are the same as the one priovided by GDAL~~
    3801 
    3802 ~~ '''Variants'''~~
    3803 
    3804 ~~ * The '''first series of variants''' only change the pixelsize of the raster.~~
    3805 
    3806 ~~ 1) ST_Resample(raster, pixelsize) - modify pixelsize only and resample using the default method~~
    3807 
    3808 ~~ 2) ST_Resample(raster, method, pixelsize) - modify pixelsize and resample using the specified method~~
    3809 
    3810 ~~ 3) ST_Resample(raster, pixelsizex, pixelsizey) - modify pixelsize with different values for x and y using the default method~~
    3811 
    3812 ~~ 4) ST_Resample(raster, method, pixelsizex, pixelsizey) - modify pixelsize with different values for x and y using the specified method~~
    3813 
    3814 ~~ * The '''second series of variants''' realign the raster and change the pixelsize~~
    3815 
    3816 ~~ 5) ST_Resample(raster, x, y, pixelsize) - realign and modify pixelsize using the default method~~
    3817 
    3818 ~~ 6) ST_Resample(raster, method, x, y, pixelsize) - realign and modify pixelsize using the specified method~~
    3819 
    3820 ~~ 7) ST_Resample(raster, x, y, pixelsizex, pixelsizey) - realign and modify pixelsize with different values for x and y using the default method~~
    3821 
    3822 ~~ 8) ST_Resample(raster, method, x, y, pixelsizex, pixelsizey) - realign and modify pixelsize with different values for x and y using the specified method~~
    3823 
    3824 ~~ 9) ST_Resample(raster, x, y, pixelsizex, pixelsizey, skewx, skewy) - realign and modify pixelsize and skew with different values for x and y using the default method~~
    3825 
    3826 ~~ 10) ST_Resample(raster, method, x, y, pixelsizex, pixelsizey, skewx, skewy) - realign and modify pixelsize and skew with different values for x and y using the specified method~~
    3827 
    3828 ~~ * The '''third series of variants''' align and resample the raster to match the alignment, pixelsize and skew of an existing raster.~~
    3829  
    3830 ~~ 11) ST_Resample(destraster, sourceraster)~~
    3831 
    3832 ~~ '''Questions'''~~
    3833 
    3834 ~~ * Should there be a band parameter?~~
    3835 
    3836 ~~ * How does GDAL allow resampling?~~
    3837 
    3838 ~~ * Which methods GDAL supports? ArcGIS supports NEAREST | BILINEAR | CUBIC | MAJORITY~~
    3839 
    3840 ~~ * Does GDAL support change in skew?~~
    3841 
    3842 ----
    3843 == '''Objective FV.22 - Making raster_columns and raster_overview as views''' ==
    3844 
    3845 raster_columns and raster_overviews are now constraint-based views.  The structure of the raster_columns views is below.
    3846 
    3847 {{{
    3848 #!html
    3849 <TABLE BORDER=1>
    3850         <THEAD>
    3851                 <TR>
    3852                         <TD ALIGN=LEFT style="font-weight: bold; font-style: italic;">Column</TD>
    3853                         <TD ALIGN=LEFT style="font-weight: bold; font-style: italic;">PostgreSQL Type</TD>
    3854                         <TD ALIGN=LEFT style="font-weight: bold; font-style: italic;">Description</TD>
    3855                 </TR>
    3856         </THEAD>
    3857         <TBODY>
    3858                 <TR>
    3859                         <TD ALIGN=LEFT>r_table_catalog</TD>
    3860                         <TD ALIGN=LEFT>name</TD>
    3861                         <TD ALIGN=LEFT>Name of the database containing the table with the raster column.</TD>
    3862                 </TR>
    3863                 <TR>
    3864                         <TD ALIGN=LEFT>r_table_schema</TD>
    3865                         <TD ALIGN=LEFT>name</TD>
    3866                         <TD ALIGN=LEFT>Name of the schema containing the table with the raster column.</TD>
    3867                 </TR>
    3868                 <TR>
    3869                         <TD ALIGN=LEFT>r_table_name</TD>
    3870                         <TD ALIGN=LEFT>name</TD>
    3871                         <TD ALIGN=LEFT>Name of the table containing a column of type raster.</TD>
    3872                 </TR>
    3873                 <TR>
    3874                         <TD ALIGN=LEFT>r_raster_column</TD>
    3875                         <TD ALIGN=LEFT>name</TD>
    3876                         <TD ALIGN=LEFT>Name of the raster column in the table.  All attribute columns following this column apply to the rasters in this column.  <span style="font-style: italic; font-weight: bold;">This column was previously known as r_column.</span></TD>
    3877                 </TR>
    3878                 <TR>
    3879                         <TD ALIGN=LEFT>srid</TD>
    3880                         <TD ALIGN=LEFT>integer</TD>
    3881                         <TD ALIGN=LEFT>ID of the spatial reference system of the rasters in this column.  Value is extracted from the ST_SRID constraint on this raster column.  <span style="font-style: italic;">The constraint is typically named enforce_srid_&lt;r_raster_column&gt;.</span></TD>
    3882                 </TR>
    3883                 <TR>
    3884                         <TD ALIGN=LEFT>scale_x</TD>
    3885                         <TD ALIGN=LEFT>double precision</TD>
    3886                         <TD ALIGN=LEFT>The scale on the X-axis of the rasters in this column.  Value is extracted from the ST_ScaleX constraint on this raster column.  <span style="font-style: italic;">The constraint is typically named enforce_scalex_&lt;r_raster_column&gt;.</span></TD>
    3887                 </TR>
    3888                 <TR>
    3889                         <TD ALIGN=LEFT>scale_y</TD>
    3890                         <TD ALIGN=LEFT>double precision</TD>
    3891                         <TD ALIGN=LEFT>The scale on the Y-axis of the rasters in this column.  Value is extracted from the ST_ScaleY constraint on this raster column.  <span style="font-style: italic;">The constraint is typically named enforce_scaley_&lt;r_raster_column&gt;.</span></TD>
    3892                 </TR>
    3893                 <TR>
    3894                         <TD ALIGN=LEFT>blocksize_x</TD>
    3895                         <TD ALIGN=LEFT>integer</TD>
    3896                         <TD ALIGN=LEFT>The width of the rasters in this column.  Value is extracted from the ST_Width constraint on this raster column.  <span style="font-style: italic;">The constraint is typically named enforce_width_&lt;r_raster_column&gt;.</span></TD>
    3897                 </TR>
    3898                 <TR>
    3899                         <TD ALIGN=LEFT>blocksize_y</TD>
    3900                         <TD ALIGN=LEFT>integer</TD>
    3901                         <TD ALIGN=LEFT>The height of the rasters in this column.  Value is extracted from the ST_Height constraint on this raster column.  <span style="font-style: italic;">The constraint is typically named enforce_height_&lt;r_raster_column&gt;.</span></TD>
    3902                 </TR>
    3903                 <TR>
    3904                         <TD ALIGN=LEFT>same_alignment</TD>
    3905                         <TD ALIGN=LEFT>boolean</TD>
    3906                         <TD ALIGN=LEFT>If TRUE, all rasters in this column are aligned.  State is extracted from the ST_SameAlignment constraint comparing the rasters in this column against coordinates known to be on the grid.  <span style="font-style: italic;">The constraint is typically named enforce_same_alignment_&lt;r_raster_column&gt;.</span></TD>
    3907                 </TR>
    3908                 <TR>
    3909                         <TD ALIGN=LEFT>regular_blocking</TD>
    3910                         <TD ALIGN=LEFT>boolean</TD>
    3911                         <TD ALIGN=LEFT>If TRUE, all rasters in this column are regularly blocked.  <span style="color: red; font-weight: bold;">Though this column is technically based upon a constraint, the constraint is purely information and does NO actual constraining.  Therefore if this column is TRUE, a user explicitly specified that the rasters in this column are regularly blocked.</span>  <span style="font-style: italic;">The constraint is typically named enforce_regular_blocking_&lt;r_raster_column&gt;.</span></TD>
    3912                 </TR>
    3913                 <TR>
    3914                         <TD ALIGN=LEFT>num_bands</TD>
    3915                         <TD ALIGN=LEFT>integer</TD>
    3916                         <TD ALIGN=LEFT>The number bands within each raster of this column.  Value is extracted from the ST_NumBands constraint on this raster column.  <span style="font-style: italic;">The constraint is typically named enforce_num_bands_&lt; r_raster_column&gt;.</span></TD>
    3917                 </TR>
    3918                 <TR>
    3919                         <TD ALIGN=LEFT>pixel_types</TD>
    3920                         <TD ALIGN=LEFT>text[]</TD>
    3921                         <TD ALIGN=LEFT>Text array of the pixel types of the bands within each raster of this column.  Value is extracted from a special function that creates an array of the band pixel types of the rasters in this column.  <span style="font-style: italic;">The constraint is typically named enforce_pixel_types_&lt;r_raster_column&gt;.</span></TD>
    3922                 </TR>
    3923                 <TR>
    3924                         <TD ALIGN=LEFT>nodata_values</TD>
    3925                         <TD ALIGN=LEFT>double precision[]</TD>
    3926                         <TD ALIGN=LEFT>Double precision array of the NODATA values of the bands within each raster of this column.  Value is extracted from a special function that creates an array of the band NODATA values of the rasters in this column.  <span style="font-style: italic;">The constraint is typically named enforce_nodata_values_&lt;r_raster_column&gt;.</span></TD>
    3927                 </TR>
    3928                 <TR>
    3929                         <TD ALIGN=LEFT>out_db</TD>
    3930                         <TD ALIGN=LEFT>boolean[]</TD>
    3931                         <TD ALIGN=LEFT>Boolean array of the out-of-database flag of the bands within each raster of this column.  Value is extracted from a special function that creates an array of the band out-of-database flags of the rasters in this column.  <span style="font-style: italic;">The constraint is typically named enforce_out_db_&lt;r_raster_column&gt;.</span></TD>
    3932                 </TR>
    3933                 <TR>
    3934                         <TD ALIGN=LEFT>extent</TD>
    3935                         <TD ALIGN=LEFT>geometry</TD>
    3936                         <TD ALIGN=LEFT>The maximum extent within which all rasters of this column must be covered by.  The maximum extent is computed by ST_ConvexHull(ST_Collect(ST_ConvexHull(raster))) of all rasters in this column at the time that the extent constraint was added..  Value is extracted from the ST_CoveredBy(ST_ConvexHull(raster)) constraint on this raster column.  <span style="font-style: italic;">The constraint is typically named enforce_max_extent_&lt;r_raster_column&gt;.</span></TD>
    3937                 </TR>
    3938         </TBODY>
    3939 </TABLE>
    3940 
    3941 }}}
    3942 
    3943 Raster constraints can be set using one of the !AddRasterConstraints functions.
    3944 
    3945 1. !AddRasterConstraints (
    3946         rastschema name,[[BR]]
    3947         rasttable name,[[BR]]
    3948         rastcolumn name,[[BR]]
    3949         VARIADIC constraints text[]
    3950 )
    3951 
    3952         constraints: keywords indicating constraint to attempt to add.  possible keywords are:
    3953 
    3954 {{{
    3955 srid
    3956 scale_x or scalex
    3957 scale_y or scaley
    3958 scale
    3959 blocksize_x or blocksizex or width
    3960 blocksize_y or blocksizey or height
    3961 blocksize
    3962 same_alignment or samealignment or alignment
    3963 regular_blocking or regularblocking
    3964 num_bands or numbands
    3965 pixel_types or pixeltypes
    3966 nodata_values or nodatavalues or nodata
    3967 out_db or outdb
    3968 extent
    3969 }}}
    3970 
    3971 {{{
    3972 SELECT AddRasterConstraints('schema', 'table', 'rast', 'srid', 'extent')
    3973 }}}
    3974 
    3975 2. !AddRasterConstraints (
    3976         rasttable name,[[BR]]
    3977         rastcolumn name,[[BR]]
    3978         VARIADIC constraints text[]
    3979 )
    3980 
    3981 3. !AddRasterConstraints (
    3982         rastschema name,[[BR]]
    3983         rasttable name,[[BR]]
    3984         rastcolumn name,[[BR]]
    3985         srid boolean DEFAULT TRUE,[[BR]]
    3986         scale_x boolean DEFAULT TRUE,[[BR]]
    3987         scale_y boolean DEFAULT TRUE,[[BR]]
    3988         blocksize_x boolean DEFAULT TRUE,[[BR]]
    3989         blocksize_y boolean DEFAULT TRUE,[[BR]]
    3990         same_alignment boolean DEFAULT TRUE,[[BR]]
    3991         regular_blocking boolean DEFAULT FALSE,[[BR]]
    3992         num_bands boolean DEFAULT TRUE,[[BR]]
    3993         pixel_types boolean DEFAULT TRUE,[[BR]]
    3994         nodata_values boolean DEFAULT TRUE,[[BR]]
    3995         out_db boolean DEFAULT TRUE,[[BR]]
    3996         extent boolean DEFAULT TRUE
    3997 )
    3998 
    3999 4. !AddRasterConstraints (
    4000         rasttable name,[[BR]]
    4001         rastcolumn name,[[BR]]
    4002         srid boolean DEFAULT TRUE,[[BR]]
    4003         scale_x boolean DEFAULT TRUE,[[BR]]
    4004         scale_y boolean DEFAULT TRUE,[[BR]]
    4005         blocksize_x boolean DEFAULT TRUE,[[BR]]
    4006         blocksize_y boolean DEFAULT TRUE,[[BR]]
    4007         same_alignment boolean DEFAULT TRUE,[[BR]]
    4008         regular_blocking boolean DEFAULT FALSE,[[BR]]
    4009         num_bands boolean DEFAULT TRUE,[[BR]]
    4010         pixel_types boolean DEFAULT TRUE,[[BR]]
    4011         nodata_values boolean DEFAULT TRUE,[[BR]]
    4012         out_db boolean DEFAULT TRUE,[[BR]]
    4013         extent boolean DEFAULT TRUE
    4014 )
    4015 
    4016 Though users can manually remove raster constraints, it is easier to use one of the !DropRasterConstraints functions
    4017 
    4018 1. !DropRasterConstraints (
    4019         rastschema name,[[BR]]
    4020         rasttable name,[[BR]]
    4021         rastcolumn name,[[BR]]
    4022         VARIADIC constraints text[]
    4023 )
    4024 
    4025         constraints: the same keywords as function !#1 for !AddRasterConstraints
    4026 
    4027 2. !DropRasterConstraints (
    4028         rasttable name,[[BR]]
    4029         rastcolumn name,[[BR]]
    4030         VARIADIC constraints text[]
    4031 )
    4032 
    4033 3. !DropRasterConstraints (
    4034         rastschema name,[[BR]]
    4035         rasttable name,[[BR]]
    4036         rastcolumn name,[[BR]]
    4037         srid boolean DEFAULT TRUE,[[BR]]
    4038         scale_x boolean DEFAULT TRUE,[[BR]]
    4039         scale_y boolean DEFAULT TRUE,[[BR]]
    4040         blocksize_x boolean DEFAULT TRUE,[[BR]]
    4041         blocksize_y boolean DEFAULT TRUE,[[BR]]
    4042         same_alignment boolean DEFAULT TRUE,[[BR]]
    4043         regular_blocking boolean DEFAULT TRUE,[[BR]]
    4044         num_bands boolean DEFAULT TRUE,[[BR]]
    4045         pixel_types boolean DEFAULT TRUE,[[BR]]
    4046         nodata_values boolean DEFAULT TRUE,[[BR]]
    4047         out_db boolean DEFAULT TRUE,[[BR]]
    4048         extent boolean DEFAULT TRUE
    4049 )
    4050 
    4051 4. !DropRasterConstraints (
    4052         rasttable name,[[BR]]
    4053         rastcolumn name,[[BR]]
    4054         srid boolean DEFAULT TRUE,[[BR]]
    4055         scale_x boolean DEFAULT TRUE,[[BR]]
    4056         scale_y boolean DEFAULT TRUE,[[BR]]
    4057         blocksize_x boolean DEFAULT TRUE,[[BR]]
    4058         blocksize_y boolean DEFAULT TRUE,[[BR]]
    4059         same_alignment boolean DEFAULT TRUE,[[BR]]
    4060         regular_blocking boolean DEFAULT TRUE,[[BR]]
    4061         num_bands boolean DEFAULT TRUE,[[BR]]
    4062         pixel_types boolean DEFAULT TRUE,[[BR]]
    4063         nodata_values boolean DEFAULT TRUE,[[BR]]
    4064         out_db boolean DEFAULT TRUE,[[BR]]
    4065         extent boolean DEFAULT TRUE
    4066 )
    4067 
    4068 
    4069 
    4070 The raster_overviews view is structured as the following:
    4071 
    4072 {{{
    4073 #!html
    4074 <TABLE BORDER=1>
    4075         <THEAD>
    4076                 <TR>
    4077                         <TD ALIGN=LEFT style="font-weight: bold; font-style: italic;">Column</TD>
    4078                         <TD ALIGN=LEFT style="font-weight: bold; font-style: italic;">PostgreSQL Type</TD>
    4079                         <TD ALIGN=LEFT style="font-weight: bold; font-style: italic;">Description</TD>
    4080                 </TR>
    4081         </THEAD>
    4082         <TBODY>
    4083                 <TR>
    4084                         <TD ALIGN=LEFT>o_table_catalog</TD>
    4085                         <TD ALIGN=LEFT>name</TD>
    4086                         <TD ALIGN=LEFT>Name of the database containing the table with the overview column.</TD>
    4087                 </TR>
    4088                 <TR>
    4089                         <TD ALIGN=LEFT>o_table_schema</TD>
    4090                         <TD ALIGN=LEFT>name</TD>
    4091                         <TD ALIGN=LEFT>Name of the schema containing the table with the overview column.</TD>
    4092                 </TR>
    4093                 <TR>
    4094                         <TD ALIGN=LEFT>o_table_name</TD>
    4095                         <TD ALIGN=LEFT>name</TD>
    4096                         <TD ALIGN=LEFT>Name of the table containing a column of type raster considered an overview.</TD>
    4097                 </TR>
    4098                 <TR>
    4099                         <TD ALIGN=LEFT>o_raster_column</TD>
    4100                         <TD ALIGN=LEFT>name</TD>
    4101                         <TD ALIGN=LEFT>Name of the overview column in the table. All attribute columns following this column apply to the rasters in this column.  <span style="font-weight: bold; font-style: italic;">This column was previously known as o_column.</span></TD>
    4102                 </TR>
    4103                 <TR>
    4104                         <TD ALIGN=LEFT>r_table_catalog</TD>
    4105                         <TD ALIGN=LEFT>name</TD>
    4106                         <TD ALIGN=LEFT>Name of the database containing the table with the raster column from which this overview column was derived.</TD>
    4107                 </TR>
    4108                 <TR>
    4109                         <TD ALIGN=LEFT>r_table_schema</TD>
    4110                         <TD ALIGN=LEFT>name</TD>
    4111                         <TD ALIGN=LEFT>Name of the schema containing the table with the raster column from which this overview column was derived.</TD>
    4112                 </TR>
    4113                 <TR>
    4114                         <TD ALIGN=LEFT>r_table_name</TD>
    4115                         <TD ALIGN=LEFT>name</TD>
    4116                         <TD ALIGN=LEFT>Name of the table containing a column of type raster from which this overview column was derived.</TD>
    4117                 </TR>
    4118                 <TR>
    4119                         <TD ALIGN=LEFT>r_raster_column</TD>
    4120                         <TD ALIGN=LEFT>name</TD>
    4121                         <TD ALIGN=LEFT>Name of the raster column in the table from which this overview column was derived.  <span style="font-weight: bold; font-style: italic;">This column was previously known as r_column.</span></TD>
    4122                 </TR>
    4123                 <TR>
    4124                         <TD ALIGN=LEFT>overview_factor</TD>
    4125                         <TD ALIGN=LEFT>integer</TD>
    4126                         <TD ALIGN=LEFT>The factor used to compute the overviews in this column.  If the factor is 2, the overviews' scale is 2x of the reference rasters.  </TD>
    4127                 </TR>
    4128         </TBODY>
    4129 </TABLE>
    4130 }}}
    4131 
    4132 '''One column has been removed from the raster_overviews views: out_db.'''
    4133 
    4134 As all overviews are rasters, overviews will have have a record in raster_columns and another record in raster_overviews.
    4135 
    4136 The distinguishing factor that makes a raster be treated as an overview is the ''enforce_overview_<o_raster_column>'' constraint.  The constraint can be applied using one of the !AddOverviewConstraints functions.  '''The overview constraint checks that the reference raster column and table exists.  If the reference raster column does not exist, the constraint is violated.'''
    4137 
    4138 1. !AddOverviewConstraints (
    4139         ovschema name, ovtable name, ovcolumn name,[[BR]]
    4140         refschema name, reftable name, refcolumn name,[[BR]]
    4141         ovfactor int
    4142 )
    4143 
    4144 2. !AddOverviewConstraints (
    4145         ovtable name, ovcolumn name,[[BR]]
    4146         reftable name, refcolumn name,[[BR]]
    4147         ovfactor int
    4148 )
    4149 
    4150 To drop an overview constraint, use !DropOverviewConstraints.
    4151 
    4152 1. !DropOverviewConstraints (
    4153         ovschema name,[[BR]]
    4154         ovtable name,[[BR]]
    4155         ovcolumn name
    4156 )
    4157 
    4158 2. !DropOverviewConstraints (
    4159         ovtable name,[[BR]]
    4160         ovcolumn name
    4161 )
    4162 
    4163 
    4164 ~~Following the conversion of geometry_columns to a view in PostGIS, it is planned to do the same with raster_column and raster_overview for the raster part. Converting to a view has a number of advantages:
    4165 
    4166 ~~ * The raster_column view rows will always be in synch with the existing list raster columns.
    4167 
    4168 ~~ * All the information will be trustworthy because it will be enforced by constraints, so people can't change the band types etc... without changing the constraints. The raster_columns information will always be correct or null (if the constraint can not be applied).
    4169 
    4170 ~~ * Idem for the raster_overview table.
    4171 
    4172 ~~PostGIS is using the typmod feature of PostgreSQL to 'store' metadata about a table and then display them in the geometry_column view. Unfortunately the typmod is limited to XXX bytes and there is too much information to 'store' about a raster table to use typmod. PostGIS raster will therefore use another approach consisting of 'storing' metadata about a raster table as constraints on the table. A set of constraints will be applied to a raster table and those constraints will be read and displayed by the raster_column view.
    4173 
    4174 ~~The '''major changes concerning the raster_column table''' are as follow:
    4175 
    4176 ~~ * There will be a flexible !AddRasterConstraint() function trying to add a set of constraint on a table. As for any constraint, each constraint will be successfully applied only when all the rows of the table fulfill this constraint. The list of potential constraint applied by the C loader (using the !AddRasterConstraint function) and hence the list of column available in the raster_column view will be: srid int, samealignment boolean, scalex float8, scaley float8, width int, height int, numberofbands int, pixeltypes text[], nodatavalues float[] in addition to the 'r_table_catalog', 'r_table_schema', 'r_table_name' and 'r_column' columns. There is still discussion about if global extent as a geometry should be added to this list. Comments are welcome.
    4177 
    4178 ~~ * The raster_column view rows will be determined by querying the PostgreSQL catalog. A typical query exists for this.
    4179 
    4180 ~~ * The raster_column view will derive columns of metadata from the constraints applied to each table listed. When a constraint does not exist (because it could not be applied successfully to the table by !AddRasterconstraint()), the column corresponding to this constraint for this table will be null.
    4181 
    4182 ~~The '''major changes concerning the raster_overview table''' are as follow:
    4183 
    4184 ~~ * The raster_overview table will also be replaced by a raster_overview view.
    4185 
    4186 ~~ * We will provide a new function to support the creation of the raster_overview view and application of three additional constraints specific to overviews (!AddOverviewConstraints). These three additional constraints are: 'reference raster table oid', 'overview factor' and 'global extent'. Those constraints will be displayed by the raster_overview view as 'r_table_catalog', 'r_table_schema', 'r_table_name', 'r_column', 'overview_factor' and 'global_extent' in addition to the 'o_table_catalog', 'o_table_schema', 'o_table_name' and 'o_column' columns.
    4187 
    4188 ~~ * We will also provide a SQL function for creating overviews (ST_CreateOverview(schemaname text, tablename teat, columnname, factor int)).
    4189 
    4190 ~~ * The raster_overview view will NOT be created by default by the rtpostgis.sql script. It will be the responsibility of applications to use the !AddOverviewConstraints and ST_CreateOverview() function to create the raster_overviews view and create overviews.
    4191 
    4192 ~~The '''major changes concerning the raster2pgsql loader''' are as follow:
    4193 
    4194 ~~ * We are working on a new C importer to get rid of any Python, Numpy and Python GDAL Binding dependency which is a major obstacle to successful installation of PostGIS 2.0.
    4195 
    4196 ~~ * This new loader will attempt to add the constraints on the loaded table using the !AddRasterConstraint() function. There will be an option to NOT try to add the constraint for applications preferring to have no constraint applied to the tables over having a raster_column view filled with the proper information.
    4197 
    4198 ~~ * This new importer will NOT have any option to create the raster_overview view, nor to create overview tables as we leave this to the application. We also think that it is more important to be able to create overviews in SQL as a post loading process query so overviews can be updated/recreated when rasters tiles are edited, added or deleted. Application wishing to create overview can implement their own overview creation code or just use the future ST_CreateOverview() SQL function.
    4199 
    4200 ~~ * We will stop supporting the old raster2pgsql.py which was creating overview tables and filling the raster_overview table. It will be available in the script/python folder for people wishing to create overviews after a slight addaptation (to the new !AddRasterConstraints() and !AddOverviewConstraints() functions).