#
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
|