本文整理汇总了Python中shapefile。Writer方法的典型用法代码示例。如果您正苦于以下问题:Pythonshapefile。Writer方法的具体用法?Pythonshapefile。Writer怎么用?Pythonshapefile。Writer使用的例子?那么恭喜您,这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shapefile的用法示例。 在下文中一共展示了shapefile。Writer方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。示例1:init需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕definit(self,filenameNone,shapeType1,hasShxTrue):self。filenamefilenameself。hasShxhasShxCountrecordsformetadataself。count0Maximumnumberofrecordsbeforediskflushself。max1000self。startedFalseself。minx0self。miny0self。maxx0self。maxy0self。numRecs0self。tmpShpcStringIO。StringIO()ifself。hasShx:self。tmpShxcStringIO。StringIO()self。tmpDbfcStringIO。StringIO()self。shpopen(s。shpself。filename,wb)ifself。hasShx:self。shxopen(s。shxself。filename,wb)self。dbfopen(s。dbfself。filename,wb)self。dbfHdrLen0self。wshapefile。Writer(shapeType)示例2:pointfile需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕defpointfile(testfile):withshapefile。Writer(testfile,shapefile。POINT)asshp:shp。field(name,C)shp。point(0,0)shp。record(point1)shp。point(0,1)shp。record(point2)shp。point(1,1)shp。record(point3)shp。point(1,0)shp。record(point4)示例3:polygonfile需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕defpolygonfile(testfile):withshapefile。Writer(testfile,shapefile。POLYGON)asshp:shp。field(name,C)shp。poly(〔〔〔。25,。25〕,〔。25,。25〕,〔。75,。25〕,〔。75,。25〕〕,〕)shp。record(polygon1)shp。poly(〔〔〔。25,。75〕,〔。75,。75〕,〔。5,1。25〕〕〕)shp。record(polygon2)示例4:polylinefile需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕defpolylinefile(testfile):withshapefile。Writer(testfile,shapefile。POLYLINE)asshp:shp。field(name,C)n5xnp。repeat(np。linspace(0,1,n),2)ynp。tile(〔0。375,0。625〕,n)shp。line(〔list(zip(x,y))〕)shp。record(line1)示例5:testbadpoints需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕deftestbadpoints():shpBytesIO()shxBytesIO()dbfBytesIO()wshapefile。Writer(shpshp,shxshx,dbfdbf)w。shapeType3w。field(spam,N)w。line(〔〔〔5,5〕,〔10,10〕〕〕)w。record(37)w。line(〔〔〔5,0〕,〔5,5〕〕〕)w。record(100)w。line(〔〔〔5,5〕,〔0,10〕〕〕)w。record(239)w。close()passalineshapefilehereinsted。pshpBytesIO()pshxBytesIO()pdbfBytesIO()pwshapefile。Writer(shppshp,shxpshx,dbfpdbf)w。shapeType3pw。field(spam,N)pw。line(〔〔〔5,5〕,〔10,10〕〕〕)pw。record(37)pw。line(〔〔〔5,0〕,〔5,5〕〕〕)pw。record(100)pw。line(〔〔〔5,5〕,〔0,10〕〕〕)pw。record(239)pw。close()withraises(ValueError):readshapefile(shp,dbfdbf,pointsshapefilepshp,pointsdbfpdbf)示例6:testpointsbuttoofar需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕deftestpointsbuttoofar():shpBytesIO()shxBytesIO()dbfBytesIO()wshapefile。Writer(shpshp,shxshx,dbfdbf)w。shapeType3w。field(spam,N)w。line(〔〔〔5,5〕,〔10,10〕〕〕)w。record(37)w。line(〔〔〔5,0〕,〔5,5〕〕〕)w。record(100)w。line(〔〔〔5,5〕,〔0,10〕〕〕)w。record(239)w。close()makeapshpBytesIO()pshxBytesIO()pdbfBytesIO()pwshapefile。Writer(shppshp,shxpshx,dbfpdbf)pw。shapeType1pw。field(eggs,N)pw。point(5,0)pw。record(2)pw。point(5,5)pw。record(4)pw。point(0,10)pw。record(8)pw。point(12,10)pw。record(6)pw。close()withraises(ValueError):readshapefile(shp,dbfdbf,pointsshapefilepshp,pointsdbfpdbf)示例7:testpointsbutnotoneone需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕deftestpointsbutnotoneone():shpBytesIO()shxBytesIO()dbfBytesIO()wshapefile。Writer(shpshp,shxshx,dbfdbf)w。shapeType3w。field(spam,N)w。line(〔〔〔5,5〕,〔10,10〕〕〕)w。record(37)w。line(〔〔〔5,0〕,〔5,5〕〕〕)w。record(100)w。line(〔〔〔5,5〕,〔0,10〕〕〕)w。record(239)w。close()makeapshpBytesIO()pshxBytesIO()pdbfBytesIO()pwshapefile。Writer(shppshp,shxpshx,dbfpdbf)pw。shapeType1pw。field(eggs,N)pw。point(5,0)pw。record(2)pw。point(5,5)pw。record(4)pw。point(0,10)pw。record(8)pw。point(10,10)pw。record(6)pw。point(10,10)pw。record(7)pw。close()withraises(ValueError):readshapefile(shp,dbfdbf,pointsshapefilepshp,pointsdbfpdbf)示例8:testpointsbutonemissing需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕deftestpointsbutonemissing():shpBytesIO()shxBytesIO()dbfBytesIO()wshapefile。Writer(shpshp,shxshx,dbfdbf)w。shapeType3w。field(spam,N)w。line(〔〔〔5,5〕,〔10,10〕〕〕)w。record(37)w。line(〔〔〔5,0〕,〔5,5〕〕〕)w。record(100)w。line(〔〔〔5,5〕,〔0,10〕〕〕)w。record(239)w。close()makeapshpBytesIO()pshxBytesIO()pdbfBytesIO()pwshapefile。Writer(shppshp,shxpshx,dbfpdbf)pw。shapeType1pw。field(eggs,N)pw。point(5,0)pw。record(2)pw。point(5,5)pw。record(4)pw。point(0,10)pw。record(8)pw。close()withraises(ValueError):readshapefile(shp,dbfdbf,pointsshapefilepshp,pointsdbfpdbf)示例9:writeshapefile需要导入模块:importshapefile〔as别名〕或者:fromshapefileimportWriter〔as别名〕defwriteshapefile(df,filename,geomtypeline,prjNone):createashapefilegivenapandasDataframethathascoordinatedatainacolumncalledcoords。importshapefiledf〔Name〕df。indexcreateashpfilewriterobjectofgeomtypepointifgeomtypepoint:wshapefile。Writer(shapefile。POINT)elifgeomtypeline:wshapefile。Writer(shapefile。POLYLINE)elifgeomtypepolygon:wshapefile。Writer(shapefile。POLYGON)usethehelpermodetoensuretheofrecordsequalstheofshapes(shapefilearemadeupofshapesandrecords,andneedbothtobevalid)w。autoBalance1addthefieldsforfieldnameindf。columns:w。field(fieldname,C)fork,rowindf。iterrows():w。record(row。tolist())w。line(parts〔row。coords〕)w。save(filename)addprojectiondatatotheshapefile,ifprjisNone:ifnotsepcified,thedefault,projectionisused(PAStatePlane)prjos。path。join(ROOTDIR,swmmiodefsdefault。prj)prjfilepathos。path。splitext(filename)〔0〕。prjshutil。copy(prj,prjfilepath)