Exemple #1
0
def validate_request(request):
    """
    Validates full request with regards to validation schemas and HTTP headers.
    
    :param request: POST or GET request from user
    :type request: Flask request
    
    :raises InvalidUsage: internal HTTP 500 error with more detailed description.
    
    :returns: validated and normalized request arguments
    :rtype: dict
    """
    if request.method == 'GET':
        v.allow_unknown = True
        v.validate(dict(request.args), schema_get)

    if request.method == 'POST':
        if not 'application/json' in request.headers['Content-Type']:
            raise api_exceptions.InvalidUsage(
                500, 4001, "Content-Type header is not application/json")

        v.validate(request.get_json(), schema_post)

    if v.errors:
        errors = []
        for error in v.errors:
            errors.append("Argument '{}': {}".format(error,
                                                     v.errors[error][0]))
        raise api_exceptions.InvalidUsage(500, 4000, ", ".join(errors))

    return v.document
Exemple #2
0
def elevationpoint():
    """
    Function called when user posts to/gets /elevation/point.
    
    :raises InvalidUsage: internal HTTP 500 error with more detailed description.

    :returns: elevation response 
    :rtype: Response class
    """

    req_args = validator.validate_request(request)
    log.debug(req_args)

    if request.method == 'POST':

        # Check incoming parameters
        req_geometry = req_args['geometry']
        format_in = req_args['format_in']
        format_out = req_args['format_out']
        dataset = req_args['dataset']

        # Get the geometry
        if format_in == 'geojson':
            geom = convert.geojson_to_geometry(req_geometry)
        elif format_in == 'point':
            geom = convert.point_to_geometry(req_geometry)
        else:
            raise api_exceptions.InvalidUsage(
                400, 4000, f"Invalid format_in value {format_in}")
    else:
        req_geometry = req_args['geometry']
        format_out = req_args['format_out']
        dataset = req_args['dataset']
        try:
            # Catch errors when parsing the input string
            point_coords = [float(x) for x in req_geometry.split(',')]
        except:
            raise api_exceptions.InvalidUsage(
                500, 4000,
                '{} is not a comma separated list of long, lat'.format(
                    req_geometry))

        geom = convert.point_to_geometry(point_coords)

    # Build response with attribution etc.
    results = ResponseBuilder().__dict__
    geom_queried = querybuilder.point_elevation(geom, format_out, dataset)

    if format_out == 'point':
        geom_out = wkt.loads(geom_queried)
        results['geometry'] = list(geom_out.coords[0])
    elif format_out == 'geojson':
        results['geometry'] = json.loads(geom_queried)
    else:
        raise api_exceptions.InvalidUsage(
            400, 4000, f'Invalid format_out value "{format_out}"')

    return jsonify(results)
Exemple #3
0
def elevationline():
    """
    Function called when user posts to /elevation/line.
    
    :raises InvalidUsage: internal HTTP 500 error with more detailed description.

    :returns: elevation response 
    :rtype: Response
    """
    # Cerberus validates and returns a processed arg dict
    req_args = validator.validate_request(request)

    # Incoming parameters
    geometry_str = req_args['geometry']
    format_in = req_args['format_in']
    format_out = req_args['format_out']
    dataset = req_args['dataset']

    # Get the geometry
    if format_in == 'geojson':
        geom = convert.geojson_to_geometry(geometry_str)
    elif format_in in ['encodedpolyline', 'encodedpolyline5']:
        geom = codec.decode(geometry_str, precision=5, is3d=False)
    elif format_in == 'encodedpolyline6':
        geom = codec.decode(geometry_str, precision=6, is3d=False)
    elif format_in == 'polyline':
        geom = convert.polyline_to_geometry(geometry_str)
    else:
        raise api_exceptions.InvalidUsage(
            400, 4000, f'Invalid format_in value "{format_in}"')

    if len(list(geom.coords)) > SETTINGS['maximum_nodes']:
        raise api_exceptions.InvalidUsage(
            status_code=400,
            error_code=4003,
            message='Maximum number of nodes exceeded.')

    results = ResponseBuilder().__dict__
    geom_queried = querybuilder.line_elevation(geom, format_out, dataset)

    # decision tree for format_out
    if format_out != 'geojson':
        geom_out = wkt.loads(geom_queried)
        coords = geom_out.coords
        if format_out in ['encodedpolyline', 'encodedpolyline5']:
            results['geometry'] = codec.encode(coords, precision=5, is3d=True)
        elif format_out == 'encodedpolyline6':
            results['geometry'] = codec.encode(coords, precision=6, is3d=True)
        else:
            results['geometry'] = list(coords)
    elif format_out == 'geojson':
        results['geometry'] = json.loads(geom_queried)
    else:
        raise api_exceptions.InvalidUsage(
            400, 4000, f'Invalid format_out value "{format_out}"')

    return jsonify(results)