ich habe einen webserver der auf meine Datenbank PostgreSQL draufzugreift.
Die Abfrage soll als JSON zurückgeschickt werden. Dieses JSON-Object soll später mittels AJAX-GET-Request in WebGL eingebunden werden.
Ich habe noch zusätzlich mal die Access-Control-Allow-Origin eingefügt damit er keine Probleme später mit der Same-Origin-Policy bekommt.
Code: Alles auswählen
import web
import psycopg2
import json
urls = (
"/", "index",
"/bbox", "bbox",
)
app = web.application(urls, globals())
web.config.debug = True
##################### Connect to an existing database (postgresql) ##################################
try:
conn = psycopg2.connect(database='...', user='...', password='...')
except:
print "I am unable to connect to the database"
######################################Database end ####################################################
class index:
def GET(self):
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
return "hallo"
class bbox:
def GET(self):
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
data = web.input(XMIN="[]", YMIN="[]", XMAX="[]", YMAX="[]")
cur = conn.cursor()
query = "SELECT a.id AS building_nr, ST_AsGeoJSON(c.Geometry) AS geometry, d.Classname AS polygon_typ FROM building a, thematic_surface b, surface_geometry c, Objectclass d WHERE c.Geometry && ST_MakeEnvelope(" + data.XMIN + ", "+ data.YMIN + ", " + data.XMAX + ", " + data.YMAX + ") AND a.id = b.building_id AND b.lod2_multi_surface_id = c.root_id AND c.geometry IS NOT NULL AND b.Objectclass_ID = d.ID AND a.ID=5;"
cur.execute(query)
r = [dict((cur.description[i][0], value) \
for i, value in enumerate(row)) for row in cur.fetchall()]
return r
# Aufruf der App
if __name__ == "__main__":
app.run()
[{'building_nr': 5, 'geometry': '{"type":"Polygon","coordinates":[[[3500267.16,5392933.95,456.904],[3500259.19,5392933.01,456.904],[3500258.586,5392938.152,456.904],[3500258.02,5392942.97,456.904],[3500265.98,5392943.94,456.904],[3500266.552,5392939.097,456.904],[3500267.16,5392933.95,456.904]]]}', 'polygon_typ': 'BuildingGroundSurface'}
1) ist das schon ein korrektes JSON-Objekt oder was gibt er mir da zurück???
Weil ich mir nur die Variable r zurückgeben lasse.
Es gibt ja noch die Methode:
json_output = json.dumps(r)
return json_output
sieht aber vom Ergebniss sogut wie identisch aus. Was ist da der Unterschied?
2) was bieten in die anderen frameworks im vergleich zu web.py außer das es nicht mehr gepflegt wird? Würdet ihr andere empfehlen?
Danke.