Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

corrections pour les rapports annuels #286

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions comptages/comptages.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def import_file(self, file_path, count_id=None):
return

QgsMessageLog.logMessage(
'{} - Prepare import file {}'.format(
'{} - Prepare import file {} ended'.format(
datetime.now(), os.path.basename(file_path)),
'Comptages', Qgis.Info)

Expand Down Expand Up @@ -389,12 +389,15 @@ def do_yearly_report_action(self):
'Comptages', Qgis.Info)
return
QgsMessageLog.logMessage(
'{} - Generate yearly report action can really begin now for count {} with file_path: {}'.format(
datetime.now(), selected_count, file_path), 'Comptages', Qgis.Info)
'{} - Generate yearly report action can really begin now for year {} with file_path: {}'.format(
datetime.now(), year, file_path), 'Comptages', Qgis.Info)

if clazz.startswith("SPCH-MD"):
yrb = YearlyReportBike(file_path, year, section_id)
yrb.run()
QgsMessageLog.logMessage(
'{} - Yearly bike report generation ended for year {} in file_path: {}'.format(
datetime.now(), year, file_path), 'Comptages', Qgis.Info)
else:
self.tm.allTasksFinished.connect(partial(self.all_tasks_finished, report))

Expand Down Expand Up @@ -543,7 +546,6 @@ def do_generate_chart_action(self, count_id):
'{} - Generate chart action ended'.format(datetime.now()),
'Comptages', Qgis.Info)


def do_delete_data_action(self, count_id):
dlg = DeleteDialog(self.iface)
tz = pytz.timezone("Europe/Zurich")
Expand Down
25 changes: 18 additions & 7 deletions comptages/core/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def _parse_line_mc(line, **kwargs):

return [parsed_line]


def _parse_line_int2(line, **kwargs):
if line.startswith('* '):
return None
Expand Down Expand Up @@ -315,7 +316,9 @@ def _parse_file_header(file_path):
elif file_header['CLASS'][:5] == 'FHWA ':
file_header['CLASS'] = 'FHWA13'
elif file_header['CLASS'] == 'CAT-Cycle_dist-empat':
file_header['CLASS'] = 'SPCH-MD 5C'
file_header['CLASS'] = 'SPCH-MD5C'
elif file_header['CLASS'] == 'SPCH-MD_5C':
file_header['CLASS'] = 'SPCH-MD5C'

return file_header

Expand Down Expand Up @@ -427,12 +430,20 @@ def guess_count(file_path):

result = models.Count.objects.filter(
Q(id_installation__name=header['SITE']) | Q(id_installation__alias=header['SITE']))
result = result.filter(
id_installation__active=True,
id_class__name=header['CLASS'],
start_service_date__lte=header['STARTREC'],
end_service_date__gte=header['STOPREC'] - timedelta(seconds=1), # To manage datetimes like 01.01.2022 00.00 that should be equal to 31.12.2021
)
if header['FORMAT'] != "MC":
result = result.filter(
id_installation__active=True,
id_class__name=header['CLASS'],
start_service_date__lte=header['STARTREC'],
end_service_date__gte=header['STOPREC'] - timedelta(seconds=1), # To manage datetimes like 01.01.2022 00.00 that should be equal to 31.12.2021
)
else:
result = result.filter(
id_installation__active=True,
id_class__name=header['CLASS'],
start_service_date__gte=header['STARTREC'],
end_service_date__lte=header['STOPREC'],
)

if len(result) > 0:
return result[0]
Expand Down
Binary file modified comptages/report/template_yearly.xlsx
Binary file not shown.
Binary file modified comptages/report/template_yearly_bike.xlsx
Binary file not shown.
84 changes: 44 additions & 40 deletions comptages/report/yearly_report_bike.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@ def __init__(self, file_path, year, section_id):
self.year = year
self.section_id = section_id

def values_by_direction(self):
# def values_by_direction(self):

# Get all the count details for section and the year
qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
timestamp__year=self.year,
# id_category__code__in=[1, 2],
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
)
# # Get all the count details for section and the year
# qs = CountDetail.objects.filter(
# id_lane__id_section__id=self.section_id,
# timestamp__year=self.year,
# # id_category__code__in=[1, 2],
# import_status=definitions.IMPORT_STATUS_DEFINITIVE,
# )

