資源簡介
實現了道格拉斯普克算法的Python源代碼,用于處理數據壓縮
代碼片段和文件信息
#?-*-?coding=utf-8?-*-
import?gpxpy
import?gpxpy.gpx
import?math
from?pyproj?import?Proj
#定義一個點類
class?cPoint:
????x=0.0
????y=0.0
????index=0
????def?__init__(selfxyindex):
????????self.x=x
????????self.y=y
????????self.index=index
#定義道格拉斯普克算法類
class?Douglas:
????cPoints=[]?#存儲點的列表
????D=1??????#在道格拉斯普克算法中需要自定義設置的閾值
????def?__init__(selfcPoints):
????????self.cPoints=cPoints
????def?compress(selfp1p2):
????????oPoints=[]
????????swichvalue=False
????????#判斷是否需要進行抽稀算法
????????#直線一般式方程的3個系數
????????A=(p1.y-p2.y)/math.sqrt(math.pow(p1.y-p2.y2)+math.pow(p1.x-p2.x2))
????????B=(p2.x-p1.x)/math.sqrt(math.pow(p1.y-p2.y2)+math.pow(p1.x-p2.x2))
????????C=(p1.x*p2.y-p2.x*p1.y)/math.sqrt(math.pow(p1.y-p2.y2)+math.pow(p1.x-p2.x2))
????????m=self.cPoints.index(p1)
????????n=self.cPoints.index(p2)
????????#print?m
????????#print?n
????????distance=[]
????????middle=None
????????if(n==m+1):????#判斷起點和終點之間是否有點
????????????return
????????#計算中間點到直線的距離
????????for?i?in?range(m+1n):
????????????d=abs(A*self.cPoints[i].x+B*self.cPoints[i].y+C)/math.sqrt(math.pow(A2)+math.pow(B2))
????????????distance.append(d)
????????dmax=max(distance)
????????#print?distance
????????if?dmax>self.D:
????????????swichvalue=True
????????else:
????????????swichvalue=False
????????#for?i?in?range(09):
????????????#print?self.cPoints[i].x
????????if(swichvalue==False):
????????????for?j?in?range(m+1n):
????????????????#del?self.cPoints[j]
????????????????self.cPoints[j].x=0
????????else:
????????????for?h?in?range(m+1n):
????????????????if(abs(A*self.cPoints[h].x+B*self.cPoints[h].y+C)/math.sqrt(math.pow(A2)+math.pow(B2))==dmax):
????????????????????middle=self.cPoints[h]
????????????self.compress(p1middle)
????????????self.compress(middlep2)
????????#for?i?in?range(0len(self.cPoints)):
????????????#if?self.cPoints[i].x!=0:
????????????????#oPoints.append(self.cPoints[i])
????????return?oPoints
????def?__init__(selfmpoin
- 上一篇:數據壓縮DP算法源代碼
- 下一篇:IEXPath.rar
評論
共有 條評論