# Calculate Bounding Box from Center at scale

So I was trying to find my answer in Calculating bounding box by given center and scale in Android?

And below is what I've implemented (Ruby) with the size heigh/width in pixels being the same as a Letter of paper (8.5x11in @ 72dpi)

``````def bounding_box_from_point(center:, size:, scale_denominator:)
dpi = 72
inches_per_unit = 4374754
resolution = 1 / (scale_denominator * inches_per_unit * dpi)
half_width_deg = (size.width * resolution) / 2
half_height_deg = (size.height * resolution) / 2

BoundingBox.new(
north: center.lat + half_height_deg,
south: center.lat - half_height_deg,
east: center.lon + half_width_deg,
west: center.lon - half_width_deg,
height: size.height,
width: size.width
)
end
``````

Calling `bounding_box_from_point(center: center, size: size, scale_denominator: scale)` with:

``````scale = 1/18055.955520 # i.e. 0.0008861342166177423
center = Geometry::Location.new(lat: 37.806336, lon: -122.270625)
size.width = 612,
size.height = 792
``````

It returns:

``````west: -122.27172131608657,
east: -122.26952868391342,
south: 37.804917238005615
north: 37.80775476199439
``````

If you go to http://www.openstreetmap.org/export and enter those bounding box locations, you can see that the ratio does not match that of a 8.5x11in piece of paper...it's slightly too tall. What am I doing wrong here or not understanding?

• I suspect that you are assuming degrees are the same in both W-E and N-S and are the same all over the page. OSM is using a Mercator projection so this is not true. – Ian Turton Feb 11 '17 at 12:17
• @iant how would you adjust for this projection? – theartofbeing Feb 12 '17 at 20:37
• I know how to adjust for mercator projecting. Going to test if this works and will report: `Math.log( Math.tan(lat_in_radians/2 + Math::PI/4))` – theartofbeing Feb 12 '17 at 20:47