python gis 实例_用Python作GIS之五:从示例入手—example函数
進(jìn)入STARS后,最簡單的學(xué)習(xí)方法就是演示示例數(shù)據(jù)。對(duì)于源碼的分析也可以從這里入手。
??????
以下為出發(fā)菜單項(xiàng)“Example Project”的函數(shù)example:
def example(self):
"""canned loading of data files and matrices for debugging"""
self.project = SProject("current",self.master,self)??????? topDir = options.getSTARSHOME()
self.project.directory = os.path.join(topDir,"data")
projectFile = os.path.join(self.project.directory,"csiss.prj")
t=self.project.ReadProjectFile(projectFile)
if hasattr(self.project,"coords"):
self.project.scaleCoords()
else:
self.report("no coords in project")
#self.master.configure(cursor=options.DEFAULTCURSOR)
#self.Editor.configure(cursor='crosshair')
self.projectSummary()??????? self.enableMenus()
加粗標(biāo)注的幾行代碼可作進(jìn)一步詳細(xì)了解:
(1)SProject:
代碼如下所示
class SProject(Project):
"""Subclass to compose STARS project inside a GUI
"""
def __init__(self,name,master,app):
"""Constructor
name (string): name of project
master (Tk): application top level window
app (App): App instance
"""
Project.__init__(self,name)
self.master = master
self.app = app
self.fnt=("Times",10)
self.screenHeight = self.app.winfo_screenheight()
self.screenWidth = self.app.winfo_screenwidth()
if self.screenWidth > 1280:
self.screenWidth = 1280 # prevent spread across 2-extended displays
s = str(self.screenWidth) + " " + str(self.screenHeight)
self.screenDim = s
其中調(diào)用了父類Project,打開star.py文件,找到Project類,對(duì)其進(jìn)行分析:
class Project:
"""Stars project.
Example Usage:
>>> from stars import Project
>>> s=Project("s")
>>> s.ReadData("csiss")
>>> income=s.getVariable("pcincome")
>>> region=s.getVariable("bea")
>>> w=spRegionMatrix(region)
>>> mi=Moran(income,w)
>>> print(mi.mi[70])
0.38918107312
"""
def __init__(self,name):
self.name = name
self.dataBase = Database()
self.getVariable = self.dataBase.getVariable
self.getMatrix = self.dataBase.getMatrix
self.addMatrix = self.dataBase.addMatrix
self.getMatrixNames = self.dataBase.getMatrixNames
self.getVariableNames = self.dataBase.getVariableNames
self.getTSVariableNames = self.dataBase.getTSVariableNames
self.getCSVariableNames = self.dataBase.getCSVariableNames
self.getCSTSVariableNames = self.dataBase.getCSTSVariableNames
(2)ReadProjectFile:
代碼如下所示
def ReadProjectFile(self,fileName):
#assumes extension is passed into fileName
#check for file existence
if os.path.exists(fileName):
self.initialize()
config = ConfigParser.ConfigParser()
config.read(fileName)
projectDir = os.path.dirname(fileName)
#print config.sections()
for section in config.sections():
options = config.options(section)
for option in options:
value = config.get(section,option)
#??????? print section,option,value
sec=self.options[section]
opt=sec[option]
opt.append(value)
sec[option]=opt
self.options[section]=sec
# self.summarizeOptions()
# read data files
dataFiles = self.options["data"]["files"]
for dataFile in dataFiles:
#???? print dataFile
dfile = os.path.join(projectDir,dataFile)
#???? print dfile
self.ReadData(dfile)
#print "data files"
# read any gal matricies
try:
galFiles = self.options["weight"]["gal"][0].split()
print galFiles
for galFile in galFiles:
#???????? print galFile
gfile = os.path.join(projectDir,galFile)
self.ReadGalMatrix(gfile)
#print "gal"
except:
print "No Weights Matrices"
# read any gis boundary files
self.listGISNames = []
gisFiles = self.options["gis"]["gis"]
for gisFile in gisFiles:
fileName = gisFile+".gis"
self.listGISNames.append(fileName)
fileName = os.path.join(projectDir,fileName)
self.ReadGisFile(fileName)
fileName = os.path.join(projectDir,gisFile+".cnt")
if os.path.exists(fileName):
self.readCentroids(fileName)
else:
self.__calcCentroids()
self.gisResolution = self.options["graphics"]["screen"]
else:
print "Error: Cannot read project file: %s"%fileName
該段代碼可以幫助解析STARS工程文件project的大致結(jié)構(gòu),打開系統(tǒng)自帶示例的工程文件csiss.prj:
[data]
files: csiss
[weight]
gal: states48
[gis]
gis: us48
[graphics]
screen: 1280 1201
可以發(fā)現(xiàn)該文件分為四段,前三段分別對(duì)應(yīng)有數(shù)據(jù)文件、權(quán)重文件、GIS文件的連接,最后一段為顯示參數(shù)。
·數(shù)據(jù)文件存儲(chǔ)統(tǒng)計(jì)數(shù)據(jù)信息,又分為兩個(gè)文件:csiss.dht存儲(chǔ)數(shù)據(jù)索引信息,csiss.dat存儲(chǔ)數(shù)據(jù)主體信息,其中注釋CS為空間序列數(shù)據(jù),TS為時(shí)間序列數(shù)據(jù),CSTS即為時(shí)空序列數(shù)據(jù)。
·權(quán)重文件存儲(chǔ)空間權(quán)重信息,后綴名為gal。此文件第一行為空間實(shí)體數(shù)目,從第二行開始每兩行構(gòu)成固定格式,形如:[第一行]實(shí)體序號(hào) 權(quán)重關(guān)聯(lián)實(shí)體個(gè)數(shù)[第二行]權(quán)重關(guān)聯(lián)實(shí)體序號(hào)列表,如此循環(huán)至最后一個(gè)實(shí)體。參見states48.gal文件。
·GIS文件(后綴名為gis)存儲(chǔ)空間實(shí)體的地圖數(shù)據(jù),結(jié)構(gòu)形如:[數(shù)據(jù)頭]空間實(shí)體編號(hào) 該實(shí)體內(nèi)多邊形編號(hào) 該多邊形節(jié)點(diǎn)數(shù)[數(shù)據(jù)體]X坐標(biāo)(換行)Y坐標(biāo)。參見us48.gis文件。
(3)projectSummary:
代碼如下所示
def projectSummary(self):
self.report(self.project.catalogue())
在guimaker.py中找到report函數(shù):
def report(self,message):
"""enters messages into main application window. use for
entering results, commands called by gui, etc."""
self.Editor.insert(END,message)
t=len(message)
self.Editor.yview_scroll(t,UNITS)
self.Editor.insert(END,"\n>")
總結(jié)
以上是生活随笔為你收集整理的python gis 实例_用Python作GIS之五:从示例入手—example函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 2287 田忌赛马(贪心)
- 下一篇: python--从入门到实践--chap