# Plotting square grids on a map and extracting each grid information using R [closed]

I have a data set which looks like this

LATITUDE LONGITUDE VALUES
28.26734 76.82973 -56.7879
28.26644 76.82972 -58.22462
28.26508 76.82969 -58.4211
28.26778 76.83076 -55.75333
28.26733 76.83075 -58.55153
28.26507 76.83071 -56.38619
28.26912 76.83129 -56.11011
28.26732 76.83126 -58.17415
28.26687 76.83125 -59.77212

I want to plot the "Values" on the map wrt its "LAT" and "LONG" and create square grids on that map. When the grid is created, each grid will contain some rows of the data set i.e. the "Values" which are plotted. and I then want to extract the values each grid is having. So the fourth column in the dataset would be "GRID" and each row belonging to a particular grid...Of course multiple rows can take single grid values.

I am able to plot these points on the map but not able to create the grid.

• What exactly do you mean by a "square grid"? What does it mean to "extract the values each grid is having"? – whuber Dec 24 '17 at 17:22

Try this:

library('sp')

# Projection
wgs.84 <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")

x <- c(76.82973, 76.82972, 76.82969, 76.83076, 76.83075, 76.83071, 76.83129, 76.83126, 76.83125)
y <- c(28.26734, 28.26644, 28.26508, 28.26778, 28.26733, 28.26507, 28.26912, 28.26732, 28.26687)
z <- c(-56.7879, -58.22462, -58.4211, -55.75333, -58.55153, -56.38619, -56.11011, -58.17415, -59.77212)

# Create data frame
dataset <- data.frame("LONGITUDE" = x, "LATITUDE" = y, "VALUES" = z)

# Create SpatialPointsDataFrame object
datasetSP <- SpatialPointsDataFrame(coords = dataset[,c(1,2)], data = data.frame("id" = 1:nrow(dataset), "values" = dataset\$VALUES), proj4string = wgs.84)

# Extent
extentDatasetSP <-extent(datasetSP)

# Make grid options

# Cell size (map units)
xCellSizeGrid <- 0.003
yCellSizeGrid <- 0.003

# Offset
xOffset <- 5 * xCellSizeGrid
yOffset <- 5 * yCellSizeGrid

# Grid
grid <- GridTopology(cellcentre.offset = c(extentDatasetSP@xmin - xOffset, extentDatasetSP@ymin - yOffset),
cellsize = c(xCellSizeGrid, yCellSizeGrid),
cells.dim = c(15, 15))

# Plot grid and points
plot(datasetSP, xlab = "Longitude", ylab = "Latitude", pch = 19)
plot(SpatialGrid(grid = grid), add = TRUE)
box()

# Create SpatialGrid object
gridSpatial <- SpatialGrid(grid = grid, proj4string = wgs.84)

# Convert to SpatialPixels object
gridSpatialPixels <- as(gridSpatial, "SpatialPixels")

# Convert to SpatialPolygons object
gridSpatialPolygons <- as(gridSpatialPixels, "SpatialPolygons")

# Add 'id' and 'values' to every polygon
gridSpatialPolygons\$id <- 1:nrow(coordinates(gridSpatialPolygons))
gridSpatialPolygons\$values <- paste("Gridvalue", 1:nrow(coordinates(gridSpatialPolygons)), sep = ":")

# Sample

# Get attributes from polygons
samplePointsInPolygons2 <- datasetSP %over% gridSpatialPolygons

# Result
datasetResult <- data.frame(dataset, samplePointsInPolygons2)

# Remove column values from sampled grid and change column name 'id' to 'GRID'
datasetResult <- datasetResult[,-5]
colnames(datasetResult)[4] <- "GRID"

# Plot results
plot(datasetSP, xlab = "Longitude", ylab = "Latitude", pch = 19)
plot(SpatialGrid(grid = grid), add = TRUE)
# Point labels
text(as.data.frame(coordinates(datasetSP)), labels = datasetSP\$id, cex = 0.7, pos = 3)
# Polygons labels
labpt <- do.call("rbind", lapply(gridSpatialPolygons@polygons, function(x) x@labpt))
labID <- do.call("rbind", lapply(gridSpatialPolygons@polygons, function(x) x@ID))
text(labpt, labels = labID, cex = 0.7, pos = 3, col = "blue")
box()

> datasetResult

LONGITUDE LATITUDE    VALUES GRID
1  76.82973 28.26734 -56.78790  126
2  76.82972 28.26644 -58.22462  141
3  76.82969 28.26508 -58.42110  141
4  76.83076 28.26778 -55.75333  126
5  76.83075 28.26733 -58.55153  126
6  76.83071 28.26507 -56.38619  141
7  76.83129 28.26912 -56.11011  127
8  76.83126 28.26732 -58.17415  127
9  76.83125 28.26687 -59.77212  127

• This is great..thanks.. But I want to plot these points and grids over a Map where these LAT & LONG are located...How would I do that? – Shubham. Sep 13 '16 at 7:04
• @Shubham. Those points are the LONGITUDE and LATITUDE values you posted in your question. I don't understand what do you mean with plot those points where the same points are located. Please, can you explain me more? Maybe you can edit your question and add an update with more details or an explanation figure. – Guzmán Sep 13 '16 at 13:30