def generate_vector_numeric_map(self, numeric_property): """Generate stops array for use with match expression in mapbox template""" vector_stops = [] function_type = getattr(self, '{}_function_type'.format(numeric_property)) lookup_property = getattr(self, '{}_property'.format(numeric_property)) numeric_stops = getattr(self, '{}_stops'.format(numeric_property)) default = getattr(self, '{}_default'.format(numeric_property)) if function_type == 'match': match_width = numeric_stops # if join data specified as filename or URL, parse JSON to list of Python dicts if type(self.data) == str: self.data = geojson_to_dict_list(self.data) for row in self.data: # map value to JSON feature using the numeric property value = numeric_map(row[lookup_property], numeric_stops, default) # link to vector feature using data_join_property (from JSON object) vector_stops.append([row[self.data_join_property], value]) return vector_stops
def generate_vector_color_map(self): """Generate color stops array for use with match expression in mapbox template""" vector_stops = [] # if join data specified as filename or URL, parse JSON to list of Python dicts if type(self.data) == str: self.data = geojson_to_dict_list(self.data) # loop through features in self.data to create join-data map for row in self.data: # map color to JSON feature using color_property color = color_map(row[self.color_property], self.color_stops, self.color_default) # link to vector feature using data_join_property (from JSON object) vector_stops.append([row[self.data_join_property], color]) return vector_stops
def test_geojson_to_dict_list_invalid(): """Ensure data converted to Python dict""" with pytest.raises(SourceDataError): geojson_to_dict_list(0)
def test_geojson_to_dict_list_url(): """Ensure data converted to Python dict""" data = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/tests/points.geojson' assert type(geojson_to_dict_list(data)) == list
def test_geojson_to_dict_list_file(): """Ensure data converted to Python dict""" data = 'tests/points.geojson' assert type(geojson_to_dict_list(data)) == list
def test_geojson_to_dict_list_json(df): """Ensure data converted to Python dict""" data = json.loads(df.to_json(orient='records')) assert type(geojson_to_dict_list(data)) == list
def create_html(self, filename=None): """Create a circle visual from a geojson data source""" if isinstance(self.style, str): style = "'{}'".format(self.style) else: style = self.style options = dict( gl_js_version=GL_JS_VERSION, accessToken=self.access_token, div_id=self.div_id, style=style, center=list(self.center), zoom=self.zoom, geojson_data=json.dumps(self.data, ensure_ascii=False), belowLayer=self.below_layer, opacity=self.opacity, minzoom=self.min_zoom, maxzoom=self.max_zoom, pitch=self.pitch, bearing=self.bearing, boxZoomOn=json.dumps(self.box_zoom_on), doubleClickZoomOn=json.dumps(self.double_click_zoom_on), scrollZoomOn=json.dumps(self.scroll_zoom_on), touchZoomOn=json.dumps(self.touch_zoom_on), popupOpensOnHover=self.popup_open_action == 'hover', includeSnapshotLinks=self.add_snapshot_links, preserveDrawingBuffer=json.dumps(self.add_snapshot_links), showScale=self.scale, scaleUnits=self.scale_unit_system, scaleBorderColor=self.scale_border_color, scalePosition=self.scale_position, scaleFillColor=self.scale_background_color, scaleTextColor=self.scale_text_color, ) if self.legend: if all([ self.legend, self.legend_gradient, self.legend_function == 'radius' ]): raise LegendError(' '.join([ 'Gradient legend format not compatible with a variable radius legend.', 'Please either change `legend_gradient` to False or `legend_function` to "color".' ])) options.update( showLegend=self.legend, legendLayout=self.legend_layout, legendFunction=self.legend_function, legendStyle=self.legend_style, # reserve for custom CSS legendGradient=json.dumps(self.legend_gradient), legendFill=self.legend_fill, legendHeaderFill=self.legend_header_fill, legendTextColor=self.legend_text_color, legendNumericPrecision=json.dumps( self.legend_text_numeric_precision), legendTitleHaloColor=self.legend_title_halo_color, legendKeyShape=self.legend_key_shape, legendKeyBordersOn=json.dumps(self.legend_key_borders_on)) if self.vector_source: options.update(vectorUrl=self.vector_url, vectorLayer=self.vector_layer_name, vectorJoinDataProperty=self.vector_join_property, joinData=json.dumps(False), dataJoinProperty=self.data_join_property, enableDataJoin=not self.disable_data_join) data = geojson_to_dict_list(self.data) if bool(data): options.update(joinData=json.dumps(data, ensure_ascii=False)) if self.label_property is None: options.update(labelProperty=None) else: options.update(labelProperty='{' + self.label_property + '}') options.update(labelColor=self.label_color, labelSize=self.label_size, labelHaloColor=self.label_halo_color, labelHaloWidth=self.label_halo_width) self.add_unique_template_variables(options) if filename: html = templates.format(self.template, **options) with codecs.open(filename, "w", "utf-8-sig") as f: f.write(html) return None else: return templates.format(self.template, **options)
def create_html(self, filename=None): """Create a circle visual from a geojson data source""" if isinstance(self.style, str): style = "'{}'".format(self.style) else: style = self.style options = dict( gl_js_version=GL_JS_VERSION, accessToken=self.access_token, div_id=self.div_id, style=style, center=list(self.center), zoom=self.zoom, geojson_data=json.dumps(self.data, ensure_ascii=False), belowLayer=self.below_layer, opacity=self.opacity, minzoom=self.min_zoom, maxzoom=self.max_zoom, pitch=self.pitch, bearing=self.bearing, boxZoomOn=json.dumps(self.box_zoom_on), doubleClickZoomOn=json.dumps(self.double_click_zoom_on), scrollZoomOn=json.dumps(self.scroll_zoom_on), touchZoomOn=json.dumps(self.touch_zoom_on) ) if self.legend: options.update( showLegend=self.legend, legendLayout=self.legend_layout, legendStyle=self.legend_style, # reserve for custom CSS legendGradient=json.dumps(self.legend_gradient), legendFill=self.legend_fill, legendHeaderFill=self.legend_header_fill, legendTextColor=self.legend_text_color, legendNumericPrecision=json.dumps(self.legend_text_numeric_precision), legendTitleHaloColor=self.legend_title_halo_color, legendKeyShape=self.legend_key_shape, legendKeyBordersOn=json.dumps(self.legend_key_borders_on) ) if self.vector_source: options.update( vectorUrl=self.vector_url, vectorLayer=self.vector_layer_name, vectorJoinDataProperty=self.vector_join_property, joinData=json.dumps(False), dataJoinProperty=self.data_join_property, enableDataJoin=not self.disable_data_join ) data = geojson_to_dict_list(self.data) if bool(data): options.update(joinData=json.dumps(data, ensure_ascii=False)) if self.label_property is None: options.update(labelProperty=None) else: options.update(labelProperty='{' + self.label_property + '}') options.update( labelColor=self.label_color, labelSize=self.label_size, labelHaloColor=self.label_halo_color, labelHaloWidth=self.label_halo_width ) self.add_unique_template_variables(options) if filename: html = templates.format(self.template, **options) with codecs.open(filename, "w", "utf-8-sig") as f: f.write(html) return None else: return templates.format(self.template, **options)