Exemplo n.º 1
0
def sobel(r):
    image = read_image(r)
    ACCESS_LOG("Response shape: {}".format(image.shape))
    image = cv.Canny(image, 300, 200)
    # image = sobel_each(image)
    output = write_image(image, format="png")

    return output
Exemplo n.º 2
0
def timeSeries():
    ACCESS_LOG(str(request.args))
    ACCESS_LOG(request.full_path)

    with open("data.json", "rb") as f:
        raw = f.read()

    data = json.loads(raw)

    # data['meta']['time']['iso_start']
    # data['meta']['time']['iso_stop']

    # for entry in data['data']:
    #     for key in entry[0].keys():
    #         if key in ['std', 'min', 'max', 'mean']:
    #             entry[0][key] = 5.0

    # ACCESS_LOG(str(data))

    # return jsonify(dict(data)), 200, {"Content-Type" : "application/json"}

    # url = "https://sealevel-nexus.jpl.nasa.gov" + request.full_path
    # ACCESS_LOG(url)
    # r = requests.get(url)

    # if r.status_code != 200:
    #     ACCESS_LOG("Request returned with status code {}".format(r.status_code))
    #     return render_template('404.html'), 404

    ACCESS_LOG("---REQUEST SUCCEEDED---")
    # ACCESS_LOG(str(r.json()))
    # ACCESS_LOG(str(r.headers))
    ACCESS_LOG("---REQUEST DONE---")

    # with open("out.json", "wb") as f:
    #     f.write(r.content)

    data['stats'] = {}
    data['meta'][0]['shortName'] = data['meta'][0]['short_name']

    return json.dumps(data, indent=4), 200, {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
    }
Exemplo n.º 3
0
def correlation(x, y, tilematrix, layer1, layer2, date1, date2):
    s = time.time()
    image1 = get_range(x, y, tilematrix, "500m", layer1, date1, date2)
    image2 = get_range(x, y, tilematrix, "250m", layer2, date1, date2)
    e = time.time()

    ACCESS_LOG("Both requests took: {}s".format(e - s))
    ACCESS_LOG("image1 shape {}, image2 shape {}".format(
        image1.shape, image2.shape))

    n = 8
    increment = image1[0].shape[1] // n

    data1 = skimage.measure.block_reduce(image1, (1, n, n, 1), np.average)
    data2 = skimage.measure.block_reduce(image2, (1, n, n, 1), np.average)

    # data1 = np.zeros((n, n, image1[0].shape[-1], image1.shape[0]))
    # for i in range(n):
    #     for j in range(n):
    #         data1[i, j] = image1[:, increment * i : increment * (i + 1), increment * j : increment * (j + 1), 0:3].mean(axis=(1, 2)).T

    # data2 = np.zeros((n, n, image2[0].shape[-1], image2.shape[0]))
    # for i in range(n):
    #     for j in range(n):
    #         data2[i, j] = image2[:, increment * i : increment * (i + 1), increment * j : increment * (j + 1), 0:3].mean(axis=(1, 2)).T

    output = np.zeros(
        (image1[0].shape[0], image1[0].shape[1], image1[0].shape[-1]))

    s = time.time()
    for i in range(n):
        for j in range(n):
            for k in range(output.shape[2]):
                output[increment * i:increment * (i + 1),
                       increment * j:increment * (j + 1),
                       k] = np.corrcoef(data1[i, j, k], data2[i, j, k])[1, 0]

    e = time.time()
    ACCESS_LOG("output shape: {}. Correlation took {}s".format(
        output.shape, e - s))

    return write_image(output, format="jpeg")
