楼主: diannaoasd
2653 4

[编程问题求助] 500论坛币求stata代码,关于坐标距离计算 [推广有奖]

  • 8关注
  • 7粉丝

已卖:1334份资源

讲师

32%

还不是VIP/贵宾

-

威望
0
论坛币
79917 个
通用积分
175.0726
学术水平
9 点
热心指数
5 点
信用等级
5 点
经验
5816 点
帖子
180
精华
0
在线时间
621 小时
注册时间
2010-12-6
最后登录
2025-11-4

楼主
diannaoasd 发表于 2025-7-23 18:14:46 |AI写论文
500论坛币
(1)有3个基准经纬度坐标,分别是(110.23 36.23;111.33 33.45;116.80 40.35)
(2)有9个id的坐标,如示例数据。
(3)在第一个基准坐标(110.23 36.23)半径5km之内,或者,在第二个基准坐标(111.33 33.45)半径3km之内,或者在第三个基准坐标半径1km之内,wanted=1,否则wanted=0.
(4)实际数据量,id有几万条,基准坐标有几千个。请问代码如何编写呢?

  1. * Example generated by -dataex-. For more info, type help dataex
  2. clear
  3. input float(id lon lat) str3 wanted是否在范围内
  4. 1 116.78294 39.60197 "?"
  5. 2 115.97266 40.44241 "?"
  6. 3 116.11597 39.89557 "?"
  7. 4 116.49863 39.80755 "?"
  8. 5  116.8082  40.3568 "?"
  9. 6 116.13786 39.71119 "?"
  10. 7 116.64865 40.12924 "?"
  11. 8 115.91908 40.36859 "?"
  12. 9 116.66607 39.64987 "?"
  13. end
复制代码



最佳答案

kirin_guess 查看完整内容

The code is fine, but there seems a problem with the "复制代码" button. Don't hit that button. To copy the code, highlight it, right-click, and select 'Copy.' Alternatively, use the attached do file: Once the code is executed, the output will appear as follows:
关键词:500论坛币 Stata tata 0论坛币 论坛币

沙发
kirin_guess 发表于 2025-7-23 18:14:47
diannaoasd 发表于 2025-7-26 08:27
stata提示:refcoord not foundinvalid syntax
The code is fine, but there seems a problem with the "复制代码" button.
Don't hit that button. To copy the code, highlight it, right-click, and select 'Copy.'

Alternatively, use the attached do file: dist_20250724.do (1.03 KB)

Once the code is executed, the output will appear as follows:
Output

藤椅
kirin_guess 发表于 2025-7-24 03:19:43
Try the following code to compute distance:
  1. clear all
  2. input float(id lon lat)
  3. 1 116.78294 39.60197
  4. 2 115.97266 40.44241
  5. 3 116.11597 39.89557
  6. 4 116.49863 39.80755
  7. 5 116.80820 40.35680
  8. 6 116.13786 39.71119
  9. 7 116.64865 40.12924
  10. 8 115.91908 40.36859
  11. 9 116.66607 39.64987
  12. end

  13. scalar lon1 =110.23 //point1
  14. scalar lat1 = 36.23

  15. scalar lon2 =111.33 //point2
  16. scalar lat2 = 33.45

  17. scalar lon3 =116.80 //point3
  18. scalar lat3 = 40.35

  19. scalar earthradius =6371
  20. scalar radians =c(pi) /180

  21. forvalues i =1/3 {
  22.     generate double dist`i'= ///
  23.         2 *earthradius * ///
  24.         asin(sqrt((1 - ///
  25.         cos((lat`i' -lat) *radians) + ///
  26.         cos( lat`i'       *radians) * ///
  27.         cos(         lat  *radians) * ///
  28.     (1 -cos((lon`i' -lon) *radians)))/2))
  29. }

  30. label variable dist1 "distance from point1 in km"
  31. label variable dist2 "distance from point2 in km"
复制代码

See also the Haversine formula: https://en.wikipedia.org/wiki/Haversine_formula

板凳
kirin_guess 发表于 2025-7-24 14:51:19
Here is an updated version:
  1. clear all
  2. input float(id lon lat wanted)
  3. 1  116.78294 39.60197 0
  4. 2  115.97266 40.44241 0
  5. 3  116.11597 39.89557 0
  6. 4  116.49863 39.80755 0
  7. 5  116.80820 40.35680 0
  8. 6  116.13786 39.71119 0
  9. 7  116.64865 40.12924 0
  10. 8  115.91908 40.36859 0
  11. 9  116.66607 39.64987 0
  12. end

  13. matrix refcoord =110.23, 36.23, 5 \ ///
  14.                  111.33, 33.45, 3 \ ///
  15.                  116.80, 40.35, 1
  16. matrix colname refcoord ="lon" "lat" "radius"
  17. //this refcoord matrix may be imported from external files

  18. scalar earthradius =6371
  19. scalar radians =c(pi) /180

  20. forvalues i =1/`=rowsof(refcoord)' {
  21.     loc loni =el(refcoord,`i',1)
  22.     loc lati =el(refcoord,`i',2)
  23.     loc radi =el(refcoord,`i',3)

  24.     capture drop dist
  25.     generate double dist= ///
  26.         2 *earthradius * ///
  27.         asin(sqrt((1 - ///
  28.         cos((`lati' -lat) *radians) + ///
  29.         cos( `lati'       *radians) * ///
  30.         cos(         lat  *radians) * ///
  31.     (1 -cos((`loni' -lon) *radians)))/2))

  32.     replace wanted =`i' if wanted==0 & dist<=`radi'
  33. }
复制代码


报纸
diannaoasd 发表于 2025-7-26 08:27:07
kirin_guess 发表于 2025-7-24 14:51
Here is an updated version:
stata提示:refcoord not foundinvalid syntax

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-5 14:58