1.梳理、总结经纬度处理在Maxcompute平台上的实战应用,如根据gps轨迹数据计算停留点信息。
2.欢迎批评指正,跪谢一键三连!
1.参考代码
#!/usr/bin/env python # coding: utf-8 import numpy as np from odps.udf import annotate from odps.udf import BaseUDAF from datetime import datetime @annotate('string,string,double,string->string') class ret_tld_by_gps(BaseUDAF): """计算停留点""" def new_buffer(self): return [] def iterate(self, buffer, lati, longi, speed, gpstime): buffer.append([lati, longi, speed, gpstime]) def merge(self, buffer, pbuffer): buffer += pbuffer def terminate(self, buffer): buf = np.array(buffer) # try: buf2 = buf[:, 3] # return '%s %s %d %d' % (type(buf), type(buffer), buf.shape[0], buf.shape[1]) index = np.lexsort((buf2,)) buf3 = buf[index] # return '%s %d %d' % (','.join(buf3[0]), buf3.shape[0], buf3.shape[1]) tld = [] cnt, total_cnt, _flag = 0, 0, 0 lati ,longi ,speed ,first_gpstime ,last_gpstime = '','',0.0,'','' for rw in buf3: total_cnt += 1 if float(rw[2])>0: if _flag==1: # 速度变化点 if cnt==1: last_gpstime = first_gpstime # return '114, %d %s %d ' % (_flag, str(rw[2]), _flag) # if (first_gpstime<>'') and (last_gpstime<>''): diff_time = (datetime.strptime(last_gpstime, "%Y-%m-%d %H:%M:%S")-datetime.strptime(first_gpstime, "%Y-%m-%d %H:%M:%S")).seconds # tld.append(','.join([lati, longi, first_gpstime, last_gpstime, '%.2f' % (diff_time/60.0) , str(cnt)])) if (diff_time>=600) and (diff_time<=2400): tld.append(','.join([lati, longi, first_gpstime, last_gpstime, '%.2f' % (diff_time/60.0) , str(cnt)])) _flag, cnt = 0, 0 elif float(rw[2])<=0: # 起始停留时间 cnt += 1 if _flag==0: # 持续为0点轨迹点 lati, longi = rw[0], rw[1] first_gpstime, _flag = rw[3], 1 else: last_gpstime = rw[3] if cnt!=1: # print('') # if (first_gpstime<>'') and (last_gpstime<>''): diff_time = (datetime.strptime(last_gpstime, "%Y-%m-%d %H:%M:%S")-datetime.strptime(first_gpstime, "%Y-%m-%d %H:%M:%S")).seconds if (diff_time>=600) and (diff_time<=2400): tld.append(','.join([lati, longi, first_gpstime, last_gpstime, '%.2f' % (diff_time/60.0) , str(cnt)])) # return '%s' % (str(total_cnt)) return '%s' % (';'.join(tld))