当前位置: 首页 > 工具软件 > pop-gis > 使用案例 >

python gis框架_用Python做GIS:上菜篇

柏高洁
2023-12-01

伍:上菜篇1、wkb源代码

2、sketch.py源代码#-*- encoding:GBK -*-importwxclassSketchWindow(wx.Window):def__init__(self, parent, ID):

wx.Window.__init__(self, parent, ID)

self.SetBackgroundColour("White")

self.color="Black"self.brush=wx.Brush("Blue")

self.thickness=2self.pen=wx.Pen(self.color, self.thickness, wx.SOLID)#1 创建一个wx.Pen对象self.lines=[]

self.curLine=[]

self.pos=(0, 0)

self.size=[]

self.extent=[]

self.ratio=0.0self.InitBuffer()#2 连接事件self.Bind(wx.EVT_SIZE, self.OnSize)

self.Bind(wx.EVT_IDLE, self.OnIdle)

self.Bind(wx.EVT_PAINT, self.OnPaint)#self.Bind(wx.EVT_MENU, self.OnQuit, id=109)wx.EVT_MENU(parent,211, self.OnLine)

wx.EVT_MENU(parent,212, self.OnPolygon)#MenumenuBar=wx.MenuBar()

mFile=wx.Menu()

mFile.Append(101,'打开(&O)','打开文件')

mFile.Append(102,'保存(&S)','保存文件')

mFile.Append(103,'关闭(&C)','关闭文件')

mFile.AppendSeparator()

mFile.Append(109,'退出(&X)','退出系统')

menuBar.Append(mFile,'文件(&F)')

mView=wx.Menu()

mView.Append(201,'放大(&I)','放大视图')

mView.Append(202,'缩小(&O)','缩小视图')

mView.Append(203,'平移(&P)','平移视图')

mView.AppendSeparator()

mView.Append(211,'线划(&L)','线段样式')

mView.Append(212,'填充(&S)','填充样式')

menuBar.Append(mView,'视图(&V)')

parent.SetMenuBar(menuBar)#parent:SetMenuBar对应于frame,故使用parentdefInitBuffer(self):

self.size=self.GetClientSize()#3 创建一个缓存的设备上下文self.buffer=wx.EmptyBitmap(self.size.width, self.size.height)

dc=wx.BufferedDC(None, self.buffer)#dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)#4 使用设备上下文dc.SetBackground(wx.Brush(self.GetBackgroundColour()))

dc.Clear()

self.DrawLines(dc)

self.reInitBuffer=FalsedefGetLinesData(self):returnself.lines[:]defSetLinesData(self, lines):

self.lines=lines[:]

self.InitBuffer()

self.Refresh()defSetExtent(self, Extent):ifself.extent==[]:foriinrange(4):  self.extent.append(Extent[i])else:ifExtent[0]self.extent[1]:  self.extent[1]=Extent[1]ifExtent[2]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])ifRatioX

self.ratio=RatioXelse:

self.ratio=RatioYdefGetLines(self, List, Type):whilelen(List):

y=List.pop()

x=List.pop()

self.curLine.append((x,y))

self.lines.append((Type, self.curLine))

self.curLine=[]

self.reInitBuffer=TruedefOnSize(self, event):

self.reInitBuffer=True#11 处理一个resize事件defOnIdle(self, event):#12 空闲时的处理ifself.reInitBuffer:

self.InitBuffer()

self.Refresh(False)defOnPaint(self, event):

dc=wx.BufferedPaintDC(self, self.buffer)#13 处理一个paint(描绘)请求defOnLine(self, event):

colorData=wx.ColourData()

colorData.SetColour(self.color)

dlg=wx.ColourDialog(self, colorData)ifdlg.ShowModal()==wx.ID_OK:

colorData=dlg.GetColourData()

self.SetColor(colorData.GetColour())

self.reInitBuffer=True

dlg.Destroy()defOnPolygon(self, event):

colorData=wx.ColourData()

colorData.SetColour(self.brush.GetColour())

dlg=wx.ColourDialog(self, colorData)ifdlg.ShowModal()==wx.ID_OK:

