2

I am new to geoDjango. I have a polygon field defined in the model like this:

my_poly = models.PolygonField(null=True, blank=True, spatial_index=True, geography=True, srid=4326)

I am getting the record set in view like this:

rset = TestUnit.objects.extra(select={'o':'my_poly'}).filter(reduce(operator.and_, criteria)).values('o')

I see it is returning polygon data looks like this format:

0103000020E610000001000000090000004AB6BA9C127557C060915F3FC4C64440C7BDF90D137557C0187D0569C6C644408690F3FE3F7557C0E35295B6B8CA44403ECA880B407557C05F402FDCB9CA44400F46EC13407557C07D259012BBCA4440F7031E18407557C0C98E8D40BCCA444021B1DD3D407557C03D0CAD4ECECA4440BFB9BF7ADC7457C0D525E318C9D044404AB6BA9C127557C060915F3FC4C64440

I need it to return WKT or GeoJSON format so that I can draw polygons using Leaflet. What are my options?

1
  • Can you paste how you build criteria?
    – Udi
    Commented Mar 10, 2017 at 11:03

3 Answers 3

2

Try this:

for o in TestUnit.objects.all():
    print(o.my_poly)
    print(o.my_poly.geojson)
    print(o.my_poly.wkt)
    print()

To filter:

for o in TestUnit.objects.filter(field1=value1, field2=value2):
    print(o.my_poly)

For example:

TestUnit.objects.filter(level=5, answered=True):

See also: Making Queries in Django's docs.

1

I think @Udi is right, your filtering setup is overly complicated for what you want to do. When filtering objects in Django, the result is an iterable queryset, which contains the model instances matching your filter criteria.

So if you have something like

rset = TestUnit.objects.filter(reduce(operator.and_, criteria))

then you can loop through rset, and each object in that iterator is a model instance. To access the values you can then simply use the field properties, just as @Udi explained in his answer.

So if you want a list of WKT geometries, you can use this expression

rset_wkt = [test_unit.my_poly.wkt for test_unit in rset]

Just to give some context:

PostGIS stores geometries internally as binary blobs. That is what you see in your output. You see the raw data because you use the select and values combination, it gets the "raw" values from PostGIS.

In regular GeoDjango querys, such as shown by @Uli, the binary format is converted automatically to OGRGeometry objects. The my_poly attribute becomes a pythonic object. You can access its attributes and methods such as srid, wkt, geojson, transform, intersect, etc.

0

In my case I used serializer to convert the django model data to geojson.

In your views.py file:

from django.shortcuts import render
from django.core.serializers import serialize
from django.http import HttpResponse
from .models import DataModel

# datamodel views

def data(request):
    data=serialize('geojson',DataModel.objects.all())
    return HttpResponse(data,content_type='application/json')

import the created data views to urls.py:

from django.conf.urls import url
from .views import data

# create your urls here

urlpatterns=[
    url(r'^data_geojson/$',data,name='data'),
    
]

Run python manage.py migrate in your command prompt or terminal. Then python manage.py runserver.

Type in your web browser http://localhost:8000/data/, your geojson will be displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.