Commit 6157378f authored by Niklas Selke's avatar Niklas Selke
Browse files

modified the HTAP regions tier1 service

parent 17ca4823
Pipeline #96837 passed with stages
in 50 seconds
......@@ -52,11 +52,10 @@ INSTALLED_APPS = [
'services.climatic_zones',
# 'services.nox_emissions',
# 'services.omi_no2',
# 'services.htap_regions_tier1',
'services.htap_regions_tier1',
# 'services.htap_regions_tier2',
'services.population_number',
'services.landcover',
'services.htap_region_tier1',
# 'services.built_up_area',
# 'services.population_density_ghs',
]
......@@ -168,6 +167,7 @@ DEFAULT_LAT = 48.869359 # home sweet home
DEFAULT_LON = 9.348595
DEFAULT_YEAR = 2012
DEFAULT_RELATIVE = False
DEFAULT_COUNTRY = 'DE'
# Check only position or circle around position within bounds
DEFAULT_CHECK_IS_CIRCLE_INSIDE = False
......
......@@ -16,11 +16,10 @@ urlpatterns = [
re_path(r'climatic_zones/', include('services.climatic_zones.urls'), name='climatic_zones'),
# re_path(r'nox_emissions/', include('services.nox_emissions.urls'), name='nox_emissions'),
# re_path(r'omi_no2/', include('services.omi_no2.urls'), name='omi_no2'),
# re_path(r'htap_regions_tier1/', include('services.htap_regions_tier1.urls'), name='htap_regions_tier1'),
re_path(r'htap_regions_tier1/', include('services.htap_regions_tier1.urls'), name='htap_regions_tier1'),
# re_path(r'htap_regions_tier2/', include('services.htap_regions_tier2.urls'), name='htap_regions_tier2'),
re_path(r'population_number/', include('services.population_number.urls'), name='population_number'),
re_path(r'landcover/', include('services.landcover.urls'), name='landcover'),
re_path(r'htap_region_tier1/', include('services.htap_region_tier1.urls'), name='htap_region_tier1'),
# re_path(r'built_up_area/', include('services.built_up_area.urls'), name='built_up_area'),
# re_path(r'population_density_ghs/', include('services.population_density_ghs.urls'), name='population_density_ghs')
]
......
......@@ -43,11 +43,10 @@ class LocationServicesRootView(APIView):
('climatic_zones', request.build_absolute_uri()+'climatic_zones/'),
# ('nox_emissions', request.build_absolute_uri()+'nox_emissions/'),
# ('omi_no2', request.build_absolute_uri()+'omi_no2/'),
# ('htap_regions_tier1', request.build_absolute_uri()+'htap_regions_tier1/'),
('htap_regions_tier1', request.build_absolute_uri()+'htap_regions_tier1/'),
# ('htap_regions_tier2', request.build_absolute_uri()+'htap_regions_tier2/'),
('population_number', request.build_absolute_uri()+'population_number/'),
('landcover', request.build_absolute_uri()+'landcover/'),
('htap_region_tier1', request.build_absolute_uri()+'htap_region_tier1'),
# ('built_up_area', request.build_absolute_uri()+'built_up_area/'),
# ('population_density_ghs', request.build_absolute_uri()+'population_density_ghs/')
]),
......
......@@ -5,7 +5,6 @@ Django>=2.0.6
djangorestframework>=3.8.2
idna>=2.7
numpy>=1.16
pandas>=1.0
PyGeodesy>=20.5.20,<=21.2.1
pytz>=2018.4
requests>=2.19.1
......
from django.apps import AppConfig
class HtapRegionTier1Config(AppConfig): # pragma: no cover
name = 'services.htap_region_tier1'
Country Code,HTAP ID
AF,5.0
AX,4.0
AL,4.0
DZ,9.0
AS,2.0
AD,4.0
AO,10.0
AI,12.0
AQ,17.0
AG,12.0
AR,13.0
AM,14.0
AW,12.0
AU,8.0
AT,4.0
AZ,14.0
BS,12.0
BH,11.0
BD,5.0
BB,12.0
BY,14.0
BE,4.0
BZ,12.0
BJ,10.0
BM,2.0
BT,5.0
BO,13.0
BQ,12.0
BA,4.0
BW,10.0
BV,2.0
BR,13.0
IO,2.0
UM,2.0
VG,12.0
VI,12.0
BN,7.0
BG,4.0
BF,10.0
BI,10.0
KH,7.0
CM,10.0
CA,3.0
CV,10.0
KY,12.0
CF,10.0
TD,10.0
CL,13.0
CN,6.0
CX,7.0
CC,7.0
CO,12.0
KM,10.0
CG,10.0
CD,10.0
CK,
CR,12.0
HR,4.0
CU,12.0
CW,
CY,4.0
CZ,4.0
DK,4.0
DJ,10.0
DM,12.0
DO,12.0
EC,13.0
EG,9.0
SV,12.0
GQ,10.0
ER,10.0
EE,4.0
ET,10.0
FK,13.0
FO,4.0
FJ,
FI,4.0
FR,4.0
GF,12.0
PF,
TF,
GA,10.0
GM,10.0
G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0
ZW,10.0
"""serializer for HTAP region tier 1
"""
from utils.agg_serializer import GeneralAggSerializer
# serializer classes
class AggSerializer(GeneralAggSerializer):
def __init__(self, instance):
instance['unit'] = ''
super(AggSerializer, self).__init__(instance)
from django.test import TestCase
from services.htap_region_tier1.serializers import AggSerializer
from services.htap_region_tier1.views import HTAPRegionTier1View
import geo_location_services.settings as settings
from collections import OrderedDict
class TestSerializers(TestCase):
def test_AggSerializer(self):
rawdata = OrderedDict([
("lat", settings.DEFAULT_LAT),
("lon", settings.DEFAULT_LON),
("radius", 5000),
("direction", None),
("bins", None),
("agg_function", 'mean'),
('mean', 10),
])
serializer = AggSerializer(rawdata)
self.assertEqual(serializer.instance["unit"], "")
self.assertEqual(serializer.instance["radius"], 5000)
class TestViews(TestCase):
def test_HTAPRegionTier1View(self):
view = HTAPRegionTier1View()
from django.urls import re_path
from .views import HTAPRegionTier1View
urlpatterns = [
re_path(r'^$', HTAPRegionTier1View.as_view()),
]
from collections import OrderedDict
import pandas as pd
from rest_framework.response import Response
from rest_framework.views import APIView
from .serializers import AggSerializer
from utils.views_commons import CommonViews
class HTAPRegionTier1View(APIView):
@staticmethod
def get_query_params(params):
lat = None
country = None
for k in params:
k = k.lower()
if k == "lat" or k == "latitude":
lat = params[k]
if lat is None:
if settings.DEBUG:
lat = settings.DEFAULT_LAT
else:
raise KeyError('lat is a mandatory query argument')
lat = float(lat)
if lat < -90. or lat > 90.:
raise ValueError('query argument for lat (%f) outside allowed range (-90..+90)' % (lat))
elif k == "country":
country = params[k]
if len(country) != 2:
raise ValueError('country code must have exactly two letters')
country = country.upper()
if country is None:
raise ValueError('latitude and country code are required')
return lat, country
def get(self, request, format=None):
lat, country = self.get_query_params(request.query_params)
if lat > 66:
result = 16
elif lat < -66:
result = 17
else:
try:
result = pd.read_csv("services/htap_region_tier1/regions.csv", index_col="Country Code").loc[country][0]
except KeyError:
raise ValueError('country code does not exist') from None
rawdata = OrderedDict([
("lat", lat),
("lon", None),
("radius", None),
("direction", None),
("bins", None),
("agg_function", country),
(country, result),
("provenance", None),
])
response = AggSerializer(rawdata).data
return Response(response)
from django.apps import AppConfig
class HtapRegionsConfig(AppConfig): # pragma: no cover
name = 'htap_regions_tier1'
Country Code,HTAP ID
AF,5
AX,4
AL,4
DZ,9
AS,2
AD,4
AO,10
AI,12
AQ,17
AG,12
AR,13
AM,14
AW,12
AU,8
AT,4
AZ,14
BS,12
BH,11
BD,5
BB,12
BY,14
BE,4
BZ,12
BJ,10
BM,2
BT,5
BO,13
BQ,12
BA,4
BW,10
BV,2
BR,13
IO,2
UM,2
VG,12
VI,12
BN,7
BG,4
BF,10
BI,10
KH,7
CM,10
CA,3
CV,10
KY,12
CF,10
TD,10
CL,13
CN,6
CX,7
CC,7
CO,12
KM,10
CG,10
CD,10
CK,-1
CR,12
HR,4
CU,12
CW,-1
CY,4
CZ,4
DK,4
DJ,10
DM,12
DO,12
EC,13
EG,9
SV,12
GQ,10
ER,10
EE,4
ET,10
FK,13
FO,4
FJ,-1
FI,4
FR,4
GF,12
PF,-1
TF,-1
GA,10
GM,10
GE,14
DE,4
GH,10
GI,4
GR,4
GL,16
GD,12
GP,12
GU,7
GT,12
GG,4
GN,10
GW,10
GY,12
HT,12
HM,-1
VA,4
HN,12
HK,6
HU,4
IS,4
IN,5
ID,7
CI,10
IR,11
IQ,11
IE,4
IM,4
IL,11
IT,4
JM,12
JP,6
JE,4
JO,11
KZ,15
KE,10
KI,-1
KW,11
KG,15
LA,7
LV,4
LB,11
LS,10
LR,10
LY,9
LI,4
LT,4
LU,4
MO,6
MK,4
MG,10
MW,10
MY,7
MV,-1
ML,10
MT,4
MH,-1
MQ,-1
MR,-1
MU,10
YT,-1
MX,12
FM,-1
MD,4
MC,4
MN,6
ME,4
MS,-1
MA,-1
MZ,10
MM,7
,10
NR,-1
NP,5
NL,4
NC,-1
NZ,8
NI,12
NE,10
NG,10
NU,-1
NF,-1
KP,6
MP,-1
NO,4
OM,11
PK,5
PW,-1
PS,11
PA,12
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