Commit 65f0b672 authored by Niklas Selke's avatar Niklas Selke
Browse files

activated the htap region tier1 service

parent fe7cbeb1
Pipeline #96769 passed with stages
in 1 minute
......@@ -56,6 +56,7 @@ INSTALLED_APPS = [
# 'services.htap_regions_tier2',
'services.population_number',
'services.landcover',
'services.htap_region_tier1',
# 'services.built_up_area',
# 'services.population_density_ghs',
]
......
......@@ -20,6 +20,7 @@ urlpatterns = [
# 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')
]
......
......@@ -47,6 +47,7 @@ class LocationServicesRootView(APIView):
# ('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,6 +5,7 @@ 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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
PL,4.0
PT,4.0
PR,12.0
QA,
XK,4.0
RE,
RO,4.0
RU,14.0
RW,10.0
BL,
SH,
KN,
LC,
MF,
PM,
VC,
WS,
SM,4.0
ST,
SA,11.0
SN,10.0
RS,4.0
SC,
SL,10.0
SG,
SX,
SK,4.0
SI,4.0
SB,
SO,10.0
ZA,10.0
GS,
KR,6.0
SS,10.0
ES,4.0
LK,5.0
SD,10.0
SR,12.0
SJ,
SZ,10.0
SE,4.0
CH,4.0
SY,11.0
TW,
TJ,15.0
TZ,10.0
TH,7.0
TL,
TG,10.0
TK,
TO,
TT,
TN,
TR,4.0
TM,15.0
TC,
TV,
UG,10.0
UA,14.0
AE,
GB,4.0
US,3.0
UY,13.0
UZ,15.0
VU,
VE,12.0
VN,7.0
WF,
EH,
YE,11.0
ZM,10.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_regions_tier1.serializers import get_provenance, AggSerializer
from services.htap_regions_tier1.views import HTAPView
import geo_location_services.settings as settings
from collections import OrderedDict
class TestSerializers(TestCase):
def test_get_provenance(self):
required_elements = ['dataset_name', 'dataset_description', 'data_source', 'datacenter_url', 'download_date',
'timestamp', 'Terms of use']
self.assertListEqual(required_elements, list(get_provenance(None).keys()))
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)
self.assertTrue(serializer.instance["get_provenance"] is get_provenance)
class TestViews(TestCase):
def test_HTAPView(self):
view = HTAPView()
self.assertEqual(view.min_valid, 2)
self.assertEqual(view.max_valid, 17)
self.assertTrue(view.AggSerializer is AggSerializer)
self.assertListEqual(view.AGG_ALLOWED, ['maxclass', 'frequency'])
self.assertEqual(view.STATS["frequency"].keywords["bins"], 16)
self.assertEqual(view.STATS["frequency"].keywords["start_bin"], 2)
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)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment