2009-10-04 18:06:44 +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_frmIntersectLines 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 )
QObject . connect ( self . inLine1 , SIGNAL ( " currentIndexChanged(QString) " ) , self . update1 )
QObject . connect ( self . inLine2 , SIGNAL ( " currentIndexChanged(QString) " ) , self . update2 )
self . setWindowTitle ( self . tr ( " Line intersections " ) )
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 . Line ] )
2012-02-15 15:21:02 +02:00
self . inLine1 . blockSignals ( True )
self . inLine2 . blockSignals ( True )
2011-11-12 13:05:27 +02:00
self . inLine1 . clear ( )
self . inLine2 . clear ( )
2012-02-15 15:21:02 +02:00
self . inLine1 . blockSignals ( False )
self . inLine2 . blockSignals ( False )
2010-03-10 00:16:03 +00:00
self . inLine1 . addItems ( layers )
self . inLine2 . addItems ( layers )
2009-01-20 22:54:27 +00:00
2010-03-10 00:16:03 +00:00
def update1 ( self , inputLayer ) :
self . inField1 . clear ( )
changedLayer = ftools_utils . getVectorLayerByName ( unicode ( inputLayer ) )
changedField = ftools_utils . getFieldList ( changedLayer )
2013-02-07 00:31:24 +01:00
for f in changedField :
self . inField1 . addItem ( unicode ( f . name ( ) ) )
2009-01-20 22:54:27 +00:00
2010-03-10 00:16:03 +00:00
def update2 ( self , inputLayer ) :
self . inField2 . clear ( )
changedLayer = ftools_utils . getVectorLayerByName ( unicode ( inputLayer ) )
changedField = ftools_utils . getFieldList ( changedLayer )
2013-02-07 00:31:24 +01:00
for f in changedField :
self . inField2 . addItem ( unicode ( f . name ( ) ) )
2009-01-20 22:54:27 +00: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 . inLine1 . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Locate Line Intersections " ) , self . tr ( " Please specify input line layer " ) )
elif self . outShape . text ( ) == " " :
QMessageBox . information ( self , self . tr ( " Locate Line Intersections " ) , self . tr ( " Please specify output shapefile " ) )
elif self . inLine2 . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Locate Line Intersections " ) , self . tr ( " Please specify line intersect layer " ) )
elif self . inField1 . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Locate Line Intersections " ) , self . tr ( " Please specify input unique ID field " ) )
elif self . inField2 . currentText ( ) == " " :
QMessageBox . information ( self , self . tr ( " Locate Line Intersections " ) , self . tr ( " Please specify intersect unique ID field " ) )
else :
line1 = self . inLine1 . currentText ( )
line2 = self . inLine2 . currentText ( )
field1 = self . inField1 . currentText ( )
field2 = self . inField2 . currentText ( )
outPath = self . outShape . text ( )
self . outShape . clear ( )
self . compute ( line1 , line2 , field1 , field2 , outPath , self . progressBar )
self . progressBar . setValue ( 100 )
2013-05-31 14:57:34 +04:00
addToTOC = QMessageBox . question ( self , self . tr ( " Generate Centroids " ) , self . tr ( " Created output point shapefile: \n %s \n \n Would you like to add the new layer to the TOC? " ) % ( outPath ) , QMessageBox . Yes , QMessageBox . No , QMessageBox . NoButton )
2010-03-10 00:16:03 +00:00
if addToTOC == QMessageBox . Yes :
if not ftools_utils . addShapeToCanvas ( unicode ( outPath ) ) :
2013-05-31 14:57:34 +04:00
QMessageBox . warning ( self , self . tr ( " Geoprocessing " ) , self . tr ( " Error loading output shapefile: \n %s " ) % ( unicode ( outPath ) ) )
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
2013-06-03 16:05:08 +04:00
self . outShape . setText ( self . shapefileName )
2009-01-20 22:54:27 +00:00
2010-03-10 00:16:03 +00:00
def compute ( self , line1 , line2 , field1 , field2 , outPath , progressBar ) :
2013-01-25 20:40:20 +01:00
2010-03-10 00:16:03 +00:00
layer1 = ftools_utils . getVectorLayerByName ( line1 )
provider1 = layer1 . dataProvider ( )
fieldList = ftools_utils . getFieldList ( layer1 )
index1 = provider1 . fieldNameIndex ( field1 )
field1 = fieldList [ index1 ]
field1 . setName ( unicode ( field1 . name ( ) ) + " _1 " )
2013-01-25 20:40:20 +01:00
layer2 = ftools_utils . getVectorLayerByName ( line2 )
provider2 = layer2 . dataProvider ( )
2010-03-10 00:16:03 +00:00
fieldList = ftools_utils . getFieldList ( layer2 )
index2 = provider2 . fieldNameIndex ( field2 )
field2 = fieldList [ index2 ]
field2 . setName ( unicode ( field2 . name ( ) ) + " _2 " )
2013-01-25 20:40:20 +01:00
2013-02-07 00:31:24 +01:00
fieldList = QgsFields ( )
fieldList . append ( field1 )
fieldList . append ( field2 )
2010-03-10 00:16:03 +00:00
sRs = provider1 . crs ( )
check = QFile ( self . shapefileName )
if check . exists ( ) :
if not QgsVectorFileWriter . deleteShapeFile ( self . shapefileName ) :
return
2013-01-25 20:40:20 +01:00
2010-03-10 00:16:03 +00:00
writer = QgsVectorFileWriter ( self . shapefileName , self . encoding , fieldList , QGis . WKBPoint , sRs )
inFeat = QgsFeature ( )
inFeatB = QgsFeature ( )
outFeat = QgsFeature ( )
2013-07-06 10:14:28 -03:00
outFields = QgsFields ( )
outFields . append ( field1 )
outFields . append ( field2 )
outFeat . setFields ( outFields )
2010-03-10 00:16:03 +00:00
start = 15.00
add = 85.00 / layer1 . featureCount ( )
2013-01-25 20:40:20 +01:00
2010-03-10 00:16:03 +00:00
index = ftools_utils . createIndex ( provider2 )
2013-01-25 20:40:20 +01:00
2013-07-06 10:47:44 -03:00
singlelayer_tempList = [ ]
2013-06-19 13:26:47 +02:00
fit1 = provider1 . getFeatures ( QgsFeatureRequest ( ) . setSubsetOfAttributes ( [ index1 ] ) )
2013-02-01 01:03:19 +01:00
while fit1 . nextFeature ( inFeat ) :
2010-03-10 00:16:03 +00:00
inGeom = inFeat . geometry ( )
2013-02-01 01:03:19 +01:00
v1 = inFeat . attributes ( ) [ index1 ]
2013-01-25 20:40:20 +01:00
2010-03-10 00:16:03 +00:00
lineList = index . intersects ( inGeom . boundingBox ( ) )
2013-01-25 20:40:20 +01:00
for i in lineList :
2013-02-01 01:03:19 +01:00
provider2 . getFeatures ( QgsFeatureRequest ( ) . setFilterFid ( int ( i ) ) . setSubsetOfAttributes ( [ index2 ] ) ) . nextFeature ( inFeatB )
2013-01-25 20:40:20 +01:00
tmpGeom = QgsGeometry ( inFeatB . geometry ( ) )
2013-02-01 01:03:19 +01:00
v2 = inFeatB . attributes ( ) [ index2 ]
2013-01-25 20:40:20 +01:00
if inGeom . intersects ( tmpGeom ) :
tempGeom = inGeom . intersection ( tmpGeom )
if tempGeom . type ( ) == QGis . Point :
tempList = [ ]
if tempGeom . isMultipart ( ) :
tempList = tempGeom . asMultiPoint ( )
else :
tempList . append ( tempGeom . asPoint ( ) )
for j in tempList :
2013-07-06 10:47:44 -03:00
# if same layer, avoid insert duplicated points
if line1 == line2 :
if not j in singlelayer_tempList :
singlelayer_tempList . append ( j )
outFeat . setGeometry ( tempGeom . fromPoint ( j ) )
outFeat . setAttribute ( 0 , v1 )
outFeat . setAttribute ( 1 , v2 )
writer . addFeature ( outFeat )
else :
outFeat . setGeometry ( tempGeom . fromPoint ( j ) )
outFeat . setAttribute ( 0 , v1 )
outFeat . setAttribute ( 1 , v2 )
writer . addFeature ( outFeat )
2013-01-25 20:40:20 +01:00
2010-03-10 00:16:03 +00:00
start = start + add
progressBar . setValue ( start )
2013-01-25 20:40:20 +01:00
2010-03-10 00:16:03 +00:00
del writer