drought/disaster/views.py

168 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import datetime
from urllib.parse import unquote
import httplib2
from django.db.models import Count
from django.http import JsonResponse, HttpResponse, HttpResponseNotAllowed
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from .util.utils import *
from .radar.process import *
from .spi.process import *
from dashboard.models import Spi
PROXY_FORMAT = 'http://%s:%d%s' % ('210.77.68.250', 8080, '%s')
def index(request):
month = request.GET.get('month', '1')
months = range(1, 13)
spis = Spi.objects.filter(month=month)
return render(request, 'disaster/index.html', {'month': month, 'months': months, 'spis': spis})
def hail(request):
return render(request, 'disaster/hail.html')
## 冰雹相关
'''取出冰雹范围'''
@csrf_exempt
def hailstones(request):
# csv_path = '../data/radar/radar-bj.csv'
csv_path = handle_uploaded_file(request.FILES['file_path']) # 冰雹雷达数据
'''
读取csv文件中的雷达数据
code,position,seq,time,yangjiao,no.,X,Y,huibo,dinggao,field_11
:param csv_path: 包含上述title的csv文件主要是X,Y,huibo三个字段
'''
hailstones_data = get_hailstones(csv_path)
return JsonResponse(hailstones_data.to_json(), safe=False)
def get_type_path(type):
'''
if type == '棉花':
type = 'cotton'
elif type == '玉米':
type = 'corn'
elif type == '小麦':
type = 'wheat'
elif type == '人口':
type = 'population'
elif type == 'GDP':
type = 'gdp'
'''
return DATA_BASE_PATH + 'raster/%s_3857.tif' % type
'''计算冰雹的影响'''
@csrf_exempt
def calc_hailstones_impact(request):
type = request.POST['type']
county = request.POST['county'] # 是否划分县域 1 or 0
calc_dem = request.POST['dem'] # 是否计算dem 1 or 0
csv_path = handle_uploaded_file(request.FILES['file_path']) # 冰雹雷达数据
hailstones_data = get_hailstones(csv_path)
if county == 1:
hailstones_data = intersection_xzqh(hailstones_data)
tif_path = get_type_path(type)
if calc_dem == 0:
sum = agg_raster_by_gdf_value(tif_path, hailstones_data)
else:
sum = agg_raster_by_gdf_area_and_aspect_slope(tif_path, get_type_path('aspect'), get_type_path('slope'),
hailstones_data)
res = {}
res['agg'] = sum
res['geo'] = hailstones_data.to_json()
return JsonResponse(res, safe=False)
def prevent_hailstones_data():
prevent_data = read_xls(DATA_BASE_PATH + 'fbd/fbd.xlsx')
return get_buffer_data(prevent_data, dis=10000)
'''防雹点分布情况'''
@csrf_exempt
def prevent_hailstones(request):
prevent_data = prevent_hailstones_data()
return JsonResponse(prevent_data.to_json(), safe=False)
'''计算防雹点范围内冰雹的影响情况'''
@csrf_exempt
def calc_prevent_impact(request):
csv_path = handle_uploaded_file(request.FILES['file_path']) # 冰雹雷达数据
prevent_data = prevent_hailstones_data()
hailstones_data = get_hailstones(csv_path)
prevent_intersection_disater_data = intersection(prevent_data, hailstones_data)
print(prevent_intersection_disater_data)
agg = agg_gdf_by_gdf_area(prevent_data, prevent_intersection_disater_data)
# print(res) # 单个点的面积
# print(res.sum()) # 总面积
res = {}
res['agg'] = agg
res['sum'] = agg.sum()
res['geo'] = prevent_intersection_disater_data.to_json()
return JsonResponse(res, safe=False)
## SPI 相关
'''获取SPI或者SPEI分布情况'''
@csrf_exempt
def get_spi_region(request):
spi_type = request.POST['spi_type'] # spi.spei
month = request.POST['month'] # 几月
file_path = handle_uploaded_file(request.FILES['file_path']) # 上传文件, name, rain, temp
station_buffer = get_station_polygon()
spi_gdf = get_spi_dataframe(station_buffer, file_path, int(month))
spi_disaster = get_disaster_gdf(spi_gdf, spi_type)
return JsonResponse(spi_disaster.to_json(), safe=False)
'''计算spi或spei对各种因素的影响'''
@csrf_exempt
def calc_spi_diaster(request):
spi_type = request.POST['spi_type'] # spi.spei
county = request.POST['county'] # 是否划分县域 1 or 0
type = request.POST['type'] # 栅格种类
month = request.POST['month'] # 几月
file_path = handle_uploaded_file(request.FILES['file_path']) # name, rain, temp
station_buffer = get_station_polygon()
spi_gdf = get_spi_dataframe(station_buffer, file_path, int(month))
spi_disaster = get_disaster_gdf(spi_gdf, spi_type)
if county:
spi_disaster = intersection_xzqh(spi_disaster)
res_spi = agg_raster_by_gdf_value(get_type_path(type), spi_disaster)
res = {}
res['geo'] = spi_disaster.to_json()
res['agg'] = res_spi
return JsonResponse(res, safe=False)
def handle_uploaded_file(f):
from django.conf import settings
file_name = f.name
# _, ext = os.path.splitext(file_name)
# id = str(uuid.uuid4())
media_root = settings.MEDIA_ROOT
if not os.path.exists(media_root):
os.mkdir(media_root)
path = '%s\%s' % (media_root, file_name)
print(path)
with open(path, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
return path