2017-03-25 3 views
1

Postgis를 사용하여 공간 통계를 수행하려고합니다. 가끔은 ST_Clip을 사용하여 쿼리를 중단하고 쿼리를 중단합니다. 폴리곤이 래스터와 거의 교차하지 않을 때 이것이 발생한다고 생각합니다. 아래 샘플을 참조하십시오.지오메트리가 거의 교차하지 않으면 래스터 ST_Clip이 실패합니다.

SELECT ST_Summary(
     ST_Clip(
       ST_AddBand(
         ST_MakeEmptyRaster(16, 16, 0, 0, 1, 1, 0, 0), 
         ARRAY[ 
         ROW(1, '8BUI'::text, 0, 255), 
         ROW(2, '8BUI'::text, 0, 255), 
         ROW(3, '8BUI'::text, 0, 255) 
         ]::addbandarg[] 
       ) 
       -- this works 
       --, ST_GeomFromText('POLYGON((15.999999 15.999999, 15.999999 17, 17 17, 17 15.999999, 15.999999 15.999999))') 
       -- this fails 
       , ST_GeomFromText('POLYGON((15.9999999 15.9999999, 15.9999999 17, 17 17, 17 15.9999999, 15.9999999 15.9999999))') 
     ) 
); 

위의 쿼리에서 다음 오류가 발생합니다.

psql:demo_clip_fail_barelyintersects.sql:16: ERROR: RASTER_clip: Could not get band from working raster 
CONTEXT: PL/pgSQL function st_clip(raster,integer[],geometry,double precision[],boolean) line 8 at RETURN 

대신 레코드가 반환되거나 비어있는 래스터가 표시되기를 바라고 있습니다. 제작 코드에서 기하학/래스터 쌍은 다각형과 래스터 사이에 ST_Intersects(r.rast, p.geom)에 의해 발견되었습니다. 한 가지 방법은 내가 래스터의 범위보다 약간 작은 래스터에 대한 경계 상자 만드는 방법에 대한 생각,하지만이

는 는

포스트 그레스와 PostGIS와의 나의 버전은

  • PostgreSQL은에 9.6.1 ... 아주 못생긴 gcc (GCC)에 의해 컴파일 된 x86_64-pc-linux-gnu 4.9.1, 64 비트
  • POSTGIS = "2.3.1 r15264"GEOS = "3.6.0-CAPI-1.10.0 r0"PROJ = " 만족도 2016 년 8 월 15 일 4.9.3 GDAL 2.1.2, 20 16/10/24 배포 됨 "LIBXML ="2.9.4 "LIBJSON ="0.12.1 "RASTER

감사!

답변

1

제 임시 해결책은 begin/exception/end 블록으로 랩하고 예외 부분이 빈 래스터를 반환하도록합니다. 성능이 저하됩니다 (~ 두 번). 위음성은 만들지 만 찾으려면 무엇을해야할지 확실하지 않습니다 ...

-- function to work around bug in st_clip (fails when polygon barely intersects with raster) 
-- not sure how much damage this has on performance 
create or replace function st_clip_fuzzy(
     rast raster, nband integer[], 
     geom geometry, 
     nodataval double precision[] DEFAULT NULL, crop boolean DEFAULT TRUE 
) 
     returns raster 
     as $$ 
     declare 
     rec record; 
     g geometry; 
     begin 
       return st_clip($1, $2, $3, $4, $5); 
     exception 
     when others then 
       select st_intersection(st_envelope(rast), geom) into g; 
       raise warning 'st_clip_fuzzy: intersection %', st_astext(g); 
       raise warning 'st_clip_fuzzy: area intersection %', st_area(g); 
       raise warning 'st_clip_fuzzy: area pixel %', abs(ST_ScaleX(rast) * ST_ScaleY(rast)); 
       raise warning 'st_clip_fuzzy: area ratio %', st_area(g)/abs(ST_ScaleX(rast) * ST_ScaleY(rast)); 

       return ST_MakeEmptyRaster(0, 0, ST_UpperLeftX(rast), ST_UpperLeftY(rast), ST_ScaleX(rast), ST_ScaleY(rast), ST_SkewX(rast), ST_SkewY(rast), ST_SRID(rast)); 
     end; 
     $$ language 'plpgsql' immutable; 

CREATE OR REPLACE FUNCTION st_clip_fuzzy(
     rast raster, nband integer, 
     geom geometry, 
     nodataval double precision, crop boolean DEFAULT TRUE 
) 
-- four more interfaces with different set of arguments