Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Improvements in R Connection (more or less working now)
added R toolbox actions git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@31 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
- Loading branch information
volayaf@gmail.com
committed
Mar 8, 2012
1 parent
1b68f20
commit 7e93081
Showing
10 changed files
with
226 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from sextante.core.GeoAlgorithm import GeoAlgorithm | ||
import os.path | ||
from PyQt4 import QtGui | ||
from PyQt4.QtCore import * | ||
from PyQt4.QtGui import * | ||
from qgis.core import * | ||
from sextante.parameters.ParameterVector import ParameterVector | ||
from sextante.core.QGisLayers import QGisLayers | ||
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException | ||
from sextante.outputs.OutputVector import OutputVector | ||
|
||
class LayerFromExtent(GeoAlgorithm): | ||
|
||
INPUT = "INPUT" | ||
OUTPUT = "OUTPUT" | ||
|
||
def getIcon(self): | ||
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/layer_extent.png") | ||
|
||
def processAlgorithm(self, progress): | ||
settings = QSettings() | ||
systemEncoding = settings.value( "/UI/encoding", "System" ).toString() | ||
output = self.getOutputValue(LayerFromExtent.OUTPUT) | ||
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(LayerFromExtent.INPUT)) | ||
fields = { | ||
0 : QgsField( "MINX", QVariant.Double ), | ||
1 : QgsField( "MINY", QVariant.Double ), | ||
2 : QgsField( "MAXX", QVariant.Double ), | ||
3 : QgsField( "MAXY", QVariant.Double ), | ||
4 : QgsField( "CNTX", QVariant.Double ), | ||
5 : QgsField( "CNTY", QVariant.Double ), | ||
6 : QgsField( "AREA", QVariant.Double ), | ||
7 : QgsField( "PERIM", QVariant.Double ), | ||
8 : QgsField( "HEIGHT", QVariant.Double ), | ||
9 : QgsField( "WIDTH", QVariant.Double ) } | ||
|
||
writer = QgsVectorFileWriter( output, systemEncoding, fields, QGis.WKBPolygon, self.vlayer.crs() ) | ||
rect = vlayer.extent() | ||
minx = rect.xMinimum() | ||
miny = rect.yMinimum() | ||
maxx = rect.xMaximum() | ||
maxy = rect.yMaximum() | ||
height = rect.height() | ||
width = rect.width() | ||
cntx = minx + ( width / 2.0 ) | ||
cnty = miny + ( height / 2.0 ) | ||
area = width * height | ||
perim = ( 2 * width ) + (2 * height ) | ||
rect = [ | ||
QgsPoint( minx, miny ), | ||
QgsPoint( minx, maxy ), | ||
QgsPoint( maxx, maxy ), | ||
QgsPoint( maxx, miny ), | ||
QgsPoint( minx, miny ) ] | ||
geometry = QgsGeometry().fromPolygon( [ rect ] ) | ||
feat = QgsFeature() | ||
feat.setGeometry( geometry ) | ||
feat.setAttributeMap( { | ||
0 : QVariant( minx ), | ||
1 : QVariant( miny ), | ||
2 : QVariant( maxx ), | ||
3 : QVariant( maxy ), | ||
4 : QVariant( cntx ), | ||
5 : QVariant( cnty ), | ||
6 : QVariant( area ), | ||
7 : QVariant( perim ), | ||
8 : QVariant( height ), | ||
9 : QVariant( width ) } ) | ||
writer.addFeature( feat ) | ||
del writer | ||
|
||
def defineCharacteristics(self): | ||
self.name = "Layer from layer extent" | ||
self.group = "Geometry tools" | ||
self.addParameter(ParameterVector(LayerFromExtent.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY)) | ||
self.addOutput(OutputVector(LayerFromExtent.OUTPUT, "Output layer")) | ||
#========================================================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from sextante.script.EditScriptDialog import EditScriptDialog | ||
from sextante.gui.ToolboxAction import ToolboxAction | ||
import os | ||
from PyQt4 import QtGui | ||
from sextante.r.EditRScriptDialog import EditRScriptDialog | ||
|
||
class CreateNewRScriptAction(ToolboxAction): | ||
|
||
def __init__(self): | ||
self.name="Create new R script" | ||
self.group="Tools" | ||
|
||
def getIcon(self): | ||
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/r.png") | ||
|
||
def execute(self): | ||
dlg = EditRScriptDialog(None) | ||
dlg.exec_() | ||
if dlg.update: | ||
self.toolbox.updateTree() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from sextante.gui.ContextAction import ContextAction | ||
from sextante.r.RAlgorithm import RAlgorithm | ||
from sextante.r.EditRScriptDialog import EditRScriptDialog | ||
|
||
class EditRScriptAction(ContextAction): | ||
|
||
def __init__(self): | ||
self.name="Edit R script" | ||
|
||
def isEnabled(self): | ||
return isinstance(self.alg, RAlgorithm) | ||
|
||
def execute(self): | ||
dlg = EditRScriptDialog(self.alg) | ||
dlg.exec_() | ||
if dlg.update: | ||
self.toolbox.updateTree() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from PyQt4 import QtCore, QtGui | ||
from PyQt4.QtCore import * | ||
from PyQt4.QtGui import * | ||
from sextante.r.RUtils import RUtils | ||
|
||
class EditRScriptDialog(QtGui.QDialog): | ||
def __init__(self, alg): | ||
self.alg = alg | ||
QtGui.QDialog.__init__(self) | ||
self.setModal(True) | ||
self.setupUi() | ||
self.update = False | ||
|
||
def setupUi(self): | ||
self.setObjectName("Dialog") | ||
self.resize(655, 360) | ||
self.setWindowTitle("Edit script") | ||
self.text = QtGui.QTextEdit(self) | ||
self.text.setGeometry(QtCore.QRect(5, 5, 550, 350)) | ||
self.text.setObjectName("text") | ||
self.text.setEnabled(True) | ||
if self.alg != None: | ||
self.text.setText(self.alg.script) | ||
self.saveButton = QtGui.QPushButton(self) | ||
self.saveButton.setGeometry(QtCore.QRect(570, 300, 80, 23)) | ||
self.saveButton.setObjectName("saveButton") | ||
self.saveButton.setText("Save") | ||
self.cancelButton = QtGui.QPushButton(self) | ||
self.cancelButton.setGeometry(QtCore.QRect(570, 327, 80, 23)) | ||
self.cancelButton.setObjectName("cancelButton") | ||
self.cancelButton.setText("Cancel") | ||
QObject.connect(self.saveButton, QtCore.SIGNAL("clicked()"), self.saveAlgorithm) | ||
QObject.connect(self.cancelButton, QtCore.SIGNAL("clicked()"), self.cancel) | ||
QtCore.QMetaObject.connectSlotsByName(self) | ||
|
||
def saveAlgorithm(self): | ||
if self.alg!=None: | ||
filename = self.alg.descriptionFile | ||
else: | ||
filename = QtGui.QFileDialog.getSaveFileName(self, "Save Script", RUtils.RScriptsFolder(), "R-SEXTANTE scripts (*.rsx)") | ||
if filename: | ||
text = self.text.toPlainText() | ||
fout = open(filename, "w") | ||
fout.write(text) | ||
fout.close() | ||
self.update = True | ||
self.close() | ||
|
||
def cancel(self): | ||
self.update = False | ||
self.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters