• 用Python做GIS:上菜篇


    伍:上菜篇

    1、wkb源代码


    2、sketch.py源代码

    #
    -*- encoding:GBK -*- 
    import wx

    class SketchWindow(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 
    = 2
    self.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.0
    self.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)
    #Menu
            menuBar=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,故使用parent
        def InitBuffer(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 
    = False
    def GetLinesData(self):
    return self.lines[:]
    def SetLinesData(self, lines):
    self.lines 
    = lines[:]
    self.InitBuffer()
    self.Refresh()
    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
    def GetLines(self, List, Type):
    while len(List):
    = List.pop()
    = List.pop()
    self.curLine.append((x,y))
    self.lines.append((Type, self.curLine))
    self.curLine 
    = []
    self.reInitBuffer 
    = True
    def OnSize(self, event):
    self.reInitBuffer 
    = True #11 处理一个resize事件
        def OnIdle(self, event):#12 空闲时的处理
            if self.reInitBuffer:
    self.InitBuffer()
    self.Refresh(False)
    def OnPaint(self, event):
    dc 
    = wx.BufferedPaintDC(self, self.buffer)#13 处理一个paint(描绘)请求
        def OnLine(self, event):
    colorData 
    = wx.ColourData()
    colorData.SetColour(self.color)
    dlg 
    = wx.ColourDialog(self, colorData)
    if dlg.ShowModal() == wx.ID_OK:
    colorData 
    = dlg.GetColourData()
    self.SetColor(colorData.GetColour())
    self.reInitBuffer 
    = True
    dlg.Destroy()
    def OnPolygon(self, event):
    colorData 
    = wx.ColourData()
    colorData.SetColour(self.brush.GetColour())
    dlg 
    = wx.ColourDialog(self, colorData)
    if dlg.ShowModal() == wx.ID_OK:
    colorData 
    = dlg.GetColourData()
    self.brush 
    = wx.Brush(colorData.GetColour())
    self.reInitBuffer 
    = True
    dlg.Destroy()
    #14 绘制所有的线条
        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)
    elsepass
    def SetColor(self, color):
    self.color 
    = color
    self.pen 
    = wx.Pen(self.color, self.thickness, wx.SOLID)
    def SetThickness(self, num):
    self.thickness 
    = num
    self.pen 
    = wx.Pen(self.color, self.thickness, wx.SOLID)

    class SketchFrame(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)
    def OnQuit(self, event):
    dlg 
    = wx.MessageDialog(None,'确定退出?','提示',wx.YES_NO|wx.ICON_QUESTION)
    result 
    = dlg.ShowModal()
    if result == 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 -*- 
    from wkb import *
    from sketch import *

    def OnOpen(event):  
    dialog 
    = wx.FileDialog(None, '打开Shape文件''.''''Shape File (*.shp)|*.shp|All Files (*.*)|*.*', style = wx.OPEN )
    if dialog.ShowModal() == wx.ID_OK:
    ShpToCoord(dialog.GetPath())
    dialog.Destroy()

    def OnClose(event):
    frame.sketch.SetLinesData([])
    frame.sketch.extent 
    = []

    def ShpToCoord(fileIn):

    shpFile
    =ogr.Open(fileIn)
    shpLayer
    =shpFile.GetLayer()
    shpExtent
    =shpLayer.GetExtent()
    frame.sketch.SetExtent(shpExtent)
    #print shpExtent
        shpFeature=shpLayer.GetNextFeature()

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

    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()

    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()



    e-mail:shisong.zhu@gmail.com
    GISer in China, for engineering
  • 相关阅读:
    支付相关备忘
    大文件跨服务器传送
    redis与memcache区别总结
    linux grep 查找字符串
    db2 连接报错connect。 ERRORCODE=-4499, SQLSTATE=08001(转载)
    linux查看匹配内容的前后几行(转)
    weblogic线程阻塞性能调优(图解)转
    ThreadPool has stuck threads
    Shell脚本中调用另外一个脚本的方法
    MUTT+MSMTP利用163服务器发送邮件
  • 原文地址:https://www.cnblogs.com/columbus2/p/1130102.html
Copyright © 2020-2023  润新知