views:

73

answers:

3

What R packages are available to calculate the minimum bounding box for a great circle?

For example:

box <- polycirc( c( longitude, latitude ), distance=35 )

This would return the bounding box for the circle with a radius of 35 kilometres from the central point at the given coordinates (on Earth). Where:

  box.longitude_min = The longitude of the circle's western-most point.
  box.longitude_max = The longitude of the circle's eastern-most point.
  box.latitude_min = The latitude of the circle's southern-most point.
  box.latitude_max = The latitude of the circle's northern-most point.

Something like this should already exist in R, but I cannot find it. The closest I've found (from SO), which I am currently transmogrifying to R, is:

http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates

Also, what is the word (if any) for minimum bounding rectangle of a circle? (The opposite of circumscribed.)

Thank you!

A: 

In R:

  SPHERE <- 6378.137
  TO_RAD <- pi / 180
  TO_DEG <- 180 / pi

  MINIMUM_LATITUDE  <-  -90 * TO_RAD
  MAXIMUM_LATITUDE  <-   90 * TO_RAD
  MINIMUM_LONGITUDE <- -180 * TO_RAD
  MAXIMUM_LONGITUDE <-  180 * TO_RAD

  lat_rad <- latitude * TO_RAD
  lng_rad <- longitude * TO_RAD

  dist_rad <- distance / SPHERE
  lat_min <- lat_rad - dist_rad
  lat_max <- lat_rad + dist_rad

  if( lat_min > MINIMUM_LATITUDE && lat_max < MAXIMUM_LATITUDE ) {
    lng_delta <- asin( sin( dist_rad ) / cos( lat_rad ) )
    lng_min <- lng_rad - lng_delta
    lng_max <- lng_rad + lng_delta

    if( lng_min < MINIMUM_LONGITUDE ) { lng_min <- lng_min + 2 * pi }
    if( lng_max > MAXIMUM_LONGITUDE ) { lng_max <- lng_max - 2 * pi }
  }
  else {
    lat_min = max( lat_min, MINIMUM_LATITUDE )
    lat_max = min( lat_max, MAXIMUM_LATITUDE )
    lng_min = MINIMUM_LONGITUDE
    lng_max = MAXIMUM_LONGITUDE
  }

  data.frame(
    lat_min * TO_DEG,
    lng_min * TO_DEG,
    lat_max * TO_DEG,
    lng_max * TO_DEG
  )
Dave Jarvis
A: 

Use the polycirc function to generate the points of the circle and then min and max to found the bounding box :)

require(pgirmess)
circle <- polycirc(20, c(10, 20))
plot(circle, type = "l")
rect(min(circle[,1]), min(circle[,2]), max(circle[,1]), max(circle[,2]))
nico
@nico: Will this work for a circle on the surface of a sphere? (Crossing the poles or 180th Meridian?) And how would you use latitude and longitude?
Dave Jarvis
@Dave Jarvis: Oooops.... I did not read your question correctly (sorry, was early in the morning). No, this would need some tweaking then.
nico
@nico: No worries. I saw `polycirc` while searching, but discounted it (and many other similar R functions) for not using a spherical surface.
Dave Jarvis
A: 

Given to me:

  library( geosphere )
  p <- c( longitude, latitude )
  box <- apply( destPoint( p, c(0, 90, 180, 270), distance ), 2, range )
  print( box )
Dave Jarvis