Skip to content
Permalink
Browse files

applied path for #6574

applied patch for multiple grass sessions
added points in polygon weighted
  • Loading branch information
volaya committed Oct 27, 2012
1 parent 0b1ebaa commit 5c762ef741b251dbeba4e17d4f30bf4b59ebaa0e
@@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from sextante.ftools.PointsInPolygonWeighted import PointsInPolygonWeighted

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -76,7 +77,7 @@ class FToolsAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.alglist = [SumLines(), PointsInPolygon(), BasicStatisticsStrings(),
self.alglist = [SumLines(), PointsInPolygon(), PointsInPolygonWeighted(), BasicStatisticsStrings(),
BasicStatisticsNumbers(), NearestNeighbourAnalysis(),
MeanCoords(), LinesIntersection(), UniqueValues(), PointDistance(),
# data management
@@ -5,7 +5,7 @@
FToolsUtils.py
---------------------
Date : September 2012
Copyright : (C) 2012 by Victor Olaya
Copyright : (C) 2012 by Carson Farmer, Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
@@ -17,9 +17,9 @@
***************************************************************************
"""

__author__ = 'Victor Olaya'
__author__ = 'Carson, Farmer, Victor Olaya'
__date__ = 'September 2012'
__copyright__ = '(C) 2012, Victor Olaya'
__copyright__ = '(C) 2012, Carson Farmer, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

@@ -55,7 +55,6 @@ def getIcon(self):
def defineCharacteristics(self):
self.name = "Count points in polygon"
self.group = "Analysis tools"

self.addParameter(ParameterVector(self.POLYGONS, "Polygons", ParameterVector.VECTOR_TYPE_POLYGON))
self.addParameter(ParameterVector(self.POINTS, "Points", ParameterVector.VECTOR_TYPE_POINT))
self.addParameter(ParameterString(self.FIELD, "Count field name", "NUMPOINTS"))
@@ -0,0 +1,131 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
PointsInPolygon.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from sextante.parameters.ParameterTableField import ParameterTableField

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *

from qgis.core import *

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteLog import SextanteLog

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterString import ParameterString

from sextante.outputs.OutputVector import OutputVector

from sextante.ftools import FToolsUtils as utils


class PointsInPolygonWeighted(GeoAlgorithm):

POLYGONS = "POLYGONS"
POINTS = "POINTS"
OUTPUT = "OUTPUT"
FIELD = "FIELD"
WEIGHT = "WEIGHT"

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/sum_points.png")

def defineCharacteristics(self):
self.name = "Count points in polygon(weighted)"
self.group = "Analysis tools"

self.addParameter(ParameterVector(self.POLYGONS, "Polygons", ParameterVector.VECTOR_TYPE_POLYGON))
self.addParameter(ParameterVector(self.POINTS, "Points", ParameterVector.VECTOR_TYPE_POINT))
self.addParameter(ParameterTableField(self.WEIGHT, "Weight field", self.POINTS))
self.addParameter(ParameterString(self.FIELD, "Count field name", "NUMPOINTS"))

self.addOutput(OutputVector(self.OUTPUT, "Result"))

def processAlgorithm(self, progress):
polyLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POLYGONS))
pointLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
fieldName = self.getParameterValue(self.FIELD)
fieldidx = pointLayer.dataProvider().fieldNameIndex(self.getParameterValue(self.WEIGHT))

polyProvider = polyLayer.dataProvider()
pointProvider = pointLayer.dataProvider()
if polyProvider.crs() != pointProvider.crs():
SextanteLog.addToLog(SextanteLog.LOG_WARNING,
"CRS warning: Input layers have non-matching CRS. This may cause unexpected results.")

idxCount, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), fieldName)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
polyProvider.geometryType(), polyProvider.crs())

spatialIndex = utils.createSpatialIndex(pointProvider)

pointProvider.rewind()
pointProvider.select()

allAttrs = polyLayer.pendingAllAttributesList()
polyLayer.select(allAttrs)

ftPoly = QgsFeature()
ftPoint = QgsFeature()
outFeat = QgsFeature()
geom = QgsGeometry()

current = 0
total = 100.0 / float(polyProvider.featureCount())
hasIntersections = False

while polyLayer.nextFeature(ftPoly):
geom = ftPoly.geometry()
atMap = ftPoly.attributeMap()

count = 0
hasIntersections = False
points = spatialIndex.intersects(geom.boundingBox())
if len(points) > 0:
hasIntersections = True

if hasIntersections:
for i in points:
pointLayer.featureAtId(int(i), ftPoint, True, False)
tmpGeom = QgsGeometry(ftPoint.geometry())
if geom.contains(tmpGeom):
try:
weight = float(ftPoint.attributeMap()[fieldidx])
except:
weight = 1
count += weight

outFeat.setGeometry(geom)
outFeat.setAttributeMap(atMap)
outFeat.addAttribute(idxCount, QVariant(count))
writer.addFeature(outFeat)

current += 1
progress.setPercentage(int(current * total))

del writer
@@ -5,7 +5,7 @@
ftools_utils.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Copyright : (C) 2012 by Carson Farmer, Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
@@ -17,9 +17,9 @@
***************************************************************************
"""

__author__ = 'Victor Olaya'
__author__ = 'Carson Farmer, Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
__copyright__ = '(C) 2012, Carson Farmer, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

@@ -163,11 +163,18 @@ def processAlgorithm(self, progress):
commands = []
self.exportedLayers = {}

# if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS
# otherwise start a new session
existingSession = GrassUtils.sessionRunning
if existingSession:
self.exportedLayers = GrassUtils.getSessionLayers()
else:
GrassUtils.startGrassSession()


#self.calculateRegion()
region = str(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER))
regionCoords = region.split(",")
GrassUtils.createTempMapset();

command = "g.region"
command += " n=" + str(regionCoords[3])
command +=" s=" + str(regionCoords[2])
@@ -178,19 +185,26 @@ def processAlgorithm(self, progress):
else:
command +=" res=" + str(self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER));
commands.append(command)

