Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import os
  2. from qgis.gui import *
  3. from qgis.core import *
  4. import processing
  5.  
  6. #------------------------------------------------------
  7. # User Variables:
  8.  
  9. #path to data location
  10. soil_path = "D:\soil\soildata"
  11. shp_path = "D:\soil\field_boundaries"
  12. print ("Soil data: " + soil_path)
  13. print ("Field Boundaries: " + shp_path)
  14.  
  15.  
  16. #-------------------------------------------------------
  17.  
  18. def get_data(path, ending):
  19. data_list = []
  20.  
  21. for root, dirs, files in os.walk(path):
  22.  
  23. for data in sorted(files):
  24. if data.endswith(ending):
  25. data_list.append(os.path.join(root, data))
  26.  
  27. return data_list
  28. print (str(len(data_list)) + " shapefiles found")
  29.  
  30.  
  31.  
  32. def clip(vor_poly):
  33. clipped_data = []
  34. shp_data = get_data(shp_path, ".shp")
  35. print (str(len(shp_data)) + " field boundaries found")
  36. for f in shp_data:
  37. print (f)
  38. print (vor_poly)
  39. if f.geometry().intersects(vor_poly.geometry()):
  40.  
  41. OUTPUT = os.path.splitext(polygons)[0] + os.path.basename(f) + ".shp"
  42.  
  43. processing.runalg('qgis:clip', vor_poly, f, OUTPUT)
  44.  
  45. print ("clipped " + OUTPUT)
  46.  
  47. clipped_data.append(OUTPUT)
  48. return clipped_data
  49.  
  50.  
  51.  
  52. def qgisprocess(soilpts):
  53.  
  54. #voronoi polygons with 0 size buffer
  55.  
  56. print("Voronoi Polygons")
  57.  
  58. for f in soilpts:
  59. split_path = os.path.splitext(f)[0]
  60. inlayer = f
  61. outlayer = split_path + "_vor_poly.shp"
  62.  
  63. processing.run("qgis:voronoipolygons", { 'INPUT':inlayer, 'BUFFER': 0, 'OUTPUT': outlayer})
  64.  
  65. clippedpolys = clip(outlayer)
  66.  
  67.  
  68. def main():
  69. #list of data
  70. soil_data = get_data(soil_path, ".shp")
  71.  
  72. print("running QGIS processes")
  73. qgisprocess(soil_data)
  74.  
  75.  
  76. if __name__ == '__console__':
  77. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement