Example #1
0
def places():
    """
    Function called when user posts or gets to /places.

    :return: This function will either return category statistics or poi information within a given
    geometry. Depending on GET or POST it will prepare the payload accordingly.
    :type: string
    """

    if request.method == 'POST':

        if 'application/json' in request.headers['Content-Type'] and request.is_json:

            all_args = request.get_json(silent=True)

            raw_request = copy.deepcopy(all_args)

            if all_args is None:
                raise api_exceptions.InvalidUsage(status_code=500, error_code=4000)

            try:
                schema(all_args)
            except MultipleInvalid as error:
                raise api_exceptions.InvalidUsage(status_code=500, error_code=4000, message=str(error))
            # query stats
            if all_args['request'] == 'list':
                r = Response(json.dumps(categories_tools.categories_object), mimetype='application/json; charset=utf-8')
                return r

            if 'filters' in all_args and 'category_group_ids' in all_args['filters']:
                all_args['filters']['category_ids'] = categories_tools.unify_categories(all_args['filters'])

            if 'limit' not in all_args:
                all_args['limit'] = ops_settings['response_limit']

            if 'geometry' not in all_args:
                raise api_exceptions.InvalidUsage(status_code=500, error_code=4002)

            are_required_geom_present(all_args['geometry'])

            # check restrictions and parse geometry
            all_args['geometry'] = parse_geometries(all_args['geometry'])

            features = request_pois(all_args)

            query_info = QueryInfo(raw_request).__dict__

            features["information"] = query_info

            # query pois
            r = Response(json.dumps(features), mimetype='application/json; charset=utf-8')
            return r

    else:

        raise api_exceptions.InvalidUsage(status_code=500, error_code=4006)
Example #2
0
def places():
    """
    Function called when user posts or gets to /places.

    :return: This function will either return category statistics or poi information within a given
    geometry. Depending on GET or POST it will prepare the payload accordingly.
    :type: string
    """

    if request.method == 'POST':

        if request.is_json:

            all_args = request.get_json(silent=True)

            raw_request = copy.deepcopy(all_args)

            if all_args is None:
                raise api_exceptions.InvalidUsage(status_code=400,
                                                  error_code=4000)

            try:
                schema(all_args)
            except MultipleInvalid as error:
                raise api_exceptions.InvalidUsage(status_code=400,
                                                  error_code=4000,
                                                  message=str(error))
            # query stats
            if all_args['request'] == 'list':
                r = Response(json.dumps(categories_tools.categories_object),
                             mimetype='application/json; charset=utf-8')
                return r

            if 'filters' in all_args and 'category_group_ids' in all_args[
                    'filters']:
                all_args['filters'][
                    'category_ids'] = categories_tools.unify_categories(
                        all_args['filters'])

            if 'limit' not in all_args:
                all_args['limit'] = ops_settings['response_limit']

            if 'geometry' not in all_args:
                raise api_exceptions.InvalidUsage(status_code=400,
                                                  error_code=4002)

            are_required_geom_present(all_args['geometry'])

            # check restrictions and parse geometry
            all_args['geometry'] = parse_geometries(all_args['geometry'])
            features = []
            gj = all_args['geometry'].get('geojson')
            if gj and gj.get('type') == 'MultiPolygon':
                polygons = list(all_args['geometry']['geom'])

                for polygon in polygons:
                    all_args['geometry']['geom'] = polygon
                    tmp = request_pois(all_args)
                    query_info = QueryInfo(raw_request).__dict__
                    tmp["information"] = query_info
                    features.append(tmp)

            else:
                tmp = request_pois(all_args)
                query_info = QueryInfo(raw_request).__dict__
                tmp["information"] = query_info
                features.append(tmp)

            # query pois
            r = Response(json.dumps(features),
                         mimetype='application/json; charset=utf-8')
            return r

        else:
            with open("osm/invalid-requests.log", "a") as f:
                time = datetime.now()
                req = request.data.decode().strip().replace("\n", "").replace(
                    " ", "")
                f.write(f"{time} {request.remote_addr}: - {req}\n")
                f.close()

            raise api_exceptions.InvalidUsage(status_code=400, error_code=4009)

    else:

        raise api_exceptions.InvalidUsage(status_code=400, error_code=4006)