-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm2_api.py
137 lines (121 loc) · 4.14 KB
/
asm2_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 26 18:06:12 2019
@author: danielm
"""
import os
import time
import psycopg2
from psycopg2 import sql
from flask import Flask, flash, request, redirect, url_for, jsonify
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.secret_key = "super secret key"
@app.after_request
def after_request(response):
header = response.headers
header['Access-Control-Allow-Origin'] = '*'
return response
@app.route("/PVuur/<loc>")
def get_PVuur(loc):
query = """
SELECT array_to_json(array_agg(row_to_json(tab)))
FROM (
SELECT dag*24 + uur AS x, (a.percentage_pvpower * b.benut_potentieel) AS y
FROM asm2_zon_2020.pv_profiel2_uur a
JOIN asm2_zon_2020.pvhuidig_buurt b
ON b.code = %s
ORDER BY dag, uur
) tab
"""
connection = psycopg2.connect(user = "danielm",
password = "",
host = "localhost",
port = "5432",
database = "pico")
cursor = connection.cursor()
cursor.execute(query, (loc,))
result = cursor.fetchone()[0]
connection.close()
return jsonify(result)
@app.route("/PVdag/<loc>")
def get_PVdag(loc):
query = """
SELECT array_to_json(array_agg(row_to_json(tab)))
FROM (
SELECT dag AS x, SUM(a.percentage_pvpower * b.benut_potentieel) AS y
FROM asm2_zon_2020.pv_profiel2_uur a
JOIN asm2_zon_2020.pvhuidig_buurt b
ON b.code = %s
GROUP BY dag
ORDER BY dag
) tab
"""
connection = psycopg2.connect(user = "danielm",
password = "",
host = "localhost",
port = "5432",
database = "pico")
cursor = connection.cursor()
cursor.execute(query, (loc,))
result = cursor.fetchone()[0]
connection.close()
return jsonify(result)
@app.route("/buurt/<z>/<x>/<y>")
def get_buurt_tiles(z, x, y):
query = """
SELECT ST_AsMVT(q, 'buurt', 4096, 'mvt_geom')
FROM (
SELECT
id, name, gemeente,
ST_AsMVTGeom(
geom_3857,
TileBBox(%(z)s, %(x)s, %(y)s),
4096,
256,
false
) mvt_geom
FROM danielm.buurten_tiles
WHERE geom_3857 && TileBBox(%(z)s, %(x)s, %(y)s)
AND ST_Intersects(geom_3857, TileBBox(%(z)s, %(x)s, %(y)s))
) q;
"""
connection = psycopg2.connect(user = "danielm", password = "", host = "localhost", port = "5432", database = "pico")
cursor = connection.cursor()
cursor.execute(query, {'z': z, 'x': x, 'y': y})
result = cursor.fetchone()[0]
connection.close()
response = app.make_response(bytes(result))
response.headers['Content-Type'] = 'application/x-protobuf'
response.headers['Access-Control-Allow-Origin'] = "*"
return response
@app.route("/winddag/<loc>")
def get_winddag(loc):
query = """
SELECT array_to_json(array_agg(row_to_json(tab)))
FROM (
SELECT c.jaardag AS x, SUM(c.yield_kwh) * MAX(d.fractie) AS y
FROM resgebieden.resgebieden2018 a
JOIN asm2_wind_2020.wind_turbine_locations_present b
ON ST_WITHIN(b.geom, a.geom)
JOIN danielm.buurt_res_fractie d
ON a.res_code = d.res_code
AND d.bu_code = %s
JOIN asm2_wind_2020.wind_turbine_profile c
ON b.turbine_id = c.turbine_id
GROUP BY c.jaardag
ORDER BY c.jaardag
) tab
"""
connection = psycopg2.connect(user = "danielm",
password = "",
host = "localhost",
port = "5432",
database = "pico")
cursor = connection.cursor()
cursor.execute(query, (loc,))
result = cursor.fetchone()[0]
connection.close()
return jsonify(result)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=1700)