colorData=dlg.GetColourData()

self.brush=wx.Brush(colorData.GetColour())

self.reInitBuffer=True

dlg.Destroy()#14 绘制所有的线条defDrawLines(self, dc):fortype, lineinself.lines:

pen=wx.Pen(self.color, self.thickness, wx.SOLID)

dc.SetPen(pen)

dline=[]forcoordsinline:

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))iftype=='D':

dc.DrawPointList(dline)iftype=='L':

dc.DrawLines(dline)iftype=='P':

dc.SetBrush(self.brush)

dc.DrawPolygon(dline)else:passdefSetColor(self, color):

self.color=color

self.pen=wx.Pen(self.color, self.thickness, wx.SOLID)defSetThickness(self, num):

self.thickness=num

self.pen=wx.Pen(self.color, self.thickness, wx.SOLID)classSketchFrame(wx.Frame):def__init__(self, parent):

wx.Frame.__init__(self, parent,-1,"Sketch Shape Show", size=(1024,768))

self.sketch=SketchWindow(self,-1)

wx.EVT_MENU(self,109, self.OnQuit)defOnQuit(self, event):

dlg=wx.MessageDialog(None,'确定退出?','提示',wx.YES_NO|wx.ICON_QUESTION)

result=dlg.ShowModal()ifresult==wx.ID_YES:

self.Close()

dlg.Destroy()if__name__=='__main__':

app=wx.PySimpleApp()

frame=SketchFrame(None)

frame.Show(True)

frame.sketch.GetLines([1,2,156,124,158,756,451,784,125,441],[0,500,1,800])

app.MainLoop()

3、readShp.py源代码

#-*- encoding:GBK -*-fromwkbimport*fromsketchimport*defOnOpen(event):

dialog=wx.FileDialog(None,'打开Shape文件','.','','Shape File (*.shp)|*.shp|All Files (*.*)|*.*', style=wx.OPEN )ifdialog.ShowModal()==wx.ID_OK:

ShpToCoord(dialog.GetPath())

dialog.Destroy()defOnClose(event):

frame.sketch.SetLinesData([])

frame.sketch.extent=[]defShpToCoord(fileIn):

shpFile=ogr.Open(fileIn)

shpLayer=shpFile.GetLayer()

shpExtent=shpLayer.GetExtent()

frame.sketch.SetExtent(shpExtent)#print shpExtentshpFeature=shpLayer.GetNextFeature()

frame.SetCursor(wx.StockCursor(wx.CURSOR_ARROWWAIT))whileshpFeature:

geoFeature=shpFeature.GetGeometryRef()

geoWKB=geoFeature.ExportToWkb()

geoList=WkbUnPacker(geoWKB)ifgeoList[1]==1:

tmpList=[]

tmpList.extend(geoList[2])

frame.sketch.GetLines(tmpList,'D')#print 'single dot'ifgeoList[1]==2:

tmpList=geoList[2].tolist()

frame.sketch.GetLines(tmpList,'L')#print 'single polyline'ifgeoList[1]==3:

tmpList=geoList[2][0].tolist()

tmpList.extend(tmpList[:2])

frame.sketch.GetLines(tmpList,'P')#print 'single polygon'ifgeoList[1]==4:foriinrange(len(geoList[2])):

tmpList=[]

tmpList.extend(geoList[2][i])

frame.sketch.GetLines(tmpList,'D')#print 'multi dots '+str(len(geoList[2]))ifgeoList[1]==5:foriinrange(len(geoList[2])):

tmpList=geoList[2][i].tolist()

frame.sketch.GetLines(tmpList,'L')#print 'multi polylines '+str(len(geoList[2]))ifgeoList[1]==6:foriinrange(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:passshpFeature=shpLayer.GetNextFeature()

frame.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))if__name__=='__main__':

app=wx.PySimpleApp()

frame=SketchFrame(None)

wx.EVT_MENU(frame,101, OnOpen)

wx.EVT_MENU(frame,103, OnClose)

frame.Show()

app.MainLoop()

 类似资料: