Method and device for determining distance based on longitude and latitude
Patent Information
- Application Number
- CN202410747406.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-11
- Publication Date
- 2025-08-29
- Estimated Expiration
- 2044-06-11
AI Technical Summary
[0002]在实际应用中,很多时候需要通过经纬度计算两点之间精确的地理距离(弧长),而现有技术中求距离方法计算两点之间的距离比较复杂,且在计算平台无法使用浮点数时又很大程度的降低了距离计算结果的精度,满足不了计算精度要求
[0032] This algorithm combines the haversine formula and the cordic algorithm to improve the traditional algorithm for calculating geographic distance based on longitude and latitude. The improved algorithm can adjust the accuracy of the calculation results according to external needs, reducing the complexity of programming the algorithm and improving the algorithm's computational efficiency. In addition, the calculation process uses integer data while meeting the accuracy requirements, which meets the requirements of many computing platforms that cannot use floating-point numbers.
Smart Images

Figure CN118758251B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of distance measurement technology, and in particular to a method, device, computer-readable storage medium, and electronic device for determining distance based on longitude and latitude. Background Art
[0002] In practical applications, it is often necessary to calculate the precise geographic distance (arc length) between two points using longitude and latitude. However, the existing distance calculation methods are relatively complex, and when the computing platform cannot use floating-point numbers, the accuracy of the distance calculation results is greatly reduced, which cannot meet the calculation accuracy requirements. Summary of the Invention
[0003] To address the above problems, the present invention proposes a method, device, computer-readable storage medium, and electronic device for determining distance based on longitude and latitude. Integer data is used in the calculation process while meeting accuracy requirements, thereby improving calculation efficiency.
[0004] In a first aspect, a method for determining distance based on longitude and latitude is provided, the method comprising:
[0005] Get the longitude lon1 and latitude lat1 of the first location, and the longitude lon2 and latitude lat2 of the second location;
[0006] Determine the difference in longitude dlon and latitude dlat between the second location and the first location;
[0007] determining a weight coefficient based on the latitude lat1 of the first position, the latitude lat2 of the second position, the difference in longitude dlon, and the difference in latitude dlat;
[0008] Based on the weight coefficient and the radius of the earth, a distance between the first position and the second position is determined.
[0009] Furthermore, the weight coefficient is determined based on the latitude lat1 of the first position, the latitude lat2 of the second position, the longitude difference dlon, and the latitude difference dlat. The specific formula is:
[0010]
[0011] Wherein, a is the weight coefficient.
[0012] Furthermore, the distance between the first position and the second position is determined based on the weight coefficient and the radius of the earth. The specific formula is:
[0013]
[0014] Wherein, distance is the distance between the first position and the second position.
[0015] In a second aspect, a device for determining distance based on longitude and latitude is provided, comprising:
[0016] An acquisition module, configured to acquire the longitude lon1 and latitude lat1 of the first position, and the longitude lon2 and latitude lat2 of the second position;
[0017] a determination module, configured to determine a difference in longitude dlon and a difference in latitude dlat between the second location and the first location;
[0018] The determination module is further configured to determine a weight coefficient based on the latitude lat1 of the first position, the latitude lat2 of the second position, the difference between the longitudes dlon, and the difference between the latitudes dlat;
[0019] The determination module is further used to determine the distance between the first position and the second position based on the weight coefficient and the radius of the earth.
[0020] Furthermore, the determining module is further configured to perform the following steps:
[0021] The weight coefficient is determined based on the following formula:
[0022]
[0023] Wherein, a is the weight coefficient.
[0024] Furthermore, the determining module is further configured to perform the following steps:
[0025] The distance between the first position and the second position is determined based on the following formula:
[0026]
[0027] Wherein, distance is the distance between the first position and the second position.
[0028] In a third aspect, a computer-readable storage medium is provided, which stores a program or instruction. When the program or instruction is run on a computer, the computer executes the method for determining distance based on longitude and latitude as described in any of the above solutions.
[0029] In a fourth aspect, an electronic device is provided, comprising: a processor coupled to a memory,
[0030] The processor is used to read and execute the computer program stored in the memory to implement the method for determining distance based on longitude and latitude as described in any of the above solutions.
[0031] One or more technical solutions provided in this application have at least the following technical effects or advantages:
[0032] This algorithm combines the haversine formula and the cordic algorithm to improve the traditional algorithm for calculating geographic distance based on longitude and latitude. The improved algorithm can adjust the accuracy of the calculation results according to external needs, reducing the complexity of programming the algorithm and improving the algorithm's computational efficiency. In addition, the calculation process uses integer data while meeting the accuracy requirements, which meets the requirements of many computing platforms that cannot use floating-point numbers.
[0033] Other features and advantages of the present invention will be described in the following description, and in part will become apparent from the description, or will be understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained by the structures pointed out in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following is a brief introduction to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0035] Figure 1 A schematic diagram of a flow chart of a method for determining distance based on longitude and latitude provided in an embodiment of the present invention;
[0036] Figure 2 Schematic diagram of a unit circle in an embodiment of the present invention;
[0037] Figure 3 A schematic diagram of the structure of an apparatus for determining distance based on longitude and latitude provided in an embodiment of the present invention;
[0038] Figure 4 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0039] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.
[0040] Figure 1Schematic diagram of the flow of the method for determining distance based on longitude and latitude provided by an embodiment of the present invention. Figure 1 As shown, the method for determining distance based on longitude and latitude in an embodiment of the present invention includes:
[0041] Step S101: Acquire the longitude lon1 and latitude lat1 of the first position, and the longitude lon2 and latitude lat2 of the second position.
[0042] Step S102: Determine the longitude difference dlon and latitude difference dlat between the second position and the first position.
[0043] in,
[0044] dlon=lon2-lon1,
[0045] dlat=lat2-lat1.
[0046] Step S103: Determine a weight coefficient based on the latitude lat1 of the first position, the latitude lat2 of the second position, the difference between the longitudes dlon and the difference between the latitudes dlat,
[0047] The specific formula is:
[0048]
[0049] Among them, a is the weight coefficient.
[0050] Step S104: Determine the distance between the first position and the second position based on the weight coefficient and the radius of the earth.
[0051] The specific formula is:
[0052]
[0053] Wherein, distance is the distance between the first position and the second position.
[0054] The derivation process of the above formula is as follows:
[0055] The longitude and latitude of the first location A are (lon1, lat1), the longitude and latitude of the second location B are (lon2, lat2), and the radius of the earth is r.
[0056] Combining the latitude and longitude formula for finding geographic location and the haversine formula, we can get the following formula:
[0057]
[0058] distance=c*r*100000(cm) (3),
[0059] in,
[0060] dlon=lon2-lon1 (4),
[0061] dlat=lat2-lat1 (5),
[0062] To facilitate integer operations, formulas (1) to (3) are converted. The conversion process is as follows:
[0063] Considering that the difference in longitude and latitude between two points on the earth in actual applications is very small, combined with the sine operation rule:
[0064] sin(x)=x(x tends to 0) (6),
[0065] The following formula can be obtained:
[0066]
[0067] According to formulas (7) to (8), formulas (1) to (3) are converted to:
[0068]
[0069] distance=c*r*100000(cm) (12),
[0070] From formulas (10) to (12), we can see that the converted distance formula focuses on solving the cosine value. Usually, the algorithm for solving the cosine value is relatively complex and requires the use of floating-point numbers. However, many computing platforms do not support the use of floating-point numbers. We convert the cosine calculation formula based on the cordic algorithm so that integer numbers are used when calculating the cosine value. The specific derivation process is as follows:
[0071] like Figure 2 As shown, on the unit circle, vector P rotates To P' (counterclockwise is positive), there is a coordinate rotation formula:
[0072]
[0073] Extract Easy to see The geometric meaning is just a scaling factor
[0074]
[0075] Treat an angle θ as a series of angles θ i The sum of θ i The selection satisfies:
[0076]
[0077] If you select:
[0078] tanθ0=1 (19),
[0079]
[0080] Then the coordinate rotation formula for the i-th rotation of the unit element is:
[0081] x i+1 =K i [x i -y i ·d i 2 -i ] (twenty three),
[0082] y i+1 =K i [y i +x i ·d i 2 -i ] (twenty four),
[0083] in
[0084]
[0085] d i = ±1 (26),
[0086] To determine d i The value of , introduce a new variable z, defined as:
[0087] z i+1 =z i -d i tan -1 (2 -i ) (27)
[0088] Initialize z to θ, that is, z0 = θ, and according to the conditions, i Perform addition or subtraction of tan -1 (2 -i ) operation, so that the final value of z is 0, this condition is determined by d i Decide:
[0089]
[0090] The iterative process of z is the process of converging z to 0, which is also the process of decomposing θ into a series of θ i process, so z i It can be considered as the remaining angle of the i-th rotation. Assume
[0091]
[0092] As the number of iterations increases, K i Tends to 1, A n The values will also tend to be stable, as shown in the following table:
[0093]
[0094] Therefore, K can be ignored in the iterative formula i =cos(tan -1 2 -i ), then:
[0095] x i+1 =x i -y i ·di · 2 -1 (30),
[0096] y i+1 =y i +x i ·d i 2 -1 (31),
[0097] z i+1 =z i -d i tan -1 (2 -1 ) (32),
[0098]
[0099] Combining formulas (13) to (33), we can obtain the following results:
[0100] x n =A n [x0cos z0-y0sinz0] (34),
[0101] y n =A n [y0cosz0+x0sinz0] (35),
[0102] z n =0 (36),
[0103]
[0104] From formulas (34) to (37), we can get:
[0105] y0=0 (38),
[0106]
[0107] The final result of the iteration is:
[0108] x n =cosz0=cosθ (40),
[0109] It is known that 1 degree of longitude and latitude is 3600 seconds, 45 degrees is 162000 seconds, and the same is true for other degrees and rounded. Since θ 18 = 1s, so the number of iterations is 19.
[0110] Pick
[0111] The x to be iterated n =cosz0=cosθmagnification2 19 times to ensure the accuracy of integer operations during iteration. For dlon and dlat, the unit is milliseconds.
[0112] So the distance formula becomes:
[0113]
[0114] Formulas (41) and (42) are the improved fast algorithms for calculating geographic distance based on longitude and latitude, and integer data are used in the calculation process.
[0115] Different platforms have different computing capabilities, and different situations have different requirements for the accuracy of calculation results. Therefore, when calculating a, the cosine value can be reduced by a certain multiple based on the platform computing power and the accuracy requirements to prevent a from overflowing during the calculation process. This can both meet the platform computing power and achieve the accuracy requirements, but the reduction multiple will result in a loss of accuracy in the calculation result, for example:
[0116] cos(lat1)=cos(lat1) / 2(43)
[0117] cos(lat2))=cos(lat2) / 26(44)
[0118] The fast algorithm for calculating geographic distance based on longitude and latitude becomes:
[0119]
[0120] Figure 3 The embodiment of the present invention also provides a device for determining distance based on longitude and latitude, such as Figure 3 As shown, the device comprises:
[0121] An acquisition module 201 is configured to acquire the longitude lon1 and latitude lat1 of a first location, and the longitude lon2 and latitude lat2 of a second location;
[0122] A determination module 202 is configured to determine a difference in longitude dlon and a difference in latitude dlat between the second location and the first location;
[0123] The determination module 202 is further configured to determine a weight coefficient based on the latitude lat1 of the first position, the latitude lat2 of the second position, the difference in longitude dlon, and the difference in latitude dlat;
[0124] The determination module 202 is further configured to determine the distance between the first position and the second position based on the weight coefficient and the radius of the earth.
[0125] It should be noted here that the above-mentioned acquisition module 201 and determination module 202 correspond to steps S101 to S104 in the method embodiment for determining distance based on longitude and latitude. The examples and application scenarios implemented by the above-mentioned modules and corresponding steps are the same, but are not limited to the contents disclosed in the above-mentioned embodiments.
[0126] The technical solutions in the above embodiments of the present application have at least the following technical effects or advantages:
[0127] This algorithm combines the haversine formula and the cordic algorithm to improve the traditional algorithm for calculating geographic distance based on longitude and latitude. The improved algorithm can adjust the accuracy of the calculation results according to external needs, reducing the complexity of programming the algorithm and improving the algorithm's computational efficiency. In addition, the calculation process uses integer data while meeting the accuracy requirements, which meets the requirements of many computing platforms that cannot use floating-point numbers.
[0128] An embodiment of the present invention further provides a computer-readable storage medium storing a program or instruction. When the program or instruction is executed on a computer, the computer executes the method for determining distance based on longitude and latitude as described in the first embodiment.
[0129] Figure 4 The embodiment of the present invention further provides an electronic device, such as Figure 4 As shown, the electronic device includes: a processor 301, the processor 301 is coupled to a memory 302,
[0130] The processor 301 is configured to read and execute a computer program stored in the memory to implement the method for determining distance based on longitude and latitude as described in the above method embodiment.
[0131] Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for determining distance based on longitude and latitude, characterized in that: include: Get the longitude of the first location lon1 and latitude lat1 , the longitude of the second location lon2 and latitude lat2 ; Determine the difference in longitude between the second location and the first location dlon and the difference in latitude dlat ; Based on the latitude of the first location lat1 , the latitude of the second location lat2 , the difference in longitude dlon The difference between the latitude dlat , determine the weight coefficient : ; Determine the distance between the first position and the second position based on the weight coefficient and the radius of the earth : , Where r is the radius of the Earth.
2. A device for determining distance based on longitude and latitude, characterized in that: include: Acquisition module, used to obtain the longitude of the first location lon1 and latitude lat1 , the longitude of the second location lon2 and latitude lat2 ; A determination module for determining the difference in longitude between the second location and the first location dlon and the difference in latitude dlat ; The determining module is further configured to determine the latitude of the first position based on the latitude of the first position. lat1 , the latitude of the second location lat2 , the difference in longitude dlon The difference between the latitude dlat , determine the weight coefficient : ; The determination module is further configured to determine the distance between the first position and the second position based on the weight coefficient and the radius of the earth. : , Where r is the radius of the Earth.
3. A computer-readable storage medium, characterized in that A program or instruction is stored, and when the program or instruction is run on a computer, the computer is caused to execute the method for determining distance according to latitude and longitude as claimed in claim 1.
4. An electronic device, characterized in that: include: a processor coupled to the memory, The processor is configured to read and execute the computer program stored in the memory to implement the method for determining distance based on latitude and longitude as claimed in claim 1.
Citation Information
Patent Citations
Dynamic camera coordinate mapping establishment method and device and readable storage medium
CN113012047A