通过百度aip获取商场定位(pyodps)
获取ak和sk
sn计算方式
获取经纬度
python版本:2.7
# -*- coding: utf-8 -*-
import urllib
import hashlib
import requests
import json
import math
import datetime as dt
import time
from odps import options
# UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def get_today(bizdate):
yesterday = dt.datetime.strptime(bizdate, "%Y%m%d").date()
today = yesterday + dt.timedelta(days=1)
return today.strftime('%Y%m%d')
def get_preday(bizdate):
yesterday = dt.datetime.strptime(bizdate, "%Y%m%d").date()
today = yesterday - dt.timedelta(days=1)
return today.strftime('%Y%m%d')
def get_one_month_ago(bizdate):
yesterday = dt.datetime.strptime(bizdate, "%Y%m%d").date()
today = yesterday - dt.timedelta(days=30)
return today.strftime('%Y%m%d')
# 传入时间参数
bizdate = args['bizdate']
today = get_today(bizdate)
prebizdate = get_preday(bizdate)
one_month_ago = get_one_month_ago(bizdate)
# 获取定位信息
def getlnglat(address):
url = 'http://api.map.baidu.com'
# output = 'json'
ak = 'xxx'
sk = 'xxx'
# 以get请求为例http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=yourak
queryStr = '/geocoder?address={}&output=json&ak={}'.format(address,ak)
# 对queryStr进行转码,safe内的保留字符不转换
encodedStr = urllib.quote(queryStr, safe="/:=&?#+!$,;'@()*[]")
# 在最后直接追加上sk
rawStr = encodedStr + sk
# md5计算出的sn值
sn = hashlib.md5(urllib.quote_plus(rawStr)).hexdigest()
# 形成http
http = url+ queryStr + '&'+ sn
# 获取http get请求
req = requests.get(http)
# 获取数据
content = req.content.decode()
temp = json.loads(content)
lat = str(temp['result']['location']['lat'])
lng = str(temp['result']['location']['lng'])
# 查询失败
if lat == '39.910925':
lat = '0'
if lng == '116.413384':
lng = '0'
return lng + "," + lat
# 获取门店数据
def get_store_info():
sql = '''
select store_id
, store_name
, store_mall
, longitude
, latitude
from stores
where ds = '{bizdate}'
group by store_id
, store_name
, longitude
, latitude
;
'''.format(bizdate = bizdate)
result = []
with o.execute_sql(sql).open_reader(tunnel =True,limit=False) as reader:
for record in reader:
res = []
store_id = record.store_id
store_name = record.store_name
store_mall = record.store_mall
latitude = record.latitude
longitude = record.longitude
locations = getlnglat(store_mall)
lng = str.split(locations, ",")[0]
lat = str.split(locations, ",")[1]
if lng == '0':
lng = longitude
if lat == '0':
lat = latitude
res.append(store_id)
res.append(store_name)
res.append(store_mall)
res.append(longitude)
res.append(latitude)
res.append(lng)
res.append(lat)
result.append(res)
return result
# 写表
def write_data(datas, table, bizdate):
# -- create table dim_participant_store_stores_locations_mf
# -- (
# -- store_id string comment '门店id',
# -- store_name string comment '门店名称',
# -- store_mall string comment '门店所在商场',
# -- longitude string comment '经度',
# -- latitude string comment '纬度',
# -- baidu_longitude string comment '百度经度',
# -- baidu_latitude string comment '百度纬度'
# -- )comment '门店百度经纬度信息'
# -- partitioned by (ds string comment '分区日期')
# -- ;
# 写入数据
table = o.get_table(table)
ds = 'ds={bizdate}'.format(bizdate=bizdate)
# 删除分区
table.delete_partition(ds, if_exists=True)
print("分区已删除")
with table.open_writer(partition=ds, create_partition=True) as writer:
writer.write(datas)
print("写入完成")
if __name__ == '__main__':
datas = get_store_info()
write_data(datas, 'dim_participant_store_stores_locations_mf', bizdate)