Skip to content

Commit

Permalink
fix(extension): Add a module for HourlyPlot to VisualizationSet
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed Oct 30, 2022
1 parent 198cf85 commit b51ac77
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 11 deletions.
3 changes: 3 additions & 0 deletions ladybug_display/_extend_ladybug.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from ladybug.compass import Compass
from ladybug.sunpath import Sunpath
from ladybug.windrose import WindRose
from ladybug.hourlyplot import HourlyPlot

# import the extension functions
from .extension.compass import compass_to_vis_set
from .extension.sunpath import sunpath_to_vis_set
from .extension.windrose import wind_rose_to_vis_set
from .extension.hourlyplot import hourly_plot_to_vis_set

# inject the methods onto the classes
Compass.to_vis_set = compass_to_vis_set
Sunpath.to_vis_set = sunpath_to_vis_set
WindRose.to_vis_set = wind_rose_to_vis_set
HourlyPlot.to_vis_set = hourly_plot_to_vis_set
2 changes: 1 addition & 1 deletion ladybug_display/extension/compass.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def from_linesegment2d(line, z, line_width=1):
# generate the labels and tick marks for the azimuths
if custom_angles is None:
for line in compass.major_azimuth_ticks:
result.append(from_linesegment2d(line, z))
result.append(from_linesegment2d(line, z, 2))
for txt, pt in zip(compass.MAJOR_TEXT, compass.major_azimuth_points):
txt_pln = Plane(o=Point3D(pt.x, pt.y, z), x=xaxis)
result.append(
Expand Down
101 changes: 101 additions & 0 deletions ladybug_display/extension/hourlyplot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""Method to draw an HourlyPlot as a VisualizationSet."""
from ladybug_geometry.geometry3d import Plane

from ladybug_display.geometry3d import DisplayLineSegment3D, DisplayPolyline3D, \
DisplayText3D
from ladybug_display.visualization import VisualizationSet, AnalysisGeometry, \
VisualizationData, ContextGeometry


def hourly_plot_to_vis_set(
hourly_plot, z=0, custom_hours=(0, 3, 6, 9, 12, 15, 18, 21, 24),
include_title=True):
"""Get a Ladybug HourlyPlot represented as a VisualizationSet.
Args:
hourly_plot: A Ladybug HourlyPlot object.
z: A number for the Z-coordinate to be used in translation. (Default: 0).
custom_hours: A tuple of integers from 0 to 24 to indicate which hours
of the day should appear in the Hour_Axis of the visualization.
The default is (0, 3, 6, 9, 12, 15, 18, 21, 24).
include_title: Boolean to note whether the title should be included
in the output visualization. (Default: True).
Returns:
A VisualizationSet with the hourly plot represented several ContextGeometries
(and an AnalysisGeometry. This includes these
objects in the following order.
- Hour_Axis -- A ContextGeometry with lines and text for the hour-of-the-day
axis of the hourly plot.
- Month_Axis -- A ContextGeometry with lines and text for the hour-of-the-day
axis of the hourly plot,
- Title -- A ContextGeometry with text for the title of the hourly plot.
This layer will be excluded if include_title is False.
- Analysis_Data -- An AnalysisGeometry for the data on the hourly plot.
"""
# establish the VisualizationSet object
data_header = hourly_plot.data_collection.header
data_type, unit = data_header.data_type, data_header.unit
set_id = 'Hourly_Plot_{}'.format(data_type.name.replace(' ', '_'))
vis_set = VisualizationSet(set_id, ())

# get global variables used in other places
chart_border = DisplayPolyline3D(hourly_plot.chart_border3d, line_width=2)
txt_h = hourly_plot.legend_parameters.text_height
font = hourly_plot.legend_parameters.font
major_hr = hourly_plot.HOUR_LABELS

# add the hour axis
dis_hour, dis_hour_text = [], []
h_lines = hourly_plot.custom_hour_lines3d(custom_hours)
h_pts = hourly_plot.custom_hour_label_points3d(custom_hours)
h_text = hourly_plot.custom_hour_labels(custom_hours)
for hr, lin, pt, txt in zip(custom_hours, h_lines, h_pts, h_text):
if hr in major_hr:
lt, t_sz = 'Continuous', txt_h
else:
lt, t_sz = 'Dotted', txt_h * 0.8
dis_hour.append(DisplayLineSegment3D(lin, line_width=1, line_type=lt))
d_txt = DisplayText3D(txt, Plane(o=pt), t_sz, None, font, 'Right', 'Middle')
dis_hour_text.append(d_txt)
hour_axis = ContextGeometry('Hour_Axis', [chart_border] + dis_hour + dis_hour_text)
hour_axis.display_name = 'Hour Axis'
vis_set.add_geometry(hour_axis)

# add the month axis
dis_month, dis_month_text = [], []
m_lines = hourly_plot.month_lines3d
m_pts = hourly_plot.month_label_points3d
m_text = hourly_plot.month_labels
for lin in m_lines:
dis_month.append(DisplayLineSegment3D(lin))
for pt, txt in zip(m_pts, m_text):
d_txt = DisplayText3D(txt, Plane(o=pt), t_sz, None, font, 'Center', 'Top')
dis_month_text.append(d_txt)
month_axis = ContextGeometry(
'Month_Axis', [chart_border] + dis_month + dis_month_text)
month_axis.display_name = 'Month Axis'
vis_set.add_geometry(month_axis)

if include_title:
tit_txt = DisplayText3D(
hourly_plot.title_text, hourly_plot.lower_title_location, txt_h,
None, font, 'Left', 'Bottom')
title = ContextGeometry('Title', [tit_txt])
title.display_name = 'Title'
vis_set.add_geometry(title)

# add the colored mesh
vis_data = VisualizationData(
hourly_plot.values, hourly_plot.legend_parameters, data_type, unit)
mesh_geo = AnalysisGeometry(
'Analysis_Data', [hourly_plot.colored_mesh3d], [vis_data])
mesh_geo.display_name = data_type.name
mesh_geo.display_mode = 'Surface'
vis_set.add_geometry(mesh_geo)

return vis_set
26 changes: 16 additions & 10 deletions ladybug_display/extension/windrose.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Method to draw a WindRose as a VisualizationSet."""
from ladybug_geometry.geometry2d import Polyline2D
from ladybug_geometry.geometry3d import Point3D, Plane, LineSegment3D, \
Polyline3D, Mesh3D
Expand All @@ -9,12 +10,14 @@
from ladybug_display.extension.compass import compass_to_vis_set


def wind_rose_to_vis_set(windrose, z=0):
def wind_rose_to_vis_set(windrose, z=0, frequency_labels=True):
"""Get a Ladybug WindRose represented as a VisualizationSet.
Args:
windrose: A Ladybug WindRose object.
z: A number for the Z-coordinate to be used in translation. (Default: 0)
z: A number for the Z-coordinate to be used in translation. (Default: 0).
frequency_labels: A boolean to note whether frequency labels should be
included in the output visualization. (Default: True).
Returns:
A VisualizationSet with the wind rose represented several ContextGeometries
Expand Down Expand Up @@ -57,15 +60,18 @@ def wind_rose_to_vis_set(windrose, z=0):
freq_line = [Polyline3D.from_polyline2d(Polyline2D.from_polygon(poly), wr_pln)
for poly in windrose.frequency_lines[:-1]]
dis_freq, freq_text = [], []
f_int = windrose.frequency_hours
txt_h = min((windrose.frequency_spacing_distance / 4, txt_h))
freqs = range(0, f_int * windrose.frequency_intervals_compass, f_int)
for i, (lin, val) in enumerate(zip(freq_line, freqs)):
for lin in freq_line:
dis_freq.append(DisplayPolyline3D(lin, line_type='Dotted'))
if i % 2 == 0 and i != 0:
b_pln = Plane(o=lin.segments[0].midpoint)
d_txt = DisplayText3D(str(val), b_pln, txt_h, None, font, 'Center', 'Bottom')
freq_text.append(d_txt)
if frequency_labels:
f_int = windrose.frequency_hours
txt_h = min((windrose.frequency_spacing_distance / 4, txt_h))
freqs = range(0, f_int * windrose.frequency_intervals_compass, f_int)
for i, (lin, val) in enumerate(zip(freq_line, freqs)):
if i % 2 == 0 and i != 0:
b_pln = Plane(o=lin.segments[0].midpoint)
d_txt = DisplayText3D(str(val), b_pln, txt_h, None, font,
'Center', 'Bottom')
freq_text.append(d_txt)
freq_geo = ContextGeometry('Frequency_Lines', dis_freq + freq_text)
freq_geo.display_name = 'Frequency Lines'
vis_set.add_geometry(freq_geo)
Expand Down

0 comments on commit b51ac77

Please sign in to comment.