예제 #1
0
    def __init__(self,iface):
        QtGui.QDialog.__init__(self)
        # Set up the user interface from QTDesigner.
        self.ui = Ui_spatial_dynamics() # read GUI 
	self.ui.setupUi(self)
        self.iface = iface
        self.dir = os.path.realpath(os.path.curdir)# Return the canonical path of the specified filename
	        
        self.layers = [] #set a empty list?(is it necessary steps?)
        for i in range(self.iface.mapCanvas().layerCount()):    #this for loop adds current layers
            layer = self.iface.mapCanvas().layer(i)             #to dropdown menu
            self.layers += [layer]
            if layer.type() ==layer.VectorLayer:
		self.ui.activecombobox.addItem(layer.name()) #set dynamic labels in the combobox
	    elif layer.type == layer.RasterLayer:
	    	pass
	    else:
		pass
예제 #2
0
class spatial_dynamicsdialog(QtGui.QDialog):
    def __init__(self,iface):
        QtGui.QDialog.__init__(self)
        # Set up the user interface from QTDesigner.
        self.ui = Ui_spatial_dynamics() # read GUI 
	self.ui.setupUi(self)
        self.iface = iface
        self.dir = os.path.realpath(os.path.curdir)# Return the canonical path of the specified filename
	        
        self.layers = [] #set a empty list?(is it necessary steps?)
        for i in range(self.iface.mapCanvas().layerCount()):    #this for loop adds current layers
            layer = self.iface.mapCanvas().layer(i)             #to dropdown menu
            self.layers += [layer]
            if layer.type() ==layer.VectorLayer:
		self.ui.activecombobox.addItem(layer.name()) #set dynamic labels in the combobox
	    elif layer.type == layer.RasterLayer:
	    	pass
	    else:
		pass


    @pyqtSignature('') #prevents actions being handled twice
    def on_inputbutton_clicked(self):
	myFile1 = QFileDialog.getOpenFileName(self, "Select a shapefile","","comma_separatedfile(*.csv)")
	if self.ui.inputbutton != None:
		self.ui.inputline.setText(myFile1)
	else:
		pass

	#create a new combobox(1)use pysal to open file(2) read  in
    #for saved shapefile to show columns
	openfile=str(self.ui.inputline.text()) 
	f=pysal.open(openfile)
	#opendbf=openfile[:-3] + "dbf" #open the same file only with dbf 
	#f_dbf = pysal.open(opendbf)
	self.fileheader=f.header #find columns, already in a list
	
	for i in self.fileheader: #i is in a string
		self.ui.startcombobox.addItem(i)
		self.ui.endcombobox.addItem(i)


    #@pyqtSignature('') #prevents actions being handled twice
    #def on_activecombobox_currentIndexChanged(self):	#when selecting	any shapefile from active layers, but do not know how to get the file path from active layers?
    #for active layer to show columns
    	#self.ui.activecombobox.currentIndexChanged(int)
	#QMessageBox.information(self,"Vector file","Layer is ok")
	#self.ui.activecombobox.text()
    	#openfile=str(self.ui.activecombobox.getPath().Text())
	#f=pysal.open(openfile)
	#opendbf=openfile[:-3] + "dbf" #open the same file only with dbf 
	#f_dbf = pysal.open(opendbf)
	#self.fileheader=f.header #find columns, already in a list
	
	#for i in self.fileheader: #i is in a string
	#	self.ui.selectcombobox.addItem(i)


    @pyqtSignature('') #prevents actions being handled twice
    def on_inputweightsbutton_clicked(self):
        myFile2 = QFileDialog.getOpenFileName (self, "Select a weights file","","*.gal;;*.gwt;;*.mat")
        self.ui.inputweightsline.setText(myFile2)
    
    @pyqtSignature('') #prevents actions being handled twice
    def on_inputweightscreate_clicked(self):
	dlg = WeightsDialog(self.iface)
        dlg.show()
        results = dlg.exec_()
	if self.ui.inputweightscreate != None: #how to call variable from other files?
		#self.ci=WeightsDialog(WeightsDialog.accept)
		#myfile4=WeightsDialog.accept(savefile)
		#self.ui.Inputweightsline.setText(myfile4)
		pass
	else:
		pass

    @pyqtSignature('') #prevents actions being handled twice
    def on_saveoutputbutton_clicked(self):
	dlg = QFileDialog()
	myFile3 = dlg.getSaveFileName(self, "Save Matrix", "", "comma_separatedfile(*.csv)")
	myFile3 += dlg.selectedNameFilter()[0] #?
        self.ui.saveoutputline.setText(myFile3[0:-1])

