地理位置geo处理之mysql函数的详细介绍(附代码)


本文摘自php中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于地理位置geo处理之mysql函数的详细介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

目前越来越多的业务都会基于LBS,附近的人,外卖位置,附近商家等等,现就讨论离我最近这一业务场景的解决方案。

目前已知解决方案有:

mysql 自定义函数计算mysql geo索引mongodb geo索引postgresql PostGis索引redis geoElasticSearch

本文测试下mysql 函数运算的性能

准备工作

创建数据表

1

2

3

4

5

6

CREATE TABLE `driver` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `lng` float DEFAULT NULL,

  `lat` float DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

创建测试数据

在创建数据之前先了解下基本的地理知识:

  • 全球经纬度的取值范围为: 纬度-90~90,经度-180~180
  • 中国的经纬度范围大约为: 纬度3.86~53.55,经度73.66~135.05
  • 北京行政中心的纬度为39.92,经度为116.46
  • 越北面的地方纬度数值越大,越东面的地方经度数值越大
  • 度分转换: 将度分单位数据转换为度单位数据,公式:度=度+分/60
  • 分秒转换: 将度分秒单位数据转换为度单位数据,公式:度 = 度 + 分 / 60 + 秒 / 60 / 60

在纬度相等的情况下:

  • 经度每隔0.00001度,距离相差约1米

在经度相等的情况下:

  • 纬度每隔0.00001度,距离相差约1.1米

mysql函数计算

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

DELIMITER //

CREATE DEFINER=`root`@`localhost` FUNCTION `getDistance`(

    `lng1` float(10,7)

    ,

    `lat1` float(10,7)

    ,

    `lng2` float(10,7)

    ,

    `lat2` float(10,7)

 

) RETURNS double

    COMMENT '计算2坐标点距离'

BEGIN

    declare d double;

    declare radius int;

    set radius = 6371000; #假设地球为正球形,直径为6371000米

    set d = (2*ATAN2(SQRT(SIN((lat1-lat2)*PI()/180/2)  

        *SIN((lat1-lat2)*PI()/180/2)+  

        COS(lat2*PI()/180)*COS(lat1*PI()/180)  

        *SIN((lng1-lng2)*PI()/180/2)  

        *SIN((lng1-lng2)*PI()/180/2)),  

        SQRT(1-SIN((lat1-lat2)*PI()/180/2)  

        *SIN((lat1-lat2)*PI()/180/2)  

        +COS(lat2*PI()/180)*COS(lat1*PI()/180)  

        *SIN((lng1-lng2)*PI()/180/2)  

        *SIN((lng1-lng2)*PI()/180/2))))*radius;

    return d;

END//

DELIMITER ;

创建数据python脚本

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

# coding=utf-8

from orator import DatabaseManager, Model

import logging

import random

import threading

 

""" 中国的经纬度范围 纬度3.86~53.55,经度73.66~135.05。大概0.00001度差距1米 """

 

# 创建 日志 对象

logger = logging.getLogger()

handler = logging.StreamHandler()

formatter = logging.Formatter(

    '%(asctime)s %(name)-12s %(levelname)-8s %(message)s')

handler.setFormatter(formatter)

logger.addHandler(handler)

logger.setLevel(logging.DEBUG)

 

# Connect to the database

 

config = {

    'mysql': {

        'driver': 'mysql',

        'host': 'localhost',

        'database': 'dbtest',

        'user': 'root',

        'password': '',

        'prefix': ''

    }

}

 

db = DatabaseManager(config)

Model.set_connection_resolver(db)

 

 

class Driver(Model):

    __table__ = 'driver'

    __timestamps__ = False

    pass

 

 

def ins_driver(thread_name,nums):

    logger.info('开启线程%s' % thread_name)

    for _ in range(nums):

        lng = '%.5f' % random.uniform(73.66, 135.05)

        lat = '%.5f' % random.uniform(3.86, 53.55)

 

        driver = Driver()

        driver.lng = lng

        driver.lat = lat

        driver.save()

 

thread_nums = 10

for i in range(thread_nums):

    t = threading.Thread(target=ins_driver, args=(i, 400000))

    t.start()

4033700-dda526bdfcc5c759.png

阅读剩余部分

相关阅读 >>

Python如何调用api接口

Python tkinter教程 数字猜谜游戏

如何利用Python函数求导数

Python是怎么操作文件和目录的?

Python如何安装库

Python线程优先级队列是什么?线程优先级的设置方法有哪些?

Python实现的hmacmd5加密算法示例_Python

基于Python requests库中的代理实例

Python里int什么意思

Python要安装什么软件

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...