Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from geoserver.wps import process
- from geoscript.feature import Feature
- from geoscript.layer import Layer
- from geoscript.workspace import Directory
- from geoscript import geom
- from geoscript import proj
- from geoscript.feature import Schema
- from geoscript.layer.io.gml import readGML
- from geoscript.layer.io.gml import writeGML
- from geoscript.layer.io.json import readJSON
- import os
- @process(
- title = 'title',
- description = 'des',
- inputs = {
- 'bound': (str, 'the boundry to cut shps'),
- 'uid': (str, 'the uid of current request')
- },
- outputs = {
- 'output1': (str, 'test'),
- 'output2': (str, 'test')
- }
- )
- def run(bound, uid):
- # Set source/destination directories
- wfs_source_dir = 'path'
- shp_source_dir = Directory('path')
- des_dir = 'path'
- output1 = ''
- output2 = ''
- # Get cutting boundary
- boundary = geom.fromWKT(bound)
- # Get wfs layers and put them in wfs_layers
- wfs_layers = []
- for filename in os.listdir(wfs_source_dir):
- file = open(wfs_source_dir + '/' + filename, 'r')
- if filename.startswith('esri'):
- layer = readJSON(file)
- # output1 = str(file)
- wfs_layers.append(layer)
- output2 = str(filename)
- else:
- layer = readGML(file)
- wfs_layers.append(layer.reproject('epsg:3857'))
- output1 = str(file)
- has_wfs = 0
- if len(wfs_layers) > 0:
- has_wfs = 1
- # Get shp layers and put them in shp_layers
- shp_name_list = shp_source_dir.layers()
- shp_layers = []
- for shpName in shp_name_list:
- shp = shp_source_dir.get(shpName)
- shp_layers.append(shp.reproject('epsg:3857'))
- # Create new schema which is combination of all layers
- schema = ''
- fields = []
- if has_wfs == 1:
- for index, layer in enumerate(wfs_layers):
- if index == 0:
- fields = layer.schema.fields
- fields[3].typ = geom.Geometry
- else:
- for field in layer.schema.fields[4:]:
- fields.append(field)
- for index, layer in enumerate(shp_layers):
- for field in layer.schema.fields[1:]:
- fields.append(field)
- else:
- for index, layer in enumerate(shp_layers):
- if index == 0:
- fields = layer.schema.fields
- fields[0].typ = geom.Geometry
- else:
- for field in layer.schema.fields[1:]:
- fields.append(field)
- schema = Schema(uid, fields)
- # Create new layer and add all features to it
- outlayer = Layer(schema = schema)
- output_array = []
- for layer in wfs_layers:
- cursor = layer.cursor()
- features = cursor.read(layer.count())
- for feature in features:
- geometry = feature.geom
- if geometry.intersects(boundary):
- new_feature = feature
- new_geometry = geometry.intersection(boundary)
- new_feature.set('the_geom', new_geometry)
- outlayer.add(new_feature)
- cursor.close()
- for layer in shp_layers:
- cursor = layer.cursor()
- features = cursor.read(layer.count())
- for feature in features:
- geometry = feature.geom
- if geometry.intersects(boundary):
- new_feature = feature
- new_geometry = geometry.intersection(boundary)
- new_feature.set('the_geom', new_geometry)
- outlayer.add(new_feature)
- cursor.close()
- # Write new layer as GML
- out = open(des_dir + '/' + uid + '.gml', 'w')
- writeGML(outlayer, output = out)
- return {'output1': output1, 'output2': output2}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement