Skip to content

Plugin

VersionedTiledMapPlugin

Bases: SingletonPlugin

Map plugin which uses the versioned-datastore-tile-server to render a map of the data in a resource.

Source code in ckanext/tiledmap/plugin.py
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class VersionedTiledMapPlugin(SingletonPlugin):
    """
    Map plugin which uses the versioned-datastore-tile-server to render a map of the
    data in a resource.
    """

    implements(interfaces.IConfigurer)
    implements(interfaces.IBlueprint, inherit=True)
    implements(interfaces.ITemplateHelpers)
    implements(interfaces.IResourceView, inherit=True)
    implements(interfaces.IConfigurable)
    if status_available:
        implements(IStatus)

    # from IConfigurer interface
    def update_config(self, config):
        """
        Add our various resources and template directories to the list of available
        ones.
        """
        toolkit.add_template_directory(config, 'theme/templates')
        toolkit.add_public_directory(config, 'theme/public')
        toolkit.add_resource('theme/assets', 'tiledmap')

    ## IBlueprint
    def get_blueprint(self):
        return routes.blueprints

    # from ITemplateHelpers interface
    def get_helpers(self):
        """
        Add a template helper for formatting mustache templates server side.
        """
        return {'mustache': mustache_wrapper, 'dwc_field_title': dwc_field_title}

    # from IConfigurable interface
    def configure(self, config):
        plugin_config.update(config)

    # from IResourceView interface
    def info(self):
        """
        Return generic info about the plugin.
        """
        return {
            'name': 'versioned_tiledmap',
            'title': 'Map',
            'schema': {
                # plot settings
                'enable_plot_map': [ignore_empty, boolean_validator],
                'plot_point_radius': [ignore_empty, int],
                'plot_point_colour': [ignore_empty, validators.colour_validator],
                'plot_border_width': [ignore_empty, int],
                'plot_border_colour': [ignore_empty, validators.colour_validator],
                # gridded settings
                'enable_grid_map': [ignore_empty, boolean_validator],
                'gridded_grid_resolution': [ignore_empty, int],
                'gridded_cold_colour': [ignore_empty, validators.colour_validator],
                'gridded_hot_colour': [ignore_empty, validators.colour_validator],
                'gridded_range_size': [ignore_empty, int],
                # heatmap settings
                'enable_heat_map': [ignore_empty, boolean_validator],
                'heatmap_point_radius': [ignore_empty, int],
                'heatmap_cold_colour': [ignore_empty, validators.colour_validator],
                'heatmap_hot_colour': [ignore_empty, validators.colour_validator],
                'heatmap_intensity': [ignore_empty, validators.float_01_validator],
                # utfgrid settings
                'enable_utf_grid': [ignore_empty, boolean_validator],
                'utf_grid_title': [ignore_empty, validators.is_datastore_field],
                'utf_grid_fields': [ignore_empty, validators.is_datastore_field],
                # other settings
                'repeat_map': [ignore_empty, boolean_validator],
                'overlapping_records_view': [ignore_empty, validators.is_view_id],
                '__extras': [ignore_empty],
            },
            'icon': 'compass',
            'iframed': True,
            'filterable': True,
            'preview_enabled': False,
            'full_page_edit': False,
        }

    # from IResourceView interface
    def view_template(self, context, data_dict):
        return 'map_view.html'

    # from IResourceView interface
    def form_template(self, context, data_dict):
        return 'map_form.html'

    # from IResourceView interface
    def can_view(self, data_dict):
        """
        Only datastore resources can use this view and they have to have both latitude
        and longitude field names set.
        """
        required_fields = ['datastore_active', '_latitude_field', '_longitude_field']
        return all(data_dict['resource'].get(field, False) for field in required_fields)

    # from IResourceView interface
    def setup_template_variables(self, context, data_dict):
        """
        Setup variables available to templates.
        """
        # TODO: Apply variables to appropriate view
        resource = data_dict['resource']
        resource_view = data_dict['resource_view']
        resource_view_id = resource_view.get('id', None)
        # get the names of the fields on this resource in the datastore
        fields = get_resource_datastore_fields(resource['id'])
        # find all the views on this resource currently
        views = toolkit.get_action('resource_view_list')(
            context, {'id': resource['id']}
        )

        # build a list of view options, adding a default view option of no view first
        view_options = [{'text': toolkit._('(None)'), 'value': ''}]
        # then loop through and add the other views
        for view in views:
            # but make sure we don't add this view to the list of options
            if resource_view_id == view['id']:
                continue
            view_options.append({'text': view['title'], 'value': view['id']})

        return {
            'resource_json': json.dumps(resource),
            'resource_view_json': json.dumps(resource_view),
            'map_fields': [{'text': field, 'value': field} for field in fields],
            'available_views': view_options,
            'defaults': plugin_config,
            'is_new': resource_view_id is None,
        }

    ## IStatus
    def modify_status_reports(self, status_reports):
        tileserver_text = get_tileserver_status()

        # report_value should be the same as toolkit._(tileserver_text) but is defined
        # explicitly just in case it's returning something unexpected
        if tileserver_text == 'unknown':
            report_value = toolkit._('unknown')
            tileserver_state = 'neutral'
        elif tileserver_text == 'available':
            report_value = toolkit._('available')
            tileserver_state = 'good'
        else:
            report_value = toolkit._('unavailable')
            tileserver_state = 'bad'

        status_reports.append(
            {
                'label': toolkit._('Maps'),
                'value': report_value,
                'help': toolkit._(
                    'Connection to the server that plots data points on maps'
                ),
                'state': tileserver_state,
            }
        )

        return status_reports

