Python數據轉換實現代碼深入分析
對于大多數開發人員來說,掌握Python這一編程語言其實是比較容易的,雖然其中有很多不同于其他語言的使用方法,但是其應用方便簡單的特點使其在開發領域中占據主要地位。在這里我們就可以先從Python數據轉換的實現方法來看看這一語言的具體編寫方式。
學了幾天的Python和FME Objects,發現之間教程里的很多對象已經過時了,而且語法和vb相差比較大,但概念還是一樣的,下面寫一段Python代碼,將這幾天學習的對象聯系一下,下面的代碼功能是讀schema feature和data feature,并顯示feature的屬性和關聯的坐標系,并使用FMEDialog對象來設置源和目標,并做格式轉換,以下Python數據轉換代碼經過測試,運行正常。
- import pyfme
- FME_GEOMETRY_TYPE={
- 0:"FME_GEOM_UNDEFINED",
- 1:"FME_GEOM_POINT",
- 2:"FME_GEOM_LINE",
- 4:"FME_GEOM_POLYGON",
- 8:"FME_GEOM_DONUT",
- 256:"FME_GEOM_PIP",
- 512:"FME_GEOM_AGGREGATE",
- }
- session=pyfme.FMESession()
- dialog=session.createDialog()
- pr=dialog.sourcePrompt("","")
- reader=pyfme.FMEReader(pr[0])
- reader.open(pr[1],pr[2])
- pw=dialog.destPrompt("","")
- writer=pyfme.FMEWriter(pw[0])
- writer.open(pw[1],pw[2])
- theend=True
- while theend:
- feature=pyfme.FMEFeature()
- theend=reader.readSchema(feature)
- if theend:
- print "\n------------%s----------" % "Schema Feature Infomation"
- print "Geometry Type: " + str(FME_GEOMETRY_TYPE
[feature.getGeometryType()])- print "Feature Type: " + str(feature.getFeatureType())
- print "CoordinateSystem Name: " + feature.getCoordinateSystem()
- csm=pyfme.FMECoordSysManager()
- csp=csm.getOGCCoordSys(feature.getCoordinateSystem())
- print "CoordinateSystem Information:\n%s\n" % csp
- writer.addSchema(feature)
- theend=True
- while theend:
- feature=pyfme.FMEFeature()
- theend=reader.read(feature)
- if theend:
- print "\n------------%s----------\n" % "Data Feature Infomation"
- print "GeometryType: " + str(FME_GEOMETRY_TYPE
[feature.getGeometryType()])- print "FeatureType: " + str(feature.getFeatureType())
- print "CoordinateSystem: " + feature.getCoordinateSystem()
- print "CoordinateSystem List:\n%s\n" % feature.getCoordinates()
- writer.write(feature)
- reader.close()
- writer.close()
- del reader
- del writer
Python數據轉換運行環境 FME DESKTOP 2009 ,pyfme for Python 2.5,Python 2.5
【編輯推薦】