Using GeoJson data format how can we write dynamic query for "properties dictionary fields "?

224 views Asked by At

I have data in geojson format.

Here is my data :

{
    "type" : "FeatureCollection",
    "features" : [
        {
            "type" : "Feature",
            "properties" : {
                "year" : 2015,
                "season" : "rabbi",
                "crop" : "banana",
                "district" : "pune",
                "taluka" : "haveli",
                "circle" : "uralikanchan",
                "farmer" : 100
            },
            "geometry" : {
                "type" : "Polygon",
                "coordinates" : [
                    [
                        74.129992,
                        18.505494
                    ],
                    [
                        74.129047,
                        18.505494
                    ],
                    [
                        74.128275,
                        18.504436
                    ],
                    [
                        74.127588,
                        18.503052
                    ],
                    [
                        74.114456,
                        18.498331
                    ],
                    [
                        74.113941,
                        18.498331
                    ],
                    [
                        74.112482,
                        18.493773
                    ],
                    [
                        74.112053,
                        18.491494
                    ],
                    [
                        74.143724,
                        18.473992
                    ],
                    [
                        74.144497,
                        18.474888
                    ],
                    [
                        74.145269,
                        18.476027
                    ],
                    [
                        74.15617,
                        18.486854
                    ],
                    [
                        74.155912,
                        18.488319
                    ],
                    [
                        74.145956,
                        18.502564
                    ],
                    [
                        74.129992,
                        18.505494
                    ]
                ]
            }
        }
    ]
}

views.py file :

from rest_framework_mongoengine.viewsets import ModelViewSet as MongoModelViewSet
from app.serializers import *
from rest_framework_mongoengine.generics import *    


def index_view(request):
    context = {}
    return TemplateResponse(request, 'index.html', context)



class ToolViewSet(MongoModelViewSet):
    serializer_class = ToolSerializer
    my_filter_fields = ('type','features','geometry',) # specify the fields on which you want to filter

    def get_kwargs_for_filtering(self):
        filtering_kwargs = {} 

        for field in  self.my_filter_fields: # iterate over the filter fields
            field_value = self.request.query_params.get(field) # get the value of a field from request query parameter
            if field_value: 
                if ',' in field_value: # put your queryParams into an array and use the built-in django filter method '__in'
            filtering_kwargs[field + '__in'] = field_value.split(',')                    
                else:
                    filtering_kwargs[field] = field_value
        return filtering_kwargs 

    def get_queryset(self):
        queryset = Tool.objects.all() 
        filtering_kwargs = self.get_kwargs_for_filtering() # get the fields with values for filtering 
        if filtering_kwargs:
            queryset = Tool.objects.filter(**filtering_kwargs) # filter the queryset based on 'filtering_kwargs'
        return queryset

This dynamic query works for field "type","features" and "geometry". But, I have to show data using my properties fields ( i.e : year,season,crop,district,taluka. this all fields in fields{properties{"crop","district",.....}}

what I have to changes in this code for working dictionary fields then If I have go to server http://api/tool/?crop=banana then it's display this data ?

0

There are 0 answers