I have the following problem:

I have zip code regions, which basically have one single value attached to them most of the time. Then I have a grid consisting of grid boxes, which you can put over these zip code regions of interest.

What I want is the following: For every zip code region of interest, I want to attach the value from the zip code region to the grid coordinate, which is the closest to the centroid (see first figure below) of that zip code region. That grid box must be inside of that zip code region. It's enough actually, when I know which grid box in the zip code area is actually the closest one to the centroid of the zip code region. I can use these coordinates to index the grid boxes of relevance to me from the pixels plotted in figure 2 (see figure 2, where I didnt select the grid boxes closest to the centroids of a zip code region [figure 1]).

enter image description here

trueCentroids.zip_code_regions <- gCentroid(SpatialPolyDf.zip_code, byid=TRUE)  % Gives me the centroids of my zip code regions

Initially, I didnt care and just gave every grid box, which was inside of a specific zip code region the value, which is attached to this region (In the case a value existed for a specific region). The following figure and code should make that clearer:

> class(Spatialpixels_grid)
[1] "SpatialPixels"
[1] "sp"

Spatialpixels_grid basically just contains coordinates, which I attach values to from the SpatialPolygonsDataframe object SpatialPolyDf.zip_code (see figure 2). For some regions, there are no values, so you see white pixels. The points represent the grid boxes, which are saved in Spatialpixels_grid.

Spatialpixels_grid$value_zip_code_region <- over(Spatialpixels_grid, SpatialPolyDf.zip_code)$value

Example for using the function over

enter image description here

Is there the possibility to modify over, so I can achieve my goal?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.