肆:炒菜篇
1、通过之前介绍的读取shape文件的py脚本,以下面方式将坐标值转换成wkb格式列表
def ShpToCoord(fileIn):
shpFile=ogr.Open(fileIn)
shpLayer=shpFile.GetLayer()
shpExtent=shpLayer.GetExtent()
frame.sketch.SetExtent(shpExtent)
#print shpExtent
shpFeature=shpLayer.GetNextFeature()
while shpFeature:
geoFeature=shpFeature.GetGeometryRef()
geoWKB=geoFeature.ExportToWkb()
geoList=WkbUnPacker(geoWKB)
if geoList[1]==1:
tmpList=[]
tmpList.extend(geoList[2])
frame.sketch.GetLines(tmpList,'D')
#print 'single dot'
if geoList[1]==2:
tmpList=geoList[2].tolist()
frame.sketch.GetLines(tmpList,'L')
#print 'single polyline'
if geoList[1]==3:
tmpList=geoList[2][0].tolist()
tmpList.extend(tmpList[:2])
frame.sketch.GetLines(tmpList,'P')
#print 'single polygon'
if geoList[1]==4:
for i in range(len(geoList[2])):
tmpList=[]
tmpList.extend(geoList[2][i])
frame.sketch.GetLines(tmpList,'D')
#print 'multi dots '+str(len(geoList[2]))
if geoList[1]==5:
for i in range(len(geoList[2])):
tmpList=geoList[2][i].tolist()
frame.sketch.GetLines(tmpList,'L')
#print 'multi polylines '+str(len(geoList[2]))
if geoList[1]==6:
for i in range(len(geoList[2])):
tmpList=geoList[2][i][0].tolist()
tmpList.extend(tmpList[:2])
frame.sketch.GetLines(tmpList,'P')
#print 'multi polygons '+str(len(geoList[2]))
else:
pass
shpFeature=shpLayer.GetNextFeature()
2、接下来建立窗口,初始化工作包括:
def __init__(self, parent, ID):
wx.Window.__init__(self, parent, ID)
self.SetBackgroundColour("White")#底色
self.color = "Black"#边线颜色
self.brush = wx.Brush("Blue")#填充颜色
self.thickness = 2#边线粗细
self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)#1 创建一个wx.Pen对象
self.lines = []#存储一个图层坐标
self.curLine = []#存储一个feature的坐标
self.pos = (0, 0)
self.size = []
self.extent = []#用以存储图层外接多边形坐标
self.ratio = 0.0#用以存储缩放比例
self.InitBuffer()#执行上篇所述重读缓存数据的函数
3、设置外接多边形的extent
def SetExtent(self, Extent):
if self.extent == []:
for i in range(4): self.extent.append(Extent[i])
else:
if Extent[0] < self.extent[0]: self.extent[0] = Extent[0]
if Extent[1] > self.extent[1]: self.extent[1] = Extent[1]
if Extent[2] < self.extent[2]: self.extent[2] = Extent[2]
if Extent[3] > self.extent[3]: self.extent[3] = Extent[3]
#wx.MessageDialog(None,str(self.extent)).ShowModal()
RatioX = self.size.width / (self.extent[1] - self.extent[0])
RatioY = self.size.height / (self.extent[3] - self.extent[2])
if RatioX < RatioY:
self.ratio = RatioX
else:
self.ratio = RatioY
4、向窗口缓存添加一个feature的坐标
def GetLines(self, List, Type):
while len(List):
y = List.pop()
x = List.pop()
self.curLine.append((x,y))
self.lines.append((Type, self.curLine))#Type用以表示点、线、面
self.curLine = []
self.reInitBuffer = True
5、读取完毕后绘制所有的坐标
def DrawLines(self, dc):
for type, line in self.lines:
pen = wx.Pen(self.color, self.thickness, wx.SOLID)
dc.SetPen(pen)
dline = []
for coords in line:
x_new = (coords[0] - self.extent[0]) * self.ratio
y_new = self.size.height - (coords[1] - self.extent[2]) * self.ratio
dline.append((x_new, y_new))
if type == 'D':
dc.DrawPointList(dline)
if type == 'L':
dc.DrawLines(dline)
if type == 'P':
dc.SetBrush(self.brush)
dc.DrawPolygon(dline)
else: pass
如上,基本完成读取与绘制工作