Given a set of points (latitude and longitude) in the area of Milan,Italy I want to decide to which "neighborhood" each point belongs Here is what I have tried
library(shapefiles)
library(maptools)
library(maps)
library(sp)
library(rgdal)
mn.zip.map <- readShapePoly("NILZone.shp") #shape file that contains the shapes of the neighborhoods
#latlon inizialization and declaration
latlon$lat<-data$latitude
latlon$lon<-data$longitude
#at this point I have a data frame with N rows and 2 columns
coordinates(latlon) = ~lat+lon
So my first guess was:
over(latlon, mn.zip.map)
Of course this was a failure, when I tried
summary(mn.zip.map)
Object of class SpatialPolygonsDataFrame
Coordinates:
min max
x 1503202 1521750
y 5025952 5042528
Is projected: NA
proj4string : [NA]
Data attributes: ....
summary(latlon)
Object of class SpatialPoints
Coordinates:
min max
lat 45.391091 45.534113
lon 9.046796 9.274482
Is projected: NA
proj4string : [NA]
Number of points: 155226
So what I tried was:
map<-readOGR(dsn="C:...",layer="NILZone") #wHERE I FOUND THE RIGHT PARAMETERS TO CRS
coordinates(latlon) = ~lat+lon
proj4string(mn.zip.map) <- CRS("+proj=tmerc +lat_0=0 +lon_0=9 +k=0.9996 +x_0=1500000 +y_0=0 +ellps=intl +units=m +no_defs ")
mn.zip.map <- spTransform(mn.zip.map, CRS("+proj=longlat"))
proj4string(latlon) <- CRS(proj4string(mn.zip.map))
head(over(latlon, mn.zip.map))
FID_1 FID_1_1 ID_NIL NIL AreaHA AreaMQ
1 NA NA NA <NA> NA NA
2 NA NA NA <NA> NA NA
3 NA NA NA <NA> NA NA
4 NA NA NA <NA> NA NA
5 NA NA NA <NA> NA NA
6 NA NA NA <NA> NA NA
Any guess of why this happens??
Thank you very much, sorry for the very long question!
EDIT: here you can download an archive.zip containing several files of the shapes and other information of the neighborhoods of Milan and a file latlon.txt which has the coordinates of 30 points that I'm interested to classify into a neighborhood
https://www.dropbox.com/s/l935nm3edhn5cc7/Archive.zip?dl=0
@G.Cocca I changed the parameters of CRS(...) now the regions should be overlapping!
mn.zip.map@bbox
min max
x 9.040939 9.278398
y 45.386074 45.535303
latlon@bbox
min max
lat 45.391091 45.534113
lon 9.046796 9.274482
But still no result!