Calculating distance between coordinates from point to a box?

I have two different WGS84 coordinates, A and B. A is just a (lat,lon,z) point and B is a (lat,lon,z) point inside the center of a box with the dimension height, width, depth (in meters).

I need to calculate the distance from A to the box, not to the center of the box but to the closest side/surface of the box. Any ideas?

I already have a method to calculate between two coordinates ((lat,lon) to (lat,lon)).

Currently using PostGIS, but open to suggestions for other software.

• Would be helpful to know what software you are using...
– Matt
Mar 10, 2017 at 12:20
• Looking at PostGIS right now but I don't use anything yet since I don't really know how to solve the problem. Anything that you can recommend? :) Mar 10, 2017 at 12:28
• Is your box always aligned North-South/East-West, or does it have a rotation term? Mar 12, 2017 at 22:04

ST_3DDistance would calculate if it is not inside the box:

http://www.postgis.org/docs/ST_3DDistance.html

So something like:

``````SELECT ST_3DDistance(b.geom, p.geom)
FROM box b, points p
WHERE
b.osm_id = '39259759'
AND
p.osm_id = '899525912'
``````

You can also test it with ST_3DClosestPoint:

``````SELECT ST_3DDistance(ST_3DClosestPoint(p.geom, b.geom), p.geom)
FROM box b, points p
WHERE
b.osm_id = '39259759'
AND
p.osm_id = '899525912'
``````

Which will return the same distance.