# Total by day of the week (0->monday, 7->sunday) and by direction
result = qs.annotate(weekday=ExtractIsoWeekDay('timestamp')) \
.values('weekday') \
.annotate(total=Sum('times')) \
.values('weekday', 'id_lane__direction', 'total')
# # Total by day of the week (0->monday, 7->sunday) and by direction
# result = qs.annotate(weekday=ExtractIsoWeekDay('timestamp')) \
# .values('weekday') \
# .annotate(total=Sum('times')) \
# .values('weekday', 'id_lane__direction', 'total')

# return result

def values_by_day_and_hour(self):
# Get all the count details for section and the year
Expand All @@ -47,19 +48,19 @@ def values_by_day_and_hour(self):
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
)

# TODO: don't divide by 51 but actually aggregate first by the
# TODO: don't divide by 51 in .annotate(tjm=Sum('times') / 51) but actually aggregate first by the
# real days (with sum) and then aggregate by weekday (with average)

# Total by day of the week (0->monday, 6->sunday) and by hour (0->23)
result = qs.annotate(weekday=ExtractIsoWeekDay('timestamp')) \
.annotate(hour=ExtractHour('timestamp')) \
.values('weekday', 'hour') \
.annotate(tjm=Sum('times') / 51) \
.annotate(tjm=Sum('times')) \
.values('weekday', 'hour', 'tjm')

return result

def values_by_hour_and_direction(self, direction, weekdays=[0, 1, 2, 3, 4, 5, 6]):
def values_by_hour_and_direction(self, direction, weekdays=[1, 2, 3, 4, 5, 6, 7]):
# Get all the count details for section and the year
qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
Expand All @@ -69,12 +70,12 @@ def values_by_hour_and_direction(self, direction, weekdays=[0, 1, 2, 3, 4, 5, 6]
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
)

# TODO: don't divide by 365
# TODO: don't divide by 365 in .annotate(tjm=Sum('times') / 365)

# Total by hour (0->23)
result = qs.annotate(hour=ExtractHour('timestamp')) \
.values('hour') \
.annotate(tjm=Sum('times') / 365) \
.annotate(tjm=Sum('times')) \
.values('hour', 'tjm')

return result
Expand All @@ -87,14 +88,14 @@ def values_by_day_and_month(self):
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
)

# TODO: don't divide by 12 but actually aggregate first by the
# TODO: don't divide by 12 in .annotate(tjm=Sum('times') / 12) but actually aggregate first by the
# real days (with sum) and then aggregate by weekday (with average)

# Total by day of the week (0->monday, 6->sunday) and by month (1->12)
result = qs.annotate(weekday=ExtractIsoWeekDay('timestamp')) \
.annotate(month=ExtractMonth('timestamp')) \
.values('weekday', 'month') \
.annotate(tjm=Sum('times') / 12) \
.annotate(tjm=Sum('times')) \
.values('weekday', 'month', 'tjm')

return result
Expand Down Expand Up @@ -128,7 +129,7 @@ def values_by_day_of_week(self):
# Group by day of the week (0->monday, 7->sunday)
result = qs.annotate(weekday=ExtractIsoWeekDay('timestamp')) \
.values('weekday') \
.annotate(tjm=Sum('times') / 51) \
.annotate(tjm=Sum('times')) \
.values('weekday', 'tjm')

return result
Expand All @@ -144,7 +145,7 @@ def values_by_class(self):
result = qs.annotate(res=Sum('times')).values('res').values('id_category__code').annotate(tjm=Count('id_category__code'))
return result

def tjm_direction_bike(self, categories, direction, weekdays=[0, 1, 2, 3, 4, 5, 6]):
def tjm_direction_bike(self, categories, direction, weekdays=[1, 2, 3, 4, 5, 6, 7]):

qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
Expand All @@ -156,21 +157,23 @@ def tjm_direction_bike(self, categories, direction, weekdays=[0, 1, 2, 3, 4, 5,
)

# TODO: avoid the division?
return qs.aggregate(res=Sum('times'))['res'] / 365
return qs.aggregate(res=Sum('times'))['res'] #/ 365

def total(self, categories=[1]):
def total(self, categories=[1, 2, 3, 4, 5]):

qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
timestamp__year=self.year,
id_category__code__in=categories,
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
)

return qs.aggregate(res=Sum('times'))['res']

def max_day(self, categories=[1]):
def max_day(self, categories=[1, 2, 3, 4, 5]):

qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
timestamp__year=self.year,
id_category__code__in=categories,
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
Expand All @@ -179,9 +182,10 @@ def max_day(self, categories=[1]):

return qs[0]['total'], qs[0]['date']

def max_month(self, categories=[1]):
def max_month(self, categories=[1, 2, 3, 4, 5]):

qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
timestamp__year=self.year,
id_category__code__in=categories,
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
Expand All @@ -190,9 +194,10 @@ def max_month(self, categories=[1]):

return qs[0]['total'], qs[0]['month']

def min_month(self, categories=[1]):
def min_month(self, categories=[1, 2, 3, 4, 5]):

qs = CountDetail.objects.filter(
id_lane__id_section__id=self.section_id,
timestamp__year=self.year,
id_category__code__in=categories,
import_status=definitions.IMPORT_STATUS_DEFINITIVE,
Expand Down Expand Up @@ -243,7 +248,7 @@ def run(self):
lanes = Lane.objects.filter(id_installation=count.id_installation)

ws['B13'] = lanes[0].direction_desc
if(len(lanes) > 1):
if (len(lanes) > 1):
ws['B14'] = lanes[1].direction_desc

ws['B11'] = lanes[0].id_section.place_name
Expand Down Expand Up @@ -271,10 +276,10 @@ def run(self):

ws = workbook['CV_LV']

ws['F11'] = self.tjm_direction_bike([1], 1, weekdays=[0, 1, 2, 3, 4])
ws['G11'] = self.tjm_direction_bike([1], 2, weekdays=[0, 1, 2, 3, 4])
ws['H11'] = self.tjm_direction_bike([2, 3, 4, 5], 1, weekdays=[0, 1, 2, 3, 4])
ws['I11'] = self.tjm_direction_bike([2, 3, 4, 5], 2, weekdays=[0, 1, 2, 3, 4])
ws['F11'] = self.tjm_direction_bike([1], 1, weekdays=[1, 2, 3, 4, 5])
ws['G11'] = self.tjm_direction_bike([1], 2, weekdays=[1, 2, 3, 4, 5])
ws['H11'] = self.tjm_direction_bike([2, 3, 4, 5], 1, weekdays=[1, 2, 3, 4, 5])
ws['I11'] = self.tjm_direction_bike([2, 3, 4, 5], 2, weekdays=[1, 2, 3, 4, 5])

ws['F12'] = self.tjm_direction_bike([1], 1)
ws['G12'] = self.tjm_direction_bike([1], 2)
Expand Down Expand Up @@ -355,7 +360,7 @@ def run(self):
row_offset = 37
column_offset = 3

data = self.values_by_hour_and_direction(1, [5, 6])
data = self.values_by_hour_and_direction(1, [6, 7])
row = row_offset
for i in data:
ws.cell(
Expand All @@ -368,7 +373,7 @@ def run(self):
row_offset = 37
column_offset = 4

data = self.values_by_hour_and_direction(2, [5, 6])
data = self.values_by_hour_and_direction(2, [6, 7])
row = row_offset
for i in data:
ws.cell(
Expand All @@ -386,17 +391,16 @@ def run(self):
row = row_offset
for i in data:
ws.cell(
row=row,
row=row_offset + i['id_category__code'],
column=column_offset,
value=i['tjm']
)
row += 1

ws = workbook['AN_GR']
ws.print_area = 'A1:Z62'
# ws = workbook['AN_GR']
# ws.print_area = 'A1:Z62'

ws = workbook['CAT']
ws.print_area = 'A1:Z62'
# ws = workbook['CAT']
# ws.print_area = 'A1:Z62'

# Save the file
output = os.path.join(
Expand Down