Advertisement
Guest User

Connect line and point

a guest
Jun 28th, 2013
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. """
  2. /***************************************************************************
  3. Name : JH Plugin
  4. Description : Connects houses to rads
  5. Date : 17/Jun/13
  6. copyright : (C) 2013 by Johan Holtby
  7. email : johan.holtby@gmail.com
  8. ***************************************************************************/
  9.  
  10. /***************************************************************************
  11. * *
  12. * This program is free software; you can redistribute it and/or modify *
  13. * it under the terms of the GNU General Public License as published by *
  14. * the Free Software Foundation; either version 2 of the License, or *
  15. * (at your option) any later version. *
  16. * *
  17. ***************************************************************************/
  18. """
  19. from PyQt4 import QtCore, QtGui
  20. from PyQt4.QtCore import *
  21. from PyQt4.QtGui import *
  22. from qgis.core import *
  23.  
  24. import csv
  25. import os.path
  26. import operator
  27.  
  28. from Ui_JHPlugin import Ui_JHPlugin
  29. from Ui_JHPlugin import *
  30. # create the dialog for JHPlugin
  31. class JHPluginDialog(QtGui.QDialog):
  32.  
  33. def browse_outfile(self):
  34. newname = QFileDialog.getSaveFileName(None, QString.fromLocal8Bit("Output File"),
  35. self.ui.filename.displayText(), "*.csv")
  36. if newname != None:
  37. self.ui.filename.setText(QString(newname))
  38.  
  39. def run(self):
  40. #This inform the user that this will take some time.
  41. #QMessageBox.critical(self.iface.mainWindow(), "Start processing", "The processing will start, this my take some time.")
  42.  
  43. #Import the list of layers________________________________________________________
  44. self.canvas = self.iface.mapCanvas()
  45.  
  46.  
  47. #Get the layers selected to be processed _________________________________________
  48. self.HouseLayer=self.canvas.layer(self.ui.HouseLayer.currentIndex())
  49. self.RoadLayer=self.canvas.layer(self.ui.RoadLayer.currentIndex())
  50.  
  51.  
  52. #Add the columns___________________________________________________________________
  53. self.HouseLayer.startEditing()
  54. self.providerHouse=self.HouseLayer.dataProvider()
  55. self.providerRoad=self.RoadLayer.dataProvider()
  56. columns = self.providerHouse.fields()
  57. startColumn=len(columns)
  58.  
  59. #Coordinates of the house
  60. self.providerHouse.addAttributes( [ QgsField('X', QVariant.Double) ] )
  61. self.providerHouse.addAttributes( [ QgsField('Y', QVariant.Double) ] )
  62.  
  63. #Coordinates of the centroid of the road
  64. self.providerHouse.addAttributes( [ QgsField('R_ID', QVariant.Double) ] )
  65. self.providerHouse.addAttributes( [ QgsField('R_X', QVariant.Double) ] )
  66. self.providerHouse.addAttributes( [ QgsField('R_Y', QVariant.Double) ] )
  67. self.providerHouse.addAttributes( [ QgsField('(R_ID2)', QVariant.Double) ] )
  68.  
  69. #Go through all of the roads attributes and add them to the House layer
  70. self.columnsRoad = self.providerRoad.fields()
  71. for key,value in self.columnsRoad.items():
  72. self.providerHouse.addAttributes( [ QgsField('R_' + value.name(), value.type()) ] )
  73.  
  74. #Commit all changes
  75. self.HouseLayer.commitChanges()
  76.  
  77.  
  78. #Set up the spatial index___________________________________________________________
  79. self.spIndex = QgsSpatialIndex() #create spatial index object
  80.  
  81. self.feat = QgsFeature()
  82. self.allAttrs = self.providerRoad.attributeIndexes()
  83. self.providerRoad.select(self.allAttrs)
  84.  
  85. # insert features to index
  86. while self.providerRoad.nextFeature(self.feat):
  87. self.spIndex.insertFeature(self.feat)
  88.  
  89. #Find all the closest roads__________________________________________________________
  90. self.feat2 = QgsFeature()
  91. self.feat3 = QgsFeature()
  92.  
  93. self.allAttrs = self.providerHouse.attributeIndexes()
  94. self.providerHouse.select(self.allAttrs)
  95.  
  96. self.HouseLayer.startEditing()
  97.  
  98. #Go through all features and connect the house with the road
  99. while self.providerHouse.nextFeature(self.feat2):
  100. #House X&Y
  101. self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn, self.feat2.geometry().centroid().asPoint().x())
  102. self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+1, self.feat2.geometry().centroid().asPoint().y())
  103.  
  104. #Road ID
  105. nearestIds = self.spIndex.nearestNeighbor(self.feat2.geometry().centroid().asPoint(),1) # Get the closest neighbour
  106. self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+2, nearestIds[0])
  107.  
  108. #Road centroid X&Y
  109. self.RoadLayer.featureAtId(nearestIds[0], self.feat3, True, True)
  110. self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+3, self.feat3.geometry().centroid().asPoint().x())
  111. self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+4, self.feat3.geometry().centroid().asPoint().y())
  112.  
  113. #The attributes of the road
  114. for key,value in self.columnsRoad.items():
  115. self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+6+key, self.feat3[key])
  116.  
  117. #If there is more then one closest get both
  118. if(len(nearestIds)>1):
  119. self.HouseLayer.changeAttributeValue(self.feat2.id(), startColumn+5, nearestIds[1])
  120.  
  121.  
  122. self.HouseLayer.commitChanges()
  123. QMessageBox.critical(self.iface.mainWindow(), "Done processing!", "The processing is done.")
  124.  
  125. def __init__(self,iface):
  126. QtGui.QDialog.__init__(self)
  127.  
  128. # Set up the user interface from Designer.
  129. self.iface = iface
  130. self.ui = Ui_JHPlugin ()
  131. self.ui.setupUi(self)
  132.  
  133. #Add the browser functionality
  134. QtCore.QObject.connect(self.ui.browse, SIGNAL("clicked()"), self.browse_outfile)
  135.  
  136. #Add the function to the OK button
  137. QtCore.QObject.connect(self.ui.buttonBox, QtCore.SIGNAL("accepted()"), self.run)
  138.  
  139. #Set the file name
  140. self.ui.filename.setText(os.getcwd() + "/temp.csv")
  141.  
  142. #Add the layers
  143. self.canvas=self.iface.mapCanvas()
  144. for layer in self.canvas.layers():
  145. if layer.type() == QgsMapLayer.VectorLayer:
  146. self.ui.HouseLayer.addItem(layer.name())
  147. self.ui.RoadLayer.addItem(layer.name())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement