Hi Frank
Below is a function to calculate the point on a line closet to a given
point, it woork for short distances in a UTM projection, i use it to
calculate the intersections of adresses with a road network
function GetClosePoint(byval l as object, byval p as object, minPkt as
tPkt) as object
dim x1, x2, y1, y2, xPkt, yPkt, dist2, dist1, dist3 as float,
helLinje, Vinkelret, xi, yi as float,
testRect, TmpPkt as object,
minToSegment as tPkt,
i, N as integer
N = ObjectInfo(l, OBJ_INFO_NPNTS)
x1 = ObjectNodex(l,1,1)
y1 = ObjectNodey(l,1,1)
xPkt = CentroidX(p)
yPkt = CentroidY(p)
for i = 2 to N
x2 = ObjectNodex(l,1,i )
y2 = ObjectNodey(l,1,i )
xPkt = CentroidX(p)
yPkt = CentroidY(p)
dist1 = Distance(x1, Y1, xPkt, yPkt, "m")
dist2 = Distance(x2, Y2, xPkt, yPkt, "m")
if dist1 < dist2 then
minToSegment.Dist = dist1
minToSegment.X = X1
minToSegment.Y = Y1
else
minToSegment.Dist = dist2
minToSegment.X = X2
minToSegment.Y = Y2
end if
if x1 = x2 then
print "X"
if yPkt between y1 and y2 then
minToSegment.Dist = Distance(x2, yPkt, xPkt, yPkt, "m")
minToSegment.X = X2
minToSegment.Y = yPkt
end if
elseIf y1 = y2 then
print "Y"
if xPkt between x1 and x2 then
minToSegment.Dist = Distance(xPkt, Y2, xPkt, yPkt, "m")
minToSegment.X = xPkt
minToSegment.Y = Y2
end if
else
HelLinje = (y2 - y1)/(x2 - x1)
Vinkelret = -1 / HelLinje
xi = (yPkt - y1 + (HelLinje * x1) - (Vinkelret * xPkt))/(HelLinje
- Vinkelret)
yi = (HelLinje * xi) + y1 - (HelLinje * x1)
Create Rect Into variable testRect( x1, y1) ( x2, y2)
if createPoint(Xi,Yi) within testRect then
'print "punkt ligger paa linje"
minToSegment.Dist = Distance(Xi, Yi, xPkt, yPkt, "m")
minToSegment.X = Xi
minToSegment.Y = Yi
end if
end if
if i = 2 then
minPkt.Dist = minToSegment.Dist
minPkt.X = minToSegment.X
minPkt.Y = minToSegment.y
else
if minPkt.Dist > minToSegment.Dist then
minPkt.Dist = minToSegment.Dist
minPkt.X = minToSegment.X
minPkt.Y = minToSegment.y
end if
end if
xi = 0
yi = 0
x1 = x2
y1 = y2
next
tmpPkt = createPoint(minPkt.X,minPkt.Y)
GetClosePoint = tmpPkt
end function
[EMAIL PROTECTED] (Frank Tanser) - 08-06-2001 07:56:27
Besvar venligst til [EMAIL PROTECTED] (Frank Tanser)
Til: [EMAIL PROTECTED]
cc: (bcc: Peter Laulund/MI/KMS)
Vedr�rende: MI-L distance to nearest point
Hi List
Please can you help me. I have a table of approximately 10 000 points each
with a unique ID. For each point I want to be able to derive the ID of the
nearest point and the distance to that point. Does anyone know of an
existing utility that can perform this calculation? Alternatively can
someone tell me how to write a simle mapbasic utility to do this
calculation?
Many thanks for your anticipated response.
Frank
_______________________________________________________________________
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, send e-mail to [EMAIL PROTECTED] and
put "unsubscribe MapInfo-L" in the message body.
Med venlig hilsen
Peter Laulund
*************************************************************
Kort & Matrikelstyrelsen � www.kms.dk
National Survey and Cadastre - Denmark
Rentemestervej 8 � DK 2400 K�benhavn NV
E-mail: [EMAIL PROTECTED] � Home: [EMAIL PROTECTED]
Phone: +45 3587 5050 � Direct: +45 3587 5173
*************************************************************
_______________________________________________________________________
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, send e-mail to [EMAIL PROTECTED] and
put "unsubscribe MapInfo-L" in the message body.