Skip to content

Commit

Permalink
Merge 61d3d4c into 437078d
Browse files Browse the repository at this point in the history
  • Loading branch information
zoharby committed Aug 25, 2018
2 parents 437078d + 61d3d4c commit 53ad1ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 19 additions & 1 deletion mapboxgl/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import codecs

from .colors import color_ramps, common_html_colors
from chroma import Color, Scale
import geojson
Expand All @@ -20,7 +22,7 @@ def row_to_geojson(row, lon, lat, precision):


def df_to_geojson(df, properties=None, lat='lat', lon='lon', precision=6, filename=None):
"""Serialize a Pandas dataframe to a geojson format Python dictionary
"""Serialize a Pandas dataframe to a geojson format Python dictionary / file
"""

if not properties:
Expand Down Expand Up @@ -64,6 +66,22 @@ def df_to_geojson(df, properties=None, lat='lat', lon='lon', precision=6, filena
return geojson.FeatureCollection(features)


def gdf_to_geojson(gdf, properties=None, filename=None):
"""Serialize a GeoPandas dataframe to a geojson format Python dictionary / file
"""

gdf_out = gdf[['geometry'] + properties or []]

geojson_str = gdf_out.to_json()

if filename:
with codecs.open(filename, "w", "utf-8-sig") as f:
f.write(geojson_str)
return None
else:
return json.loads(geojson_str)


def scale_between(minval, maxval, numStops):
""" Scale a min and max value to equal interval domain with
numStops discrete values
Expand Down
11 changes: 9 additions & 2 deletions mapboxgl/viz.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import codecs
import json
import os

Expand Down Expand Up @@ -136,7 +137,7 @@ def show(self, **kwargs):
def add_unique_template_variables(self, options):
pass

def create_html(self):
def create_html(self, filename=None):
"""Create a circle visual from a geojson data source"""
if isinstance(self.style, str):
style = "'{}'".format(self.style)
Expand Down Expand Up @@ -179,7 +180,13 @@ def create_html(self):

self.add_unique_template_variables(options)

return templates.format(self.template, **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)


class CircleViz(MapViz):
Expand Down

0 comments on commit 53ad1ab

Please sign in to comment.