How to use Shape.Distance?

I have 2 shape file. Both in the WGS84. I select A shape in file 1 and select B shape in file 2.
Then calc distance:

Dim SF1 As New MapWinGIS.Shapefile, SF2 As New MapWinGIS.Shapefile
Dim SF1_proj As New MapWinGIS.Shapefile, SF2_proj As New MapWinGIS.Shapefile
Dim sh1 As New MapWinGIS.Shape, sh2 As New MapWinGIS.Shape
Dim sh1_proj As New MapWinGIS.Shape, sh2_proj As New MapWinGIS.Shape
Dim z, Dist, Dist_proj
SF1.Open ("D:\01.shp")
SF2.Open ("D:\02.shp")

' select A and B shape:
Set sh1 = SF1.Shape(A)
Set sh2 = SF2.Shape(B)

z = GeoProjSrc.ImportFromProj4("+proj=longlat +datum=WGS84 +no_defs ")

' EPSG:3857:
z = GeoProjTrg.ImportFromProj4("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs")
' make copy original with new proj:
Set SF1_proj = Utils.ReprojectShapefile(NP, GeoProjSrc, GeoProjTrg)
Set SF2_proj = Utils.ReprojectShapefile(SNT, GeoProjSrc, GeoProjTrg)

' select A and B shape:
Set sh1_proj = SF1_proj.Shape(A)
Set sh2_proj = SF2_proj.Shape(B)

' calc distance:
Dist= sh1.distance(sh2)
Dist_proj = sh1_proj.distance(sh2_proj)
' result: Dist = 0.054578417, Dist_proj = 6086.310946

In what units was the result obtained?
If measured on the map, it turns out 3843 meters.
I would be very happy to receive any help.
Best regards, Clapter.

Hello @Clapter

It’s hard to tell exactly what is happening without having your actual Shapefiles and Shapes, or the actual code. If you don’t mind uploading your two shapefiles, and clarify what values you are using for A, B, NP, and SNT, we could perhaps step through the code to see what is happening.

That said, I believe that the first distance is in decimal degrees, since that is your original projection, and the second one should presumably be meters, since it is presumably in Google Mercator.

Also to consider is that the measurement on the map will depend on the projection set into the Map control itself.

One thing to try, rather than the ImportFromProj4, would be the following calls:

z = GeoProjSrc.SetWgs84()
z = GeoProjTrg.SetGoogleMercator()

just to see if that makes any difference.

Regards,
Jerry.

Hello Jerry!
Thanks for the comment.
Here is the full code and shp files:

Sub test2()

Dim SF1 As New MapWinGIS.Shapefile
Dim SF2 As New MapWinGIS.Shapefile
Dim SF1_proj As New MapWinGIS.Shapefile
Dim SF2_proj As New MapWinGIS.Shapefile
Dim sh1 As New MapWinGIS.Shape
Dim sh2 As New MapWinGIS.Shape
Dim sh1_proj As New MapWinGIS.Shape
Dim sh2_proj As New MapWinGIS.Shape
Dim Utils As New MapWinGIS.Utils
Dim GeoProjSrc As New MapWinGIS.GeoProjection
Dim GeoProjTrg As New MapWinGIS.GeoProjection
Dim z, Dist, Dist_proj

SF1.Open ("D:\D1.shp")  ' shapefile with 1 shape from 1 polygon
SF2.Open ("D:\Point.shp")   ' shapefile with 1 shape from 1 point
z = GeoProjSrc.SetWgs84()
z = GeoProjTrg.SetGoogleMercator()

Set SF1_proj = Utils.ReprojectShapefile(SF1, GeoProjSrc, GeoProjTrg)
Set SF2_proj = Utils.ReprojectShapefile(SF2, GeoProjSrc, GeoProjTrg)

Set sh1_proj = SF1_proj.Shape(0)
Set sh2_proj = SF2_proj.Shape(0)

Set sh1 = SF1.Shape(0)
Set sh2 = SF2.Shape(0)

' calc dist with WGS84
Dist = sh1.distance(sh2)   

' calc dist with Merc
Dist_proj = sh1_proj.distance(sh2_proj)

' result: Dist = 0.006814783150152, Dist_proj = 808.355795157245

End Sub

After measurements in Arcgis, it became clear that Dist - planar measurement method.
It’s written in the file Point.shp in Field “NEAR_PLANA”. It = 0.006815.
In Field “NEAR” save result calc distanse Geodesic = 470.518861097

After measurements on the map I got distance 783m from point to shape centroid and distance 789m from point to shape center.

I have 2 questions:

  1. what is Dist_proj = 808.355795157245
  2. How to get value 783 (789) m ?

Thank you for attention,
best regards, Clapter.
Shapes.zip (5.1 KB)