Ejemplo n.º 1
0
def dashboard_baseline(request,
                       filterLock,
                       flag,
                       code,
                       includes=[],
                       excludes=[],
                       response=dict_ext()):

    if include_section('getCommonUse', includes, excludes):
        response = dict_ext(getCommonUse(request, flag, code))
    # baseline = getBaseline(request, filterLock, flag, code, includes, excludes, inject, response=dict(response))
    response['source'] = baseline = response.pathget(
        'cache', 'getBaseline', 'baseline') or getBaseline(
            request,
            filterLock,
            flag,
            code,
            includes,
            excludes,
            response=dict_ext(response))
    panels = response.path('panels')

    response['healthfacility'] = {
        k: baseline['healthfacility'].get(k, 0)
        for k in HEALTHFAC_GROUP7
    }
    response['healthfacility']['other'] += sum([
        v or 0 for k, v in baseline['healthfacility'].items()
        if k not in HEALTHFAC_GROUP7
    ])
    # response.path('panels','healthfacility')['other'] = sum([baseline['healthfacility'].get(k) for k in ['rh','sh','mh','datc','pic','other','mc','mht']])

    # for sub in ['pop','area','building']:
    # 	response.path('panels')[sub+'_lc'] = baseline[sub+'_lc']
    # 	for k,v in LANDCOVER_TYPES_GROUP.items():
    # 		response.path('panels',sub+'_lcgroup')[k] = sum([baseline[sub+'_lc'].get(i) or 0 for i in v])

    transfers = [
        'pop_total', 'area_total', 'building_total', 'settlement_total',
        'healthfacility_total', 'road_total', 'GeoJson'
    ]
    response.update(
        {key: baseline[key]
         for key in transfers if key in baseline})
    response['references'] = {
        'HEALTHFAC_TYPES': HEALTHFAC_TYPES,
        'LANDCOVER_TYPES': LANDCOVER_TYPES,
        'ROAD_TYPES': ROAD_TYPES,
    }

    # convert to pre sort list format
    total_titles = {
        'pop': 'Total Population',
        'building': 'Total Buildings',
        'area': 'Total Area (km2)',
        'settlement': 'Number of Settlements',
        'healthfacility': 'Health Facilities',
        'road': 'Total Length of Road (km)'
    }
    charts = dict_ext({
        'pop_lc': {
            'title':
            _('Population Graph'),
            'labels': [LANDCOVER_TYPES[k] for k in LANDCOVER_INDEX.values()],
            'values':
            [baseline['pop_lc'].get(k) or 0 for k in LANDCOVER_INDEX.values()]
        },
        'area_lc': {
            'title':
            _('Area Graph'),
            'labels': [LANDCOVER_TYPES[k] for k in LANDCOVER_INDEX.values()],
            'values': [
                baseline['area_lc'].get(k) or 0
                for k in LANDCOVER_INDEX.values()
            ]
        },
        'building_lc': {
            'title':
            _('Building Graph'),
            'labels': [LANDCOVER_TYPES[k] for k in LANDCOVER_INDEX.values()],
            'values': [
                baseline['building_lc'].get(k) or 0
                for k in LANDCOVER_INDEX.values()
            ]
        },
        'healthfacility': {
            'title':
            _('Health Facilities Graph'),
            'labels': [HEALTHFAC_TYPES[k] for k in HEALTHFAC_GROUP7],
            'values':
            [response['healthfacility'].get(k) or 0 for k in HEALTHFAC_GROUP7]
        },
        'road': {
            'title': _('Road Network Graph'),
            'labels': [ROAD_TYPES[k] for k in ROAD_INDEX.values()],
            'values':
            [baseline['road'].get(k) or 0 for k in ROAD_INDEX.values()]
        },
    })

    panels['total'] = {
        'title':
        _('Total'),
        'labels': [
            total_titles[k] for k in [
                'pop', 'building', 'area', 'settlement', 'healthfacility',
                'road'
            ]
        ],
        'values': [
            baseline.get(k + '_total') or 0 for k in [
                'pop', 'building', 'area', 'settlement', 'healthfacility',
                'road'
            ]
        ]
    }

    for v in panels.values():
        v['total'] = sum(v['values'])

    panels['charts'] = charts.within('pop_lc', 'area_lc', 'building_lc',
                                     'healthfacility', 'road')
    # childs = dict_ext({
    # 	'pop': [{'title':LANDCOVER_TYPES[k],'value':baseline['pop_lc'].get(k,0)} for k in LANDCOVER_INDEX.values()],
    # 	'area': [{'title':LANDCOVER_TYPES[k],'value':baseline['area_lc'].get(k,0)} for k in LANDCOVER_INDEX.values()],
    # 	'building': [{'title':LANDCOVER_TYPES[k],'value':baseline['building_lc'].get(k,0)} for k in LANDCOVER_INDEX.values()],
    # 	'healthfacility': [{'title':HEALTHFAC_TYPES[k],'value':response.path('panels')['healthfacility'].get(k,0)} for k in HEALTHFAC_GROUP14],
    # 	'road': [{'title':ROAD_TYPES[k],'value':baseline['road'].get(k,0)} for k in ROAD_INDEX.values()],
    # 	'total': [{'title':total_titles[k],'value':baseline.get(k+'_total',0)} for k in ['pop','building','area','settlement','healthfacility','road']],
    # })
    # panels = {k:{'title': PANEL_TITLES[k],'child': childs[k],'total': baseline[k+'_total'],} for k in ['pop','area','building','healthfacility','road']}
    # panels['total'] = {'title':'Totals','child': childs['total']}

    tables = dict_ext()

    if include_section('adm_lc', includes, excludes):
        response['adm_lc'] = baseline['adm_lc']
        tables['adm_lcgroup_pop_area'] = {
            'title':
            _('Overview of Population and Area'),
            'parentdata': [
                response['parent_label'],
                baseline['building_total'],
                baseline['settlement_total'],
                baseline['pop_lcgroup']['built_up'],
                baseline['area_lcgroup']['built_up'],
                baseline['pop_lcgroup']['cultivated'],
                baseline['area_lcgroup']['cultivated'],
                baseline['pop_lcgroup']['barren'],
                baseline['area_lcgroup']['barren'],
                baseline['pop_total'],
                baseline['area_total'],
            ],
            'child': [{
                'values': [
                    v['na_en'],
                    v['total_buildings'],
                    v['settlements'],
                    v['built_up_pop'],
                    v['built_up_area'],
                    v['cultivated_pop'],
                    v['cultivated_area'],
                    v['barren_pop'],
                    v['barren_area'],
                    v['Population'],
                    v['Area'],
                ],
                'code':
                v['code'],
            } for v in baseline['adm_lc']],
        }

    if include_section('adm_hlt_road', includes, excludes):
        response['adm_hlt_road'] = baseline['adm_hlt_road']
        hlt_other = sum([
            v for k, v in baseline['healthfacility'].items()
            if k not in HEALTHFAC_GROUP7
        ]) + baseline['healthfacility']['other']
        tables['adm_healthfacility'] = {
            'title':
            _('Health Facility'),
            'parentdata': [
                response['parent_label'],
                baseline['healthfacility']['h1'],
                baseline['healthfacility']['h2'],
                baseline['healthfacility']['h3'],
                baseline['healthfacility']['chc'],
                baseline['healthfacility']['bhc'],
                baseline['healthfacility']['shc'],
                hlt_other,
                baseline['healthfacility_total'],
            ],
            'child': [{
                'values': [
                    v['na_en'],
                    v['hlt_h1'],
                    v['hlt_h2'],
                    v['hlt_h3'],
                    v['hlt_chc'],
                    v['hlt_bhc'],
                    v['hlt_shc'],
                    v['hlt_others'],
                    v['hlt_total'],
                ],
                'code':
                v['code'],
            } for v in baseline['adm_hlt_road']],
        }

        tables['adm_road'] = {
            'title':
            _('Road Network'),
            'parentdata': [
                response['parent_label'],
                baseline['road']['highway'],
                baseline['road']['primary'],
                baseline['road']['secondary'],
                baseline['road']['tertiary'],
                baseline['road']['residential'],
                baseline['road']['track'],
                baseline['road']['path'],
                baseline['road_total'],
            ],
            'child': [{
                'values': [
                    v['na_en'],
                    v['road_highway'],
                    v['road_primary'],
                    v['road_secondary'],
                    v['road_tertiary'],
                    v['road_residential'],
                    v['road_track'],
                    v['road_path'],
                    v['road_total'],
                ],
                'code':
                v['code'],
            } for v in response['adm_hlt_road']],
        }

    panels['tables'] = tables.within('adm_lcgroup_pop_area',
                                     'adm_healthfacility', 'adm_road')

    # response['panels_list'] = [panels[k] for k in ['total','pop','building','area','adm_lcgroup_pop_area','adm_healthfacility','healthfacility','road','adm_road']]

    if include_section('GeoJson', includes, excludes):
        response['GeoJson'] = geojsonadd(response)

    # dataLC = []
    # dataLC.append([_('landcover type'),_('population'), { 'role': 'annotation' },_('buildings'), { 'role': 'annotation' },_('area (km2)'), { 'role': 'annotation' }])
    # # dataLC.append([_('Built-up'),round((response['built_up_pop'] or 0)/(response['Population'] or 0)*100,0), response['built_up_pop'],round((response['built_up_buildings'] or 0)/(response['Buildings'] or 0)*100,0), response['built_up_buildings'], round((response['built_up_area'] or 0)/(response['Area'] or 0)*100,0), response['built_up_area'] ])
    # # dataLC.append([_('Cultivated'),round((response['cultivated_pop'] or 0)/(response['Population'] or 0)*100,0), response['cultivated_pop'],round((response['cultivated_buildings'] or 0)/(response['Buildings'] or 0)*100,0), response['cultivated_buildings'], round((response['cultivated_area'] or 0)/(response['Area']*100 or 0),0), response['cultivated_area'] ])
    # # dataLC.append([_('Barren/Rangeland'),round((response['barren_pop'] or 0)/(response['Population'] or 0)*100,0), response['barren_pop'],round((response['barren_buildings'] or 0)/(response['Buildings'] or 0)*100,0), response['barren_buildings'], round((response['barren_area'] or 0)/(response['Area'] or 0)*100,0), response['barren_area'] ])
    # response['landcover_chart'] = gchart.BarChart(
    # 	SimpleDataSource(data=dataLC),
    # 	html_id="pie_chart1",
    # 	options={
    # 		'title': _('Landcover Population and area overview'),
    # 		# 'subtitle': 'figure as percent from total population and area',
    # 		'width': 450,
    # 		'height': 300,
    # 		# 'legend': { 'position': 'none' },
    # 		# 'chart': { 'title': 'Landcover Population and area overview', 'subtitle': 'figure as percent from total population and area' },
    # 		'bars': 'horizontal',
    # 		'axes': {
    # 			'x': {
    # 			  '0': { 'side': 'top', 'label': _('Percentage')}
    # 			},

    # 		},
    # 		'bar': { 'groupWidth': '90%' },
    # 		'chartArea': {'width': '50%'},
    # 		'titleX':_('percentages from total population, buildings & area'),
    # })
    # response.path('total')['hltfac'] = response.path('total')['hltfac'] or 0.000001
    # # if total['hltfac']==0:
    # #     total['hltfac'] = 0.000001

    # dataHLT = []
    # hf = response['panel']['hltfac']
    # total = response['total']
    # dataHLT.append([_('health facility type'),_('percent of health facility'), { 'role': 'annotation' }])
    # dataHLT.append([_('H1'),round(hf['h1']/total['hltfac']*100,0), hf['h1'] ])
    # dataHLT.append([_('H2'),round(hf['h2']/total['hltfac']*100,0), hf['h2'] ])
    # dataHLT.append([_('H3'),round(hf['h3']/total['hltfac']*100,0), hf['h3'] ])
    # dataHLT.append([_('CHC'),round(hf['chc']/total['hltfac']*100,0), hf['chc'] ])
    # dataHLT.append([_('BHC'),round(hf['bhc']/total['hltfac']*100,0), hf['bhc'] ])
    # dataHLT.append([_('SHC'),round(hf['shc']/total['hltfac']*100,0), hf['shc'] ])
    # dataHLT.append([_('Others'),round(hf['others']/total['hltfac']*100,0), hf['others'] ])
    # response['hlt_chart'] = gchart.BarChart(
    # 	SimpleDataSource(data=dataHLT),
    # 	html_id="pie_chart2",
    # 	options={
    # 		'title': _('Health facilities overview'),
    # 		# 'subtitle': 'figure as percent from total population and area',
    # 		'width': 450,
    # 		'height': 300,
    # 		'legend': { 'position': 'none' },
    # 		# 'chart': { 'title': 'Landcover Population and area overview', 'subtitle': 'figure as percent from total population and area' },
    # 		'bars': 'horizontal',
    # 		'axes': {
    # 			'x': {
    # 			  '0': { 'side': 'top', 'label': _('Percentage')}
    # 			},

    # 		},
    # 		'bar': { 'groupWidth': '90%' },
    # 		'chartArea': {'width': '50%'},
    # 		'titleX':_('percentages from total health facilities'),
    # })

    # dataRDN = []
    # road = response['panel']['road']
    # dataRDN.append([_('road network type'),_('percent of road network'), { 'role': 'annotation' }])
    # dataRDN.append([_('Highway'),round(road['highway']/total['roadnetwork']*100,0), road['highway'] ])
    # dataRDN.append([_('Primary'),round(road['primary']/total['roadnetwork']*100,0), road['primary'] ])
    # dataRDN.append([_('Secondary'),round(road['secondary']/total['roadnetwork']*100,0), road['secondary'] ])
    # dataRDN.append([_('Tertiary'),round(road['tertiary']/total['roadnetwork']*100,0), road['tertiary'] ])
    # dataRDN.append([_('Residential'),round(road['residential']/total['roadnetwork']*100,0), road['residential'] ])
    # dataRDN.append([_('Track'),round(road['track']/total['roadnetwork']*100,0), road['track'] ])
    # dataRDN.append([_('Path'),round(road['path']/total['roadnetwork']*100,0), road['path'] ])
    # dataRDN.append([_('River crossing'),round(road['river_crossing']/total['roadnetwork']*100,0), road['river_crossing'] ])
    # dataRDN.append([_('Bridge'),round(road['bridge']/total['roadnetwork']*100,0), road['bridge'] ])
    # response['rdn_chart'] = gchart.BarChart(
    # 	SimpleDataSource(data=dataRDN),
    # 	options={
    # 		'title': _('Road network overview'),
    # 		# 'subtitle': 'figure as percent from total population and area',
    # 		'width': 450,
    # 		'height': 300,
    # 		'legend': { 'position': 'none' },
    # 		# 'chart': { 'title': 'Landcover Population and area overview', 'subtitle': 'figure as percent from total population and area' },
    # 		'bars': 'horizontal',
    # 		'axes': {
    # 			'x': {
    # 			  '0': { 'side': 'top', 'label': _('Percentage')}
    # 			},

    # 		},
    # 		'bar': { 'groupWidth': '90%' },
    # 		'chartArea': {'width': '50%'},
    # 		'titleX':_('percentages from total length of road network'),
    # })

    # print response['poi_points']
    # print response['additional_child']
    # for i in response['additional_child']:
    #     test = [item for item in response['poi_points'] if item['code'] == i['code']][0]
    #     i['x'] = test['x']
    #     i['y'] = test['y']

    # response['additional_child'] = json.dumps(response['additional_child'])
    # print response['additional_child']

    # if include_section('GeoJson', includes, excludes):
    #     response['GeoJson'] = json.dumps(getGeoJson(request, flag, code))
    # response['GeoJson'] = getGeoJson(request, flag, code)

    #print 'It took', time.time()-start, 'seconds.'

    # response['references'] = {'HEALTHFAC_TYPES': HEALTHFAC_TYPES,'LANDCOVER_TYPES': LANDCOVER_TYPES,'ROAD_TYPES': ROAD_TYPES,}

    return response
Ejemplo n.º 2
0
def dashboard_drought(request,
                      filterLock,
                      flag,
                      code,
                      date=None,
                      includes=[],
                      excludes=[],
                      **kwargs):

    date = date or request.GET.get('date') or datetime.datetime.now().strftime(
        "%Y-%m-%d")
    date_parsed = datetime.datetime.strptime(date, "%Y-%m-%d")
    woy = getClosestDroughtWOY(
        '%s%03d' % (date_parsed.year, date_parsed.isocalendar()[1]))

    if not woy:
        return {'nodata': True, 'nodata_desc': 'No Week of Year data found.'}

    response = dict_ext()

    if include_section('getCommonUse', includes, excludes):
        response.update(getCommonUse(request, flag, code))

    response['source'] = source = dict_ext(
        getDroughtRisk(request, filterLock, flag, code, woy=woy))
    response.update(source.within('woy', 'woy_datestart', 'woy_dateend'))
    panels = response.path('panels')
    donutcharts = panels.path('charts', 'donut')
    barcharts = panels.path('charts', 'bar')
    tables = panels.path('tables')

    panels.path('charts')['donut'] = donutcharts = {
        k: {
            'key':
            k,
            'child':
            [[r['label'], float(r['child'][k])]
             for kr, r in source['drought_data']['group_by']['risk'].items()]
        }
        for k in ['pop', 'area', 'building']
    }
    donutcharts['pop']['title'] = _('Drought Population')
    donutcharts['area']['title'] = _('Drought Area')
    donutcharts['building']['title'] = _('Drought Building')
    donutcharts['pop']['child'] += [[
        _('Not at Risk'), source['Population'] -
        float(sum([r[1] for r in donutcharts['pop']['child']]))
    ]]
    donutcharts['area']['child'] += [[
        _('Not at Risk'), source['Area'] -
        float(sum([r[1] for r in donutcharts['area']['child']]))
    ]]
    donutcharts['building']['child'] += [[
        _('Not at Risk'), source['Buildings'] -
        float(sum([r[1] for r in donutcharts['building']['child']]))
    ]]

    lcrisk = dict_ext(source['drought_data']['group_by']['landcover_risk'])
    panels.path('charts')['bar'] = barcharts = dict_ext()
    for k in ['pop', 'area']:
        barcharts.path(k)['key'] = k
        barcharts.path(k)['labels'] = [lc for lc in lcrisk]
        barcharts.path(k)['child'] = [{
            'name':
            _(DROUGHTRISK_TYPES[r]),
            'data': [
                float(lcrisk.pathget(lc, 'risk_child', r, 'child', k) or 0)
                for lc in barcharts.path(k)['labels']
            ],
        } for r in DROUGHTRISK_TYPES_ORDER]
        barcharts.path(k)['child'].append({
            'name':
            _("Not at Risk"),
            'data': [
                float(lcrisk[lc]['total_child'][k] -
                      lcrisk[lc]['total_risk_child'][k])
                for lc in barcharts.path(k)['labels']
            ],
        })
    barcharts['pop']['title'] = _('Drought Population Landcover')
    barcharts['area']['title'] = _('Drought Area Landcover')

    colcount = 16
    custom_slugify = Slugify(separator='_', to_lower=True)
    for klc in DROUGHTLANDCOVER_TYPES_ORDER:
        key = klc
        lc = DROUGHTLANDCOVER_TYPES[klc]
        table = tables.path(key)
        table['title'] = _(lc)
        table['key'] = key
        table['parentdata'] = [response['parent_label']]
        for r in DROUGHTRISK_TYPES_ORDER:
            cat = dict_ext(
                source.pathget('drought_data', 'group_by', 'landcover_risk',
                               lc, 'risk_child', r, 'child'))
            table['parentdata'] += [int(cat.pathget('pop') or 0)] + [
                int(cat.pathget('building') or 0)
            ] + [float(cat.pathget('area') or 0)]
        # table['parentdata'] += [0]*(colcount-len(table['parentdata']))
        table['child'] = []
        for kadm, adm in source.pathget('drought_data', 'group_by',
                                        'landcover_area_risk', 'lc_child', lc,
                                        'adm_child').items():
            row = [
                adm['label'],
            ]
            for kr, r in adm['risk_child'].items():
                row += [int(r['child']['pop'])] + [
                    int(r['child']['building'])
                ] + [float(r['child']['area'])]
            row += [0] * (colcount - len(row))
            table['child'].append({
                'code': adm['code'],
                'value': row,
            })

    if include_section('GeoJson', includes, excludes):
        response['GeoJson'] = geojsonadd_droughtrisk(response)

    return response
