Drucken

Inhalt

Navigation

tuts: OpenLayers

GeoJSON und Django

Dependencies:

sudo easy_install vectorformats
.

Try this Code:

>>> from django.contrib.gis.geos import *
>>> from bud_django.arp_schwachstellen import models
>>> poly = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) )
>>> gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly)
>>> gt = models.GeoTest(att1=123, att2=443, geometry=gc)
>>> gt.save()
>>> gt = models.GeoTest.objects.all()
>>> from vectorformats.Formats import Django, GeoJSON
>>> djf = Django.Django(geodjango="geometry", properties=['att1', 'att2'])
>>> geoj = GeoJSON.GeoJSON()
>>> string = geoj.encode(djf.decode(gt))
>>> string
'{'crs': 'null',
 'features': [{'geometry': {'coordinates': [[0.0, 0.0],
                                            [[0.0, 0.0], [1.0, 1.0]],
                                            [[[0.0, 0.0],
                                              [0.0, 1.0],
                                              [1.0, 1.0],
                                              [0.0, 0.0]]]],
                            'type': 'GeometryCollection'},
               'id': 1,
               'properties': {'att1': 123, 'att2': 443},
               'type': 'Feature'}],
 'type': 'FeatureCollection'}'