can_view(data_dict)

Only datastore resources can use this view and they have to have both latitude and longitude field names set.

Source code in ckanext/tiledmap/plugin.py
122
123
124
125
126
127
128
def can_view(self, data_dict):
    """
    Only datastore resources can use this view and they have to have both latitude
    and longitude field names set.
    """
    required_fields = ['datastore_active', '_latitude_field', '_longitude_field']
    return all(data_dict['resource'].get(field, False) for field in required_fields)

get_helpers()

Add a template helper for formatting mustache templates server side.

Source code in ckanext/tiledmap/plugin.py
60
61
62
63
64
def get_helpers(self):
    """
    Add a template helper for formatting mustache templates server side.
    """
    return {'mustache': mustache_wrapper, 'dwc_field_title': dwc_field_title}

info()

Return generic info about the plugin.

Source code in ckanext/tiledmap/plugin.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def info(self):
    """
    Return generic info about the plugin.
    """
    return {
        'name': 'versioned_tiledmap',
        'title': 'Map',
        'schema': {
            # plot settings
            'enable_plot_map': [ignore_empty, boolean_validator],
            'plot_point_radius': [ignore_empty, int],
            'plot_point_colour': [ignore_empty, validators.colour_validator],
            'plot_border_width': [ignore_empty, int],
            'plot_border_colour': [ignore_empty, validators.colour_validator],
            # gridded settings
            'enable_grid_map': [ignore_empty, boolean_validator],
            'gridded_grid_resolution': [ignore_empty, int],
            'gridded_cold_colour': [ignore_empty, validators.colour_validator],
            'gridded_hot_colour': [ignore_empty, validators.colour_validator],
            'gridded_range_size': [ignore_empty, int],
            # heatmap settings
            'enable_heat_map': [ignore_empty, boolean_validator],
            'heatmap_point_radius': [ignore_empty, int],
            'heatmap_cold_colour': [ignore_empty, validators.colour_validator],
            'heatmap_hot_colour': [ignore_empty, validators.colour_validator],
            'heatmap_intensity': [ignore_empty, validators.float_01_validator],
            # utfgrid settings
            'enable_utf_grid': [ignore_empty, boolean_validator],
            'utf_grid_title': [ignore_empty, validators.is_datastore_field],
            'utf_grid_fields': [ignore_empty, validators.is_datastore_field],
            # other settings
            'repeat_map': [ignore_empty, boolean_validator],
            'overlapping_records_view': [ignore_empty, validators.is_view_id],
            '__extras': [ignore_empty],
        },
        'icon': 'compass',
        'iframed': True,
        'filterable': True,
        'preview_enabled': False,
        'full_page_edit': False,
    }

setup_template_variables(context, data_dict)

Setup variables available to templates.

Source code in ckanext/tiledmap/plugin.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def setup_template_variables(self, context, data_dict):
    """
    Setup variables available to templates.
    """
    # TODO: Apply variables to appropriate view
    resource = data_dict['resource']
    resource_view = data_dict['resource_view']
    resource_view_id = resource_view.get('id', None)
    # get the names of the fields on this resource in the datastore
    fields = get_resource_datastore_fields(resource['id'])
    # find all the views on this resource currently
    views = toolkit.get_action('resource_view_list')(
        context, {'id': resource['id']}
    )

    # build a list of view options, adding a default view option of no view first
    view_options = [{'text': toolkit._('(None)'), 'value': ''}]
    # then loop through and add the other views
    for view in views:
        # but make sure we don't add this view to the list of options
        if resource_view_id == view['id']:
            continue
        view_options.append({'text': view['title'], 'value': view['id']})

    return {
        'resource_json': json.dumps(resource),
        'resource_view_json': json.dumps(resource_view),
        'map_fields': [{'text': field, 'value': field} for field in fields],
        'available_views': view_options,
        'defaults': plugin_config,
        'is_new': resource_view_id is None,
    }

update_config(config)

Add our various resources and template directories to the list of available ones.

Source code in ckanext/tiledmap/plugin.py
46
47
48
49
50
51
52
53
def update_config(self, config):
    """
    Add our various resources and template directories to the list of available
    ones.
    """
    toolkit.add_template_directory(config, 'theme/templates')
    toolkit.add_public_directory(config, 'theme/public')
    toolkit.add_resource('theme/assets', 'tiledmap')