Guest User

Connect line and point

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