Skip to content

Instantly share code, notes, and snippets.

@kenial
Created January 3, 2015 08:28
Show Gist options
  • Save kenial/6231b7881a1e838a2bdb to your computer and use it in GitHub Desktop.
Save kenial/6231b7881a1e838a2bdb to your computer and use it in GitHub Desktop.
GeoDjango sample code
import django
from django.contrib.gis.measure import D
from django.contrib.gis.geos import *
from django.contrib.gis import geos
pnt = geos.Point(-122.146, 37.458, srid=4326)
pnt.transform(900913) # in meter
area = geos.Polygon([
(pnt.x-1, pnt.y-1),
(pnt.x+1, pnt.y-1),
(pnt.x+1, pnt.y+1),
(pnt.x-1, pnt.y+1),
(pnt.x-1, pnt.y-1),
], srid=4326)
objs = IP2LocationLiteDB5.objects.filter(Q(id__lte=26000) & Q(coord_latlng__within=area))
objs.count()
objs = IP2LocationLiteDB5.objects.filter(Q(id__lte=260000) & Q(coord_900913__distance_lte=(pnt, D(m=100))))
objs.count()
pnt.transform(4326) # in lat/lng
DeviceLocation.objects.filter(Q(coord__distance_lte=(pnt, D(m=100))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment