Using Python as a GIS five: starting with an example-example function

Source: Internet
Author: User

After entering stars, the simplest way to learn is to demonstrate sample data. The analysis of the source code can also be started here.

The following is the function Example for the departure menu item "Example Project":
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 ()

A few lines of code for bold labeling can be learned in more detail:

(1) Sproject:
The code looks like this
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 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 = Prevent spread across 2-extended displays
s = str (self.screenwidth) + "" + str (self.screenheight)
Self.screendim = S

It calls the parent class project, opens the star.py file, finds the project class, analyzes it:
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:
The code looks like this
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 sections 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

This code can help resolve the approximate structure of the Stars project file project and open the project file CSISS.PRJ with the system's own example:
[Data]
Files:csiss
[Weight]
Gal:states48

[GIS]
Gis:us48

[Graphics]
screen:1280 1201

Can be found that the file is divided into four paragraphs, the first three paragraphs corresponding to the data file, weight file, GIS file connection, the last section for display parameters.
• Data files store statistical information and are divided into two files: CSISS.DHT stores data index information, Csiss.dat stores data body information, in which the comment CS is the spatial sequence data, TS is the time series data, and CSTs is the spatiotemporal sequence data.
• Weight file stores spatial weight information with the suffix named Gal. The number of first-action space entities in this file, starting from the second line, each two lines form a fixed format, such as: [First row] entity ordinal weight associated entity number [second row] Weight Association entity ordinal List , so loop to the last entity. See States48.gal file.
· A GIS file (suffix GIS) stores the map data for a spatial entity, such as: [Data Header] space entity number the polygon number in the entity number of the Polygon node [data body]x coordinate (newline) Y coordinate . See Us48.gis file.

(3) Projectsummary:
The code looks like this
def projectsummary (self):
Self.report (Self.project.catalogue ())

Find the report function in guimaker.py:
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>")

Using Python as a GIS five: starting with an example-example function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.