code for distance between points

4636
7
04-05-2013 07:05 AM
New Contributor
I am a beginner of arcobjects.
My question is to write code for Calculation of Distance Matrix between different locations (different spots) and save it automatically in a table or a *csv file. The basic data is vetor map data with points, polygons and line.

Thank you very very very much for help.
Reply
0 Kudos
7 Replies
MVP Frequent Contributor
Li,

If you are looking to return the distance between geometries then have a look at the IProximityOperator Interface.
Reply
0 Kudos
New Contributor
Thanks Duncan.

I need to return the distance between different points. The points are read from the point layer of map and the coordinates (x,y) are stored in an array. Then I need to calculate the distance using the array of the coordinates and return the distance matrix into a table or a .csv file.

I have written the following part of the code, and couldn't keep going, so I would like to ask for help.

[INDENT]Private Sub UIToolControl1_DblClick()
Dim pMxdoc As IMxDocument
Set pMxdoc = ThisDocument

Dim pmap As IMap
Set pmap = pMxdoc.FocusMap

Dim pFeatLayer As IFeatureLayer
Set pFeatLayer = player

Dim pFeatClass As IFeatureClass
Set pFeatClass = pFeatLayer.FeatureClass

'Get point features
Dim pGeometry As IGeometry
Dim pPoint As IPoint
Dim pTable As ITable
'different point has different accordant field name which shoud be the same with the output field name
Dim pfield As IField

'Count point
Dim pFeatureCursor As IFeatureCursor
Set pFeatCursor = pFeatClass.Search(Nothing, False)
fcount = pFeatClass.FeatureCount(Nothing)
Dim parray(2, fcount - 1) As Variant

Dim pfeature As IFeature
Set pfeature = pFeatCursor.NextFeature
Dim prowbuff As IRowBuffer
Dim pgeom As IPoint
xcount = 0
Do While Not pfeature Is Nothing
Set prowbuff = pfeature
parray(2, xcount) = prowbuff.Value(1) 'here 1 is the field with the name you want for each point
Set pPoint = pfeature.ShapeCopy
parray(0, xcount) = pPoint.X
parray(1, xcount) = pPoint.Y
Set pfeature = pFeatCursor.NextFeature
xcount = xcount + 1
Loop


If fcount = 0 Then
Exit Sub
Else

'Set up a loop to calculate the distance of different points
? ? ?

'return the distance matrix into a table or a .csv file
? ? ?


End Sub[/INDENT]





Li, 

If you are looking to return the distance between geometries then have a look at the   IProximityOperator Interface.
Reply
0 Kudos
Occasional Contributor
Code for calculating distance and angle between two points in C#:

PointClass p1 = new PointClass();
PointClass p2 = new PointClass();
// Add data to points for test
p1.PutCoords(34.481560, 41.515391);
p2.PutCoords(34.702832, 41.795668);

IMeasurementTool measurementTool = new MeasurementToolClass();
measurementTool.ConstructByPoints(p1, p2);
// Here we calculate distance between two points: (p1, p2)
double distance = measurementTool.Distance;

ILine lineForAngle = new Line();
GeometryEnvironment geoEnv = new GeometryEnvironment();
IConstructAngle angleCalculator = (IConstructAngle)geoEnv;

lineForAngle.PutCoords(p1, p2);
// Here we calculate angle of line between (p1, p2) and the positive x-axis
double angle = angleCalculator.ConstructLine(lineForAngle);
// Convert radian to degree
angle *= 180 / Math.PI;

Sinrecely
Reply
0 Kudos
New Contributor
Thanks Oyleiste,
Do you know the code in VBA. I am writing it in VBA and difficulities still exist.

Thank you very much!


Code for calculating distance and angle between two points in C#: 

PointClass p1 = new PointClass();
PointClass p2 = new PointClass();
// Add data to points for test
p1.PutCoords(34.481560, 41.515391);
p2.PutCoords(34.702832, 41.795668);

IMeasurementTool measurementTool = new MeasurementToolClass();
measurementTool.ConstructByPoints(p1, p2);
// Here we calculate distance between two points: (p1, p2)
double distance = measurementTool.Distance;

ILine lineForAngle = new Line();
GeometryEnvironment geoEnv = new GeometryEnvironment();
IConstructAngle angleCalculator = (IConstructAngle)geoEnv;

lineForAngle.PutCoords(p1, p2);
// Here we calculate angle of line between (p1, p2) and the positive x-axis
double angle = angleCalculator.ConstructLine(lineForAngle);
// Convert radian to degree
angle *= 180 / Math.PI;

Sinrecely
Reply
0 Kudos
Occasional Contributor
You can convert the code to VB easily.
for example
PointClass p1 = new PointClass();

should be:
Dim p1 as PointClass
Set p1 = new PointClass

or
p1.PutCoords(34.481560, 41.515391);
should be:
p1.PutCoords 34.481560, 41.515391


convert other lines like this. It's not hard. Tell me if you couldn't
Reply
0 Kudos
Occasional Contributor
I am a beginner of arcobjects.
My question is to write code for Calculation of Distance Matrix between different locations (different spots) and save it automatically in a table or a *csv file. The basic data is vetor map data with points, polygons and line.

Thank you very very very much for help.


If you want euclidean distances of each point to all other points in a Featureclass then same can be done by OD Matrix which is available in Network Analyst. The same can be called programatically too.

It automatically creates table with Origin and Destination and its distances

Check this link : http://help.arcgis.com/en/arcgisdesk...0000004r000000

Hope this helps, Best Luck
Reply
0 Kudos
New Contributor III
public static double Calc(double Lat1, double Long1, double Lat2, double Long2)
        {
              /*
                  The Haversine formula according to Dr. Math.
                  http://mathforum.org/library/drmath/view/51879.html
               
                  dlon = lon2 - lon1
                  dlat = lat2 - lat1
                  a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
                  c = 2 * atan2(sqrt(a), sqrt(1-a))
                  d = R * c
               
                  Where
                      * dlon is the change in longitude
                      * dlat is the change in latitude
                      * c is the great circle distance in Radians.
                      * R is the radius of a spherical Earth.
                      * The locations of the two points in
                          spherical coordinates (longitude and
                          latitude) are lon1,lat1 and lon2, lat2.
              */
              double dDistance = Double.MinValue;
              double dLat1InRad = Lat1 * (Math.PI / 180.0);
              double dLong1InRad = Long1 * (Math.PI / 180.0);
              double dLat2InRad = Lat2 * (Math.PI / 180.0);
              double dLong2InRad = Long2 * (Math.PI / 180.0);

              double dLongitude = dLong2InRad - dLong1InRad;
              double dLatitude = dLat2InRad - dLat1InRad;

              // Intermediate result a.
              double a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
                         Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) *
                         Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);

              // Intermediate result c (great circle distance in Radians).
              double c = 2.0 * Math.Asin(Math.Sqrt(a));

              // Distance.
              // const Double kEarthRadiusMiles = 3956.0;
              const Double kEarthRadiusKms = 6376.5;
              dDistance = kEarthRadiusKms * c;

              return dDistance;
          }
Reply
0 Kudos