I have two spatial objects, one is a spatial polygon object and the other one is a .csv file that I turned into a spatial points object. The first one is an official shape file from the chilean government for one of its communes, the other one was created by geocoding with the HERE API, street adresses of the same commune.
First I loaded the spatial polygon object with readOGR
from the :
quilpue <- readOGR( dsn= getwd() , layer="quilpue-rgdal",
encoding = "UTF-8")
Then I loaded the .csv file into R, and converted it into a spatial points object with the coordinates()
function from the sp
package.
pointsCoords<- read.csv("../quilpueR/quilpueLayer.csv", header = TRUE)
coordinates(pointsCoords) <- ~Longitude+Latitude
Then I checked the projection of each object.
proj4string(quilpue)
proj4string(pointsCoords)
"+proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
and NA
respectively.
The only projection that work for pointsCoords
was CRS("+init=epsg:3857")
.
Therefore I assigned that projection to quilpue
proj4string(pointsCoords) <- CRS("+init=epsg:3857")
quilpue_prj <- spTransform(quilpue, CRSobj = CRS(proj4string(pointsCoords)))
Nonetheless, when I check the extension of both objects with extent()
from raster()
package, they do not overlap.
extent(quilpue_prj)
class : Extent
xmin : -7957703
xmax : -7946463
ymin : -3907594
ymax : -3898059
extent(pointsCoords)
class : Extent
xmin : -71498550
xmax : -71334950
ymin : -33133030
ymax : -32769810
Therefore, when I try to plot them together, they do not overlap. i only get the plot of the first object that I choose to plot.
plot(quilpue_prj)
plot(pointsCoords, add = TRUE)
To check if there was a problem with the shapefile, or .csv file, I opened both on Maptitude
another GIS software, and it manage to automatically overlay them. I would like to be able to do the same in R.