Skip to main content
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
deleted 11 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
  • After the response_high query, I get all the building that DID NOT intersect to the HIGH type of hazard and so on and exclude it in the next query(intersects) to avoid duplication (because there is a tendency that the building will intersect to the hazard polygon several times).

After the response_high query, I get all the building that DID NOT intersect to the HIGH type of hazard and so on and exclude it in the next query (intersects) to avoid duplication (because there is a tendency that the building will intersect to the hazard polygon several times).

The code works but the response is slow, take 6 seconds to show the output. AndAs it is messed up, any optimization "technique" is appreciated. Thanks!

  • After the response_high query, I get all the building that DID NOT intersect to the HIGH type of hazard and so on and exclude it in the next query(intersects) to avoid duplication (because there is a tendency that the building will intersect to the hazard polygon several times).

The code works but the response is slow, take 6 seconds to show the output. And it is messed up, any optimization "technique" is appreciated. Thanks!

After the response_high query, I get all the building that DID NOT intersect to the HIGH type of hazard and so on and exclude it in the next query (intersects) to avoid duplication (because there is a tendency that the building will intersect to the hazard polygon several times).

The code works but the response is slow, take 6 seconds to show the output. As it is messed up, any optimization "technique" is appreciated.

Source Link

Determine what hazard type does the building belong using GeoDjango Intersects

Below is my query to identify what hazard type does the buildings belong:

def floodhazard_tbl(request):
    if request.method == "GET":
        reference_high = FloodHazard.objects.filter(hazard='High')
        reference_medium = FloodHazard.objects.filter(hazard='Medium')
        reference_low = FloodHazard.objects.filter(hazard='Low')

        #get all ids based on filter
        ids_high = reference_high.values_list('id', flat=True)
        ids_medium = reference_medium.values_list('id', flat=True)
        ids_low = reference_low.values_list('id', flat=True)

        # create a list
        k = []
        response_high = []
        response_medium = []
        response_low = []
        bldg_id_high = []
        bldg_ids_high = []
        bldg_id_medium = []
        bldg_ids_medium = []
        filtered_bldg = []

        # this code is results a messy JSON data that need underscore.js to manipulate
        # in order for us to use datatables
        for myid in ids_high:
            getgeom = FloodHazard.objects.get(id=myid).geom
            response_high = PolyStructures.objects.filter(geom__intersects=getgeom).values(
                'brgy_locat', 'municipali').annotate(counthigh=Count('brgy_locat'))
            bldg_id_high = filter(None,response_high.values_list('id',flat=True))
            bldg_ids_high = bldg_ids_high + bldg_id_high
            k.append(response_high)
            

        for myid in ids_medium:
            getgeom = FloodHazard.objects.get(id=myid).geom
            response_medium = PolyStructures.objects.exclude(id__in=bldg_ids_high).filter(geom__intersects=getgeom).values(
                'brgy_locat', 'municipali').annotate(countmedium=Count('brgy_locat'))
            bldg_id_medium = filter(None,response_medium.values_list('id',flat=True))
            bldg_ids_medium = bldg_ids_medium + bldg_id_medium
            k.append(response_medium)

        filtered_bldg = bldg_ids_medium + bldg_ids_high

        for myid in ids_low:
            getgeom = FloodHazard.objects.get(id=myid).geom
            response_low = PolyStructures.objects.exclude(id__in=filtered_bldg).filter(geom__intersects=getgeom).values(
                'brgy_locat', 'municipali').annotate(countlow=Count('brgy_locat'))
            k.append(response_low)


        #to_json = list(k for k,_ in itertools.groupby(k))
        result = []
        for d in chain.from_iterable(k):
            result.append(d)
            
        return HttpResponse(list(json.dumps(result)), content_type='application/json')

I have this method(algo):

  • After the response_high query, I get all the building that DID NOT intersect to the HIGH type of hazard and so on and exclude it in the next query(intersects) to avoid duplication (because there is a tendency that the building will intersect to the hazard polygon several times).

The code works but the response is slow, take 6 seconds to show the output. And it is messed up, any optimization "technique" is appreciated. Thanks!