Skip to main content

django-geojson is a collection of helpers to serialize GeoDjango objects into GeoJSON.

https://travis-ci.org/makinacorpus/django-geojson.png https://coveralls.io/repos/makinacorpus/django-geojson/badge.png

INSTALL

pip install django-geojson

If you need to load data from GeoJSON files (deserialize), you’ll also need shapely

pip install "django-geojson [shapely]"

USAGE

Add djgeojson to your applications :

# settings.py

INSTALLED_APPS += (
    'djgeojson',
)

(not required for views)

GeoJSON layer view

Very useful for web mapping :

# urls.py
from djgeojson.views import GeoJSONLayerView
...
url(r'^data.geojson$', GeoJSONLayerView.as_view(model=MushroomSpot), name='data'),

Consume the vector layer as usual, for example, with Leaflet loaded in Ajax:

var layer = L.geoJson();
map.addLayer(layer);
$.getJSON("{% url 'data' %}", function (data) {
    layer.addData(data);
});

Inherit only if you need a reusable set of options :

# views.py
from djgeojson.views import GeoJSONLayerView

class MapLayer(GeoJSONLayerView):
    # Options
    precision = 4   # float
    simplify = 0.5  # generalization


# urls.py
from .views import MapLayer, MeetingLayer
...
url(r'^mushrooms.geojson$', MapLayer.as_view(model=MushroomSpot, fields=('name',)), name='mushrooms')

Most common use-cases of sub-classing are low-fi precision, common list of fields between several views, etc.

Options are :

  • properties : list of properties names, or dict for mapping field names and properties

  • simplify : generalization of geometries (See simplify())

  • precision : number of digit after comma

  • geometry_field : name of geometry field (default: geom)

  • srid : projection (default: 4326, for WGS84)

GeoJSON template filter

Mainly useful to dump features in HTML output and bypass AJAX call :

// For example, with Leaflet
L.geoJson({{ object_list|geojsonfeature|safe}}).addTo(map);

Will work either for a model, a geometry field or a queryset.

{% load geojson_tags %}

var feature = {{ object|geojsonfeature|safe }};

var geom = {{ object.geom|geojsonfeature|safe }};

var collection = {{ object_list|geojsonfeature|safe }};

Basic serializer

from djgeojson.serializers import Serializer as GeoJSONSerializer

GeoJSONSerializer().serialize(Restaurants.objects.all(), use_natural_keys=True)

Dump GIS models, or fixtures

Register the serializer in your project :

# settings.py

SERIALIZATION_MODULES = {
    'geojson' : 'djgeojson.serializers'
}

Command-line dumpdata can export files, viewable in GIS software like QGis :

django dumpdata --format=geojson yourapp.Model > export.geojson

Works with loaddata as well, which can now import GeoJSON files.

AUTHORS

Version 1.X:

  • Daniel Sokolowski, serializer snippet

  • ozzmo, python 2.6 compatibility

makinacom

LICENSE

  • Lesser GNU Public License

CHANGELOG

2.0.0 (2013-07-09)

  • Complete rewrite using @jeffkistler and @glenrobertson code

  • CRS is added to GeoJSON ouput by default

  • Ability to build pk dynamically by passing a lambda

  • Ability to provide a bbox

  • Ability to use Django natural keys

  • Support of ValuesQuerySet

  • Support of ForeignKey and ManyToMany

  • Added force2d option

** Backwards incompatible changes **

  • Geom field is not guessed automatically : Use geometry_field option, default is 'geom'.

  • no more pk in properties : use feature id, or list pk in properties explicitly.

  • fields list in GeoJSONLayer was renamed properties.

1.2.0 (2013-05-22)

  • Shapely is now optional (used for deserialization only)

  • Add Django to requirements

  • Skip a step in GeoJSON conversion of geometries (fixes #6)

1.1.0 (2013-03-06)

  • Django 1.5 support

  • Fix template tag geojsonfeature on empty geometries

1.0.0 (2012-08-03)

  • Initial working version.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-geojson-2.0.0.zip (20.6 kB view details)

Uploaded Source

File details

Details for the file django-geojson-2.0.0.zip.

File metadata

  • Download URL: django-geojson-2.0.0.zip
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-geojson-2.0.0.zip
Algorithm Hash digest
SHA256 3ef2ff7777d134d7ce988ef76f4e93c1e1e034c8cf6596253a2cf894d791be50
MD5 8fca5a2f5203bd123597c579b45f1e40
BLAKE2b-256 c67c0a046bf68b2ac12e9a9621f1b434ca4a63270de2131c07cbf4703b0595e3

See more details on using hashes here.

Release history Release notifications | RSS feed

4.2.0

2 files

4.1.0

2 files

4.0.0

2 files

3.2.1

2 files

3.2.0

2 files

3.1.0

2 files

3.0.0

1 file

2.12.0

1 file

2.11.0

1 file

2.10.0

1 file

2.9.1

1 file

2.9.0

1 file

2.8.1

1 file

2.8.0

1 file

2.7.0

1 file

2.6.0

1 file

2.5.0

1 file

2.4.0

1 file

2.3.0

1 file

2.2.0

1 file

2.1.1

1 file

2.1.0

1 file

2.0.1

1 file

This release

2.0.0 This release

1 file

1.2.0

1 file

1.0.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page