[gdal-dev] Best way to test wheter point is contained in Polygon

Christopher Barker Chris.Barker at noaa.gov
Thu Sep 25 11:40:30 EDT 2008


Jose Gomez-Dans wrote:
> I have a raster file and a vector file. I want to assign to each pixel 
> in my raster size a value derived from the vector file (which are 
> polygons). To do this, I want to test that the centroid of my pixel lies 
> within a given polygon. In the past, I have done this gdal_rasterize-ing 
> the polygon file, and going through numpy arrays. It is a bit 
> cumbersome, and the rasterization is not really needed with OGR. 

Perhaps cumbersome, but in fact, at some level, you are indeed doing a 
rasterization with the P-in-P testing, as you are working with ALL the 
pixels in your raster. I bet it would be a lot faster to rasterize the 
polygons once, rather than doing point-in-polygon tests for every single 
raster point.

Think of it this way:

With rasterize, you are first finding ALL the points in a given polygon. 
With P-in-P testing, you are finding if one point is in a given polygon. 
If you had a few points to do, it would be a waste to find them all, but 
since you are, in fact, doing all the raster points, why not only do it 
all at once?

-Chris

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov


More information about the gdal-dev mailing list