#1: Export layer to grass mapset
for param in self.parameters:
if isinstance(param, ParameterRaster):
if param.value == None:
continue
value = param.value
commands.append(self.exportRasterLayer(value))
# check if the layer hasn't already been exported in, for example, previous GRASS calls in this session
if value in self.exportedLayers.keys():
continue
else:
commands.append(self.exportRasterLayer(value))
if isinstance(param, ParameterVector):
if param.value == None:
continue
value = param.value
commands.append(self.exportVectorLayer(value))
if value in self.exportedLayers.keys():
continue
else:
commands.append(self.exportVectorLayer(value))
if isinstance(param, ParameterTable):
pass
if isinstance(param, ParameterMultipleInput):
@@ -201,15 +215,21 @@ def processAlgorithm(self, progress):
continue
if param.datatype == ParameterMultipleInput.TYPE_RASTER:
for layer in layers:
commands.append(self.exportRasterLayer(layer))
if layer in self.exportedLayers.keys():
continue
else:
commands.append(self.exportRasterLayer(layer))
elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
for layer in layers:
commands.append(self.exportVectorLayer(layer))
if layer in self.exportedLayers.keys():
continue
else:
commands.append(self.exportVectorLayer(layer))

#2: set parameters and outputs
command = self.grassName
for param in self.parameters:
if param.value == None:
if param.value == None or param.value == "":
continue
if param.name == self.GRASS_REGION_CELLSIZE_PARAMETER or param.name == self.GRASS_REGION_EXTENT_PARAMETER:
continue
@@ -238,7 +258,9 @@ def processAlgorithm(self, progress):
if isinstance(out, OutputFile):
command+=(" " + out.name + "=\"" + out.value + "\"");
else:
command+=(" " + out.name + "=" + out.name);
command += (" " + out.name)
out.name += ("_"+str(len(self.exportedLayers))) # make sure output is unique within a session
command += ("=" + out.name)

command += " --overwrite"
commands.append(command)
@@ -254,14 +276,17 @@ def processAlgorithm(self, progress):
command += out.name
command += " output=\"" + filename + "\""
commands.append(command)
# add output file to exported layers, to indicate that they are present in GRASS
self.exportedLayers[filename]= out.name
if isinstance(out, OutputVector):
command = "v.out.ogr -ce input=" + out.name
command += " dsn=\"" + os.path.dirname(out.value) + "\""
command += " format=ESRI_Shapefile"
command += " olayer=" + os.path.basename(out.value)[:-4]
command += " type=auto"
commands.append(command)

self.exportedLayers[filename]= out.name

#4 Run GRASS
loglines = []
loglines.append("GRASS execution commands")
@@ -271,7 +296,12 @@ def processAlgorithm(self, progress):
if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS):
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
GrassUtils.executeGrass(commands, progress);

# if the session has been created outside of this algorithm, add the new GRASS layers to it
# otherwise finish the session
if existingSession:
GrassUtils.addSessionLayers(self.exportedLayers)
else:
GrassUtils.endGrassSession()

def exportVectorLayer(self, orgFilename):
#only export to an intermediate shp if the layer is not file-based.
@@ -319,3 +349,4 @@ def getTempFilename(self):

def commandLineName(self):
return "grass:" + self.name[:self.name.find(" ")]

@@ -32,8 +32,6 @@
from sextante.grass.GrassUtils import GrassUtils
from sextante.grass.GrassAlgorithm import GrassAlgorithm
from sextante.core.SextanteUtils import SextanteUtils
from sextante.grass.DefineGrassRegionAction import DefineGrassRegionAction
from sextante.grass.DefineGrassRegionFromLayerAction import DefineGrassRegionFromLayerAction

class GrassAlgorithmProvider(AlgorithmProvider):

@@ -52,31 +50,15 @@ def initializeSettings(self):
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell()))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False))
#SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False))
#=======================================================================
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0))
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0))
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000))
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000))
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 100))
#=======================================================================
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))

def unload(self):
AlgorithmProvider.unload(self)
if SextanteUtils.isWindows() or SextanteUtils.isMac():
SextanteConfig.removeSetting(GrassUtils.GRASS_FOLDER)
SextanteConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL)
#SextanteConfig.removeSetting(GrassUtils.GRASS_AUTO_REGION)
SextanteConfig.removeSetting(GrassUtils.GRASS_LATLON)
#=======================================================================
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMIN)
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMIN)
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMAX)
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMAX)
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_CELLSIZE)
#=======================================================================
SextanteConfig.removeSetting(GrassUtils.GRASS_HELP_FOLDER)
SextanteConfig.removeSetting(GrassUtils.GRASS_LOG_COMMANDS)
SextanteConfig.removeSetting(GrassUtils.GRASS_LOG_CONSOLE)

0 comments on commit 5c762ef

Please sign in to comment.
You can’t perform that action at this time.