Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. from geoserver.wps import process
  2. from geoscript.feature import Feature
  3. from geoscript.layer import Layer
  4. from geoscript.workspace import Directory
  5. from geoscript import geom
  6. from geoscript import proj
  7. from geoscript.feature import Schema
  8. from geoscript.layer.io.gml import readGML
  9. from geoscript.layer.io.gml import writeGML
  10. from geoscript.layer.io.json import readJSON
  11. import os
  12.  
  13.  
  14. @process(
  15. title = 'title',
  16. description = 'des',
  17. inputs = {
  18. 'bound': (str, 'the boundry to cut shps'),
  19. 'uid': (str, 'the uid of current request')
  20. },
  21. outputs = {
  22. 'output1': (str, 'test'),
  23. 'output2': (str, 'test')
  24. }
  25. )
  26.  
  27. def run(bound, uid):
  28.  
  29. # Set source/destination directories
  30. wfs_source_dir = 'path'
  31. shp_source_dir = Directory('path')
  32. des_dir = 'path'
  33.  
  34. output1 = ''
  35. output2 = ''
  36.  
  37. # Get cutting boundary
  38. boundary = geom.fromWKT(bound)
  39.  
  40. # Get wfs layers and put them in wfs_layers
  41. wfs_layers = []
  42.  
  43. for filename in os.listdir(wfs_source_dir):
  44. file = open(wfs_source_dir + '/' + filename, 'r')
  45. if filename.startswith('esri'):
  46. layer = readJSON(file)
  47. # output1 = str(file)
  48. wfs_layers.append(layer)
  49. output2 = str(filename)
  50. else:
  51. layer = readGML(file)
  52. wfs_layers.append(layer.reproject('epsg:3857'))
  53. output1 = str(file)
  54. has_wfs = 0
  55. if len(wfs_layers) > 0:
  56. has_wfs = 1
  57.  
  58. # Get shp layers and put them in shp_layers
  59. shp_name_list = shp_source_dir.layers()
  60.  
  61. shp_layers = []
  62. for shpName in shp_name_list:
  63. shp = shp_source_dir.get(shpName)
  64. shp_layers.append(shp.reproject('epsg:3857'))
  65.  
  66. # Create new schema which is combination of all layers
  67. schema = ''
  68. fields = []
  69.  
  70. if has_wfs == 1:
  71. for index, layer in enumerate(wfs_layers):
  72. if index == 0:
  73. fields = layer.schema.fields
  74. fields[3].typ = geom.Geometry
  75. else:
  76. for field in layer.schema.fields[4:]:
  77. fields.append(field)
  78. for index, layer in enumerate(shp_layers):
  79. for field in layer.schema.fields[1:]:
  80. fields.append(field)
  81. else:
  82. for index, layer in enumerate(shp_layers):
  83. if index == 0:
  84. fields = layer.schema.fields
  85. fields[0].typ = geom.Geometry
  86. else:
  87. for field in layer.schema.fields[1:]:
  88. fields.append(field)
  89.  
  90. schema = Schema(uid, fields)
  91.  
  92. # Create new layer and add all features to it
  93. outlayer = Layer(schema = schema)
  94. output_array = []
  95. for layer in wfs_layers:
  96. cursor = layer.cursor()
  97. features = cursor.read(layer.count())
  98. for feature in features:
  99. geometry = feature.geom
  100. if geometry.intersects(boundary):
  101. new_feature = feature
  102. new_geometry = geometry.intersection(boundary)
  103. new_feature.set('the_geom', new_geometry)
  104. outlayer.add(new_feature)
  105. cursor.close()
  106. for layer in shp_layers:
  107. cursor = layer.cursor()
  108. features = cursor.read(layer.count())
  109. for feature in features:
  110. geometry = feature.geom
  111. if geometry.intersects(boundary):
  112. new_feature = feature
  113. new_geometry = geometry.intersection(boundary)
  114. new_feature.set('the_geom', new_geometry)
  115. outlayer.add(new_feature)
  116. cursor.close()
  117.  
  118. # Write new layer as GML
  119. out = open(des_dir + '/' + uid + '.gml', 'w')
  120. writeGML(outlayer, output = out)
  121.  
  122. return {'output1': output1, 'output2': output2}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement