#
1 # -- coding:utf-8 -- 2 3 import socket,traceback,os,os.path,sys,time,struct,base64,gzip,array,json,zlib 4 5 ''' 6 ------------------ 7 msghdr 8 cmdtxt 9 \0\0 10 二進制流 11 ----------------- 12 視頻包由三部分構成: MetaMessage數據元封套,控制命令文本(json格式),二進制數據,后兩者之間采用連續兩個\0區分,表示開始二進制流數據 13 [metamsg,cmdtxt,bindata] 14 bindata部分格式、編碼由cmdtxt控制 15 16 # [magic,size,compress,encrypt,version],[command text(json)],[\0\0],[binary data..] 17 ''' 18 19 COMPRESS_NONE = 0 20 COMPRESS_ZLIB = 1 21 COMPRESS_BZIP2 = 2 22 23 ENCRYPT_NONE = 0 24 ENCRYPT_MD5 = 1 25 ENCRYPT_DES = 2 26 27 28 class MessageBase: 29 def __init__(self,type='',bin=None): 30 #self.type = type 31 self.attrs={'msg':type} 32 self.bin = bin 33 34 def getMsg(self): 35 return self.getValue('msg') 36 37 def getValue(self,key): 38 if self.attrs.has_key(key): 39 return self.attrs[key] 40 return None 41 42 def getBinary(self): 43 return self.bin 44 45 def marshall(self): 46 d = json.dumps(self.attrs) 47 if self.bin: 48 d+='\0\0'+ self.bin 49 return d 50 51 @classmethod 52 def unmarshall(cls,d): 53 m = None 54 sep = d.find('\0\0') 55 txt = None 56 bin = None 57 if sep == -1: 58 txt = d 59 else: 60 txt = d[:sep] 61 bin = d[sep+2:] 62 m = MessageBase() 63 try: 64 m.attrs = json.loads(txt) 65 if type(m.attrs) != type({}): 66 return None 67 m.bin = bin 68 except: 69 m = None 70 return m 71 72 class MsgCallReturn(MessageBase): 73 def __init__(self,succ=True,errno=0 ,errmsg='',value=None,bin=None): 74 MessageBase.__init__(self,'callret',bin) 75 self.attrs['succ']=succ 76 self.attrs['errno']=errno 77 self.attrs['errmsg']=errmsg 78 self.attrs['value']=value 79 80 class NetMetaPacket: 81 # [magic,size,compress,encrypt,version],[command text(json)],[\0\0],[binary data..] 82 def __init__(self,msg=None,compress=COMPRESS_NONE,encrypt = ENCRYPT_NONE ): 83 self.msg = msg 84 self.size4 = 0 85 self.compress1 = compress 86 self.encrypt1 = encrypt 87 self.ver4 = 0x01000000 # means 1.0.0.0 88 magic4=0xEFD2BB99 89 90 @classmethod 91 def minSize(cls): 92 return 14 93 94 def marshall(self): 95 d = self.msg.marshall() 96 if self.compress1 == COMPRESS_ZLIB: 97 d = zlib.compress(d) 98 else: 99 self.compress1 = COMPRESS_NONE 100 self.encrypt1 = ENCRYPT_NONE 101 # [magic,size,compress,encrypt,version],[command text(json)],[\0\0],[binary data..] 102 r = struct.pack('!BBI',self.compress1,self.encrypt1,self.ver4) 103 r+= d 104 self.size4 = len(r)+4 105 r = struct.pack('!II', self.magic4,self.size4) + r 106 # size =包含自身變量的整個包大小 107 return r 108 109 110 111 if __name__=='__main__': 112 113 print NetMetaPacket(msg=MsgCallReturn(value=range(10),bin='abc' ),compress=COMPRESS_NONE).marshall() 114 print NetMetaPacket.minSize() 115
摘要: 耗時1天,根據公司應用需求,開發一種簡易的基礎的通信框架,簡化系統其它模塊在網絡通信工作方面的復雜度簡單測試network.py service 做服務器,network.py client做客戶端,傳送多個消息報文,且能響應sock連接和斷開狀態考慮到性能和實際項目對線程需求,故都采用單連接單線程模式,預留select多路復用接口,可見: service.selectIn()Code highl... 閱讀全文
摘要: 與ks108同一個項目,ks102為編寫手持gps模塊,個人、寵物使用Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 # -- coding:utf-8 -- 2... 閱讀全文
摘要: ks108 Gps定位模塊接入服務中心,解碼類:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 # -- coding:utf-8 -- 2 #解碼器定... 閱讀全文
OGR1.6 above python2.4 above 只需要定義fields,roads數據調用serial_shpdata_road()即可一次輸出到shp文件 不適合大批量坐標一次生成shp ogr的問題在于python代碼中初始化layer之后傳遞到其他函數使用將會導致莫名錯誤,應該是個bug 1 #bin.zhang@sw2us.com 2 fields=[ 3 {'name':'roadname','type':FIELD_STRING,'width':30} 4 ] 5 roads=[ 6 { 7 'geom':[(121.1,31.1),(121.101,31.102),(121.101,31.2)], 8 'attrs':('xscott.test',) 9 } 10 ] 11 serial_shpdata_road('d:/temp3/aa.shp',fields,roads) 12 ''' 13 def serial_shpdata_road(file,fields,roads): 14 ''' 15 fields: [{name,type,width}, ] 16 roads - [{'geom':[(x,y),..],'wkt':'','attrs':[name,type,len, ]},..] 17 ''' 18 layer = None 19 20 if os.path.exists(file): 21 os.remove(file) 22 23 makePrjFile(file) 24 ds = driver.CreateDataSource(file) 25 layer = ds.CreateLayer("road",geom_type=ogr.wkbMultiLineString) 26 for f in fields: 27 fld = ogr.FieldDefn(f['name'],f['type']) 28 29 if f['type'] == FIELD_STRING : 30 w = 30 31 if f.has_key('width'): 32 w = f['width'] 33 fld.SetWidth(w) 34 layer.CreateField(fld) 35 ftrdfn = layer.GetLayerDefn() 36 37 for r in roads: 38 g = None 39 if r.has_key('wkt'): 40 g = ogr.CreateGeometryFromWkt(r['wkt']) 41 if r.has_key('geom'): 42 pts = r['geom'] 43 pts = map(lambda pt: '%s %s'%(pt[0],pt[1]),pts) 44 txt = string.join(pts,',') 45 wkt = "MULTILINESTRING((%s))"%txt 46 g = ogr.CreateGeometryFromWkt(wkt) 47 48 ftr = ogr.Feature(ftrdfn) 49 ftr.SetGeometry(g) 50 for n in range(len(r['attrs'])): 51 at = r['attrs'][n] 52 ftr.SetField(n,at) 53 layer.CreateFeature(ftr) 54 layer.SyncToDisk() 55
開發工作都想用python+qt來實現 手頭的項目涉及不同部門的成果,還要與公司產品做無縫接入,必然要用到ctypes,qt,com之類的東西了 數據編碼采用json或者xml 以com方式提供生產系統訪問接口,直覺告訴我實現會有點問題,那就是python+Qt做com組件,這個組件與影像系統通信, com組件訪問顯示qt的ui界面會存在問題,沒有做過嘗試,不知是否能行 Com方式提供服務采用獨立服務器模式還是獨立的組件模式(其實是個線程模型問題,獨立進程的com server還是加載到應用app進程的com object)
花點時間琢磨去........
在弄地圖路網接口時,北京提供的是c的實現版本,本就知道ctypes可以直接調用外部dll,之前一直用swig進行包裝給python使用,好久沒弄都忘了,再弄就嫌煩了,最簡單就用ctypes吧 要用ctypes當然要看文檔了,里面定義結構必須自己手動寫,POINTER,Structure之類的,好煩! 運氣來了,無意之間找到個pyglet的項目里面有個tools/wrap.py的東西,還是個式樣性質的東西,并沒有在他的發行代碼中,但可訪問他的svn可以獲取到,wrap.py輸入一個.h的文件便可自動生成對應的數據結構,試了一下avcodec.h,立馬出來個avcodec.py,爽啊
摘要: ■Overview⌒概述■Runtime Classes⌒實時運行類Attributes⌒屬性Enumerations⌒枚舉■Editor Classes⌒編輯器類Enumerations⌒枚舉■History⌒歷史■Index⌒總索引Clas... 閱讀全文
好久沒碰2d,3d的東西了,翻出個向量類,正好用于道路抽稀時根據轉角來剔除多余的中間節點 1 import os,os.path,sys,time,copy,shutil,math 2 from gameobjects.vector2 import Vector2 3 4 a=(2,6) 5 b= (1,2) 6 c=(5,2.48) 7 8 #計算ba與bc夾角 Labc 9 def pp_distance(p1,p2): 10 return math.sqrt( (p1[0]-p2[0])**2+ (p1[1]-p2[1])**2) 11 12 ba= Vector2.from_points(b,a) 13 bc = Vector2.from_points(b,c) 14 #點乘計算夾角 15 dot = (ba[0]*bc[0]+ba[1]*bc[1] ) 16 x = dot/ (ba.get_magnitude()*bc.get_magnitude() ) 17 angle = (math.acos(x) / math.pi) * 180 18 print angle 19
公司的shp數據都是以秒為單位存儲,要做監控和道路匹配,直接與gps接收坐標無法實現,必須轉換shp單位為度單位 1 #--coding:utf-8--- 2 # brief: 內業shp數據,轉換秒到度為單位 3 # author: scott 4 # date: 2012.1.30 5 # org: --navi.com 6 # 7 # version: v0.1.0 2012.2.1 8 # 1.create doc and test ok, types (included point|line|polygon|multipolygon) be supported 9 # 10 11 import os,os.path,sys,time,copy,shutil 12 from osgeo import ogr 13 14 15 def do_layerPoint(layer): 16 ftr = layer.ResetReading() 17 ftr = layer.GetNextFeature() 18 print 'point num:',layer.GetFeatureCount() 19 print 'extent:',layer.GetExtent() 20 cc = 1 21 while ftr: 22 #print cc 23 cc+=1 24 pt = ftr.GetGeometryRef().GetPoint(0) 25 g = ftr.GetGeometryRef() 26 #print g#,g.ExportKML() 27 if pt[0] >1000 or pt[1] > 1000: 28 g.SetPoint(0,pt[0]/3600.,pt[1]/3600.) 29 #print g 30 31 ''' 32 ng = ogr.Geometry(ogr.wkbPoint) 33 print pt 34 ng.SetPoint(0,pt[0]+40,pt[1]) 35 ftr.SetGeometry(ng) 36 ''' 37 layer.SetFeature(ftr) 38 ftr = layer.GetNextFeature() 39 40 def do_layerLine(layer): 41 ftr = layer.ResetReading() 42 ftr = layer.GetNextFeature() 43 44 while ftr: 45 g = ftr.GetGeometryRef() 46 cnt = g.GetPointCount() 47 cc = 0 48 while cc < cnt: 49 #print g.GetPoint(cc) 50 cc+=1 51 52 for n in range(cnt): 53 pt = g.GetPoint(n) 54 if pt[0]>1000 or pt[1] > 1000: 55 g.SetPoint(n,pt[0]/3600.,pt[1]/3600.0) 56 layer.SetFeature(ftr) 57 58 ftr = layer.GetNextFeature() 59 60 def do_layerPolygon(layer): 61 ftr = layer.ResetReading() 62 ftr = layer.GetNextFeature() 63 64 while ftr: 65 g = ftr.GetGeometryRef() 66 cnt = g.GetGeometryCount() 67 for n in range(cnt): 68 gg = g.GetGeometryRef(n) 69 for m in range(gg.GetPointCount() ): 70 pt = gg.GetPoint(m) 71 #print pt 72 if pt[0]>1000 or pt[1] > 1000: 73 gg.SetPoint(m,pt[0]/3600.,pt[1]/3600.0) 74 layer.SetFeature(ftr) 75 ftr = layer.GetNextFeature() 76 77 def do_shpfile(file): 78 #print file 79 print 'ready file:',file 80 driver = ogr.GetDriverByName('ESRI Shapefile') 81 #shp = driver.Open('e:/shp_data/points.shp',1) # 0 - read , 1 - write 82 shp = driver.Open(file,1) # 0 - read , 1 - write 83 84 layer = shp.GetLayer() 85 86 if layer.GetFeatureCount() == 0: 87 return 88 89 gtyp = layer.GetLayerDefn().GetGeomType() 90 91 if file.lower().find('province') == -1: 92 pass #return 93 94 if gtyp == ogr.wkbPoint: 95 do_layerPoint(layer) 96 elif gtyp == ogr.wkbLineString: 97 do_layerLine(layer) 98 elif gtyp == ogr.wkbPolygon: 99 do_layerPolygon(layer) 100 else: 101 print 'unknown type:',gtyp,' ',file 102 103 layer.SyncToDisk() 104 shp.Destroy() 105 106 107 def convert(shpdir): 108 files = os.listdir(shpdir) 109 for file in files: 110 if file.lower().find('.shp') ==-1: 111 continue 112 113 #if file == 'points.shp': 114 do_shpfile(shpdir+"/"+file) 115 116 if __name__=='__main__': 117 #convert( 'e:/shp_data' ) 118 if sys.argv[1:]: 119 convert(sys.argv[1]) 120 else: 121 convert( 'D:/temp3/mess/MESH/H51F009012') 122
|