2010-03-10 01:11:11 +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_frmSumLines 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 01:11:11 +00:00
def __init__ ( self , iface ) :
2012-01-26 00:54:56 +01:00
QDialog . __init__ ( self , iface . mainWindow ( ) )
2010-03-10 01:11:11 +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 ( " Sum line lengths " ) )
2010-05-13 22:55:59 +00:00
self . buttonOk = self . buttonBox_2 . button ( QDialogButtonBox . Ok )
2010-03-10 01:11:11 +00:00
self . progressBar . setValue ( 0 )
2011-11-12 13:05:27 +02:00
self . populateLayers ( )
def populateLayers ( self ) :
2010-03-10 01:11:11 +00:00
layers = ftools_utils . getLayerNames ( [ QGis . Line ] )
2011-11-12 13:05:27 +02:00
self . inPoint . clear ( )
2010-03-10 01:11:11 +00:00
self . inPoint . addItems ( layers )
layers = ftools_utils . getLayerNames ( [ QGis . Polygon ] )
2011-11-12 13:05:27 +02:00
self . inPolygon . clear ( )
2010-03-10 01:11:11 +00:00
self . inPolygon . addItems ( layers )
2011-11-12 13:05:27 +02:00
2010-03-10 01:11:11 +00:00
def accept ( self ) :
2010-05-13 22:55:59 +00:00
self . buttonOk . setEnabled ( False )
2010-03-10 01:11:11 +00:00
if self . inPolygon . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Sum Line Lengths In Polyons " ) , self . tr ( " Please specify input polygon vector layer " ) )
elif self . outShape . text ( ) == " " :
QMessageBox . information ( self , self . tr ( " Sum Line Lengths In Polyons " ) , self . tr ( " Please specify output shapefile " ) )
elif self . inPoint . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Sum Line Lengths In Polyons " ) , self . tr ( " Please specify input line vector layer " ) )
elif self . lnField . text ( ) == " " :
QMessageBox . information ( self , self . tr ( " Sum Line Lengths In Polyons " ) , self . tr ( " Please specify output length field " ) )
else :
inPoly = self . inPolygon . currentText ( )
inLns = self . inPoint . currentText ( )
inField = self . lnField . text ( )
outPath = self . outShape . text ( )
2013-06-14 19:51:48 +12:00
outName = ftools_utils . getShapefileName ( outPath )
2010-03-10 01:11:11 +00:00
self . compute ( inPoly , inLns , inField , outPath , self . progressBar )
self . outShape . clear ( )
2013-05-31 14:57:34 +04:00
addToTOC = QMessageBox . question ( self , self . tr ( " Sum line lengths " ) , self . tr ( " Created output shapefile: \n %s \n \n Would you like to add the new layer to the TOC? " ) % ( unicode ( outPath ) ) , QMessageBox . Yes , QMessageBox . No , QMessageBox . NoButton )
2010-03-10 01:11:11 +00:00
if addToTOC == QMessageBox . Yes :
self . vlayer = QgsVectorLayer ( outPath , unicode ( outName ) , " ogr " )
2012-12-24 11:48:08 +02:00
QgsMapLayerRegistry . instance ( ) . addMapLayers ( [ self . vlayer ] )
2011-11-12 13:05:27 +02:00
self . populateLayers ( )
2010-03-10 01:11:11 +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 01:11:11 +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
2013-06-03 16:05:08 +04:00
self . outShape . setText ( self . shapefileName )
2009-01-20 22:54:27 +00:00
2010-03-10 01:11:11 +00:00
def compute ( self , inPoly , inLns , inField , outPath , progressBar ) :
polyLayer = ftools_utils . getVectorLayerByName ( inPoly )
lineLayer = ftools_utils . getVectorLayerByName ( inLns )
polyProvider = polyLayer . dataProvider ( )
lineProvider = lineLayer . dataProvider ( )
if polyProvider . crs ( ) < > lineProvider . crs ( ) :
QMessageBox . warning ( self , self . tr ( " CRS warning! " ) , self . tr ( " Warning: Input layers have non-matching CRS. \n This may cause unexpected results. " ) )
fieldList = ftools_utils . getFieldList ( polyLayer )
index = polyProvider . fieldNameIndex ( unicode ( inField ) )
if index == - 1 :
2013-03-13 16:03:23 +04:00
index = polyProvider . fields ( ) . count ( )
2013-02-07 00:31:24 +01:00
fieldList . append ( QgsField ( unicode ( inField ) , QVariant . Double , " real " , 24 , 15 , self . tr ( " length field " ) ) )
2010-03-10 01:11:11 +00:00
sRs = polyProvider . crs ( )
inFeat = QgsFeature ( )
inFeatB = QgsFeature ( )
outFeat = QgsFeature ( )
inGeom = QgsGeometry ( )
outGeom = QgsGeometry ( )
distArea = QgsDistanceArea ( )
2013-06-01 22:23:50 +05:30
start = 0.00
add = 100.00 / polyProvider . featureCount ( )
2010-03-10 01:11:11 +00:00
check = QFile ( self . shapefileName )
if check . exists ( ) :
if not QgsVectorFileWriter . deleteShapeFile ( self . shapefileName ) :
return
writer = QgsVectorFileWriter ( self . shapefileName , self . encoding , fieldList , polyProvider . geometryType ( ) , sRs )
spatialIndex = ftools_utils . createIndex ( lineProvider )
2013-03-13 16:03:23 +04:00
polyFit = polyProvider . getFeatures ( )
2013-02-01 01:03:19 +01:00
while polyFit . nextFeature ( inFeat ) :
2010-03-10 01:11:11 +00:00
inGeom = QgsGeometry ( inFeat . geometry ( ) )
2013-02-01 01:03:19 +01:00
atMap = inFeat . attributes ( )
2010-03-10 01:11:11 +00:00
lineList = [ ]
length = 0
lineList = spatialIndex . intersects ( inGeom . boundingBox ( ) )
if len ( lineList ) > 0 : check = 0
else : check = 1
if check == 0 :
for i in lineList :
2013-02-01 01:03:19 +01:00
lineProvider . getFeatures ( QgsFeatureRequest ( ) . setFilterFid ( int ( i ) ) ) . nextFeature ( inFeatB )
2010-03-10 01:11:11 +00:00
tmpGeom = QgsGeometry ( inFeatB . geometry ( ) )
if inGeom . intersects ( tmpGeom ) :
outGeom = inGeom . intersection ( tmpGeom )
length = length + distArea . measure ( outGeom )
outFeat . setGeometry ( inGeom )
2013-06-03 16:05:08 +04:00
atMap . append ( length )
2013-02-01 01:03:19 +01:00
outFeat . setAttributes ( atMap )
2010-03-10 01:11:11 +00:00
writer . addFeature ( outFeat )
start = start + 1
2013-06-01 22:23:50 +05:30
progressBar . setValue ( start * ( add ) )
2010-03-10 01:11:11 +00:00
del writer