2010-03-10 00:16:03 +00:00
# -*- coding: utf-8 -*-
#-----------------------------------------------------------
2009-01-20 22:54:27 +00:00
#
2011-03-07 23:29:15 +00:00
# fTools
# Copyright (C) 2008-2011 Carson Farmer
2009-01-20 22:54:27 +00:00
# EMAIL: carson.farmer (at) gmail.com
2011-03-07 23:29:15 +00:00
# WEB : http://www.ftools.ca/fTools.html
#
# A collection of data management and analysis tools for vector data
2009-01-20 22:54:27 +00:00
#
#-----------------------------------------------------------
2011-03-07 23:29:15 +00:00
#
2009-01-20 22:54:27 +00:00
# licensed under the terms of GNU GPL 2
2011-03-07 23:29:15 +00:00
#
2009-01-20 22:54:27 +00:00
# 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.
2011-03-07 23:29:15 +00:00
#
2009-01-20 22:54:27 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2011-03-07 23:29:15 +00:00
#
2009-01-20 22:54:27 +00:00
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2011-03-07 23:29:15 +00:00
#
2009-01-20 22:54:27 +00:00
#---------------------------------------------------------------------
2009-02-01 15:44:02 +00:00
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
import ftools_utils
from qgis . core import *
2009-08-16 22:40:48 +00:00
from ui_frmPointsInPolygon import Ui_Dialog
2009-02-01 15:44:02 +00:00
2009-01-20 22:54:27 +00:00
class Dialog ( QDialog , Ui_Dialog ) :
2010-03-10 00:16:03 +00:00
def __init__ ( self , iface ) :
2012-01-26 00:54:56 +01:00
QDialog . __init__ ( self , iface . mainWindow ( ) )
2010-03-10 00:16:03 +00:00
self . iface = iface
# Set up the user interface from Designer.
self . setupUi ( self )
QObject . connect ( self . toolOut , SIGNAL ( " clicked() " ) , self . outFile )
self . setWindowTitle ( self . tr ( " Count Points in Polygon " ) )
2010-05-13 22:55:59 +00:00
self . buttonOk = self . buttonBox_2 . button ( QDialogButtonBox . Ok )
2010-03-10 00:16:03 +00:00
self . progressBar . setValue ( 0 )
2011-11-12 13:05:27 +02:00
self . populateLayers ( )
def populateLayers ( self ) :
2010-03-10 00:16:03 +00:00
layers = ftools_utils . getLayerNames ( [ QGis . Polygon ] )
2011-11-12 13:05:27 +02:00
self . inPolygon . clear ( )
2010-03-10 00:16:03 +00:00
self . inPolygon . addItems ( layers )
2011-11-12 13:05:27 +02:00
self . inPoint . clear ( )
2010-03-10 00:16:03 +00:00
layers = ftools_utils . getLayerNames ( [ QGis . Point ] )
self . inPoint . addItems ( layers )
2011-11-12 13:05:27 +02:00
2010-03-10 00:16:03 +00:00
def accept ( self ) :
2010-05-13 22:55:59 +00:00
self . buttonOk . setEnabled ( False )
2010-03-10 00:16:03 +00:00
if self . inPolygon . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Count Points In Polygon " ) , self . tr ( " Please specify input polygon vector layer " ) )
elif self . outShape . text ( ) == " " :
QMessageBox . information ( self , self . tr ( " Count Points In Polygon " ) , self . tr ( " Please specify output shapefile " ) )
elif self . inPoint . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Count Points In Polygon " ) , self . tr ( " Please specify input point vector layer " ) )
elif self . lnField . text ( ) == " " :
QMessageBox . information ( self , self . tr ( " Count Points In Polygon " ) , self . tr ( " Please specify output count field " ) )
else :
inPoly = self . inPolygon . currentText ( )
inPts = self . inPoint . currentText ( )
inField = self . lnField . text ( )
outPath = self . outShape . text ( )
if outPath . contains ( " \\ " ) :
outName = outPath . right ( ( outPath . length ( ) - outPath . lastIndexOf ( " \\ " ) ) - 1 )
else :
outName = outPath . right ( ( outPath . length ( ) - outPath . lastIndexOf ( " / " ) ) - 1 )
if outName . endsWith ( " .shp " ) :
outName = outName . left ( outName . length ( ) - 4 )
self . compute ( inPoly , inPts , inField , outPath , self . progressBar )
self . outShape . clear ( )
addToTOC = QMessageBox . question ( self , self . tr ( " Count Points in Polygon " ) , self . tr ( " Created output shapefile: \n % 1 \n \n Would you like to add the new layer to the TOC? " ) . arg ( unicode ( outPath ) ) , QMessageBox . Yes , QMessageBox . No , QMessageBox . NoButton )
if addToTOC == QMessageBox . Yes :
self . vlayer = QgsVectorLayer ( outPath , unicode ( outName ) , " ogr " )
QgsMapLayerRegistry . instance ( ) . addMapLayer ( self . vlayer )
2011-11-12 13:05:27 +02:00
self . populateLayers ( )
2010-03-10 00:16:03 +00:00
self . progressBar . setValue ( 0 )
2010-05-13 22:55:59 +00:00
self . buttonOk . setEnabled ( True )
2009-01-20 22:54:27 +00:00
2010-03-10 00:16:03 +00:00
def outFile ( self ) :
self . outShape . clear ( )
( self . shapefileName , self . encoding ) = ftools_utils . saveDialog ( self )
if self . shapefileName is None or self . encoding is None :
return
self . outShape . setText ( QString ( self . shapefileName ) )
2009-01-20 22:54:27 +00:00
2010-03-10 00:16:03 +00:00
def compute ( self , inPoly , inPts , inField , outPath , progressBar ) :
polyLayer = ftools_utils . getVectorLayerByName ( inPoly )
pointLayer = ftools_utils . getVectorLayerByName ( inPts )
polyProvider = polyLayer . dataProvider ( )
pointProvider = pointLayer . dataProvider ( )
if polyProvider . crs ( ) < > pointProvider . crs ( ) :
QMessageBox . warning ( self , self . tr ( " CRS warning! " ) , self . tr ( " Warning: Input layers have non-matching CRS. \n This may cause unexpected results. " ) )
allAttrs = polyProvider . attributeIndexes ( )
polyProvider . select ( allAttrs )
allAttrs = pointProvider . attributeIndexes ( )
pointProvider . select ( allAttrs )
fieldList = ftools_utils . getFieldList ( polyLayer )
index = polyProvider . fieldNameIndex ( unicode ( inField ) )
if index == - 1 :
index = polyProvider . fieldCount ( )
2010-03-10 01:11:11 +00:00
field = QgsField ( unicode ( inField ) , QVariant . Double , " real " , 24 , 15 , self . tr ( " point count field " ) )
2010-03-10 00:16:03 +00:00
fieldList [ index ] = field
sRs = polyProvider . crs ( )
check = QFile ( self . shapefileName )
if check . exists ( ) :
if not QgsVectorFileWriter . deleteShapeFile ( self . shapefileName ) :
return
writer = QgsVectorFileWriter ( self . shapefileName , self . encoding , fieldList , polyProvider . geometryType ( ) , sRs )
inFeat = QgsFeature ( )
inFeatB = QgsFeature ( )
outFeat = QgsFeature ( )
inGeom = QgsGeometry ( )
start = 15.00
add = 85.00 / polyProvider . featureCount ( )
spatialIndex = ftools_utils . createIndex ( pointProvider )
while polyProvider . nextFeature ( inFeat ) :
inGeom = inFeat . geometry ( )
atMap = inFeat . attributeMap ( )
outFeat . setAttributeMap ( atMap )
outFeat . setGeometry ( inGeom )
pointList = [ ]
count = 0
#(check, pointList) = pointLayer.featuresInRectangle(inGeom.boundingBox(), True, True)
#pointLayer.select(inGeom.boundingBox(), False)
#pointList = pointLayer.selectedFeatures()
pointList = spatialIndex . intersects ( inGeom . boundingBox ( ) )
if len ( pointList ) > 0 : check = 0
else : check = 1
if check == 0 :
for i in pointList :
pointProvider . featureAtId ( int ( i ) , inFeatB , True , allAttrs )
tmpGeom = QgsGeometry ( inFeatB . geometry ( ) )
if inGeom . contains ( tmpGeom . asPoint ( ) ) :
count = count + 1
outFeat . setAttributeMap ( atMap )
outFeat . addAttribute ( index , QVariant ( count ) )
writer . addFeature ( outFeat )
start = start + 1
progressBar . setValue ( start )
del writer