Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 50 lines (40 sloc) 1.722 kb
3c930ac8 »
2012-11-25 Initial commit of working map client/server.
1 #!/usr/bin/env python
2
3 # get_geojson.py - CGI script to get UKMap building geom/heights from PostGIS and return as GeoJson strings.
4 # Tested with lighttpd.
5
6 # Note: this is just a test script, it would be more efficient to implement this in a real web framework (i.e. GeoDjango) in WSGI format.
7
8 # Copyright Tom Holderness 2012.
9 # Released under BSD license (see LICENSE.txt)
b69ea6f1 »
2012-12-12 Tweaks to map.htm. Added screenshot for github.
10 # https://github.com/talltom/3DLondon
3c930ac8 »
2012-11-25 Initial commit of working map client/server.
11
12 __author__='Tom Holderness'
13 __year__='2012'
14 __version__ = "0.1"
15
16 import cgi
17 import cgitb; cgitb.enable() # for troubleshooting
18 import json
19 import psycopg2
20
21 # Get bbox coords from URL query, with a little minimal error checking
22 fs = cgi.FieldStorage()
23 if 'bbox_wkt' not in fs:
24 print "Content-type: text/html"
25 print
26 print "<h1>Error</h1>"
27 print "bbox_wkt was not found in URL query string."
28 else:
29 bbox_wkt = fs['bbox_wkt'].value
30
31 # Database connection
32 conn = psycopg2.connect("dbname=ukmap_london user=postgres password=postgres")
33
34 # Database cursor
35 cur = conn.cursor()
36
37 # Query
42098983 »
2012-12-10 Working map/geojson after database tweaks.
38 cur.execute('SELECT row_to_json(fc) FROM ( SELECT \'FeatureCollection\' As type, array_to_json(array_agg(f)) As features FROM (SELECT \'Feature\' As type, ST_AsGeoJSON(ST_Transform(lg.the_geom,4326))::json As geometry, row_to_json((SELECT l FROM (SELECT height) As l)) As properties FROM "3DLondon" As lg WHERE ST_Within(lg.the_geom, ST_Transform(ST_GeomFromText(%s,4326),27700)) LIMIT 100000) As f ) As fc;',[bbox_wkt])
3c930ac8 »
2012-11-25 Initial commit of working map client/server.
39 array = cur.fetchall()
40
41 # Return data as GeoJSON
42 print "Content-type: application/json\n"
43 # Format JSON data
44 jsondata = json.dumps(array[0])
45 # Hack to remove Ptthon list brackets
46 jsondata = jsondata[2:-2]
47 # Remove escaped strings
48 jsondata = jsondata.replace("\\","")
49 # Print the data
50 print jsondata
Something went wrong with that request. Please try again.