python实现位置定位_python实现物体定位
標(biāo)簽:
前段時(shí)間利用實(shí)驗(yàn)室的器材寫(xiě)了一個(gè)小小的項(xiàng)目,簡(jiǎn)單的整理了一下(并不完善),現(xiàn)在分享一下。實(shí)驗(yàn)的內(nèi)容是:使用衛(wèi)星定位信息接收器,接收物體的位置信息(包括經(jīng)度緯度等等),然后解析這些數(shù)據(jù),然后根據(jù)經(jīng)度緯度等信息通關(guān)百度地圖API獲取物體的具體位置信息,實(shí)現(xiàn)對(duì)物體的定位!
我使用的開(kāi)發(fā)語(yǔ)言重要是python,主要的有:serial串口通信,MySQLdb庫(kù),urllib2庫(kù),httplib庫(kù)等等。下面就開(kāi)始簡(jiǎn)單的說(shuō)說(shuō)實(shí)驗(yàn)中的問(wèn)題。
1.首先使用的串口通信接收衛(wèi)星接收器傳出來(lái)的信息,我使用串口助手看了一下接收到的信息的樣式,大概如下:
上圖就是實(shí)驗(yàn)過(guò)程中的數(shù)據(jù)部分,我主要提取出來(lái)的是時(shí)間,海拔,經(jīng)度,維度等信息??梢钥闯鲞@些數(shù)據(jù)并不是單獨(dú)成行的所以我需要將他們一個(gè)一個(gè)的提取出來(lái)(這并不是很難),然后利用經(jīng)度緯度信息,使用百度地圖API提供的地址解析服務(wù)獲取物體的具體位置的描述,我使用的是一個(gè)HTTP服務(wù),返回的是一個(gè)json的格式的位置信息,我事先測(cè)試庫(kù)一下,返回的json格式的位置信息大概如下所示:
然后我從中獲取具體位置描述信息,然后將它保存到數(shù)據(jù)庫(kù)中。另外在試驗(yàn)中我使用了兩張表,一張記錄從從串口提取出來(lái)的信息,一張保存解析后的位置信息,而這兩張表有相同的一項(xiàng),就是時(shí)間,表的內(nèi)容后面我會(huì)以圖片的形式放出來(lái)。下面就附上我的代碼,然后給出實(shí)驗(yàn)的結(jié)果。
# coding:utf8
'''
Created on 2016年6月14日
@author: zou
'''
import serial
import MySQLdb
import urllib2
import urllib
import httplib
import json
import time
ser = serial.Serial('COM4',9600)
######################################################
def recv(serial):
data=''
while True:
tmp = serial.read(1)
if tmp == '\n':
break
else:
data += tmp
return data
######################################################
def GetInfo(Str):
info = []
tmp = Str[7:]
strs=''
for ch in tmp:
if ch == '\n':
return
else:
if ch == ',':
info.append(strs)
strs = ''
else:
strs = strs+ch
return info
######################################################
def getYear(data):
retdata = ''
ret = ''
tail = data
#print tail
tail = tail[::-1]
#print tail
count = 0
for ch in tail:
if count == 3: #616022
if ch == ',':
break
else:
ret += ch
elif ch == ',':
count=count+1
#print ret
retdata+=ret[1]
retdata+=ret[0]
retdata+=ret[3]
retdata+=ret[2]
retdata+=ret[5]
retdata+=ret[4]
#print retdata
return retdata
######################################################
def rightNum(strs,flag):
ret=''
if cmp(flag,'t')==0:
#times 024335.00
ret=strs[0:2]
ret+=':'
ret+=strs[2:4]
ret+=':'
ret+=strs[4:6]
elif cmp(flag,'l')==0:#latitude 3422.99947N
if int(strs[0:3]) < 180:
ret=strs[0:3]
ret+='.'
ret+=strs[3:5]
ret+=strs[6:10]
else:
ret=strs[0:2]
ret+='.'
ret+=strs[2:4]
ret+=strs[5:9]
elif cmp(flag,'L')==0:#longitude 10858.95306E
if int(strs[0:3]) < 180:
ret=strs[0:3]
ret+='.'
ret+=strs[3:5]
ret+=strs[6:10]
else:
ret=strs[0:2]
ret+='.'
ret+=strs[2:4]
ret+=strs[5:9]
else:
return None
return ret
#########################################################
def Getlocation(db,ti,la,lo):
#發(fā)送http請(qǐng)求獲取具體位置信息
#import urllib
url = 'http://api.map.baidu.com/geocoder/v2/'
ak = 'ak=1aZ2PQG7OXlk9E41QPvB9WjEgq5WO8Do'
#back='&callback=renderReverse&location='
back='&location='
location='34.992654,108.589507'
output = '&output=json&pois=0'
url = url + '?' + ak + back + location + output
temp = urllib2.urlopen(url)
hjson = json.loads(temp.read())
locate = hjson["result"]["formatted_address"] #省,市,縣
#print locate
mapinfo = hjson["result"]["sematic_description"] #詳細(xì)描述
#print mapinfo
#插入數(shù)據(jù)庫(kù)
cur = db.cursor()
sql="set names utf8"
cur.execute(sql)
info=[]
info.append(ti.encode('utf8'))
info.append(locate.encode('utf8'))
info.append(mapinfo.encode('utf8'))
for val in info:
print val
sql = "insert into mapinfo values(%s,%s,%s)"
try:
cur.execute(sql,info)
except:
print 'Insert mapinfo failed'
#########################################################
#mysql , 經(jīng)度,維度
db = MySQLdb.connect('localhost','root','',"zou",3306,'utf8')
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS Location")
cursor.execute("DROP TABLE IF EXISTS mapinfo")
sql="""CREATE TABLE Location(
Time CHAR(20),
Latitude CHAR(15),
Longitude CHAR(15),
Altitude CHAR(10))"""
cursor.execute(sql)
sql = """CREATE TABLE mapinfo(
time CHAR(20),
local CHAR(100),
info CHAR(100))"""
cursor.execute(sql)
'''
#mysql , 位置描述信息
#database = MySQLdb.connect('localhost','root','',"zou",3306)
#curkey = database.cursor()
#curkey.execute("DROP TABLE IF EXISTS mapinfo")
msql = """CREATE TABLE mapinfo(
time CHAR(20),
local CHAR(100),
info CHAR(100))"""
curkey.execute(msql)
'''
##################################################################
Locat = [] ####
#提取20項(xiàng)數(shù)據(jù)
count=0
while count<10:
Info=[]
year=''
#如果輸出為 $GPGGA 開(kāi)頭,則這一行表示的是位置信息
for val in range(0,8):
data = recv(ser)
tmp = data[0:6] #截取前6個(gè)字符
if cmp(tmp,'$GPRMC') == 0:
#print data
tmpyear = data[50:]
year = getYear(tmpyear)
#print year
elif cmp(tmp,'$GPGGA') == 0: #條件滿足的話就截取
#print data
Info = GetInfo(data)
if Info == []:
break
value=[]
ti = year
ti += '-'
t = rightNum(Info[0],'t')
ti += t
#print ti
value.append(ti)
la = rightNum(Info[1],'l')
value.append(la)
lo = rightNum(Info[3],'L')
value.append(lo)
al = Info[8]
value.append(al)
#print value
sql = "insert into Location values (%s,%s,%s,%s)"
try:
cursor.execute(sql,value)
Getlocation(db,ti,la,lo)
db.commit()
except:
print 'insert error'
count=count+1
#print count
db.close()
############################################################
#關(guān)閉端口
ser.close()實(shí)驗(yàn)的結(jié)果如下:
相應(yīng)的兩張表的內(nèi)容如下:
到這里這個(gè)實(shí)驗(yàn)的內(nèi)容說(shuō)完了,大家若是有什么問(wèn)題的話歡迎給我留言。
標(biāo)簽:
總結(jié)
以上是生活随笔為你收集整理的python实现位置定位_python实现物体定位的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 利用flex实现元素水平垂直居中
- 下一篇: 初中生python怎么教_初中生如何自学