I want to perform an over from the sp package. However, the results I am getting are unexpected. My polygon overlays with nine Points. However, after the over() it only returns 1 point. Why is that?

Here a reproducible example!

library(sp)# The point datamyPoi <- structure(list(index = c(2L, 17L, 19L, 23L, 34L, 71L, 82L, 85L, 92L, 96L, 102L, 7L, 20L, 29L, 40L, 50L, 100L, 103L, 84L, 91L, 11L, 61L, 70L, 76L, 45L, 1L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 13L, 14L, 18L, 21L, 22L, 24L, 25L, 26L, 27L, 28L, 30L, 31L, 33L, 35L, 36L, 37L, 38L, 41L, 42L, 43L, 44L, 47L, 48L, 49L, 51L, 52L, 55L, 56L, 58L, 60L, 62L, 63L, 64L, 68L, 69L, 72L, 73L, 74L, 75L, 77L, 78L, 79L, 80L, 81L, 83L, 87L, 88L, 89L, 90L, 93L, 94L, 95L, 97L, 98L, 99L, 101L, 104L, 12L, 15L, 66L, 86L), x = c(13.0165026, 13.0164516, 13.0164489, 13.0164114, 13.0164328, 13.0162478, 13.016237, 13.0164516, 13.0160949, 13.0163309, 13.0164597, 13.02528, 13.0159466, 13.0437784, 13.0309044, 13.0335449, 13.0289418, 13.0253004, 13.0446292, 13.0434527, 13.0394242, 12.9720291, 13.0315502, 13.0397486, 13.0195456, 13.036048, 13.0356159, 13.0283043, 13.0178174, 13.0395106, 13.0182921, 13.0286984, 13.0382137, 13.0389044, 13.035627, 13.0179864, 13.0387113, 13.0413548, 13.0176216, 13.0395562, 13.0330164, 13.0377135, 13.0410115, 13.0176398, 13.0183699, 13.0413454, 13.0178389, 13.0373272, 13.0383505, 13.0331394, 13.0181419, 13.0184316, 13.0415063, 13.0373487, 13.0390412, 13.0179944, 13.0298012, 13.0358995, 13.041501, 13.0331575, 13.0183914, 13.0293197, 13.0389486, 13.0176076, 13.0357577, 13.017694, 13.0411228, 13.0184692, 13.0182787, 13.0184531, 13.0348261, 13.0387716, 13.0377537, 13.0358131, 13.0332953, 13.0385517, 13.0409887, 13.0360657, 13.0181661, 13.0411724, 13.0359306, 13.0382338, 13.0383103, 13.0348797, 13.0357777, 13.043133, 13.0390318, 13.0384913, 13.0412797, 13.0359298, 13.0386354, 13.0297737, 13.0294306, 13.033415), y = c(52.4009649, 52.4003774, 52.4004641, 52.40168, 52.4014313, 52.4022691, 52.4024115, 52.4012316, 52.4024066, 52.4002121, 52.4002186, 52.4011368, 52.4063829, 52.4004306, 52.408314, 52.4033442, 52.3988618, 52.4036956, 52.403118, 52.4023966, 52.4001135, 52.4045952, 52.4124868, 52.4040269, 52.4044583, 52.4021688, 52.4021458, 52.4025064, 52.4020523, 52.4024279, 52.401748, 52.4032264, 52.4022585, 52.4023951, 52.4020268, 52.400694, 52.4024549, 52.4021521, 52.4021309, 52.4020433, 52.4025458, 52.4023542, 52.4022225, 52.3996966, 52.4010574, 52.402468, 52.4005992, 52.4022168, 52.4020114, 52.4024885, 52.4007988, 52.4011998, 52.402247, 52.4020875, 52.4021521, 52.4019705, 52.4048047, 52.402235, 52.4023853, 52.4026649, 52.4016137, 52.4048988, 52.4020327, 52.3999584, 52.402231, 52.4005386, 52.4024549, 52.4013388, 52.400928, 52.4014714, 52.4013764, 52.4019623, 52.4019729, 52.4017299, 52.402618, 52.4019558, 52.4023567, 52.4020376, 52.4018642, 52.4021448, 52.4019606, 52.4021251, 52.4023698, 52.401446, 52.4019525, 52.4011382, 52.402283, 52.4024483, 52.4020605, 52.4017324, 52.4101571, 52.405268, 52.4046133, 52.4043887), poi = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L), .Label = c("artwork", "attraction", "gate", "memorial", "ruins", "statue", "viewpoint"), class = "factor"), gewichtung = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L)), .Names = c("index", "x", "y", "poi", "gewichtung"), class = "data.frame", row.names = c(NA, -94L))# corner of polygoncornersPoly <- structure(c(13.0302143096924, 13.0232620239258, 13.0237770080566, 13.0296993255615, 13.0326175689697, 52.4066068887721, 52.4047742038877, 52.400375449523, 52.3982282791922, 52.4042505656507), .Dim = c(5L, 2L))

And here my code!

# make points spatialcoordinates(myPoi) <- ~x + yproj4string(myPoi) <- CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs")# make polygon from matrix# create PolygonmyPolygon <- Polygon(cornersPoly)# create Polygon ListmyPolygonList <- Polygons(list(myPolygon), 1)# create spatial Polygon myPolygonSpatial <- SpatialPolygons(list(myPolygonList))# assign CRSproj4string(myPolygonSpatial) <- CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs")# perform the overtest <- over(myPolygonSpatial, myPoi)

edit

with the comment from Josh O'Brien I get that as a result

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2425 26 27 28 29 30 31 32 NA NA NA NA NA NA NA NA NA NA NA 1 NA NA NANA 1 1 NA NA NA NA NA NA NA NA NA 1 NA NA NA 1 33 34 35 36 37 3839 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 6162 63 64 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NANA NA NA NA 1 NA NA NA NA 1 NA NA 65 66 67 68 69 70 71 72 73 74 7576 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 NA NA NA NANA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 1 1 NA

1

Best Answer


Try

test <- over(myPolygonSpatial, myPoi, returnList = TRUE)

and look at the first element of the list returned. The issue why this looks complicated is that myPolygonSpatial may contain multiple points; the list will contain the set of sets of points. Reading vignette(over) may also pay off.