Ejemplo n.º 3
0
def getSecurity(request, filterLock, flag, code, includes=[], excludes=[], datafilter={}, response=dict_ext()):
	# rawFilterLock = None
	# if 'flag' in request.GET:
	#     rawFilterLock = filterLock
	#     filterLock = 'ST_GeomFromText(\''+filterLock+'\',4326)'

	# response = getCommonUse(request, flag, code)
	# response = dict_ext()

	# enddate = datetime.date.today()
	# startdate = datetime.date.today() - datetime.timedelta(days=365)
	# daterange = startdate.strftime("%Y-%m-%d")+','+enddate.strftime("%Y-%m-%d")

	# if 'daterange' in request.GET:
	#     daterange = request.GET['daterange']

	# datestart, dateend = daterange.split(',')
	# daterange = ','.join([datestart+' 00:00:00.000000', dateend+' 23:59:59.999999'])

	daterange = datafilter.get('daterange')

	if include_section('total_casualties', includes, excludes):
		response.update(getIncidentCasualties(request, daterange, filterLock, flag, code))
	# rawCasualties = getIncidentCasualties(request, daterange, filterLock, flag, code)
	# for i in rawCasualties:
	# 	response[i]=rawCasualties[i]

	# dataHLT = []
	# dataHLT.append(['', '# of',  { 'role': 'annotation' }])
	# dataHLT.append(['Death',rawCasualties['total_dead'], rawCasualties['total_dead'] ])
	# dataHLT.append(['Violent',rawCasualties['total_violent'], rawCasualties['total_violent'] ])
	# dataHLT.append(['Injured',rawCasualties['total_injured'], rawCasualties['total_injured'] ])
	# dataHLT.append(['# Incidents',rawCasualties['total_incident'], rawCasualties['total_incident'] ])
	# response['casualties_chart'] = gchart.BarChart(
	#     SimpleDataSource(data=dataHLT),
	#     html_id="pie_chart1",
	#     options={
	#         'title': 'Security Incident Overview',
	#         'width': 300,
	#         'height': 300,
	#         'legend': { 'position': 'none' },

	#         'bars': 'horizontal',
	#         'axes': {
	#             'x': {
	#               '0': { 'side': 'top', 'label': '# of Casualties and Incident'}
	#             },

	#         },
	#         'bar': { 'groupWidth': '90%' },
	#         'chartArea': {'width': '50%'},
	#         'titleX':'# of Casualties and Incident',
	# })

	if include_section('incident_type', includes, excludes):
		response['main_type_child'] = getSAMParams(request, daterange, filterLock, flag, code, 'main_type')
		response['incident_type'] = datafilter.get('incident_type') or [i['main_type'] for i in response['main_type_child']]

		main_type_raw_data = getSAMParams(request, daterange, filterLock, flag, code, 'main_type', datafilter=datafilter)
		response['incident_type_group'] =[{
			'count':i['count'],
			'injured':i['injured'],
			'violent':i['violent']+i['affected'],
			'dead':i['dead'],
			'main_type':i['main_type'],
			'child':list(getSAMIncident(request, daterange, filterLock, flag, code, 'type', datafilter={'incident_type':[i['main_type']]}))
		} for i in main_type_raw_data]

	# data_main_type = []
	# data_main_type.append(['', 'incident',{ 'role': 'annotation' }, 'dead',{ 'role': 'annotation' }, 'violent',{ 'role': 'annotation' }, 'injured',{ 'role': 'annotation' } ])
	# for type_item in main_type_raw_data:
	#     data_main_type.append([type_item['main_type'],type_item['count'],type_item['count'], type_item['dead'], type_item['dead'], type_item['violent']+type_item['affected'], type_item['violent']+type_item['affected'], type_item['injured'], type_item['injured'] ])
	# response['main_type_chart'] = gchart.BarChart(
	#     SimpleDataSource(data=data_main_type),
	#     html_id="pie_chart2",
	#     options={
	#         'title': 'Incident type overview and casualties',
	#         'width': 450,
	#         'height': 450,
	#         'isStacked':'true',
	#         'bars': 'horizontal',
	#         'axes': {
	#             'x': {
	#               '0': { 'side': 'top', 'label': '# of Casualties and Incident'}
	#             },

	#         },
	#         'annotations': {
	#             'textStyle': {
	#                 'fontSize':7
	#             }
	#         },
	#         'bar': { 'groupWidth': '90%' },
	#         'chartArea': {'width': '60%', 'height': '90%'},
	#         'titleX':'# of incident and casualties',
	# })

	# data_main_type.append(['TYPE', 'incident', 'dead', 'violent', 'injured' ])
	# for type_item in main_type_raw_data:
	# 	data_main_type.append([type_item['main_type'],type_item['count'], type_item['dead'], (type_item['violent'] or 0)+(type_item['affected'] or 0), type_item['injured'] ])

	# data_main_type = ['TYPE', 'incident', 'dead', 'violent', 'injured' ]+[[
	# 	item['main_type'],
	# 	item['count'],
	# 	item['dead'],
	# 	(item['violent'] or 0)+(item['affected'] or 0),
	# 	item['injured'],
	# ]
	# for item in main_type_raw_data]

	# response['main_type_chart'] = RadarChart(SimpleDataSource(data=data_main_type),
	#         html_id="pie_chart2",
	#         options={
	#             'title': _('Number of Incident by Incident Type'),
	#             'col-included' : [
	#                 {'col-no':1,'name':_('Incidents'),'fill':True}
	#             ]
	#         }
	#     ).get_image()

	# response['dead_casualties_type_chart'] = RadarChart(SimpleDataSource(data=data_main_type),
	#         html_id="pie_chart4",
	#         options={
	#             'title': _('Number of dead casualties by Incident Type'),
	#             'col-included' : [
	#                 {'col-no':2,'name':_('Dead'),'fill':True}
	#             ]
	#         }
	#     ).get_image()

	# response['injured_casualties_type_chart'] = RadarChart(SimpleDataSource(data=data_main_type),
	#         html_id="pie_chart4",
	#         options={
	#             'title': _('Number of injured casualties by Incident Type'),
	#             'col-included' : [
	#                 {'col-no':4,'name':_('Violent'),'fill':True}
	#             ]
	#         }
	#     ).get_image()

	# response['violent_casualties_type_chart'] = RadarChart(SimpleDataSource(data=data_main_type),
	#         html_id="pie_chart4",
	#         options={
	#             'title': _('Number of affected person by Incident Type'),
	#             'col-included' : [
	#                 {'col-no':3,'name':_('Injured'),'fill':True}
	#             ]
	#         }
	#     ).get_image()

	if include_section('incident_target', includes, excludes):
		response['main_target_child'] = getSAMParams(request, daterange, filterLock, flag, code, 'main_target')
		response['incident_target'] = datafilter.get('incident_target') or [i['main_target'] for i in response['main_target_child']]

		main_target_raw_data = getSAMParams(request, daterange, filterLock, flag, code, 'main_target', datafilter=datafilter)
		response['incident_target_group'] = [{
			'count':i['count'],
			'injured':i['injured'],
			'violent':i['violent']+i['affected'],
			'dead':i['dead'],
			'main_target':i['main_target'],
			'child':list(getSAMIncident(request, daterange, filterLock, flag, code, 'target', datafilter={'incident_target':[i['main_target']]}))
		} for i in main_target_raw_data]

	# data_main_target = []
	# data_main_target.append(['', 'incident',{ 'role': 'annotation' }, 'dead',{ 'role': 'annotation' }, 'violent',{ 'role': 'annotation' }, 'injured',{ 'role': 'annotation' } ])
	# for type_item in main_target_raw_data:
	#      data_main_target.append([type_item['main_target'],type_item['count'],type_item['count'], type_item['dead'], type_item['dead'], type_item['violent']+type_item['affected'], type_item['violent']+type_item['affected'], type_item['injured'], type_item['injured'] ])

	# response['main_target_chart'] = gchart.BarChart(
	#     SimpleDataSource(data=data_main_target),
	#     html_id="pie_chart3",
	#     options={
	#         'title': 'Incident target overview and casualties',
	#         'width': 450,
	#         'height': 450,
	#         # 'legend': { 'position': 'none' },
	#         'isStacked':'true',
	#         'bars': 'horizontal',
	#         'axes': {
	#             'x': {
	#               '0': { 'side': 'top', 'label': '# of Casualties and Incident'}
	#             },

	#         },
	#         'annotations': {
	#             'textStyle': {
	#                 'fontSize':7
	#             }
	#         },
	#         'bar': { 'groupWidth': '90%' },
	#         'chartArea': {'width': '60%', 'height': '90%'},
	#         'titleX':'# of incident and casualties',
	# })
	# data_main_target.append(['Target', 'incident', 'dead', 'violent', 'injured' ])
	# for type_item in main_target_raw_data:
	#      data_main_target.append([type_item['main_target'],type_item['count'], type_item['dead'], (type_item['violent'] or 0)+(type_item['affected'] or 0), type_item['injured'] ])
	# response['main_target_chart'] = RadarChart(SimpleDataSource(data=data_main_target),
	#         html_id="pie_chart3",
	#         options={
	#             'title': _('Number of Incident by Incident Target'),
	#             'col-included' : [
	#                 {'col-no':1,'name':_('Incidents'),'fill':True}
	#             ]
	#         }
	#     ).get_image()

	# response['dead_casualties_target_chart'] = RadarChart(SimpleDataSource(data=data_main_target),
	#         html_id="pie_chart4",
	#         options={
	#             'title': _('Number of dead casualties by Incident Target'),
	#             'col-included' : [
	#                 {'col-no':2,'name':_('Dead'),'fill':True}
	#             ]
	#         }
	#     ).get_image()

	# response['injured_casualties_target_chart'] = RadarChart(SimpleDataSource(data=data_main_target),
	#         html_id="pie_chart4",
	#         options={
	#             'title': _('Number of injured casualties by Incident Target'),
	#             'col-included' : [
	#                 {'col-no':4,'name':_('Injured'),'fill':True}
	#             ]
	#         }
	#     ).get_image()

	# response['violent_casualties_target_chart'] = RadarChart(SimpleDataSource(data=data_main_target),
	#         html_id="pie_chart4",
	#         options={
	#             'title': _('Number of affected person by Incident Target'),
	#             'col-included' : [
	#                 {'col-no':3,'name':_('Injured'),'fill':True}
	#             ]
	#         }
	#     ).get_image()


	# for i in response['main_type_child']:
	# 	response['incident_type'].append(i['main_type'])

	# for i in response['main_target_child']:
	# 	response['incident_target'].append(i['main_target'])

	# if 'incident_type' in request.GET:
	# 	response['incident_type'] = request.GET['incident_type'].split(',')
	# 	# print response['incident_type']

	# if 'incident_target' in request.GET:
	# 	response['incident_target'] = request.GET['incident_target'].split(',')
	# 	# print response['incident_target']

	# data = getListIncidentCasualties(request, daterange, filterLock, flag, code)
	if include_section('lc_child', includes, excludes):
		response['lc_child'] = data = getListIncidentCasualties(request, daterange, filterLock, flag, code)

	if include_section('incident_list_100', includes, excludes):
		response['incident_list_100'] = getListIncidents(request, daterange, filterLock, flag, code)

	if include_section('GeoJson', includes, excludes):
		response['GeoJson'] = getGeoJson(request, flag, code)

	return response
Ejemplo n.º 4
0
def getDroughtRisk(request,
                   filterLock,
                   flag,
                   code,
                   woy,
                   includes=[],
                   excludes=[]):

    targetBase = AfgLndcrva.objects.all()
    response = getCommonUse(request, flag, code)

    response['baseline'] = baseline = getBaseline(
        request,
        filterLock,
        flag,
        code,
        includes=['pop_lc', 'area_lc', 'building_lc'])

    # if flag not in ['entireAfg','currentProvince']:
    # 	response['Population']=getTotalPop(filterLock, flag, code, targetBase)
    # 	response['Area']=getTotalArea(filterLock, flag, code, targetBase)
    # 	response['Buildings']=getTotalBuildings(filterLock, flag, code, targetBase)
    # 	response['settlement']=getTotalSettlement(filterLock, flag, code, targetBase)
    # else :
    # 	tempData = getShortCutData(flag,code)
    # 	response['Population']= tempData['Population']
    # 	response['Area']= tempData['Area']
    # 	response['Buildings']= tempData['total_buildings']
    # 	response['settlement']= tempData['settlements']

    response['Population'] = baseline['pop_total']
    response['Area'] = baseline['area_total']
    response['Buildings'] = baseline['building_total']
    response['settlement'] = baseline['settlement_total']

    sql_tpl = '''
		SELECT
			afg_lndcrva.agg_simplified_description,
			{adm_code},
			{adm_name},
			round(history_drought.mean-1) as min, 
			COALESCE(ROUND(SUM({pop_function})::NUMERIC), 0) AS pop,
			COALESCE(ROUND(SUM({building_function})::NUMERIC), 0) AS building,
			COALESCE(ROUND(SUM({area_function})::NUMERIC / 1000000, 1), 0) AS area
		FROM afg_lndcrva
		INNER JOIN history_drought
		ON history_drought.ogc_fid = afg_lndcrva.ogc_fid
		WHERE afg_lndcrva.aggcode_simplified NOT IN ('WAT', 'BRS', 'BSD', 'SNW')
		AND aggcode NOT IN ('AGR/NHS', 'NHS/NFS', 'NHS/BRS', 'NHS/WAT', 'NHS/URB', 'URB/AGT', 'URB/AGI', 'URB/NHS', 'URB/BRS', 'URB/BSD')
		AND history_drought.woy = '{woy}'
		{extra_condition}
		GROUP BY 1, 2, 3, 4
		ORDER BY 1, 2, 3, 4
		'''

    sql_total_tpl = '''
		SELECT
			afg_lndcrva.agg_simplified_description,
			{adm_code},
			{adm_name},
			COALESCE(ROUND(SUM({pop_function})::NUMERIC), 0) AS pop,
			COALESCE(ROUND(SUM({building_function})::NUMERIC), 0) AS building,
			COALESCE(ROUND(SUM({area_function})::NUMERIC / 1000000, 1), 0) AS area
		FROM afg_lndcrva
		WHERE afg_lndcrva.aggcode_simplified NOT IN ('WAT', 'BRS', 'BSD', 'SNW')
		AND aggcode NOT IN ('AGR/NHS', 'NHS/NFS', 'NHS/BRS', 'NHS/WAT', 'NHS/URB', 'URB/AGT', 'URB/AGI', 'URB/NHS', 'URB/BRS', 'URB/BSD')
		{extra_condition}
		GROUP BY 1, 2, 3
		ORDER BY 1, 2, 3
		'''

    sql_extra_condition_tpl = "AND {parent_adm_col} = '{parent_adm_val}'"
    sql_param = {
        'woy': woy,
        'extra_condition': '',
        'pop_function': 'afg_lndcrva.area_population',
        'building_function': 'afg_lndcrva.area_buildings',
        'area_function': 'afg_lndcrva.area_sqm'
    }

    if flag == 'entireAfg':
        sql_param.update({'adm_code': 'prov_code', 'adm_name': 'prov_na_en'})
    elif flag == 'currentProvince':
        sql_param.update({
            'adm_code': 'dist_code',
            'adm_name': 'dist_na_en',
            'parent_adm_val': code
        })
        if len(str(code)) > 2:
            sql_param.update({'parent_adm_col': 'dist_code'})
        else:
            sql_param.update({'parent_adm_col': 'prov_code'})
        sql_param['extra_condition'] = sql_extra_condition_tpl.format(
            **sql_param)
    elif flag == 'drawArea':
        sql_param['adm_code'] = '0 as area_code'
        sql_param['adm_name'] = '\'drawarea\' as area_name'
        sql_param[
            'extra_condition'] = 'AND ST_Intersects(wkb_geometry, %s)' % filterLock
        sql_param['pop_function'] = 'case \
		    when ST_CoveredBy(afg_lndcrva.wkb_geometry ,' + filterLock + ') then area_population \
		    else ST_Area(ST_Intersection(afg_lndcrva.wkb_geometry,' + filterLock + ')) / ST_Area(afg_lndcrva.wkb_geometry)* area_population end'
        sql_param['building_function'] = 'case \
		    when ST_CoveredBy(afg_lndcrva.wkb_geometry ,' + filterLock + ') then area_buildings \
		    else ST_Area(ST_Intersection(afg_lndcrva.wkb_geometry,' + filterLock + ')) / ST_Area(afg_lndcrva.wkb_geometry)* area_buildings end'
        sql_param['area_function'] = 'case \
		    when ST_CoveredBy(afg_lndcrva.wkb_geometry ,' + filterLock + ') then area_sqm \
		    else ST_Area(ST_Intersection(afg_lndcrva.wkb_geometry,' + filterLock + ')) / ST_Area(afg_lndcrva.wkb_geometry)* area_sqm end'

    sql = sql_tpl.format(**sql_param)
    sql_total = sql_total_tpl.format(**sql_param)
    print linenum(), sql

    with connections['geodb'].cursor() as cursor:

        row = query_to_dicts(cursor, sql)
        counts = [i for i in row if i['min'] >= 0]
        # counts = []
        # for i in row:
        # 	if i['min']>=0:
        # 		counts.append(i)

        row_total = query_to_dicts(cursor, sql_total)
        counts_total = list(row_total)
        # counts_total = []
        # for i in row_total:
        # 	counts_total.append(i)

    # after query executed change alias statement to column name only
    if flag == 'drawArea':
        sql_param['adm_code'] = 'area_code'
        sql_param['adm_name'] = 'area_name'

    d = {}
    groupby_risk = {}
    groupby_lc_risk = {}
    groupby_adm_risk = {'adm_child': {}}

    if counts and counts_total:

        df = pd.DataFrame(counts, columns=counts[0].keys())
        df_total = pd.DataFrame(counts_total, columns=counts_total[0].keys())

        # calculate pop, building, area group by landcover, area, risk
        for lc in df['agg_simplified_description'].unique():
            d[lc] = {'adm_child': {}}
            df_lc = df[df['agg_simplified_description'] == lc]
            d[lc]['adm_parent'] = {
                'code': code,
                'label': response['parent_label'],
                'risk_child': {}
            }
            for risk in df_lc['min'].unique():
                risk_int = int(risk)
                df_risk = df_lc[df_lc['min'] == risk]
                df_risk_agg = df_risk.agg({
                    'pop': 'sum',
                    'building': 'sum',
                    'area': 'sum'
                })
                d[lc]['adm_parent']['risk_child'][risk_int] = {
                    'label': DROUGHTRISK_TYPES[risk_int],
                    'child': {
                        'pop': df_risk_agg['pop'],
                        'building': df_risk_agg['building'],
                        'area': df_risk_agg['area']
                    }
                }
            if not ((flag == 'drawArea') or
                    (flag == 'currentProvince' and len(str(code)) > 2)):
                for adm in df_lc[sql_param['adm_code']].unique():
                    df_adm = df_lc[df_lc[sql_param['adm_code']] == adm]
                    d[lc]['adm_child'][adm] = {
                        'code': adm,
                        'label': df_adm.iloc[0][sql_param['adm_name']],
                        'risk_child': {}
                    }
                    for risk in df_adm['min'].unique():
                        risk_int = int(risk)
                        df_risk = df_adm[df_adm['min'] == risk]
                        d[lc]['adm_child'][adm]['risk_child'][risk_int] = {
                            'label': DROUGHTRISK_TYPES[risk_int],
                            'child': {
                                'pop': df_risk.iloc[0]['pop'],
                                'building': df_risk.iloc[0]['building'],
                                'area': df_risk.iloc[0]['area']
                            }
                        }

        # calculate pop, building, area group by risk
        df_risk = df.groupby(['min'], as_index=False).agg({
            'pop': 'sum',
            'building': 'sum',
            'area': 'sum'
        })
        for idx in df_risk.index:
            risk_int = int(df_risk['min'][idx])
            groupby_risk[risk_int] = {
                'label': DROUGHTRISK_TYPES[risk_int],
                'child': {
                    'pop': df_risk['pop'][idx],
                    'building': df_risk['building'][idx],
                    'area': df_risk['area'][idx]
                }
            }

        # calculate pop, area group by landcover, risk
        for lc, lc_group in df.groupby(['agg_simplified_description']):
            groupby_lc_risk[lc] = {'risk_child': {}}
            agg = df_total[df_total['agg_simplified_description'] == lc].agg({
                'pop':
                'sum',
                'building':
                'sum',
                'area':
                'sum'
            })
            groupby_lc_risk[lc]['total_child'] = {
                'pop': agg['pop'],
                'building': agg['building'],
                'area': agg['area']
            }
            agg_risk = lc_group[lc_group['agg_simplified_description'] ==
                                lc].agg({
                                    'pop': 'sum',
                                    'building': 'sum',
                                    'area': 'sum'
                                })
            groupby_lc_risk[lc]['total_risk_child'] = {
                'pop': agg_risk['pop'],
                'building': agg_risk['building'],
                'area': agg_risk['area']
            }
            for risk, risk_group in lc_group.groupby(['min']):
                agg = risk_group.agg({
                    'pop': 'sum',
                    'building': 'sum',
                    'area': 'sum'
                })
                risk_int = int(risk)
                groupby_lc_risk[lc]['risk_child'][risk_int] = {
                    'label': DROUGHTRISK_TYPES[risk_int],
                    'child': {
                        'pop': agg['pop'],
                        'building': agg['building'],
                        'area': agg['area']
                    }
                }

        # calculate pop, building, area group by adm, risk
        for adm, adm_group in df.groupby(
            [sql_param['adm_code'], sql_param['adm_name']]):
            adm_code, adm_name = adm
            groupby_adm_risk['adm_child'][adm_code] = {
                'adm_code': adm_code,
                'adm_name': adm_name,
                'risk_child': {}
            }
            for risk, risk_group in adm_group.groupby(['min']):
                agg = risk_group.agg({
                    'pop': 'sum',
                    'building': 'sum',
                    'area': 'sum'
                })
                risk_int = int(risk)
                groupby_adm_risk['adm_child'][adm_code]['risk_child'][
                    risk_int] = {
                        'label': DROUGHTRISK_TYPES[risk_int],
                        'child': {
                            'pop': agg['pop'],
                            'building': agg['building'],
                            'area': agg['area']
                        }
                    }

    # geojson
    if include_section('GeoJson', includes, excludes):
        response['GeoJson'] = getGeoJson(request, flag, code)

    woy_datestart, woy_dateend = getYearRangeFromWeek(woy)

    response.update({
        # 'queryresult':counts,
        'woy': woy,
        'woy_datestart': woy_datestart,
        'woy_dateend': woy_dateend,
        'drought_data': {
            'group_by': {
                'adm_risk': groupby_adm_risk,
                'landcover_area_risk': {
                    'lc_child': d
                },
                'landcover_risk': groupby_lc_risk,
                'risk': groupby_risk
            },
        }
    })

    return response
