TGMap

A TGMap object manages the display of a map that shows the locations of the barriers in a project. The constructor is passed a reference to Project object that has barrier definitions.

Attributes:
  • map

    a Bokeh figure object, with x and y ranges defined by the locations of the barriers

  • dots

    a dictionary that maps region names to a list of circle glyphs for each barrier in a region

  • ranges

    a data frame that has the range of x and y coordinates for each region

Source code in src/tidegates/widgets.py
35
36
37
def __init__(self, bf):
    self.map, self.dots = self._create_map(bf)
    self.ranges = self._create_ranges(bf)

_create_map(bf)

Hidden method, called by the constructor to create a Bokeh figure based on the latitude and longitude of the barriers in a project.

src/tidegates/widgets.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def _create_map(self, bf):
    """
    Hidden method, called by the constructor to create a Bokeh figure 
    based on the latitude and longitude of the barriers in
    a project.
    """
    self.tile_provider = get_provider(xyz.OpenStreetMap.Mapnik)
    p = bk.figure(
        title='Oregon Coast', 
        height=900,
        width=400,
        x_range=(bf.map_info.x.min()*0.997,bf.map_info.x.max()*1.003), 
        y_range=(bf.map_info.y.min()*0.997,bf.map_info.y.max()*1.003),
        x_axis_type='mercator',
        y_axis_type='mercator',
        toolbar_location='below',
        tools=['pan','wheel_zoom','hover','reset'],
        tooltips = [
            ("ID", "@id"),
            ("Region", "@region"),
            ("Type", "@type"),
        ]
    )
    p.add_tile(self.tile_provider)
    p.toolbar.autohide = True
    dots = { }
    for r in bf.regions:
        df = bf.map_info[bf.map_info.region == r]
        c = p.circle('x', 'y', size=5, color='darkslategray', source=df, tags=list(df.id))
        dots[r] = c
        c.visible = False

    self.outer_x = (bf.map_info.x.min()*0.997,bf.map_info.x.max()*1.003)
    self.outer_y = (bf.map_info.y.min()*0.997,bf.map_info.y.max()*1.003)

    return p, dots

_create_ranges(df)

Hidden method, called by the constructor to create a Pandas Dataframe containing the range of latitudes and longitudes of the barriers in a project.

src/tidegates/widgets.py
79
80
81
82
83
84
85
86
87
88
89
90
91
def _create_ranges(self, df):
    """
    Hidden method, called by the constructor to create a Pandas Dataframe 
    containing the range of latitudes and longitudes of the barriers in
    a project.
    """
    g = df.map_info.groupby('region')
    return pd.DataFrame({
        'x_min': g.min().x,
        'x_max': g.max().x,
        'y_min': g.min().y,
        'y_max': g.max().y,
    })

display_regions(selection)

This method is called when the user clicks the checkbox next to the name of a region. Set the visible attribute of each dot to True or False depending on whether the region it is in is selected.

Parameters:
  • selection

    a list of names of regions currently selected

src/tidegates/widgets.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def display_regions(self, selection):
    """
    This method is called when the user clicks the checkbox next to the name
    of a region.  Set the visible attribute of each dot to True or False depending
    on whether the region it is in is selected.

    Arguments:
      selection:  a list of names of regions currently selected
    """
    for r, dots in self.dots.items():
        dots.visible = r in selection

zoom(selection)

Update the map, setting the x and y range based on the currently selected regions.

Parameters:
  • selection

    a list of names of regions currently selected

src/tidegates/widgets.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def zoom(self, selection):
    """
    Update the map, setting the x and y range based on the currently selected
    regions.

    Arguments:
      selection:  a list of names of regions currently selected
    """
    if len(selection) > 0:
        xmin = min([self.ranges['x_min'][r] for r in selection])
        xmax = max([self.ranges['x_max'][r] for r in selection])
        ymin = min([self.ranges['y_min'][r] for r in selection])
        ymax = max([self.ranges['y_max'][r] for r in selection])

        mx = (xmax+xmin)/2
        my = (ymax+ymin)/2
        dx = max(5000, xmax - xmin)
        dy = max(5000, ymax - ymin)
        ar = self.map.height / self.map.width

        if dy / dx > ar:
            dx = dy / ar
        else:
            dy = dx * ar

        self.map.x_range.update(start=mx-dx/2-5000, end=mx+dx/2+5000)
        self.map.y_range.update(start=my-dy/2, end=my+dy/2)
    else:
        self.map.x_range.update(start=self.outer_x[0], end=self.outer_x[1])
        self.map.y_range.update(start=self.outer_y[0], end=self.outer_y[1])
    self.map.add_tile(self.tile_provider)