1

I have some simple geoscript written in python. The script is run on geoserver wps. When I run the script, it always crashes whatever error exist.

Is there some way to handle those error and skip them?

For example, I have 5 shpfiles and one of them can not be read as a layer.

Can I do something like try to read it as a layer and, if not success, skip it?

Here is my code:

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}
  • 2
    Catching errors is nothing to do with geoscript, but in general, you want to avoid them, not catch them. – BradHards Jul 22 '17 at 1:58
  • How can I avoid them? @BradHards – zhangjinzhou Jul 22 '17 at 15:21
  • You debug the origin of the problem, not ignore the fault. – BradHards Jul 23 '17 at 3:25
  • How can I debug it without catching the error? Could you give me a simple example and post an answer here? @BradHards – zhangjinzhou Jul 25 '17 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.