Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass properties to the geojsonfeature filter #15

Closed
capooti opened this issue Mar 18, 2014 · 8 comments
Closed

How to pass properties to the geojsonfeature filter #15

capooti opened this issue Mar 18, 2014 · 8 comments

Comments

@capooti
Copy link

capooti commented Mar 18, 2014

Hi, I cannot figure out how to pass the properties parameter to the geojsonfeature filter.

With GeoJSONLayerView in urls.py this is easy:

    GeoJSONLayerView.as_view(model=MyModel,
        properties=(
            ['field1', 'field2',]
        )),
        name='myname')

But for my use case I need to filter the model, that's why I have created a view like this:

def mymodel_geojson(request):
object_list = MyModel.objects.all().filter(some_filter_here)
properties= ['field1', 'field2',]
return render_to_response(
'myapp/mytemplate.geojson',
{
'object_list': object_list,
'properties': properties,
}, )

Now in my template, how do I pass the parameters?

{% load geojson_tags %}

{{ object_list|geojsonfeature:"???" }}

According to this it should not be possible to create a filter accepting multiple parameters:
http://stackoverflow.com/questions/420703/how-do-i-add-multiple-arguments-to-my-custom-template-filter-in-a-django-templat

thanks for the great library!

@capooti
Copy link
Author

capooti commented Mar 18, 2014

I ended up using an inherited generic views, redefining the get_queryset method to return the desired queryset.
Though, still curious about how to pass the parameters to the geojsonfeature filter.

@leplatrem
Copy link
Collaborator

Hi !

It looks like you're confused between the view (serving GeoJSON) and the template filter (embedding GeoJSON within an HTML template).

So, if you need to serve GeoJSON with some properties, just add this in urls.py :

 GeoJSONLayerView.as_view(model=MyModel, properties=('field1', 'field2'), name='myname')

But if you need to embed some GeoJSON within an HTML page (less common to be honest), you may be right, the many parameters are maybe useless ! I will open another issue for that :)

@capooti
Copy link
Author

capooti commented Mar 19, 2014

Hi, no I am not confused :) I exactly did what you suggested in urls.
I had then to use the generic view approach and redefine the get_queryset as I did not find a different way to pass a filtered query set.
My question here is: how to pass the parameters to the geojsonfeature filter? Thanks in advance.

@capooti
Copy link
Author

capooti commented Mar 19, 2014

This is the code I used:

views.py

class MyModelLayer(GeoJSONLayerView):
    def get_queryset(self):
        context = MyModel.objects.all().filter(...some_filter_here...)
        return context

urls.py

url(r'^data/mymodel.geojson$', 
    MyModelLayer.as_view(model=MyModel,
        properties=(
            ['field1', 'field2',]
        )),
        name='mymodel-geojson'),

@leplatrem
Copy link
Collaborator

Ok, great, indeed a filtered queryset like this should work.

Regarding the other question: so far it looks like the template filter geojsonfeature does not allow parameters (see issue #16). Why do you need it for ?

@capooti
Copy link
Author

capooti commented Mar 19, 2014

because according to this:
https://github.com/makinacorpus/django-geojson/blob/master/djgeojson/templatetags/geojson_tags.py#L17

we may pass the following parameters to the filter:

def geojsonfeature(source, geometry_field='geom', properties=None, srid=GEOJSON_DEFAULT_SRID)

but cannot figure out how to pass the properties and srid parameters (it looks like a Django filter will accept just one parameter from template).
The only way I can figure out to use this filter if you need to modify its parameters is by inheriting from it and change the parameters in the inherited filter.
Is this the intended behaviour?
Regards

@leplatrem
Copy link
Collaborator

Yes indeed, this part of the code was written in a way that cannot be used :D (purpose of issue #16)

We could specify only one parameter as string (split on :)

 {{ object|geojsonfeature:"name,age" }}
 {{ object|geojsonfeature:"name:geom" }}
 {{ object|geojsonfeature:"title,description:geom:2154" }}
 {{ object|geojsonfeature:":geom:4326" }}

@capooti
Copy link
Author

capooti commented Mar 20, 2014

+1, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants