Skip to content

Commit 1a14c06

Browse files
KhadijaMahangaDavidLemayian
authored andcommitted
[Land] [Feature] Population Data (#100)
* population sql and profile function * add population for 2016 * refactored land.py functions * spaces
1 parent 2977afe commit 1a14c06

File tree

5 files changed

+1220
-72
lines changed

5 files changed

+1220
-72
lines changed

hurumap_land/profiles/land.py

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from wazimap.geo import geo_data
55
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
78
from django.conf import settings
89
from collections import OrderedDict
910
from wazimap.data.base import Base
@@ -15,6 +16,15 @@
1516

1617
SECTIONS = settings.HURUMAP.get('topics', {})
1718

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+
)
1828
LOCATIONNOTFOUND = {'is_missing': True,
1929
'name': 'No Data Found',
2030
'numerators': {'this': 0},
@@ -29,84 +39,78 @@
2939
u'100,001-150K',u'150,001-200K',u'200,001-300K',u'300,001-500K',
3040
u'500,001-800K',u'800,001-1M',u'Above 1M']
3141

32-
3342
def get_land_profile(geo, profile_name, request):
3443
session = get_session()
44+
3545
try:
3646
comparative_geos = geo_data.get_comparative_geos(geo)
3747
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:
4859
try:
4960
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)
5263
except KeyError as e:
5364
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)
5566
log.fatal(msg, exc_info=e)
5667
raise ValueError(msg)
57-
58-
data['landsales'] = get_landsales_profiles(geo, session)
59-
data['redistributionandrestitution'] = get_redistributionrestitution_profiles(geo, session)
6068
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)
10369
return data
10470

10571
finally:
10672
session.close()
10773

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']
110114
profiles_data = {'is_missing': True }
111115

112116
for profile in topic_profiles:
@@ -124,7 +128,26 @@ def get_land_topic_profiles(geo, session, topic_name):
124128

125129
return profiles_data
126130

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):
128151
redistributedlandusebreakdown = redistributeprogrammeprojectsbyyear = LOCATIONNOTFOUND
129152
redistributeprogrammehouseholdsbyyear = landcostrestitution = LOCATIONNOTFOUND
130153
redistributeprogrammebeneficiariesbyyear = femalepartybenefited = LOCATIONNOTFOUND
@@ -369,7 +392,7 @@ def get_redistributionrestitution_profiles(geo, session):
369392

370393
return redistributionrestitution
371394

372-
def get_landsales_profiles(geo, session):
395+
def get_landsales_profile(geo, session):
373396
landsales = {'is_missing': True }
374397
landsalestransaction = landsaleshectares = LOCATIONNOTFOUND
375398
landsalesaverageprice = landsalespricetrends = LOCATIONNOTFOUND
@@ -463,9 +486,7 @@ def get_landsales_profiles(geo, session):
463486

464487
return landsales
465488

466-
467-
468-
def get_landsales_colour_profiles(geo, session):
489+
def get_landsalescolour_profile(geo, session):
469490
landsalescolourhectares = landsalescolourhectarespermonth = LOCATIONNOTFOUND
470491
landsalescolourhectarespermonthperga = landsalescolourhectarespermonthpergu = LOCATIONNOTFOUND
471492
landsalescolourhectarespermonthperot = landsalescolourhectarespermonthperpr = LOCATIONNOTFOUND
@@ -666,6 +687,7 @@ def get_landsales_colour_profiles(geo, session):
666687
'is_missing': landsalescolourtattransaction.get('is_missing')
667688

668689
}
690+
669691
def districtdistribution(geo, session):
670692
towndistrictdistributiontransactions = all_town = LOCATIONNOTFOUND
671693
towndistrictdistributionhectares = towndistrictdistributionavgprice = LOCATIONNOTFOUND

hurumap_land/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# -------------------------------------------------------------------------------------
3333
# HURUmap Config
3434
# -------------------------------------------------------------------------------------
35-
35+
3636
HURUMAP['name'] = 'HURUmap Land'
3737
HURUMAP['url'] = 'https://land.hurumap.org'
3838
HURUMAP['description'] = 'is a joint project of City Press, Rapport, '\
@@ -107,7 +107,7 @@
107107
'generations': {
108108
'2011': '1',
109109
'2016': '2',
110-
None: '1',
110+
None: '2',
111111
}
112112
}
113113
# -------------------------------------------------------------------------------

0 commit comments

Comments
 (0)