[R-sig-Geo] Match coordinates to regional polygon

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Tue Mar 10 10:25:35 CET 2020


On Tue, 10 Mar 2020, Miluji Sb wrote:

> Dear all,
>
> Re-sending because the message was too big due to the attachment,
>
> I am trying to match a large number of coordinates (attached) sub-national
> regions using GADM shapefile.
>
> Coordinates:
> https://drive.google.com/file/d/1PUsi4d0wP7hB6Aps6UmpXsnIPSD3I1sT/view?usp=sharing
>
> Shapefile:
> https://drive.google.com/drive/folders/1DxyygjQNeg2GIS9doaFM8yM2LvBYZi8E?usp=sharing
>
>
> This is the code I am using;
>
> ###
> library(data.table)
> library(ncdf4)
> library(rgdal)
> library(raster)
> library(tidyverse)

Apart from rgdal, the remainer are pure distraction. Parsimony is 
important.

>
> ## Read coordinates
> coords <- read.csv("coords.csv")
>
> ## Read shapefile
> world <-readOGR(dsn = "/gadm36_levels_shp", layer = "gadm36_2")

This is the full GADM world map of admin units at level 2:

> dim(world)
[1] 45962    13


>
> ## Convert to spatial points
> pts <- coords
> coordinates(pts) <- ~lon+lat


This is a 0.5 degree world grid

> bbox(pts)
         min    max
lon -179.75 179.75
lat  -89.75  89.75

> SG <- as(as(pts, "SpatialPixelsDataFrame"), "SpatialGridDataFrame")
> gridparameters(SG)
     cellcentre.offset cellsize cells.dim
lon           -179.75      0.5       720
lat            -89.75      0.5       360
> bbox(pts)
         min    max
lon -179.75 179.75
lat  -89.75  89.75


>
> ## Provide the same CRS to the coordinates as the shapefile
> proj4string(pts) <- proj4string(world)
>
> ## Use over function to match coordinates to polygons
> pts_district <- over(pts, world)$NAME_1
> ###
>
> However, less than 20% of the coordinates are matched to a polygon. What am
> I doing wrong? Any help will be greatly appreciated.

plot(pts[!is.na(pts_district$GID_0),], axes=TRUE)

looks very much like world land mass to me. Oceans are big, your polygons 
omit Antarctica, and Plate Carree area (0.5x0.5 degree squares) is 
misleading.

Hope this clarifies,

Roger

>
> Regards,
>
> Milu
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en



More information about the R-sig-Geo mailing list