arcgis中python简单实例-⽤Python作GIS之五:从⽰例⼊⼿—
example函数
进⼊STARS后,最简单的学习⽅法就是演⽰⽰例数据。对于源码的分析也可以从这⾥⼊⼿。
以下为出发菜单项"Example Project”的函数example:
def example(self):
"""canned loading of data files and matrices for debugging"""
self.project = SProject("current",self.master,self) topDir = STARSHOME()
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:
#figure(cursor=options.DEFAULTCURSOR)
#figure(cursor='crosshair')
self.projectSummary() ableMenus()
加粗标注的⼏⾏代码可作进⼀步详细了解:
(1)SProject:
代码如下所⽰
class SProject(Project):
"""Subclass to compose STARS project inside a GUI
"""
python怎么读取dat文件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
其中调⽤了⽗类Project,打开star.py⽂件,到Project类,对其进⾏分析:class Project:
"""Stars project.
Example Usage:
>>> from stars import Project
>>> s=Project("s")
>>> s.ReadData("csiss")
>>> Variable("pcincome")
>>> Variable("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.addMatrix = self.dataBase.addMatrix
代码如下所⽰
def ReadProjectFile(self,fileName):
#assumes extension is passed into fileName
#check for file existence
if ists(fileName):
self.initialize()
config = ConfigParser.ConfigParser()
projectDir = os.path.dirname(fileName)
#print config.sections()
for section in config.sections():
options = config.options(section)
for option in options:
value = (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+"t") if ists(fileName):
else:
self.__calcCentroids()
self.gisResolution = self.options["graphics"]["screen"]
else:
print "Error: Cannot read project file: %s"%fileName
该段代码可以帮助解析STARS⼯程⽂件project的⼤致结构,打开系统⾃带⽰例的⼯程⽂件csiss.prj:
[data]
files: csiss
[weight]
gal: states48
[gis]
gis: us48
[graphics]
screen: 1280 1201
可以发现该⽂件分为四段,前三段分别对应有数据⽂件、权重⽂件、GIS⽂件的连接,最后⼀段为显⽰参数。
·数据⽂件存储统计数据信息,⼜分为两个⽂件:csiss.dht存储数据索引信息,csiss.dat存储数据主体信息,其中注释CS为空间序列数据,TS为时间序列数据,CSTS即为时空序列数据。
·权重⽂件存储空间权重信息,后缀名为gal。此⽂件第⼀⾏为空间实体数⽬,从第⼆⾏开始每两⾏构成
固定格式,形如:[第⼀⾏]实体序号权重关联实体个数[第⼆⾏]权重关联实体序号列表,如此循环⾄最后⼀个实体。参见states48.gal⽂件。
·GIS⽂件(后缀名为gis)存储空间实体的地图数据,结构形如:[数据头]空间实体编号 该实体内多边形编号 该多边形节点数[数据体]X坐标(换⾏)Y坐标。参见us48.gis⽂件。
(3)projectSummary:
代码如下所⽰
def projectSummary(self):
在guimaker.py中到report函数:
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," >")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。