I'm trying to find the center of a bounding box created using the function bbox, the main reason for that is that get_googlemap() requires a center for it to plot the map.

I'm trying to plot a set of CSV data with Lat and Long, with the google background. I'm already able to acquire the maps from google using the API's key.

I've tried to work with the average of my lat and long columns, but the results weren't so good.

The coordinates were acquired in datum WGS84.

  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please do not include chit chat like greetings, statements of appreciation/thanks, etc - see meta.stackexchange.com/questions/2950/…
    – PolyGeo
    Nov 10 '20 at 0:07

You can use some of the R spatial packages.

This adds the required libraries and creates a data.frame with lat/long coordinate columns that would look something like what you are reading in.


dat <- data.frame(id=1:10, 
  long=c(94.54453, 96.40702, 94.82834, 95.71447,
  96.25978, 95.41909, 95.41563, 95.96609,
  95.90538, 95.97319), lat=c(22.22784, 18.05911, 
  23.95242, 23.96676, 18.04602, 25.37593, 
  25.38081, 23.88850, 24.02268, 23.82246))

This coerces the data.frame into an sp SpatialPointsDataFrame and assigned a geographic (decimal degree) WGS84 projection (not necessary but, good practice).

coordinates(dat) <- ~long+lat
  proj4string(dat) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

Here is where you can get your center coordinates. This uses a function from the raster package, extent, to create coordinates of the bounding extent. It is nested in as to coerce the results into a sp SpatialPolygons object and finally nested in coordinates to return the centroid of the bounding polyogn.

( cpt <- coordinates(as(extent(dat), "SpatialPolygons")) )  

This coordinate-pair represents the center of your point data's extent, as you can see in this plot.

plot(as(extent(dat), "SpatialPolygons"))
  points(dat, pch=20)
    points(cpt, pch=20, col="red", cex=2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.