Maps (datascience.maps)

Draw maps using folium.

class datascience.maps.Map(features=(), ids=(), width=960, height=500, **kwargs)[source]

A map from IDs to features. Keyword args are forwarded to folium.

color(values, ids=(), key_on='feature.id', palette='YlOrBr', **kwargs)[source]

Color map features by binning values.

values – a sequence of values or a table of keys and values ids – an ID for each value; if none are provided, indices are used key_on – attribute of each feature to match to ids palette – one of the following color brewer palettes:

‘BuGn’, ‘BuPu’, ‘GnBu’, ‘OrRd’, ‘PuBu’, ‘PuBuGn’, ‘PuRd’, ‘RdPu’, ‘YlGn’, ‘YlGnBu’, ‘YlOrBr’, and ‘YlOrRd’.

Defaults from Folium:

threshold_scale: list, default None

Data range for D3 threshold scale. Defaults to the following range of quantiles: [0, 0.5, 0.75, 0.85, 0.9], rounded to the nearest order-of-magnitude integer. Ex: 270 rounds to 200, 5600 to 6000.

fill_opacity: float, default 0.6

Area fill opacity, range 0-1.

line_color: string, default ‘black’

GeoJSON geopath line color.

line_weight: int, default 1

GeoJSON geopath line weight.

line_opacity: float, default 1

GeoJSON geopath line opacity, range 0-1.

legend_name: string, default None

Title for data legend. If not passed, defaults to columns[1].

copy()[source]

Copies the current Map into a new one and returns it.

property features
format(**kwargs)[source]

Apply formatting.

geojson()[source]

Render features as a FeatureCollection.

overlay(feature, color='Blue', opacity=0.6)[source]

Overlays feature on the map. Returns a new Map.

Args:
feature: a Table of map features, a list of map features,

a Map, a Region, or a circle marker map table. The features will be overlayed on the Map with specified color.

color (str): Color of feature. Defaults to ‘Blue’

opacity (float): Opacity of overlain feature. Defaults to

0.6.

Returns:

A new Map with the overlain feature.

classmethod read_geojson(path_or_json_or_string_or_url)[source]

Read a geoJSON string, object, file, or URL. Return a dict of features keyed by ID.

class datascience.maps.Marker(lat, lon, popup='', color='blue', **kwargs)[source]

A marker displayed with Folium’s simple_marker method.

popup – text that pops up when marker is clicked color – The color of the marker. You can use: [‘red’, ‘blue’, ‘green’, ‘purple’, ‘orange’, ‘darkred’, ’lightred’, ‘beige’, ‘darkblue’, ‘darkgreen’, ‘cadetblue’, ‘darkpurple’, ‘white’, ‘pink’, ‘lightblue’, ‘lightgreen’, ‘gray’, ‘black’, ‘lightgray’]

Defaults from Folium:

marker_icon: string, default ‘info-sign’

icon from (http://getbootstrap.com/components/) you want on the marker

clustered_marker: boolean, default False

boolean of whether or not you want the marker clustered with other markers

icon_angle: int, default 0

angle of icon

popup_width: int, default 300

width of popup

The icon can be further customized by by passing in attributes into kwargs by using the attributes listed in https://python-visualization.github.io/folium/modules.html#folium.map.Icon.

copy()[source]

Return a deep copy

draw_on(folium_map)[source]

Add feature to Folium map object.

format(**kwargs)[source]

Apply formatting.

geojson(feature_id)[source]

GeoJSON representation of the marker as a point.

property lat_lons

Sequence of lat_lons that describe a map feature (for zooming).

classmethod map(latitudes, longitudes, labels=None, colors=None, areas=None, other_attrs=None, clustered_marker=False, **kwargs)[source]

Return markers from columns of coordinates, labels, & colors.

The areas column is not applicable to markers, but sets circle areas.

Arguments: (TODO) document all options

clustered_marker: boolean, default False

boolean of whether or not you want the marker clustered with other markers

other_attrs: dictionary of (key) property names to (value) property values, default None

A dictionary that list any other attributes that the class Marker/Circle should have

classmethod map_table(table, clustered_marker=False, **kwargs)[source]

Return markers from the colums of a table.

The first two columns of the table must be the latitudes and longitudes (in that order), followed by ‘labels’, ‘colors’, and/or ‘areas’ (if applicable) in any order with columns explicitly stating what property they are representing.

class datascience.maps.Circle(lat, lon, popup='', color='blue', radius=10, **kwargs)[source]

A marker displayed with Folium’s circle_marker method.

popup – text that pops up when marker is clicked color – fill color radius – pixel radius of the circle

Defaults from Folium:

fill_opacity: float, default 0.6

Circle fill opacity

More options can be passed into kwargs by following the attributes listed in https://leafletjs.com/reference-1.4.0.html#circlemarker.

For example, to draw three circles:

t = Table().with_columns([
        'lat', [37.8, 38, 37.9],
        'lon', [-122, -122.1, -121.9],
        'label', ['one', 'two', 'three'],
        'color', ['red', 'green', 'blue'],
        'radius', [3000, 4000, 5000],
    ])
Circle.map_table(t)
draw_on(folium_map)[source]

Add feature to Folium map object.

class datascience.maps.Region(geojson, **kwargs)[source]

A GeoJSON feature displayed with Folium’s geo_json method.

copy()[source]

Return a deep copy

draw_on(folium_map)[source]

Add feature to Folium map object.

format(**kwargs)[source]

Apply formatting.

geojson(feature_id)[source]

Return GeoJSON with ID substituted.

property lat_lons

A flat list of (lat, lon) pairs.

property polygons

Return a list of polygons describing the region.

  • Each polygon is a list of linear rings, where the first describes the exterior and the rest describe interior holes.

  • Each linear ring is a list of positions where the last is a repeat of the first.

  • Each position is a (lat, lon) pair.

property properties
property type

The GEOJSON type of the regions: Polygon or MultiPolygon.