Login

Tag "geodjango"

Snippet List

Google v3 geocoding for Geodjango admin site

This only works with Point geometry. [video](http://www.youtube.com/watch?v=gZ7_n177sTE&list=HL1351725584&feature=mh_lolz) Rename the snippet as gmgdav3.js and save it to template/admin with [gmgdav3.html](http://djangosnippets.org/snippets/2840/) * - *models.py*: ` from django.contrib.gis.db import models` ` class point(models.Model):` ` address = models.CharField(max_length=100, help_text='Press "Tab" to refresh the map')` ` longitude = models.FloatField(help_text='WGS84 Decimal Degree. Press "Tab" to refresh the map')` ` latitude = models.FloatField(help_text='WGS84 Decimal Degree. Press "Tab" to refresh the map')` ` in_geom = models.PointField('shp', srid=4326)` ` objects = models.GeoManager()` ` def __unicode__(self):` ` return str(self.address)` * - *admin.py*: ` from models import * ` ` from django.conf import settings` ` from django.contrib.gis import admin` ` from django.contrib.gis.geos import GEOSGeometry` ` class GoogleAdmin(admin.OSMGeoAdmin):` ` g = GEOSGeometry('POINT (9.191884 45.464254)') # Set map center` ` g.set_srid(4326)` ` g.transform(900913)` ` default_lon = int(g.x)` ` default_lat = int(g.y)` ` default_zoom = 7` ` extra_js = ["http://maps.google.com/maps/api/js?v=3.2&sensor=false"]` ` map_template = 'gmgdav3.html'` ` admin.site.register(point, GoogleAdmin)` ` # admin.site.register(your other models...,...)`

  • admin
  • google-maps
  • geocode
  • geolocation
  • geodjango
Read More

CollectionFrom (GeometryCollection <-> Geometry Fields!)

Motivation: We can't use GeometryCollections to do filters, etc in GeoDjango due to incomplete underlying libraries. But with this CollectionFrom field we can get all the benefits of working with GeometryCollections but still query based on points, lines, polys. If you're using GeometryCollectionFields and see this error: DatabaseError: Relate Operation called with a LWGEOMCOLLECTION type. This is unsupported. Then this will probably be helpful for you.

  • geodjango
  • geo
  • geometrycollection
Read More

GeoDjango maps in admin TabularInlines

This snippet adds support for OSM maps for GeometryField in Admin TabularInlines. The one possible issue with this snippet is that the OSMGeoInlineForm has to know about the parent ModelAdmin which it does through the code `model_admin_instance = admin.sites.site._registry[self.parent_model]` which won't work if you don't use the default model admin to register the model admin. I'll try and come up with a work around and edit the snippet. Due to the need to mess around with inline style sheets and IE not playing ball with just copying the innerHTML I've settled on using the jQuery.Rule plugin which I've included here as the last version published on the site was incompatible with jQuery 1.4.2 and I found a pathced version online, I also had to modify it due to the Django admin using the compatibility mode of jQuery so there is no global jQuery variable it's django.jQuery instead. 1. Create an osmgeo_inline.py file in your app and copy the top code into it. 2. Create the template file in a directory called admin within a template directory for your app, the template file must be called osmgeo_tabular_inline.html, and copy the middle code into it. 3. Create the jquery rule plugin file in your media or admin-media js directory and copy the bottom code into it. Don't forget to change the OSMGeoInlineForm's class Media's js = ('.....',) to the correct path to the file if need be. 4. In your admin.py you can create inline models using OSMGeoTabularInline just as you would TabularInline. Examples all based on the following in models.py from django.contrib.gis.db import models class MyModel(models.Model): name = models.CharField(max_length=64) route = models.LineStringField(srid=settings.WGS_SRID) class MySubModel(models.Model): mymodel = models.ForeignKey(MyModel) name = models.CharField(max_length=64) location = models.PointField(srid=settings.WGS_SRID) Example 1 - basic usage (admin.py): from django.contrib.gis.admin import OSMGeoAdmin from myapp.osmgeo_inline import OSMGeoTabularInline from myapp.models import MyModel, MySubModel class MySubModelInline(OSMGeoTabularInline): model = MySubModel class MyModelAdmin(OSMGeoAdmin): inlines = [MySubModelInline] admin.site.register(MyModel, MyModelAdmin) Example 2 - overriding the default map widget params and setting the inline map's centre point to match the main models map centre (admin.py): from django.contrib.gis.admin import OSMGeoAdmin from myapp.osmgeo_inline import OSMGeoTabularInline from myapp.models import MyModel, MySubModel class MySubModelInline(OSMGeoTabularInline): model = MySubModel class MyModelAdmin(OSMGeoAdmin): inlines = [MySubModelInline] params = {'map_width: 200, 'map_height': 200] def get_formset(self, request, obj=None, **kwargs): centre = None if obj is not None: centre = obj.route.centroid.transform(900913, clone=True) self.params['default_lon'] = centre.coords[0] self.params['default_lat'] = centre.coords[1] self.params['default_zoom'] = 12 return super(TrailSubmissionInlineBase, self).get_formset(request, obj, **kwargs) admin.site.register(MyModel, MyModelAdmin) I've not looked at StackedInlines because I don't use them but all that would be needed is to add a class OSMGeoStackedInline(StackedInline) that was a copy of OSMGeoTabularInline but with a template based on the StackedInline template - the javascript code in var initMap = function(row) would probably have to be adapted though.

  • django
  • admin
  • geodjango
  • map-widget
  • tabular-inlines
Read More

Forcing Right-Hand Rule (Counter Clockwise) Polygons in GeoDjango for Google Earth / KML

According to the KML Specification, Polygons must be oriented according to the Right-Hand Rule (Counter Clockwise orientation) for them to display correctly in Google Earth. Since not all Polygons are defined according to the Right-Hand Rule, you can use this code to orient them correctly when using GeoDjango. Thanks goes to jbronn of #geodjango on irc.freenode.net for this!

  • geodjango
  • kml
  • google-earth
  • polygon
  • orientation
  • right-hand-rule
Read More

Map GPX files to 3D GeoDjango Models

`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
Read More

compressing polygons for geodjango

The code shown allows you, in GeoDjango, to reduce the number of points in your polygons. It helps reduce storage needs and makes queries run faster, at the cost of some precision. It provides a variation on the simplify() method that comes with the GEOS API, allowing you to specify a number of points instead of a distance tolerance. It is set up as a management command so that you can run it with python manage.py. See the django docs for how to set that up. The example shown assumes a table called CountyBorders with fields "name" and "mpoly." It should be straightforward to adapt it for your needs. Look at the first three lines of the simplify() method in particular for customization. The rest of the code is pretty generic, and it should run fast enough in a one-time batch process for most needs. The algorithm tries to keep the 75 points that provide the most definition for the shape. Each point in a polygon defines a triangle with its immediate neighbors. If that triangle has no area (the degenerate case), it is a midpoint on the segment between its neighbors and adds no value whatsoever. This principle is extended to say that the larger the triangle, the more value the point has in defining the shape. (You can find more refined algorithms, but this code seems to work fine by visual inspection.)

  • geodjango
  • polygons
Read More

WorldIP - access to IP database over API

The WorldIP database provides real-world geographical location. Database is more correct than [Whois records and Whois-based databases](http://www.wipmania.com/en/blog/why-worldip-data-rather-than-whois-data-examples/), that show geographic locations of network owners, and not the geographic location of Internet-connected PC or appliance itself. See more: [WIPmania.com](http://www.wipmania.com)

  • ip
  • geolocation
  • geodjango
  • worldip
Read More

MaxMind(R) GeoIP Lite CSV Import

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
Read More

MaxMind(R) GeoIP Lite geolocation models

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
Read More

10 snippets posted so far.