def import_score(headers, score_row): """Create a score.""" areas = Area.objects.filter(ident=score_row[headers.index('OWMIDENT')]) areas = areas.filter(data_set__name__in=WATERBEHEERDER_DATASET.values) area = None if areas.exists(): area = areas[0] measuring_rod_obj = MeasuringRod.objects.get( code=score_row[headers.index('DOMGWCOD')]) score = Score(area=area, area_ident=area.ident, measuring_rod=measuring_rod_obj) score.save() return score
def insert(self, krw_scores): """Insert new measures.""" count = 0 for krw_score in krw_scores: score = Score(**krw_score.scoreAsDict()) scores = Score.objects.filter( **{'measuring_rod': score.measuring_rod, 'area': score.area}) scoreid = krw_score.identificatie.scoreid if scores.exists(): logger.warning("Score id {} exists.".format(scoreid)) continue try: logger.debug("insert score {0} for area {1}.".format( krw_score.chemischeStof.parameter, score.area.ident)) score.save() count += 1 except: logger.exception("Cannot insert score {}.".format(scoreid)) logger.info("Parsed scores {}.".format(len(krw_scores))) logger.info("Inserted scores {}.".format(count))
def get(self, request, *args, **kwargs): """ Draw the EKR graph """ dt_start, dt_end = self._dt_from_request() graph_items, graph_settings = self._graph_items_from_request() graph = DateGridGraph( width=int(graph_settings['width']), height=int(graph_settings['height'])) # # Legend. Must do this before using graph location calculations # legend_handles = [ # Line2D([], [], color=value_to_html_color(0.8), lw=10), # Line2D([], [], color=value_to_html_color(0.6), lw=10), # Line2D([], [], color=value_to_html_color(0.4), lw=10), # Line2D([], [], color=value_to_html_color(0.2), lw=10), # Line2D([], [], color=value_to_html_color(0.0), lw=10), # ] # legend_labels = [ # 'Zeer goed', 'Goed', 'Matig', 'Ontoereikend', 'Slecht'] # graph.legend(legend_handles, legend_labels, legend_location=6) yticklabels = [] block_width = (date2num(dt_end) - date2num(dt_start)) / 50 # Legend #graph.margin_right_extra += 90 # Room for legend. See also nens_graph. legend_handles = [ Line2D([], [], color=COLOR_1, lw=10), Line2D([], [], color=COLOR_2, lw=10), Line2D([], [], color=COLOR_3, lw=10), Line2D([], [], color=COLOR_4, lw=10), Line2D([], [], color=COLOR_5, lw=10), ] legend_labels = [ 'slecht', 'ontoereikend', 'matig', 'goed', 'zeer goed', ] graph.legend(legend_handles, legend_labels, legend_location=7) for index, graph_item in enumerate(graph_items): if not graph_item.location: graph_item.location = graph_settings['location'] # Find the corresponding Score. score = Score.from_graph_item(graph_item) if score.id is None: graph_item.label = '(%s)' % graph_item.label yticklabels.append(graph_item.label) # We want to draw a shadow past the end of the last # event. That's why we ignore dt_start. try: ts = graph_item.time_series(dt_end=dt_end, with_comments=True) except: logger.exception( 'HorizontalBarView crashed on graph_item.time_series of %s' % graph_item) ts = {} if len(ts) != 1: logger.warn('Warning: drawing %d timeseries on a single bar ' 'HorizontalBarView', len(ts)) # We assume there is only one timeseries. for (loc, par, unit), single_ts in ts.items(): dates, values, comments, flag_dates, flag_values, flag_comments = ( dates_values_comments(single_ts)) if not dates: logger.warning('Tried to draw empty timeseries %s %s', loc, par) continue block_dates = [] block_dates_shadow = [] for date_index in range(len(dates) - 1): dist_to_next = (date2num(dates[date_index + 1]) - date2num(dates[date_index])) this_block_width = min(block_width, dist_to_next) block_dates.append( (date2num(dates[date_index]), this_block_width)) block_dates_shadow.append( (date2num(dates[date_index]), dist_to_next)) block_dates.append( (date2num(dates[-1]), block_width)) # Ignoring tzinfo, otherwise we can't compare. last_date = max(dt_start.replace(tzinfo=None), dates[-1]) block_dates_shadow.append( (date2num(last_date), (date2num(dt_end) - date2num(dt_start)))) a, b, c, d = score.borders block_colors = [comment_to_html_color(comment) for comment in comments] # Block shadow graph.axes.broken_barh( block_dates_shadow, (index - 0.2, 0.4), facecolors=block_colors, edgecolors=block_colors, alpha=0.2) # The 'real' block graph.axes.broken_barh( block_dates, (index - 0.4, 0.8), facecolors=block_colors, edgecolors='grey') # for goal in graph_item.goals.all(): # collected_goal_timestamps.update([goal.timestamp, ]) # For each unique bar goal timestamp, generate a mini # graph. The graphs are ordered by timestamp. goal_timestamps = [ datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2027, 1, 1, 0, 0), ] subplot_numbers = [312, 313] for index, goal_timestamp in enumerate(goal_timestamps): axes_goal = graph.figure.add_subplot(subplot_numbers[index]) axes_goal.set_yticks(range(len(yticklabels))) axes_goal.set_yticklabels('') axes_goal.set_xticks([0, ]) axes_goal.set_xticklabels([goal_timestamp.year, ]) for graph_item_index, graph_item in enumerate(graph_items): # TODO: make more efficient; score is retrieved twice # in this function. score = Score.from_graph_item(graph_item) #print 'score: %s' % score #print 'doel scores: %s' % str(score.targets) #a, b, c, d = score.borders goal = score.targets[index] if goal is not None: axes_goal.broken_barh( [(-0.5, 1)], (graph_item_index - 0.4, 0.8), facecolors=comment_to_html_color(goal), edgecolors='grey') # # 0 or 1 items # goals = graph_item.goals.filter(timestamp=goal_timestamp) # for goal in goals: # axes_goal.broken_barh( # [(-0.5, 1)], (graph_item_index - 0.4, 0.8), # facecolors=value_to_html_color(goal.value), # edgecolors='grey') axes_goal.set_xlim((-0.5, 0.5)) axes_goal.set_ylim(-0.5, len(yticklabels) - 0.5) # Coordinates are related to the graph size - not graph 311 bar_width_px = 12 axes_x = float(graph.width - (graph.MARGIN_RIGHT + graph.margin_right_extra) + bar_width_px + 2 * bar_width_px * index ) / graph.width axes_y = float(graph.MARGIN_BOTTOM + graph.margin_bottom_extra) / graph.height axes_width = float(bar_width_px) / graph.width axes_height = float(graph.graph_height()) / graph.height axes_goal.set_position((axes_x, axes_y, axes_width, axes_height)) graph.axes.set_yticks(range(len(yticklabels))) graph.axes.set_yticklabels(yticklabels) graph.axes.set_xlim(date2num((dt_start, dt_end))) graph.axes.set_ylim(-0.5, len(yticklabels) - 0.5) # Set the margins, including legend. graph.set_margins() return graph.png_response( response=HttpResponse(content_type='image/png'))
def krw_waterbody_ekr_scores( request, area_ident, horizontal_bar_graph_slug='ekr-extended', template='lizard_measure/waterbody_ekr_scores.html'): """ Show screen for ekr scores. A HorizontalBarGraph with slug 'ekr-extended' must be defined. """ area = get_object_or_404(Area, ident=area_ident) location = None try: location = GeoLocationCache.objects.get(ident=area_ident) except GeoLocationCache.DoesNotExist: pass hor_bar_graph = HorizontalBarGraph.objects.get( slug=horizontal_bar_graph_slug) graph_items = hor_bar_graph.horizontalbargraphitem_set.all() for graph_item in graph_items: if not graph_item.location and location: graph_item.location = location ekr_scores = [] try: ekr_scores = [(graph_item.time_series(with_comments=True), Score.from_graph_item(graph_item), graph_item) for graph_item in graph_items] except AttributeError: # Occurs when above location = # GeoLocationCache... fails... just return nothing. pass score_tables = [] for ts, score, graph_item in ekr_scores: new_score_table = { 'title': str(graph_item), 'score': score, 'data': []} # We assume there is only one. len_ts_values = len(ts.values()) if len_ts_values != 1: logger.error('Number of TimeSeries for HorizontalBarGraphItem %s is %d' % ( graph_item, len_ts_values)) if len_ts_values == 0: new_score_table['data'] = [{'timestamp': 'Geen tijdreeks beschikbaar', 'value': None}] # a, b, c, d = score.borders for single_ts in ts.values(): data_table = [] for timestamp, (value, flag, comment) in single_ts.get_events(): # value = math.trunc(10 * value) / 10.0 # Floor at 1 decimal data_table.append({'timestamp': timestamp, 'value': value, 'color': comment_to_html_color(comment), 'comment': comment}) new_score_table['data'] = data_table new_score_table['color_target_2015'] = comment_to_html_color(score.target_2015) new_score_table['color_target_2027'] = comment_to_html_color(score.target_2027) score_tables.append(new_score_table) return render_to_response( template, {'waterbody': area, 'score_tables': score_tables, 'COLOR_1': COLOR_1, 'COLOR_2': COLOR_2, 'COLOR_3': COLOR_3, 'COLOR_4': COLOR_4, 'COLOR_5': COLOR_5, }, context_instance=RequestContext(request))