Monday 24 April 2017

List of vector to one raster in PostGIS


I need to rasterize a part of OSM map by coordinates. I found rather good algorithm, but can’t finish it. I have problems in STEP 3.



I described all algorithm here – may be someone have the same task.


STEP 1: Create a polygon (box polygon) with cover of wanted part of map.


Function (or query):


INSERT INTO rast.box (box_geom) 
VALUES (ST_GeomFromText('POLYGON ((30.200 59.920, 30.200 59.960, 30.260 59.960, 30.260 59.920, 30.200 59.920))',4326));

Order of creating points (corners coordinates): lowLeft->upperLeft->upperRight->lowRight->lowLeft. 5 points.


STEP 2: Use ST_Overlaps and ST_Within to choose geometry objects inside box polygon. In my example - polygons of buildings. My Function:


INSERT INTO rast.over
SELECT g.geom

FROM rast.spb g, rast.box b
WHERE ST_Overlaps (g.geom, b. box_geom)=true
OR
ST_Within (g.geom, b. box_geom)=true
AND
b.id=1;

b.id is id of box polygon.


STEP 3: Use ST_AsRaster and ST_Union for rasterization all of objects and union in one. I found posts about this theme (http://geospatialelucubrations.blogspot.ca/2014/05/a-guide-to-rasterization-of-vector.html, and this one is good too: postgis.17.x6.nabble.com/Rasterize-a-vector-td4997893, sorry I can post only 2 links), but I don’t understand some things:


Substep 3.1: In first guide, the author used two tables – first named “forestcover”, second named “elevation”, but I have only one table with OSM geometries. When he used ST_AsRaster, he used the second parameter: raster ref (from http://postgis.net/docs/RT_ST_AsRaster.html). Could you explain how can I use it?



Substep 3.2: My function:


CREATE TABLE rast.result  AS
SELECT ST_AsRaster(q, 100, 100, '4BUI')
FROM rast.over;

When I’m trying only to rasterize (without union) – it’s working. The table with special column is created. And I also find the result table with rows (I have 1700 geometry objects and I have 1700 rows with raster type ). But the rows are empty! Why? I try to look from pgAdmin3. And when I try to see this raster table in QGIS2.4 (through DB Manager), I have this Error:



“Error, the table rast.airports contains tiles with different srid. This feature is not yet supported by the PostGIS Raster driver. Please, specify a table that contains only tiles with the same srid or provide a 'where' constraint to select just the tiles with the same value for srid”



I checked table in ST_SRID and all of rows are in the same srid (in 4326).



Substep 3.3: When I try to union the raster from the result table or from this function:


CREATE TABLE rast.result AS
SELECT
ST_Union(ST_AsRaster(q, 100, 100, '4BUI'))
FROM rast.over;

I have this Error in pgAdmin sql-console:



rt_raster_from_two_rasters: The two rasters provided do not have the same alignment




May be the problem is with “alignment”? How can I fix it?


STEP 4: My last step should be to convert postgis raster to GEOtiff. And I’m going to use ST_AsTiff.


Please, help to solve these problems!




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...