Changes between Version 3 and Version 4 of WKTRasterTutorial01
- Timestamp:
- 06/10/10 13:39:23 (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
WKTRasterTutorial01
v3 v4 1 = WKT Raster Tutorial 1 = 2 1 3 = Intersecting vector buffers with large raster coverage using PostGIS WKT Raster = 2 4 … … 29 31 30 32 {{{ 31 33 >"c:/Program Files/PostgreSQL/8.X/bin/shp2pgsql" 32 34 }}} 33 35 34 36 shp2pgsql works in two steps: The first step is to create a .sql file that you then have to execute using psql, an application which comes with PostgreSQL. To convert the caribou point shapefile in cariboupoint.sql, we do the following: 35 37 36 {{{ >shp2pgsql.exe -s 32198 -I C:\Temp\Pierre\Data\CaribouPoints\cariboupoints.shp > C:\Temp\37 Pierre\Data\CaribouPoints\cariboupoints.sql38 {{{ 39 >shp2pgsql.exe -s 32198 -I C:\Temp\cariboupoints.shp > C:\Temp\cariboupoints.sql 38 40 }}} 39 41 … … 47 49 48 50 {{{ 49 >psql -f C:\Temp\Pierre\Data\CaribouPoints\cariboupoints.sql demodb51 >psql -f C:\Temp\cariboupoints.sql demodb 50 52 }}} 51 53 … … 63 65 64 66 {{{ 65 66 67 SELECT id, ST_AsBinary(the_geom) 68 FROM cariboupoints; 67 69 }}} 68 70 … … 76 78 77 79 {{{ 78 80 >gdal2wktraster.py -h 79 81 }}} 80 82 … … 84 86 85 87 {{{ 86 88 >gdal2wktraster.py -r C:\Temp\Pierre\Data\SRTM\tif\*.tif -t srtm_tiled -s 4326 -k 100x100 -I > C:\Temp\Pierre\Data\SRTM\srtm.sql 87 89 }}} 88 90 … … 98 100 99 101 {{{ 100 102 CREATE INDEX srtm_tiled_gist_idx ON srtm_tiled USING GIST (ST_ConvexHull(rast)); 101 103 }}} 102 104 … … 106 108 107 109 {{{ 108 110 >psql -f C:\Temp\Pierre\Data\SRTM\srtm.sql demodb 109 111 }}} 110 112 … … 114 116 115 117 {{{ 116 118 SELECT rid, ST_AsBinary(rast::geometry) FROM srtm_tiled; 117 119 }}} 118 120 … … 120 122 121 123 {{{ 122 124 SELECT ST_AsBinary(ST_Buffer(ST_Union(rast::geometry), 0.000001)) FROM srtm_tiled; 123 125 }}} 124 126 … … 128 130 129 131 {{{ 130 131 132 132 SELECT ST_AsBinary((ST_DumpAsPolygons(rast)).geom), (ST_DumpAsPolygons(rast)).val 133 FROM srtm_tiled 134 WHERE rid=34; 133 135 }}} 134 136 … … 138 140 139 141 {{{ 140 141 142 142 SELECT ST_BandHasNodataValue(rast), ST_BandNodataValue(rast) 143 FROM srtm_tiled 144 LIMIT 1; 143 145 }}} 144 146 … … 146 148 147 149 {{{ 148 SELECT ST_AsBinary((ST_DumpAsPolygons(ST_SetBandHasNodataValue(rast, FALSE) )).geom), (ST_DumpAsPolygons(ST_SetBandHasNodataValue(rast, FALSE))).val 149 FROM srtm_tiled 150 WHERE rid=34; 150 SELECT ST_AsBinary((ST_DumpAsPolygons(ST_SetBandHasNodataValue(rast, FALSE))).geom), 151 (ST_DumpAsPolygons(ST_SetBandHasNodataValue(rast, FALSE))).val 152 FROM srtm_tiled 153 WHERE rid=34; 151 154 }}} 152 155 … … 160 163 $$ 161 164 DECLARE 162 163 164 165 166 167 168 169 170 171 172 165 pt geometry; 166 xmin float8; 167 xmax float8; 168 ymin float8; 169 ymax float8; 170 xrange float8; 171 yrange float8; 172 srid int; 173 count integer := 0; 174 bcontains boolean := FALSE; 175 gtype text; 173 176 BEGIN 174 177 SELECT ST_GeometryType(geom) … … 177 180 RAISE EXCEPTION 'Attempting to get random point in a non polygon geometry'; 178 181 END IF; 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 182 SELECT ST_XMin(geom), ST_XMax(geom), ST_YMin(geom), ST_YMax(geom), ST_SRID(geom) 183 INTO xmin, xmax, ymin, ymax, srid; 184 SELECT xmax - xmin, ymax - ymin 185 INTO xrange, yrange; 186 187 WHILE count < nb LOOP 188 SELECT ST_SetSRID(ST_MakePoint(xmin + xrange * random(), ymin + yrange * random()), srid) 189 INTO pt; 190 SELECT ST_Contains(geom, pt) 191 INTO bcontains; 192 IF bcontains THEN 193 count := count + 1; 194 RETURN NEXT pt; 195 END IF; 196 END LOOP; 197 RETURN; 198 END; 196 199 $$ 197 200 LANGUAGE 'plpgsql' IMMUTABLE STRICT; … … 201 204 202 205 {{{ 203 CREATE TABLE cariboupoints AS 204 SELECT generate_series(1,1000) id, 205 ST_RandomPoint(the_geom, 1000) the_geom 206 FROM (SELECT ST_Transform(ST_Extent(rast::geometry), 32198) FROM srtm_tiled) foo; 206 CREATE TABLE cariboupoints AS 207 SELECT generate_series(1,1000) id, 208 ST_RandomPoint(the_geom, 1000) the_geom 209 FROM (SELECT ST_Transform(ST_Extent(rast::geometry), 32198) 210 FROM srtm_tiled 211 ) foo; 207 212 }}} 208 213 … … 210 215 211 216 {{{ 212 213 217 SELECT id, ST_AsBinary(the_geom) 218 FROM cariboupoints; 214 219 }}} 215 220 … … 221 226 222 227 {{{ 223 224 225 228 CREATE TABLE cariboupoint_buffers AS 229 SELECT id, ST_Buffer(the_geom, 1000) the_geom 230 FROM cariboupoints; 226 231 }}} 227 232 … … 229 234 230 235 {{{ 231 232 236 SELECT id, ST_AsBinary(the_geom) 237 FROM cariboupoint_buffers; 233 238 }}} 234 239 … … 236 241 237 242 {{{ 238 239 240 243 CREATE TABLE cariboupoint_buffers_wgs AS 244 SELECT id, ST_Transform(the_geom, 4326) the_geom 245 FROM cariboupoint_buffers; 241 246 }}} 242 247 … … 244 249 245 250 {{{ 246 247 251 SELECT id, ST_AsBinary(the_geom) 252 FROM cariboupoint_buffers_wgs; 248 253 }}} 249 254 … … 251 256 252 257 {{{ 253 254 255 258 CREATE TABLE cariboupoint_buffers_wgs AS 259 SELECT id, ST_Transform(ST_Buffer(the_geom, 1000), 4326) the_geom 260 FROM cariboupoints; 256 261 }}} 257 262 … … 259 264 260 265 {{{ 261 266 CREATE INDEX cariboupoint_buffers_wgs_geom_idx ON cariboupoint_buffers_wgs USING GIST (the_geom); 262 267 }}} 263 268 … … 267 272 268 273 {{{ 269 270 271 272 273 274 275 274 CREATE TABLE caribou_srtm_inter AS 275 SELECT id, 276 (st_intersection(rast, the_geom)).geom the_geom, 277 (st_intersection(rast, the_geom)).val 278 FROM cariboupoint_buffers_wgs, 279 srtm_tiled 280 WHERE st_intersects(rast, the_geom); 276 281 }}} 277 282 … … 279 284 280 285 {{{ 281 282 283 284 285 286 287 288 289 290 286 CREATE TABLE caribou_srtm_inter AS 287 SELECT id, 288 (gv).geom the_geom, 289 (gv).val 290 FROM (SELECT id, 291 st_intersection(rast, the_geom) gv 292 FROM srtm_tiled, 293 cariboupoint_buffers_wgs 294 WHERE st_intersects(rast, the_geom) 295 ) foo; 291 296 }}} 292 297 … … 296 301 297 302 {{{ 298 299 303 SELECT id, val, ST_AsBinary(the_geom) 304 FROM caribou_srtm_inter; 300 305 }}} 301 306 … … 308 313 309 314 {{{ 310 311 315 CREATE TABLE result01 AS 316 SELECT id, 312 317 sum(st_area(ST_Transform(the_geom, 32198)) * val) / sum(st_area(ST_Transform(the_geom, 32198))) as meanelev 313 314 315 316 }}} 318 FROM caribou_srtm_inter1 319 GROUP BY id 320 ORDER BY id; 321 }}}