Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- /***************************************************************************
- Name : JH Plugin
- Description : Connects houses to rads
- Date : 17/Jun/13
- copyright : (C) 2013 by Johan Holtby
- email : johan.holtby@gmail.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 PyQt4 import QtCore, QtGui
- from PyQt4.QtCore import *
- from PyQt4.QtGui import *
- from qgis.core import *
- import csv
- import os.path
- import operator
- from Ui_JHPlugin import Ui_JHPlugin
- from Ui_JHPlugin import *
- # create the dialog for JHPlugin
- class JHPluginDialog(QtGui.QDialog):
- def browse_outfile(self):
- newname = QFileDialog.getSaveFileName(None, QString.fromLocal8Bit("Output File"),
- self.ui.filename.displayText(), "*.csv")
- if newname != None:
- self.ui.filename.setText(QString(newname))
- def run(self):
- #This inform the user that this will take some time.
- #QMessageBox.critical(self.iface.mainWindow(), "Start processing", "The processing will start, this my take some time.")
- #Import the list of layers________________________________________________________
- self.canvas = self.iface.mapCanvas()
- #Get the layers selected to be processed _________________________________________
- self.HouseLayer=self.canvas.layer(self.ui.HouseLayer.currentIndex())
- self.RoadLayer=self.canvas.layer(self.ui.RoadLayer.currentIndex())
- #Add the columns___________________________________________________________________
- self.HouseLayer.startEditing()
- self.providerHouse=self.HouseLayer.dataProvider()
- self.providerRoad=self.RoadLayer.dataProvider()
- columns = self.providerHouse.fields()
- startColumn=len(columns)
- #Coordinates of the house
- self.providerHouse.addAttributes( [ QgsField('X', QVariant.Double) ] )
- self.providerHouse.addAttributes( [ QgsField('Y', QVariant.Double) ] )
- #Coordinates of the centroid of the road
- self.providerHouse.addAttributes( [ QgsField('R_ID', QVariant.Double) ] )
- self.providerHouse.addAttributes( [ QgsField('R_X', QVariant.Double) ] )
- self.providerHouse.addAttributes( [ QgsField('R_Y', QVariant.Double) ] )
- self.providerHouse.addAttributes( [ QgsField('(R_ID2)', QVariant.Double) ] )
- #Go through all of the roads attributes and add them to the House layer
- self.columnsRoad = self.providerRoad.fields()
- for key,value in self.columnsRoad.items():
- self.providerHouse.addAttributes( [ QgsField('R_' + value.name(), value.type()) ] )
- #Commit all changes
- self.HouseLayer.commitChanges()
- #Set up the spatial index___________________________________________________________
- self.spIndex = QgsSpatialIndex() #create spatial index object
- self.feat = QgsFeature()
- self.allAttrs = self.providerRoad.attributeIndexes()
- self.providerRoad.select(self.allAttrs)
- # insert features to index
- while self.providerRoad.nextFeature(self.feat):
- self.spIndex.insertFeature(self.feat)
- #Find all the closest roads__________________________________________________________
- self.feat2 = QgsFeature()
- self.feat3 = QgsFeature()
- self.allAttrs = self.providerHouse.attributeIndexes()
- self.providerHouse.select(self.allAttrs)
- self.HouseLayer.startEditing()
- #Go through all features and connect the house with the road
- while self.providerHouse.nextFeature(self.feat2):
- #House X&Y
- self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn, self.feat2.geometry().centroid().asPoint().x())
- self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+1, self.feat2.geometry().centroid().asPoint().y())
- #Road ID
- nearestIds = self.spIndex.nearestNeighbor(self.feat2.geometry().centroid().asPoint(),1) # Get the closest neighbour
- self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+2, nearestIds[0])
- #Road centroid X&Y
- self.RoadLayer.featureAtId(nearestIds[0], self.feat3, True, True)
- self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+3, self.feat3.geometry().centroid().asPoint().x())
- self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+4, self.feat3.geometry().centroid().asPoint().y())
- #The attributes of the road
- for key,value in self.columnsRoad.items():
- self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+6+key, self.feat3[key])
- #If there is more then one closest get both
- if(len(nearestIds)>1):
- self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+5, nearestIds[1])
- self.HouseLayer.commitChanges()
- QMessageBox.critical(self.iface.mainWindow(), "Done processing!", "The processing is done.")
- def __init__(self,iface):
- QtGui.QDialog.__init__(self)
- # Set up the user interface from Designer.
- self.iface = iface
- self.ui = Ui_JHPlugin ()
- self.ui.setupUi(self)
- #Add the browser functionality
- QtCore.QObject.connect(self.ui.browse, SIGNAL("clicked()"), self.browse_outfile)
- #Add the function to the OK button
- QtCore.QObject.connect(self.ui.buttonBox, QtCore.SIGNAL("accepted()"), self.run)
- #Set the file name
- self.ui.filename.setText(os.getcwd() + "/temp.csv")
- #Add the layers
- self.canvas=self.iface.mapCanvas()
- for layer in self.canvas.layers():
- if layer.type() == QgsMapLayer.VectorLayer:
- self.ui.HouseLayer.addItem(layer.name())
- self.ui.RoadLayer.addItem(layer.name())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement