Snippet List
Here's how to find the 10 closest locations to a point for a model that has latitude and longitude columns, if you're not using GeoDjango.
This runs a brute force distance calculation against every row, so it should only be used on smaller tables - probably less than 100,000 rows.
For larger tables you should use GeoDjango instead.
See also [my TIL](https://til.simonwillison.net/postgresql/closest-locations-to-a-point) about this.
- orm
- gis
- latitude
- longitude
- location
`GPXMapping` is a subclass of `LayerMapping` that imports GPX files into 3D GeoDjango models (requires Django 1.2 or SVN r11742 and higher). Here's an example of GeoDjango models for GPX points and tracks, respectively:
from django.contrib.gis.db import models
class GPXPoint(models.Model):
timestamp = models.DateTimeField()
point = models.PointField(dim=3)
objects = models.GeoManager()
def __unicode__(self):
return unicode(self.timestamp)
class GPXTrack(models.Model):
track = models.MultiLineStringField(dim=3)
objects = models.GeoManager()
Assuming the above models, then `GPXMapping` may be used to load GPX tracks and waypoints (including elevation Z values):
track_point_mapping = {'timestamp' : 'time',
'point' : 'POINT',
}
track_mapping = {'track' : 'MULTILINESTRING'}
gpx_file = '/path/to/file.gpx'
lm = GPXMapping(GPXPoint, gpx_file, track_point_mapping, layer='track_points')
lm.save(verbose=True)
lm = GPXMapping(GPXTrack, gpx_file, track_mapping, layer='tracks')
lm.save(verbose=True)
- gis
- geodjango
- 3d
- gpx
- layermapping
This GeoDjango subclass substitutes in the Google Maps base layer instead of the default one provided by Open Street Map. Requires the [google.html](http://www.djangosnippets.org/snippets/1145/) and [google.js](http://www.djangosnippets.org/snippets/1146/) templates (must be placed in `gis/admin` somewhere in your template path).
Requires a Google Maps API key -- please abide by Google's [terms of service](http://code.google.com/apis/maps/terms.html).
- gis
- google
- map
- gmaps
- layer
- openlayers
The class LocationField renders a form field with map (from google maps) and a mark. It allows the user to drag the mark to point at some particular location, whose value (lat, lng) is saved in a hidden field.
It requires jquery and google maps.
- newforms
- forms
- gis
- google
- field
- maps
- widget
Use this script to import the Maxmind GeoIP lite CSV datasets into your database. This takes at least 200MB of RAM; the resulting database will be ~400MB. Stick in the same directory as the [models](http://www.djangosnippets.org/snippets/327/). Make sure to set `DEBUG=False` to prevent running out of memory during import.
- log
- csv
- gis
- ip
- geolocation
- maxmind
- geodjango
- import
This provides GeoDjango models for the maxmind GeoIP Lite data products. Use the corresponding [CSV import script](http://www.djangosnippets.org/snippets/328/) for data import. Requires: [GeoDjango](http://code.djangoproject.com/wiki/GeoDjango) and the [BigIntegerField patch](http://code.djangoproject.com/attachment/ticket/399/django-bigint-20070712.patch) by Peter Nixon.
- log
- gis
- ip
- geolocation
- maxmind
- geodjango
This code assumes a) that you are using PostgreSQL with PostGIS and b) that the geometry column in your model's table is populated with points, no other type of geometry.
It also returns a list instead of a QuerySet, because it was simpler to sort by distance from the given point and return that distance as part of the payload than to muck around with QuerySet. So bear in mind that any additional filtering, excluding, &c., is outside the scope of this code.
'Distance' is in the units of whatever spatial reference system you define, however if you do not intervene degrees decimal is used by default (SRID 4326, to be exact).
To get distance in units a little easier to work with, use a spatial ref close to your domain set: in my case, SRID 26971 defines a projection centered around Illinois, in meters. YMMV.
- gis
- postgis
- geography
- geometry
- nearby
- nearest
- distance
9 snippets posted so far.