Skip to content

Instantly share code, notes, and snippets.

@hanneshapke
Created April 15, 2013 13:51
Show Gist options
  • Save hanneshapke/5388215 to your computer and use it in GitHub Desktop.
Save hanneshapke/5388215 to your computer and use it in GitHub Desktop.
Query comparison between Point and Polygon geometries
In [1]: data_point = WindData.objects.raw('SELECT * FROM meteo_winddata ORDER BY geom <-> ST_SetSRID(ST_MakePoint(%s, %s),4326) LIMIT 1', [foo.location.x, foo.location.y])[0]
DEBUG (0.282) SELECT * FROM meteo_winddata ORDER BY geom <-> ST_SetSRID(ST_MakePoint( -0.12574, 51.50853),4326) LIMIT 1; args=[-0.12574, 51.50853]
In [2]: data_point = WindDataPolygon.objects.get(geom__intersects = foo.location)
DEBUG (0.003) SELECT "meteo_winddatapolygon"."id", "meteo_winddatapolygon"."lat", "meteo_winddatapolygon"."lng", "meteo_winddatapolygon"."windspeed", "meteo_winddatapolygon"."geom" FROM "meteo_winddatapolygon" WHERE ST_Intersects("meteo_winddatapolygon"."geom", ST_GeomFromEWKB('\x0101000020e6100000f146e6913f18c0bf7784d38217c14940'::bytea)); args=(<django.contrib.gis.db.backends.postgis.adapter.PostGISAdapter object at 0x11080f810>,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment