mongo添加地圖索引時常會遇到兩個問題: 坐標(biāo)點沖突和坐標(biāo)點重合, 所以之前寫了個腳本解決一下, 主要思路就是:
當(dāng)添加地圖索引報錯時, 根據(jù)error信息,判斷坐標(biāo)點沖突or 坐標(biāo)點重合的地方, 根據(jù)正則修正坐標(biāo)點, 然后通過遞歸or循環(huán)的方式, 重新嘗試, 代碼如下:

-*- coding: utf-8 -*-
import pymongo
import re
conn = pymongo.MongoClient(host='127.0.0.1', port=27017)
citytest2 = conn."db_name"."collection_name" # 連接表
i = 0 # 用來統(tǒng)計刪除坐標(biāo)點次數(shù)及防止棧溢出
def decoordinates(citytest2):
'''
名稱: decoordinates
功能: 添加坐標(biāo)索引,若發(fā)生坐標(biāo)沖突,則刪除靠后的坐標(biāo),直至成功(坐標(biāo)點相同問題尚未添加)
return: None
'''
try:
global i
citytest2.create_index([("polygons", "2dsphere")])
print(i)
except Exception as e:
global i
e = str(e)
retest = re.compile(r'\[(.*?)\]')
if "Duplicate vertices" in str(e): # 坐標(biāo)重合問題
retest2 = re.compile(r'and (\d+)')
test2 = int(retest2.findall(str(e))[0]) # 獲取出錯在第幾條
print test2
elif "locations in degrees" in str(e): # 坐標(biāo)沖突問題
retest2 = re.compile(r'(\d+?)\s+?cross')
test2 = retest2.findall(str(e))[0] # 獲取出錯在第幾條
i += 1
idtest = re.compile(r'_id:\s+(\d+)')
idtest = idtest.findall(str(e))[0] # 獲取出錯id
test = ' [' + retest.findall(str(e))[int(test2)].strip() + '],' # 獲取該條出錯的坐標(biāo)
errortest = citytest2.find_one({"_id": int(idtest)})
e = eval(str(errortest).replace(test, ''))
citytest2.update({"_id": int(idtest)}, e)
if i < 1000:
decoordinates(citytest2)
if name == 'main':
decoordinates(citytest2)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:python實現(xiàn)mongo自動添加地圖索引,自動修正遇到的問題-創(chuàng)新互聯(lián)
文章位置:http://chinadenli.net/article2/diiioc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、商城網(wǎng)站、企業(yè)網(wǎng)站制作、App開發(fā)、網(wǎng)站維護、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容