Demo entry 2129579

Area

   

Submitted by anonymous on Jul 08, 2015 at 11:56
Language: Python. Code size: 1.3 kB.

import json, os, subprocess
from pywps import Process, ComplexInput, LiteralOutput, Format
from pywps.wpsserver import temp_dir

class Area(Process):
    def __init__(self):
        inputs = [ComplexInput('layer', 'Layer', [Format('GML')])]
        outputs = [LiteralOutput('area', 'Area', data_type='string')]

        super(Area, self).__init__(
            self._handler,
            identifier='area',
            title='Process Area',
            inputs=inputs,
            outputs=outputs,
            store_supported=True,
            status_supported=True
        )

    def _handler(self, request, response):
        from shapely.geometry import shape
        with temp_dir() as tmp:
            input_gml = request.inputs['layer'].file
            input_geojson = os.path.join(tmp, 'input.geojson')
            subprocess.check_call(['ogr2ogr', '-f', 'geojson',
                                   str(input_geojson), input_gml])
            with open(input_geojson, 'rb') as f:
                data = json.loads(f.read())
            for feature in data['features']:
                geom = shape(feature['geometry'])
                feature['area'] = geom.area
            response.outputs['area'].data = [
            	feature['area'] for feature in data['features']]
            return response

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).