# Create closest-distance line in MapInfo

As part of a larger program (basically a remake and extension of DistanceCalc) I want to programmatically create a line between the two closest points of two regions in MapInfo.

The ObjectDistance function effectively measures this line and returns its length but there is no way to store the line itself.

The "Nearest" statement almost does what I want but it does too much! - it measures from one site (or each item in a table) to each item in another table. Also it puts the results in a table whereas I want to manipulate them before storing them.

Is there a simple way to do this?

-

Have a look at this MapBasic function:

ConnectObjects( ) function

Purpose

Returns an object representing the shortest or longest distance between two objects. You can call this function from the MapBasic Window in MapInfo Professional.

-
Yes! That's exactly what I was looking for! Thank you! – Mr_Chimp Jan 12 '11 at 10:50

Ten years ago I wanted exactly the same functionality in Avenue in Arcview 3.x but there was not to much to do about it.

A few years ago I started using PostGIS and since it is Open Source there were new possibilities. Now PostGIS has this functionality in ST_Shortestline since version 1.5.

Here is a shortversion how to make it (In 2D):
There is two possibilities in 2D where you can find the shortest line (or distance)
1) Between a vertex point in one geometry and a vertex point in the other geometry
2) Between an edge in one geometry and a vertex point in the other

In 2D the shortest distance will not be between two edges (well, it can be if the edges is paralell, but then you will always get the same min distance between one of the vertex points and one of the edges)

So what you have to do is iterating through the vertex points and edges in the geometries and compare all combinations of vertex points and edges that is possible.

To compare the vertex points combinations you just use Pythagoras theorem When you get the shortest distance (after testing all possibilities) your shortest line should be built from those vertex points.

When comparing vertex point to edge you can use the algorithm from part 1.02 of http://www.faqs.org/faqs/graphics/algorithms-faq/ to find the point on the edge that is closest to the vertex point in the other geometry

When you have that point you can do exactly like in the vertex-vertex case.

I am curious, does anyone know, in what gis-products is this functionality included?

Regards Nicklas

-

There are a few good starting points in the Nearest neighbor between a point and a line layer question that may be of help.

-