Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to calculate distance in this case?

    Dear all,

    I would like to calculate a distance as follows: distance is defined as the straight-line distance between a survey cluster and the origin of the event. The point location of a survey cluster is determined by the longitude and latitude of each survey cluster. The origin of the event is assumed to be the middle point of country CM whose latitude and longitude is (−6.31, 23.59). Any help is much appreciated. Thank you.
    Code:
    clear
    input str2 country int cluster double(latnum longnum)
    "BF"  1 12.3667 -1.5167
    "BF"  2 15.4567 -2.6167
    "BF"  3 20.1235 -0.6167
    "BF"  4 5.2346 -8.2314
    "BF"  5 6.1676 -5.4177
    "CM"   1  4.0526  9.7240
    "CM"   2  9.1209 13.5651
    "CM"   3 10.3284 15.2224
    "CM"   4 12.7535 14.5469
    "CM"   5  4.5154 10.2928
    "ET"  1 11.0753 20.3376
    "ET"  2 9.14200 18.3946
    "ET"  3 12.2618 16.5945
    "ET"  4 7.8851  17.04987
    "ET"  5 13.0943 27.4918
    "GH"  1    5.1 -3.0833
    "GH"  2 5.0833   -2.55
    "GH"  3 4.9667 -2.3833
    "GH"  4 5.0167 -2.1667
    "GH"  5 4.8833    -1.9
    "GN" 1101 6.9544 -13.8085
    "GN" 1102 9.6652 -12.5995
    "GN" 1103 8.6687 -11.5976
    "GN" 1104 5.7362 -9.5834
    "GN" 1105 3.9242 -8.2895
    "ML"   1 9.5884 -10.895643
    "ML"   2 10.4519 -11.43128
    "ML"   3 7.4345 -11.441427
    "ML"   4 12.8021 -10.85005
    "ML"   5 15.6098 -9.034747
    "MW" 1001 -9.5721 20.3730
    "MW" 1002 -6.54736 30.2916
    "MW" 1003 -10.8601 43.4302
    "MW" 1004 -12.5576 12.4524
    "MW" 1005 -15.4934 11.6710
    end
    la var latnum "latitude"
    la var longnum "longitude"

  • #2
    The user-written module -geodist-, available at SSC to be described and installed, provides one solution. With your data and problem, you could do this:
    Code:
    geodist latnum longnum -6.31 23.59, generate(distkm)

    -search distance latitude longitude- reveals other possible relevant commands.


    Comment


    • #3
      Dear Prof. Mike,

      Thank you so much for the kind help. Your code works perfectly. I have a follow-up question, that is do you know how to calculate Euclidean distance using data in #1? I think code in #2 does Haversine distance.

      Comment


      • #4
        No, I don't know anything about this.

        Comment


        • #5
          try this

          gen euclidean_distance = .
          sort country cluster
          by country: gen latnum_diff = latnum -6.31
          by country: gen longnum_diff = longnum - 23.59
          by country: replace euclidean_distance = sqrt(latnum_diff^2 + longnum_diff^2)

          Comment


          • #6
            Note that this formula assumes a flat Earth and may not be suitable for large distances or for use in applications where the Earth's curvature must be taken into account. In those cases, the geodist function, which calculates distances on an ellipsoid, should be used instead.

            Comment

            Working...
            X