#pysal seems not to support all filetypes I know. (write here for backup)"comma_separatedfile(*.csv);;textfile(*.txt);;excelfile(*.xls);;pythonfile(*.py);;accessfile(*.asc);;arcgisfile(*.dbf);;spssfile(*.sav);;multi_usagefile(*.dat)"

###############################################################################################
####                                                                                       #### 
####                                                                                       ####     
####       This is the method we need to implement.  When they click OK this method runs   ####
####                                                                                       #### 
####                                                                                       ####  
###############################################################################################
#classical markov procedures: transfer data from string to array, read data by each columns, data classification, transpose, matrixs 
#spatial markov procedures: transfer data from string to array, read data by each columns, data classification, transpose, standardization, input spatial weights with transform, matrix

    def accept(self):
	if self.ui.savedshpradio.isChecked(): #when selecting saved shp
		openfile=str(self.ui.inputline.text()) #make a string of saved file
		savefile = str(self.ui.saveoutputline.text()) #this will be a string like "c:\output.(.csv)"
		weightsfile=str(self.ui.inputweightsline.text())
		if self.ui.matrixcheckbox.checkState():

		#run spatial Matrix
			f=pysal.open(openfile) #read a shp file, not need to read
			w=pysal.open(weightsfile).read() #read a weights file
			#opendbf=openfile[:-3] + "dbf" #open the same file only with dbf 
			#f_dbf = pysal.open(opendbf) #read the dbf attribute file
			fileheader=f.header

		#select a column and let it function
			columnindex1=int(self.ui.startcombobox.currentText()) #when select a column
			columnindex2=int(self.ui.endcombobox.currentText())+1 #avoid random selection?
		
		#change into array, by_col function is only for dbf file
			pci=np.array([f.by_col[str(y)] for y in range(columnindex1, columnindex2)]) 
			#only number? by_col works for dbf, but the sample data use csv?

			#q5 = np.array([pysal.Quantiles(y).yb for y in pci]) #map classification?

			pci=pci.transpose()
			rpci = pci / (pci.mean(axis = 0)) #standardization
			w.transform='r'
		
			sm=pysal.Spatial_Markov(rpci, w, fixed=True, k=5) #what did k mean? does it equal to quantile?
		
		#results
			transition_matrix=sm.p #numpy.matrixlib.defmatrix.matrix
			results = "\n".join([ "\t".join(map(str,row)) for row in transition_matrix])
			#results=repr(transition_matrix).replace('matrix',' ')
			#results='      '+''.join([ c for c in s if c not in ('(', ')','[',']',',')])
			output=pysal.open(savefile,'w')
			output.write(results)
			output.close

			if self.ui.probabilitiescheckbox.checkState():
				for p in sm.P:
					transition_probabilities=p
					#results=repr(transition_probabilities).replace('matrix',' ')
					#results='      '+''.join([ c for c in s if c not in ('(', ')','[',']',',')])
					results = "\n".join([ "\t".join(map(str,row)) for row in transition_probabilities])
					output=pysal.open(savefile,'w')
					output.write(results)
					output.close
			#else:
			#	pass

			elif self.ui.steadystatecheckbox.checkState():
				steady_state_distribution=sm.S
				#results=repr(Steady_State_Distribution).replace('matrix',' ')
				#results='      '+''.join([ c for c in s if c not in ('(', ')','[',']',',')])
				results = "\n".join([ "\t".join(map(str,row)) for row in steady_state_distribution])
				output=pysal.open(savefile,'w')
				output.write(results)
				output.close
			#else:
				#pass

			elif self.ui.firstcheckbox.checkState():
				for f in sm.F:
					first_mean_passage_time=f
					#resultss=repr(first_mean_passage_time).replace('matrix',' ')
					#results='      '+''.join([ c for c in s if c not in ('(', ')','[',']',',')])
					results = "\n".join([ "\t".join(map(str,row)) for row in first_mean_passage_time])
					output=pysal.open(savefile,'w')
					output.write(results)
					output.close
			else:
				pass

		else:
			pass
	
	elif self.ui.activecombobox.isChecked(): #when selecting active shp and then import pysal
		layer = self.layers[self.ui.activecombobox.currentIndex()] #select a shp layer
		savefile = str(self.ui.outputline.text())
		weightsfile=str(self.ui.Inputweightsline.text())
		
		pass
		
		#if 
			#f=pysal.open() #calculate Moran's I and other value, but do not know how to get the file path from active layers?
		#else:
		#	return

        self.close() #close the dialog window