1

I have 2 shape files: data (spatial point dataframe) and polys (polygons dataframe). I want to do an overlap but is seems that it does not work.

Here are data and polys:

> data
class       : SpatialPointsDataFrame 
features    : 12527 
extent      : 10.20075, 20.6108, 54.08669, 57.75905  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
variables   : 3
names       :    timestamp_pretty,     timestamp,     imo 
min values  : 01/04/2006 00:00:55, 1143849655232, 9048392 
max values  : 30/04/2006 23:59:36, 1146441576823, 9191541 
> polys
class       : SpatialPolygonsDataFrame 
features    : 436375 
extent      : 4210000, 5441000, 3395000, 4813000  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
variables   : 2
names       :    Id, Count 
min values  :     0,     0 
max values  : 99999,     9

to manage the overlapping, I use

proj4string(data) <- proj4string(polys)                            # to confirm the same reference system
inside <- !is.na(over(data, as(polys, "SpatialPolygons")))    # overlapping shape file and data

and then mean(inside) to check the average of points in polys.

But there is nothing happening, mean is always 0. I used this many times before and it always work, I guess it does not work not because the extent of the 2 sph files are different. Is there a way to edit this?

Thank you!

Floni
  • 475
  • 2
  • 13

1 Answers1

0

From my experience, the problem is probably in the way you assign the projection.

require(geosphere)
require(rgeos)
require(rgdal)
proj4string(data) <- spTransform(polys, "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs")   #this is the correct way to really project the spatial object on the same projection of polys
over(data, polys)   #this is just to check whether the two sp objects interact

try to run this code and tell me if something happens.

Seymour
  • 3,104
  • 2
  • 22
  • 46
  • I have this error: `Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘proj4string<-’ for signature ‘"SpatialPointsDataFrame", "SpatialPolygonsDataFrame"` . – Floni Jan 16 '18 at 11:55
  • It is difficult without a reproducible example. However, my view is that you need to assign the projection using `spTransform` of `rgdal` package and not by simply `proj4string(data) <- proj4string(polys)` which does not apply a real transformation to the projection of the spatial object – Seymour Jan 16 '18 at 12:10