0

I've produced a raster using IDW to make predictions across California, and now I'm trying to overlay the raster image with the border/shapefile of California.

I've ensured the CRS projections are the same for both plots, however I still can't get both to show up.

The following is the code for how I produced the raster:

x.range <- as.double(range(zip_table@coords[,1]))
y.range <- as.double(range(zip_table@coords[,2]))

grd <- expand.grid(x=seq(from=x.range[1], to=x.range[2], by=0.01),
                   y=seq(from=y.range[1], to=y.range[2], by=0.01))

## convert grid to SpatialPixel class
coordinates(grd) <- ~ x + y
gridded(grd) <- TRUE

idw1 <- gstat::idw(TELERATIO ~ 1, zip_table, grd, idp = 2) 

idw.output <- as.data.frame(idw1)
names(idw.output)[1:3] <- c("lng","lat","var1.pred")

r <- raster(idw1)```

And the CA state file I downloaded using a work around of the raster::getData function:

CA <- gadm36_USA_1_sp[gadm36_USA_1_sp$NAME_1 =='California',]

When I produce my plots, only the raster shows up:

plot(CA, add = TRUE)

enter image description here

4
  • Whenever two datasets that should align don't, then one or both are invalid.
    – Vince
    Commented Aug 9, 2022 at 23:36
  • Thanks for the reply. In what way should I test their validity? Because alone, both plot just fine and both have the same crs.
    – Dougie G
    Commented Aug 9, 2022 at 23:47
  • 1
    Edit the Question to provide the CRS and the envelope extents of the sources. One is likely to be messed up.
    – Vince
    Commented Aug 10, 2022 at 0:21
  • 1
    Looking at your raster, and last time I checked, California wasn't 120 degrees SOUTH of the equator...
    – Spacedman
    Commented Aug 10, 2022 at 8:04

1 Answer 1

1

California is not at latitude -120, unless there's been an earthquake and its drifted off into the Pacific (although +30 longitude probably puts it in the South Atlantic...). It seems that latitude and longitude have been transposed.

Going through your code from the first line with a known-good lat-long data set results in a lat-long-correct raster output, so I think your zip_table object has its coordinates transposed already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.