Changes between Version 61 and Version 62 of PostGIS_Raster_SoC_Idea_2012/Distance_Analysis_Tools


Ignore:
Timestamp:
07/06/12 22:47:01 (13 years ago)
Author:
qliu
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PostGIS_Raster_SoC_Idea_2012/Distance_Analysis_Tools

    v61 v62  
    4949<LI><A href=http://trac.osgeo.org/postgis/wiki/PostGIS_Raster_SoC_Idea_2012/Distance_Analysis_Tools#Week5Report>Week 5 Report (June 18th - June 23th)</A></LI>
    5050<LI><A href=http://trac.osgeo.org/postgis/wiki/PostGIS_Raster_SoC_Idea_2012/Distance_Analysis_Tools#Week6Report>Week 6 Report (June 25th - June 29th)</A></LI>
     51<LI><A href=http://trac.osgeo.org/postgis/wiki/PostGIS_Raster_SoC_Idea_2012/Distance_Analysis_Tools#Week6Report>Week 7 Report (July 2nd - July 6th)</A></LI>
    5152</UL>
    5253<br>
     
    692693Document is here:
    693694[https://docs.google.com/document/d/1GAnvNDHhwUKoCOeY5GIfnAaTqya9I_HOEdotQleExCY/edit]
     695
     696----
     697{{{
     698#!html
     699<div style='float: right; width:100px;'>
     700<a href="#PostGISRasterSoCIdea2012-DistanceAnalysisToolsforPostGISRaster">&nbsp;&nbsp;&nbsp;<img src="https://lh3.googleusercontent.com/-YR7gUJULQrM/T8nEPQ-AtzI/AAAAAAAAA1M/T_6vRjE8E4Q/s40/scroll_to_top_icon_40x40.png"><br>back to top</a>
     701</div>
     702}}}
     703
     704== Week 7 Report ==
     705
     706'''What did you get done this week?'''
     707 * Disscussed with the Mentor, Came up with a final document of "Creating an Euclidean Distance Raster in PostGIS"
     708
     709Creating an Euclidean Distance Raster in PostGIS
     710
     711
     712Objectives
     713
     714We want to develop a raster/vector integrated approach to generate a distance raster coverage (optionnaly a tiled coverage) from a point coverage (could also be lines or polygons coverage) with some raster specifications, representing the euclidean distance to those points (lines or polygons).
     715
     716This approach should be reusable and opening the way to further implementation of interpolation raster, since the distance raster is a specific and simpler case of an interpolation raster, in terms of storing the value associated with the nearest point(s) instead of distance to it.
     717
     718
     719Constraints
     720
     7211.      The source table of geometries (points, line or polygons) can contain one geometry or many (eventually millions). We want the method to scale well whatever the number of source geometry.
     7222.      The desired raster is specified with parameters or by referencing an existing raster.
     7233.      Sometimes the resolution of the desired raster is so high that the whole raster coverage cannot be stored in one PostgreSQL row. The approach must be able to produce a tiled raster coverage stored into many rows.
     7244.      Source geometries might be outside the extent of the desired raster.
     7255.      The approach should work well in a number of situations:
     726a.      small number of sources vs low resolution raster,
     727b.      small number of sources vs high resolution raster
     728c.      large number of sources vs low resolution
     729d.      large number of sources vs high resolution Large raster coverage
     7306.      The user can specify a maximum distance to the source. When the source is too far from the geometry it gets assigned a nodata value.
     7317.      We want the implementation to be generic enough to be reused to implement more general interpolation methods like nearest neighbor, IDW, spline or kriging. Otherwise we want it to be generic enough to be reused to implement more general cost distance methods.
     732
     733Different Envisioned Approaches
     734
     7351.      The raster source approach - The first approach is similar to what is found in most GIS package. The source points (or geometries) are converted to a raster of sources and this raster is passed to the function. The function iterates over the pixels (or over the source points???) assigning the distance to the nearest point (or assigning minimum values to pixels within a circle progressively growing around each point???).
     736
     737Converting one or a set of source geometries to one raster can be performed like this in PostGIS:
     738 
     739raster ST_Union(raster ST_AsRaster(source_geom, list of parameters for raster specifications))
     740
     741The Euclidean distance from each pixel to its nearest source pixel can then be computed using ST_MapAlgebra.
     742
     743                CREATE FUNCTION euclidean_fct(pos1 integer[], pos2 integer[])
     744RETURNS float
     745AS $$
     746BEGIN
     747SELECT ST_Distance(ST_Point(pos1), ST_Point(pos2)) as euclidean_dist;
     748        RETURN euclidean_dist;
     749END;
     750$$
     751LANGUAGE 'plpgsql';
     752
     753CREATE TABLE distcoverage AS
     754SELECT rid, ST_MapAlgebraFct(rast, 'euclidean_fct(position of computed pixel, position of source point pixel)'::regprocedure) rast
     755FROM myexistingcoveragetable
     756       
     757Remarks:
     758○       In case source table containing only one point (constraint 1), converting source point to raster could utilize ST_AsRaster() to create a raster with the point geometry and raster specifications.
     759○       In case source table containing more than one point but the number of source points is small (constraint 5), computing distance could rescan all the source points and assign the shortest distance to the pixel being computed.
     760
     761Pros
     762○       Simplest approach already implemented in other GIS like GRASS.
     763○       
     764Cons:
     765○       Producing an intermediate raster is costly if the requested raster resolution is very high (constraint 3, 5b & 5d).
     766○       ST_Union could be very inefficient at producing the required raster from a large set of geometries and there is no efficient method to produce such a raster right now in PostGIS (constraints 1, 5c & 5d).
     767○       It could be very inefficient to rescan all the source points to find the one nearest to the current pixel (constraint 6, 7, 8 & 12). We still have to see how GRASS does this efficiently.
     768○       Only rasters which extent contains all the source points could be produced.
     769○       This approach does not answer well to the requirement of developing a generic reusable solution for more interpolation needs (constraint 7).
     770
     7712.      The TIN approach - The second approach involve creating a TIN (triangulated irregular network) from the table of source geometries first and then to use this TIN to determine the nearest neighbors for each pixel.
     772
     773The TIN is stored temporarily. A function iterating over each pixel determines in which part of the TIN the current pixel falls and determine the triangle corner nearest to the centroid of the pixel.
     774
     775        s
     776
     777Remarks:
     778•       Nearest point to pixel could be determined by interesting the pixel centroid with the TIN in order to determine the triangle including the centroid. The nearest point is then determined from the three corner of the triangle.
     779•       It could also be determined by producing the TIN with the set of point including all the pixel centroid. Would this fasten the search for the nearest neighbor? This is still to determine.
     780•       Would this work with a table of line or geometries? How do we produce a TIN from a line or polygon coverage?
     781•       PostGIS might not be ready to produce a TIN from a point coverage.
     782
     783        Pros:
     784○       This would work well with a large number of source points (constraints 1, 5c & 5d) since we would not have to  rescan all the source points for each pixel calculation.
     785○       This approach fulfills constraint 7 in that it would allow easy implementation of other interpolation methods.
     786Cons:
     787○       We have no idea for now about the algorithm and performance of building TIN from source geometries in PostGIS if the number of source point is very large (Constraint 7 & 8)
     788○       Might be very inefficient and a waste of computing TIN in case there is only one point in the source table (Constraint 1) or there are very small number of source points and the requested raster is relatively small too (Constraint 5).
     789
     7903.      The KNN index approach – The third approach is similar to the second but use the new KNN indexing facilities of PostGIS to determine the nearest neighbor points of each pixel.
     791
     792       
     793        CREATE FUNCTION euclidean_fct(raster rast, text dbschema, text sourcetable, text sourcegeomcolumn)
     794RETURNS float
     795AS $$
     796BEGIN
     797      SELECT ST_Distance (ST_GeomFromText('POINT(columnx rowy)',ST_SRID(rast)), ST_Transform(sourcegeom, Find_SRID(dbschema,sourcetable,sourcegeomcolumn), ST_SRID(rast)) as eucliean_dist
     798      FROM sourcetable
     799      ORDER BY $1 <-> $2
     800      LIMIT 1;
     801      RETURN euclidean_dist
     802END;
     803$$
     804LANGUAGE 'plpgsql';
     805
     806CREATE TABLE distcoverage AS
     807SELECT rid, ST_MapalgebraFct(rast, 'euclidean_fct(rast, dbschema, sourcetable, sourcegeomcolumn)'::regprocedure) rast
     808FROM myexistingcoveragetable
     809
     810        Pros:
     811○       Source points do not have to be in the raster extent (constraint 4)
     812○       Should work well  with large number of source points (constraint 1, 5c & 5d) since we will be using KNN index to find the nearest point.
     813○       Make it possible to to createa tiled raster coverage aligned to an existing one (constraint 3)
     814○       Opening to be able to reused for further computing interpolation since the method to find neighbors is quite generic.
     815        Cons:
     816○       Might not be as light as Approach 1 in case there is only one point in the source table (Constraint 1) or there are very small number of source points and the requested raster is relatively small too (Constraint 5)
     817○       Have no idea how to deal with Constraint 9.
     818
     819
     820Preferred Approach
     821
     822Approach 3 is the preferred approach for the following reasons:
     823●       it exploits and benefits from a very unique and performant new feature of PostGIS: KNN indexing,
     824●       it is compliant with PostGIS Raster powerful raster/vector interactions
     825●       in terms of scalability, it provides the possibility to generate a distance raster from any coverage of point (however numerous they are) since the time to find the nearest neighbor for each pixel is relatively constant (thanks to the KNN index),
     826●       it makes it possible to work with huge tiled coverage (constraint 3) since the extent of the raster is independent of the source extent,
     827●       it provides a reusable approach for other types of distances (Constraint 13) and interpolation.
     828
     829Tentative functions signatures:
     830
     831Generate a raster having the same alignment as a reference raster:
     832
     833ST_EuclideanDistance(raster ref, text sourceschema, text sourcetable, text sourcegeomcolumn, text pixeltype, double precision value=1, double precision nodataval=0)
     834
     835Generate a raster based on raster specifications:
     836
     837●       Set the dimensions of the raster by providing the parameters of a pixel size (scalex & scaley and skewx & skewy). The width & height of the resulting raster will be adjusted to fit the extent of the geometry
     838
     839ST_EuclideanDistance(text dbschema, text sourcetable, text sourcegeomcolumn, double precision scalex, double precision scaley, double precision gridx, double precision gridy, text pixeltype, double precision value=1, double precision nodataval=0, double precision skewx=0, double precision skewy=0)
     840
     841●       Fix the dimensions of the raster by providing the dimensions of the raster (width & height). The parameters of the pixel size (scalex & scaley and skewx & skewy) of the resulting raster will be adjusted to fit the extent of the source geometry
     842
     843ST_EuclideanDistance(text dbschema, text sourcetable, text sourcegeomcolumn, geometry geom, integer width, integer height, double precision gridx, double precision gridy, text pixeltype, double precision value=1, double precision nodataval=0, double precision skewx=0, double precision skewy=0)
     844
     845 
     846For Constraint 6: (just write down the function that takes a reference raster for example)
     847ST_EuclideanDistance(text dbschema, text sourcetable, text sourcegeomcolumn, raster ref, text pixeltype, double precision value=1, double precision nodataval=0, double precision maxdistance)
     848
     849
     850''' What do you plan on doing next week?'''
     851
     852 * Start to work on implementing a plpgsql prototype of the preferred solution in the document