Commit 38672e0b authored by gaoming's avatar gaoming

距离更新

parent 1a8bcc2b
import redis
import logging
from django.conf import settings
from django.shortcuts import render
from math import radians, cos, sin, asin, sqrt
from django.http import HttpResponse,JsonResponse
import random
import requests
import ipdb
from gm_rpcd.all import bind,RPCDFaultException
r = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT,db=settings.REDIS_DB,password=settings.REDIS_PASSWORD,decode_responses=True)
rpc_client =settings.RPC_INVOKER
def refresh_cache_locations():
'''刷新缓存中医院的数据'''
print("地理位置信息过期---,重新拿取---")
locations = rpc_client['api/hospital/location']().unwrap()
cache_key="hospital_location"
cache_diuration=24*60*60
#将地理位置信息加入redis缓存
for item in locations:
lat=item.get("baidu_loc_lat") if item.get("baidu_loc_lat") else item.get("google_loc_lat")
lng=item.get("baidu_loc_lng") if item.get("baidu_loc_lng") else item.get("google_loc_lng")
try:
r.geoadd(cache_key,float(lng),float(lat),item.get("id"))
except Exception as e:
print(e)
pass
r.expire(cache_key,cache_diuration)
\ No newline at end of file
......@@ -6,8 +6,11 @@ from math import radians, cos, sin, asin, sqrt
from django.http import HttpResponse,JsonResponse
import random
import requests
import uuid
import ipdb
from gm_rpcd.all import bind,RPCDFaultException
from .unit import refresh_cache_locations
#from rpc.decorators import bind_context, bind
r = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT,db=settings.REDIS_DB,password=settings.REDIS_PASSWORD,decode_responses=True)
......@@ -27,23 +30,9 @@ def nearly_hospitals(q_lat,q_lng,radis=500,unit="km",count=20):
#q_lat,q_lng=40.001930265172,116.4871890387796
#数据缓存时间,现在为24小时
cache_key="hospital_location"
cache_diuration=24*60*60
#ipdb.set_trace()
if not r.exists(cache_key):
print("地理位置信息过期---,重新拿取---")
locations = rpc_client['api/hospital/location']().unwrap()
#将地理位置信息加入redis缓存
for item in locations:
lat=item.get("baidu_loc_lat") if item.get("baidu_loc_lat") else item.get("google_loc_lat")
lng=item.get("baidu_loc_lng") if item.get("baidu_loc_lng") else item.get("google_loc_lng")
try:
r.geoadd(cache_key,float(lng),float(lat),item.get("id"))
except Exception as e:
print(e)
pass
r.expire(cache_key,cache_diuration)
refresh_cache_locations()
result=r.georadius(cache_key,q_lng,q_lat,radis,unit,withdist=True,count=20)
......@@ -54,6 +43,39 @@ def nearly_hospitals(q_lat,q_lng,radis=500,unit="km",count=20):
}
return data
@bind("geoserver/home/hospitals_distance")
def specify_hospitals_distance(q_lng,q_lat,hospital_ids):
'''
到指定医院距离排序
'''
cache_key="hospital_location"
#锁定临时的key
result=[]
temp_key="hospital_location_temp"
lock_key="hospital_location_lock"
with r.lock(lock_key, blocking_timeout=15) as lock:
r.geoadd(cache_key,float(q_lng),float(q_lat),temp_key)
for item in hospital_ids:
distance=r.geodist(cache_key,temp_key,item)
if not distance:
distance=0
result.append({"id":item,"distance":distance})
#排序
result=sorted(result,key=lambda x:x.get("distance"))
data={
"result":True,
"data":result,
"message":""
}
return data
def get_location_by_address(request,address):
"""由地址获获取经纬度坐标
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment