3
3
4
4
from wazimap .geo import geo_data
5
5
from wazimap .data .tables import get_model_from_fields , get_datatable
6
- from wazimap .data .utils import get_session , calculate_median , merge_dicts , get_stat_data , get_objects_by_geo , group_remainder , LocationNotFound
6
+ from wazimap .data .utils import get_session , calculate_median , \
7
+ merge_dicts , get_stat_data , get_objects_by_geo , group_remainder , LocationNotFound
7
8
from django .conf import settings
8
9
from collections import OrderedDict
9
10
from wazimap .data .base import Base
15
16
16
17
SECTIONS = settings .HURUMAP .get ('topics' , {})
17
18
19
+ PROFILE_SECTIONS = (
20
+ 'demographics' , # population group
21
+ 'farmland' , # farm and Agricultural land
22
+ 'ervenland' , # erven land
23
+ 'sectionaltitleland' , # sectional title land
24
+ 'redistributionandrestitution' , # redistribution and restitution
25
+ 'landsales' , #
26
+ 'landsalescolour' , # land sales transcations per color
27
+ )
18
28
LOCATIONNOTFOUND = {'is_missing' : True ,
19
29
'name' : 'No Data Found' ,
20
30
'numerators' : {'this' : 0 },
29
39
u'100,001-150K' ,u'150,001-200K' ,u'200,001-300K' ,u'300,001-500K' ,
30
40
u'500,001-800K' ,u'800,001-1M' ,u'Above 1M' ]
31
41
32
-
33
42
def get_land_profile (geo , profile_name , request ):
34
43
session = get_session ()
44
+
35
45
try :
36
46
comparative_geos = geo_data .get_comparative_geos (geo )
37
47
data = {}
38
- land_sections = ['farmland' , 'ervenland' , 'sectionaltitleland' ]
39
- #for each topic in sections
40
- #get data for that topic profiles
41
- for section in land_sections :
42
- topic_name = SECTIONS [section ]['topic' ]
43
- data [topic_name ] = get_land_topic_profiles (geo , session , topic_name )
44
-
45
- # get profiles data for comparative geometries for land sections
46
- for comp_geo in comparative_geos :
47
- if not data [topic_name ]['is_missing' ]:
48
+
49
+ sections = list (PROFILE_SECTIONS )
50
+
51
+ for section in sections :
52
+ function_name = 'get_%s_profile' % section
53
+ if function_name in globals ():
54
+ func = globals ()[function_name ]
55
+ data [section ] = func (geo , session )
56
+
57
+ # get profiles for comparative geometries
58
+ for comp_geo in comparative_geos :
48
59
try :
49
60
merge_dicts (
50
- data [topic_name ], get_land_topic_profiles ( comp_geo , session , topic_name ),
51
- comp_geo .geo_level )
61
+ data [section ], func (
62
+ comp_geo , session ), comp_geo .geo_level )
52
63
except KeyError as e :
53
64
msg = "Error merging data into %s for section '%s' from %s: KeyError: %s" % (
54
- geo .geoid , topic_name , comp_geo .geoid , e )
65
+ geo .geoid , section , comp_geo .geoid , e )
55
66
log .fatal (msg , exc_info = e )
56
67
raise ValueError (msg )
57
-
58
- data ['landsales' ] = get_landsales_profiles (geo , session )
59
- data ['redistributionandrestitution' ] = get_redistributionrestitution_profiles (geo , session )
60
68
data ['districtdistribution' ] = districtdistribution (geo , session )
61
- data ['landsalescolour' ] = get_landsales_colour_profiles (geo , session )
62
-
63
- for comp_geo in comparative_geos :
64
- if not data ['landsales' ]['is_missing' ]:
65
- try :
66
- merge_dicts (
67
- data ['landsales' ],
68
- get_landsales_profiles (comp_geo , session ),
69
- comp_geo .geo_level )
70
- except KeyError as e :
71
- msg = "Error merging data into %s for section landsale " \
72
- "from %s: KeyError: %s" % (
73
- geo .geoid , comp_geo .geoid , e )
74
- log .fatal (msg , exc_info = e )
75
- raise ValueError (msg )
76
-
77
- if not data ['redistributionandrestitution' ]['is_missing' ]:
78
- try :
79
- merge_dicts (
80
- data ['redistributionandrestitution' ],
81
- get_redistributionrestitution_profiles (comp_geo , session ),
82
- comp_geo .geo_level )
83
- except KeyError as e :
84
- msg = "Error merging data into %s for section " \
85
- "redistributionandrestitution from %s: KeyError: %s" % (
86
- geo .geoid , comp_geo .geoid , e )
87
- log .fatal (msg , exc_info = e )
88
- raise ValueError (msg )
89
-
90
-
91
- if not data ['landsalescolour' ]['is_missing' ]:
92
- try :
93
- merge_dicts (
94
- data ['landsalescolour' ],
95
- get_landsales_colour_profiles (comp_geo , session ),
96
- comp_geo .geo_level )
97
- except KeyError as e :
98
- msg = "Error merging data into %s for land sale colour " \
99
- "from %s: KeyError: %s" % (
100
- geo .geoid , comp_geo .geoid , e )
101
- log .fatal (msg , exc_info = e )
102
- raise ValueError (msg )
103
69
return data
104
70
105
71
finally :
106
72
session .close ()
107
73
108
- def get_land_topic_profiles (geo , session , topic_name ):
109
- topic_profiles = SECTIONS [topic_name ]['profiles' ]
74
+
75
+ def get_demographics_profile (geo , session ):
76
+ pop_dist_data = LOCATIONNOTFOUND
77
+ total_pop = 0
78
+ try :
79
+ pop_dist_data , total_pop = get_stat_data (
80
+ ['population group' ], geo , session )
81
+ except LocationNotFound :
82
+ pass
83
+
84
+ return {
85
+ 'total_population' : {
86
+ "name" : "People" ,
87
+ "values" : {"this" : total_pop },
88
+ },
89
+ 'is_missing' : pop_dist_data .get ('is_missing' , False )
90
+
91
+ }
92
+
93
+ def get_farmland_profile (geo , session ):
94
+ topic_profiles = SECTIONS ['farmland' ]['profiles' ]
95
+ profiles_data = {'is_missing' : True }
96
+
97
+ for profile in topic_profiles :
98
+ try :
99
+ profile_table = profile .lower ()
100
+ profile_name = profile .lower ().replace (' ' , '_' )
101
+ profiles_data [profile_name ] = LOCATIONNOTFOUND
102
+ profiles_data [profile_name ], _ = get_stat_data ([profile_table ],
103
+ geo , session )
104
+ except LocationNotFound :
105
+ pass
106
+
107
+ profiles_data ['is_missing' ] = profiles_data .get ('is_missing' ) and \
108
+ profiles_data [profile_name ].get ('is_missing' , False )
109
+
110
+ return profiles_data
111
+
112
+ def get_ervenland_profile (geo , session ):
113
+ topic_profiles = SECTIONS ['ervenland' ]['profiles' ]
110
114
profiles_data = {'is_missing' : True }
111
115
112
116
for profile in topic_profiles :
@@ -124,7 +128,26 @@ def get_land_topic_profiles(geo, session, topic_name):
124
128
125
129
return profiles_data
126
130
127
- def get_redistributionrestitution_profiles (geo , session ):
131
+ def get_sectionaltitleland_profile (geo , session ):
132
+ topic_profiles = SECTIONS ['sectionaltitleland' ]['profiles' ]
133
+ profiles_data = {'is_missing' : True }
134
+
135
+ for profile in topic_profiles :
136
+ try :
137
+ profile_table = profile .lower ()
138
+ profile_name = profile .lower ().replace (' ' , '_' )
139
+ profiles_data [profile_name ] = LOCATIONNOTFOUND
140
+ profiles_data [profile_name ], _ = get_stat_data ([profile_table ],
141
+ geo , session )
142
+ except LocationNotFound :
143
+ pass
144
+
145
+ profiles_data ['is_missing' ] = profiles_data .get ('is_missing' ) and \
146
+ profiles_data [profile_name ].get ('is_missing' )
147
+
148
+ return profiles_data
149
+
150
+ def get_redistributionandrestitution_profile (geo , session ):
128
151
redistributedlandusebreakdown = redistributeprogrammeprojectsbyyear = LOCATIONNOTFOUND
129
152
redistributeprogrammehouseholdsbyyear = landcostrestitution = LOCATIONNOTFOUND
130
153
redistributeprogrammebeneficiariesbyyear = femalepartybenefited = LOCATIONNOTFOUND
@@ -369,7 +392,7 @@ def get_redistributionrestitution_profiles(geo, session):
369
392
370
393
return redistributionrestitution
371
394
372
- def get_landsales_profiles (geo , session ):
395
+ def get_landsales_profile (geo , session ):
373
396
landsales = {'is_missing' : True }
374
397
landsalestransaction = landsaleshectares = LOCATIONNOTFOUND
375
398
landsalesaverageprice = landsalespricetrends = LOCATIONNOTFOUND
@@ -463,9 +486,7 @@ def get_landsales_profiles(geo, session):
463
486
464
487
return landsales
465
488
466
-
467
-
468
- def get_landsales_colour_profiles (geo , session ):
489
+ def get_landsalescolour_profile (geo , session ):
469
490
landsalescolourhectares = landsalescolourhectarespermonth = LOCATIONNOTFOUND
470
491
landsalescolourhectarespermonthperga = landsalescolourhectarespermonthpergu = LOCATIONNOTFOUND
471
492
landsalescolourhectarespermonthperot = landsalescolourhectarespermonthperpr = LOCATIONNOTFOUND
@@ -666,6 +687,7 @@ def get_landsales_colour_profiles(geo, session):
666
687
'is_missing' : landsalescolourtattransaction .get ('is_missing' )
667
688
668
689
}
690
+
669
691
def districtdistribution (geo , session ):
670
692
towndistrictdistributiontransactions = all_town = LOCATIONNOTFOUND
671
693
towndistrictdistributionhectares = towndistrictdistributionavgprice = LOCATIONNOTFOUND
0 commit comments