Ejemplo n.º 5
0
def dashboard_security(request, filterLock, flag, code, includes=[], excludes=[], datafilter={}, response=dict_ext()):

	datafilter = datafilter or get_datafilter(request.GET)

	# incident_type = request.GET.get('incident_type','').split(',')
	# incident_target = request.GET.get('incident_target','').split(',')

	# response = dict_ext()

	if include_section('getCommonUse', includes, excludes):
		response.update(getCommonUse(request, flag, code))

	response['source'] = source = response.pathget('cache','getSecurity') or dict_ext(getSecurity(request, filterLock, flag, code, includes=includes, excludes=excludes, datafilter=datafilter))
	panels = response.path('panels')
	barcharts = response.path('panels','charts','bar')
	spidercharts = response.path('panels','charts','spider')
	tables = response.path('panels','tables')

	# titles = {
	# 	'casualties':{
	# 		'number_of_casualties_by_incident_type':'Number of Casualties by Incident Type',
	# 		'number_of_casualties_by_target_type':'Number of Casualties by Target Type', 
	# 	},
	# 	'incidents':{
	# 		'number_of_incidents_by_incident_type':'Number of Incidents by Incident Type',
	# 		'number_of_incidents_by_target_type':'Number of Incidents by Target Type',
	# }
	# labels = {
	# 	'type':source['incident_type'],
	# 	'target':source['incident_target'],
	# }
	# numbersource = {
	# 	'type':source['incident_type_group'],
	# 	'target':source['incident_target_group'],
	# }
	# for k,t in titles.items():
	# 	barcharts.path(k)['key'] = k
	# 	barcharts.path(k)['title'] = t
	# 	barcharts.path(k)['labels'] = source['incident_type']
	# 	barcharts.path(k)['child'] = [{
	# 		'name':CASUALTY_TYPES[t],
	# 		'data':[i[t] for i in source['incident_type_group']]
	# 	} for t in CASUALTY_TYPES_ORDER]

	if source.containall('incident_type','incident_type_group'):

		k = 'number_of_casualties_by_incident_type'
		barcharts[k] = {
			'key':k,
			'title':'Number of Casualties by Incident Type',
			'labels':source['incident_type'],
			'child':[{
				'name':CASUALTY_TYPES[t],
				'data':[i[t] for i in source['incident_type_group']]
			} for t in ['dead','injured']],
		}

		k = 'number_of_incidents_by_incident_type'
		barcharts[k] = {
			'key':k,
			'title':'Number of Incidents by Incident Type',
			'labels':source['incident_type'],
			'child':[{
				'name':CASUALTY_TYPES[t],
				'data':[i[t] for i in source['incident_type_group']]
			} for t in ['count','violent']],
		}

		chart = spidercharts.path('graph_of_incident_and_casualties_trend_by_incident_type')
		chart['key'] = 'graph_of_incident_and_casualties_trend_by_incident_type'
		chart['title'] = 'Graph of Incident and Casualties Trend by Incident Type'
		chart['labels'] = source['incident_type']
		chart['labels_all'] = [{
			'name':t['main_type'],
			'count':t['count'],
			'selected':t['main_type'] in source['incident_type'],
		} for t in source['main_type_child']]
		chart['child'] = [{
			'type':CASUALTY_TYPES[t],
			'data':[i[t] for i in source['incident_type_group']]
		} for t in CASUALTY_TYPES_ORDER]

	if source.containall('incident_target','incident_target_group'):

		k = 'number_of_casualties_by_target_type'
		barcharts[k] = {
			'key':k,
			'title':'Number of Casualties by Target Type',
			'labels':source['incident_target'],
			'child':[{
				'name':CASUALTY_TYPES[t],
				'data':[i[t] for i in source['incident_target_group']]
			} for t in ['dead','injured']],
		}

		k = 'number_of_incidents_by_target_type'
		barcharts[k] = {
			'key':k,
			'title':'Number of Incidents by Target Type',
			'labels':source['incident_target'],
			'child':[{
				'name':CASUALTY_TYPES[t],
				'data':[i[t] for i in source['incident_target_group']]
			} for t in ['count','violent']],
		}

		chart = spidercharts.path('graph_of_incident_and_casualties_trend_by_target_type')
		chart['key'] = 'graph_of_incident_and_casualties_trend_by_target_type'
		chart['title'] = 'Graph of Incident and Casualties Trend by Target Type'
		chart['labels'] = source['incident_target']
		chart['labels_all'] = [{
			'name':t['main_target'],
			'count':t['count'],
			'selected':t['main_target'] in source['incident_target'],
		} for t in source['main_target_child']]
		chart['child'] = [{
			'type':CASUALTY_TYPES[t],
			'data':[i[t] for i in source['incident_target_group']]
		} for t in CASUALTY_TYPES_ORDER]

	# titles = {
	# 	'number_of_casualties_by_target_type':'Number of Casualties by Target Type', 
	# 	'number_of_incidents_by_target_type':'Number of Incidents by Target Type'
	# }
	# for k,t in titles.items():
	# 	barcharts.path(k)['key'] = k
	# 	barcharts.path(k)['title'] = t
	# 	barcharts.path(k)['labels'] = source['incident_target']
	# 	barcharts.path(k)['child'] = [{
	# 		'name':CASUALTY_TYPES[t],
	# 		'data':[i[t] for i in source['incident_target_group']]
	# 	} for t in CASUALTY_TYPES_ORDER]

	# titles = {
	# 	'graph_of_incident_and_casualties_trend_by_incident_type':'Graph of Incident and Casualties Trend by Incident Type', 
	# 	'graph_of_incident_and_casualties_trend_by_target_type':'Graph of Incident and Casualties Trend by Target Type'
	# }
	# for k,t in titles.items():
	# 	spidercharts.path(k)['key'] = k
	# 	spidercharts.path(k)['title'] = t
	# 	spidercharts.path(k)['labels'] = source['incident_type']
	# 	spidercharts.path(k)['child'] = [{
	# 		'type':CASUALTY_TYPES[t],
	# 		'data':[i[t] for i in source['incident_type_group']]
	# 	} for t in CASUALTY_TYPES_ORDER]

	if source.containall('incident_target_group'):
		table = tables['incidents_and_casualties_by_incident_type'] = dict_ext()
		table['key'] = 'incidents_and_casualties_by_incident_type'
		table['title'] = _('Incidents and Casualties by Incident Type')
		table['child'] = []
		for i in source['incident_type_group']:
			table['child'] += [{
				'isgroup':True,
				'value':[i['main_type'],i['count'],i['violent'],i['injured'],i['dead']],
			}] + [{
				'value':[j['type'],j['count'],j['violent'],j['injured'],j['dead']],
			} for j in i['child']]

	if source.containall('incident_target_group'):
		table = tables['incident_and_casualties_trend_by_target_type'] = dict_ext()
		table['key'] = 'incident_and_casualties_trend_by_target_type'
		table['title'] = _('Incidents and Casualties by Target Type')
		table['child'] = []
		for i in source['incident_target_group']:
			table['child'] += [{
				'isgroup':True,
				'value':[i['main_target'],i['count'],i['violent'],i['injured'],i['dead']],
			}] + [{
				'value':[j['target'],j['count'],j['violent'],j['injured'],j['dead']],
			} for j in i['child']]

	if source.containall('parent_label') and source.containall('lc_child','total_incident','total_violent','total_injured','total_dead'):
		tables['number_of_incident_and_casualties_overview'] = {
			'title':_('Number of Incident and Casualties Overview'),
			'key':'number_of_incident_and_casualties_overview',
			'parentdata':[response['parent_label'],source['total_incident'],source['total_violent'],source['total_injured'],source['total_dead']],
			'child':[{
				'code':i['code'],
				'value':[i['na_en'],i['total_incident'],i['total_violent'],i['total_injured'],i['total_dead']],
			}
			for i in source['lc_child']],
		}

	if source.containall('incident_target','incident_list_100'):
		tables['list_of_latest_incidents'] = {
			'title':_('List of Latest Incidents'),
			'key':'list_of_latest_incidents',
			'child':[[i['incident_date'],i['description'],] for i in source['incident_list_100']],
		}

	if include_section('GeoJson', includes, excludes):
		response['GeoJson'] = geojsonadd_security(response)

	return response