Exemplo n.º 1
0
 def test_start_end(self):
     start = (12.3, -45.6)
     end = (12.9, -45.9)
     lats = [start[0], end[0]]
     lons = [start[1], end[1]]
     rlats, rlons = utils.sample_points_on_path(lats, lons, 2)
     assert len(rlats) == 2
     assert len(rlons) == 2
     assert (rlats[0], rlons[0]) == start
     assert (rlats[1], rlons[1]) == end
Exemplo n.º 2
0
 def test_two_points(self):
     start = (12.3, -45.6)
     middle = (12.7, 45.1)
     end = (12.9, 45.9)
     lats = [start[0], middle[0], end[0]]
     lons = [start[1], middle[1], end[1]]
     rlats, rlons = utils.sample_points_on_path(lats, lons, 2)
     assert len(rlats) == 2
     assert len(rlons) == 2
     assert (rlats[0], rlons[0]) == start
     assert (rlats[1], rlons[1]) == end
Exemplo n.º 3
0
 def test_lon_wraparound(self):
     """Path should take short route over date line."""
     lon = 178
     n_points = 18
     start = (1, -lon)
     end = (-1, lon)
     lats = [start[0], end[0]]
     lons = [start[1], end[1]]
     rlats, rlons = utils.sample_points_on_path(lats, lons, n_points)
     assert len(rlats) == n_points
     assert len(rlons) == n_points
     assert all(rlon >= lon or rlon <= -lon for rlon in rlons)
Exemplo n.º 4
0
 def test_lat_wraparound(self):
     """Path should take short route over the north pole."""
     lat = 88
     n_points = 7
     start = (lat, -90)
     end = (lat, 90)
     lats = [start[0], end[0]]
     lons = [start[1], end[1]]
     rlats, rlons = utils.sample_points_on_path(lats, lons, n_points)
     assert len(rlats) == n_points
     assert len(rlons) == n_points
     assert all(rlat >= lat for rlat in rlats)
Exemplo n.º 5
0
def get_elevation(dataset_name):
    """Calculate the elevation for the given locations.

    Args:
        dataset_name: String matching a dataset in the config file.

    Returns:
        Response.
    """

    try:
        # Parse inputs.
        interpolation = _parse_interpolation(
            _find_request_argument(request, "interpolation"))
        nodata_value = _parse_nodata_value(
            _find_request_argument(request, "nodata_value"))
        lats, lons = _parse_locations(
            _find_request_argument(request, "locations"),
            _load_config()["max_locations_per_request"],
        )

        # Check if need to do sampling.
        n_samples = _parse_n_samples(
            _find_request_argument(request, "samples"),
            _load_config()["max_locations_per_request"],
        )
        if n_samples:
            lats, lons = utils.sample_points_on_path(lats, lons, n_samples)

        # Get the z values.
        datasets = _get_datasets(dataset_name)
        elevations, dataset_names = backend.get_elevation(
            lats, lons, datasets, interpolation, nodata_value)

        # Build response.
        results = []
        for z, dataset_name, lat, lon in zip(elevations, dataset_names, lats,
                                             lons):
            results.append({
                "elevation": z,
                "dataset": dataset_name,
                "location": {
                    "lat": lat,
                    "lng": lon
                },
            })
        data = {"status": "OK", "results": results}
        return jsonify(data)

    except (ClientError, backend.InputError) as e:
        return jsonify({"status": "INVALID_REQUEST", "error": str(e)}), 400
    except config.ConfigError as e:
        return (
            jsonify({
                "status": "SERVER_ERROR",
                "error": "Config Error: {}".format(e)
            }),
            500,
        )
    except Exception as e:
        if app.debug:
            raise e
        app.logger.error(e)
        msg = "Unhandled server error, see server logs for details."
        return jsonify({"status": "SERVER_ERROR", "error": msg}), 500