Exemplo n.º 4
0
def single_tile(projection, kind, product, date, resolution, tilematrix, x, y,
                extension):
    url = "http://onearth-tile-services" + request.path

    ACCESS_LOG(f"URL: {url}")
    ACCESS_LOG(
        f"Projection {projection}, kind: {kind}, product: {product}, date: {date}, resolution: {resolution}"
    )
    ACCESS_LOG(f"Args: {request.query_string}")

    r, status_code, headers = make_request(url, headers=request.headers)

    if status_code == 404:
        ACCESS_LOG("Status_code 404")
        return render_template('404.html'), 404, headers
    if status_code == 304:
        ACCESS_LOG("Status_code 304")
        return "", 304, headers

    try:
        method, arg_dict = parse_args(request.args)
    except Exception as e:
        ACCESS_LOG("Error in arg parsing!")
        error_dict = {"Error": str(e), "Code": 404, "Request": request.path}
        return jsonify(error_dict), 404

    if method is None:
        output = r.content
    else:
        output = method(r, **arg_dict)

    # resp = handle_varnish(r, resp)
    # ACCESS_LOG(str(headers))
    return output, status_code, dict(headers)
Exemplo n.º 5
0
def get_range(x, y, tilematrix, resolution, layer, date1, date2):
    base_url = "http://onearth-tile-services/wmts/epsg4326/best/{layer}/default/{date}/{resolution}/{tilematrix}/{x}/{y}.png"
    dates = []

    d1 = datetime.datetime.strptime(date1, "%Y-%m-%d")
    d2 = datetime.datetime.strptime(date2, "%Y-%m-%d")

    delta = d2 - d1  # timedelta

    for i in range(delta.days + 1):
        dates.append((d1 + datetime.timedelta(days=i)).strftime('%Y-%m-%d'))

    urls = []
    for date in dates:
        url = base_url.format(layer=layer,
                              date=date,
                              resolution=resolution,
                              tilematrix=tilematrix,
                              x=x,
                              y=y)
        urls.append(url)
        # ACCESS_LOG(url)

    s = time.time()
    rs = (grequests.get(u) for u in urls)
    responses = grequests.map(rs)
    e = time.time()

    ACCESS_LOG("Requests took: {}s".format(e - s))

    s = time.time()
    data = np.stack((imageio.imread(r.content) for r in responses))
    e = time.time()

    ACCESS_LOG("Stacking took {}s".format(e - s))

    return data
Exemplo n.º 6
0
def generic(tilematrix, x, y):
    ACCESS_LOG(str(request.args))

    try:
        method, arg_dict = parse_args(request.args)
    except Exception as e:
        error_dict = {"Error": str(e), "Code": 404, "Request": request.path}
        return jsonify(error_dict), 404

    if method is None:
        error_dict = {
            "Error": "Method not found",
            "Code": 404,
            "Request": request.path
        }
        return jsonify(error_dict), 404

    output = method(x, y, tilematrix, **arg_dict)

    # resp = handle_varnish(r, resp)
    return output, 200, {'Content-Type': 'image/png'}
Exemplo n.º 7
0
def make_request(url, headers=None):
    startr = time.time()

    # ACCESS_LOG(str(headers))
    response = requests.get(url, headers=headers)

    endr = time.time()

    ACCESS_LOG("--Subrequest start--")
    ACCESS_LOG("Request: " + url)
    ACCESS_LOG("Response Length: " + str(len(response.content)))
    ACCESS_LOG("Response Status Code: " + str(response.status_code))
    ACCESS_LOG("Headers:\n" + str(response.headers))
    ACCESS_LOG("Time for request: " + str(endr - startr))
    ACCESS_LOG("--Subrequest ended--")

    if response.status_code == 404 or response.status_code == 400 or len(
            response.content) == 0:
        return None, response.status_code, None

    return response, 200, response.headers
Exemplo n.º 8
0
def downsample(r, n):
    image = read_image(r)

    ACCESS_LOG("Response shape: {}".format(image.shape))

    if n == 0:
        return r.content

    increment = image.shape[0] // n
    for i in range(n):
        for j in range(n):
            image[increment * i:increment * (i + 1),
                  increment * j:increment * (j + 1),
                  0:3] = image[increment * i:increment * (i + 1),
                               increment * j:increment * (j + 1),
                               0:3].mean(axis=(0, 1))

    if image.shape[2] == 4:
        image[:, :, 3] = 255

    output = write_image(image, format="png")

    return output
Exemplo n.º 9
0
def test():
    ACCESS_LOG("---Executing write test of log file---")
    ERROR_LOG("---Executing write test of log file---")

    return render_template('404.html'), 404