-
Notifications
You must be signed in to change notification settings - Fork 2.2k
TimeDynamicGeoJson plugin #736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
folium/map.py
Outdated
@@ -37,6 +41,9 @@ | |||
'https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.0.0/leaflet.markercluster.js'), # noqa | |||
] | |||
|
|||
for js in _d3_js: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W291 trailing whitespace
import json | ||
from jinja2 import Template | ||
|
||
from branca.element import MacroElement, Figure, JavascriptLink, CssLink |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
F401 'branca.element.JavascriptLink' imported but unused
F401 'branca.element.Figure' imported but unused
F401 'branca.element.MacroElement' imported but unused
F401 'branca.element.CssLink' imported but unused
>>> GeoJson(geojson, style_function=style_function) | ||
|
||
""" | ||
def __init__(self, data, styledict, style_function=None, name=None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C901 'TimeDynamicGeoJson.init' is too complex (13)
overlay=True, control=True, smooth_factor=None, | ||
highlight_function=None): | ||
super(TimeDynamicGeoJson, self).__init__(name=name, overlay=overlay, | ||
control=control) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E128 continuation line under-indented for visual indent
raise ValueError('Unhandled object {!r}.'.format(data)) | ||
|
||
|
||
self.styledict = styledict |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E303 too many blank lines (2)
|
||
|
||
self.styledict = styledict | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
|
||
# make set of timestamps | ||
self.timestamps = set() | ||
for feature in self.styledict.values(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W291 trailing whitespace
|
||
{% endmacro %} | ||
""") # noqa | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
raise ValueError('Unhandled object {!r}.'.format(data)) | ||
|
||
self.styledict = styledict | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
|
||
{% endmacro %} | ||
""") # noqa | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
overlay=True, control=True, smooth_factor=None, | ||
highlight_function=None): | ||
super(TimeDynamicGeoJson, self).__init__(name=name, overlay=overlay, | ||
control=control) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E128 continuation line under-indented for visual indent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a lot of good ideas here but you need to use the latest master
to avoid conflicts and you can reduce a lot of the code by inheriting from the GeoJson
class instead of Layer
.
folium/map.py
Outdated
@@ -22,6 +22,10 @@ | |||
|
|||
ENV = Environment(loader=PackageLoader('folium', 'templates')) | |||
|
|||
_d3_js = [ | |||
('d3', 'http://d3js.org/d3.v4.min.js'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in the plugin and not in the global map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright.
@@ -0,0 +1,257 @@ | |||
import json | |||
from ..map import Layer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use absolute imports to keep it consistent with the rest of the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
import json | ||
from ..map import Layer | ||
from jinja2 import Template | ||
from branca.utilities import none_min, none_max, iter_points |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are now in folium.utilities
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.
|
||
self.smooth_factor = smooth_factor | ||
|
||
self._template = Template(u""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably inherit from the GeoJson class and save a lot of the duplication by only changing the template.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ocefpaf Good idea!
Looks cool! Thanks for the PR!!
See https://github.com/python-visualization/folium/blob/master/folium/plugins/draw.py#L67-L71
Not really, you can add it to the examples folder in this PR. Remember to also add some tests and an entry to the changelog. |
@ocefpaf Thank you for reviewing the code! I'll implement the changes and push again :) |
var datestring = new Date(parseInt(current_timestamp)*1000).toDateString(); | ||
d3.select("output#slider-value").text(datestring); | ||
fill_map(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W291 trailing whitespace
{% endif %} | ||
).addTo({{this._parent.get_name()}} | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
).addTo({{this._parent.get_name()}} | ||
); | ||
|
||
{{this.get_name()}}.setStyle(function(feature) {feature.properties.style;}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E101 indentation contains mixed spaces and tabs
W191 indentation contains tabs
); | ||
|
||
{{this.get_name()}}.setStyle(function(feature) {feature.properties.style;}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
layer._path.id = 'feature-' + layer.feature.id; | ||
}); | ||
|
||
d3.selectAll('path').attr('stroke', 'white').attr('stroke-width', 0.8).attr('stroke-dasharray', '5,5').attr('fill-opacity', 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (143 > 120 characters)
|
||
d3.selectAll('path').attr('stroke', 'white').attr('stroke-width', 0.8).attr('stroke-dasharray', '5,5').attr('fill-opacity', 0); | ||
fill_map(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W293 blank line contains whitespace
import folium | ||
from branca.colormap import linear | ||
|
||
def test_timedynamic_geo_json(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E302 expected 2 blank lines, found 1
for country in gdf.index: | ||
pdf = pd.DataFrame({'color': np.random.normal(size=n_periods), | ||
'opacity': np.random.normal(size=n_periods)}, | ||
index=dt_index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E127 continuation line over-indented for visual indent
min_color = min(max_color, data['color'].min()) | ||
|
||
cmap = linear.PuRd.scale(min_color, max_color) | ||
norm = lambda x: (x - x.min())/(x.max()-x.min()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E731 do not assign a lambda expression, use a def
).addTo({{this._parent.get_name()}} | ||
); | ||
|
||
{{this.get_name()}}.setStyle(function(feature) {feature.properties.style;}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E101 indentation contains mixed spaces and tabs
W191 indentation contains tabs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mix of space and tabs are a no-no. Can you try to find the tabs and make everything spaces?
for country in gdf.index: | ||
pdf = pd.DataFrame( | ||
{'color': np.random.normal(size=n_periods), | ||
'opacity': np.random.normal(size=n_periods)}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E128 continuation line under-indented for visual indent
#assert expected_timestamps in out | ||
|
||
expected_styledict = json.dumps(styledict).replace('"', "'") | ||
#assert expected_styledict in out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E265 block comment should start with '# '
@ocefpaf I've made the changes that you requested :). Buut:
|
Don't worry about the lints right now. We can fix those later.
It seems that only Python 2.7 is failing. I'll download your code an investigate it locally. |
smooth_factor: float, default None | ||
How much to simplify the polyline on each zoom level. More means | ||
better performance and smoother look, and less means more accurate | ||
representation. Leaflet defaults to 1.0. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably remove all that, refer to the GeoJson
docs, and add only what is different about this plugin
.
).addTo({{this._parent.get_name()}} | ||
); | ||
|
||
{{this.get_name()}}.setStyle(function(feature) {feature.properties.style;}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mix of space and tabs are a no-no. Can you try to find the tabs and make everything spaces?
figure = self.get_root() | ||
assert isinstance(figure, Figure), ('You cannot render this Element ' | ||
'if it is not in a Figure.') | ||
figure.header.add_child(JavascriptLink('https://d3js.org/d3.v4.min.js')) # noqa |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
fill_map(); | ||
}); | ||
|
||
{% if this.highlight %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: unfortunately this repetition is needed b/c of the current design we cannot extend the jinja template.
expected_timestamps = ("var timestamps = ['1454166000', '1456671600', '1459350000', " | ||
"'1461942000', '1464620400', '1467212400', '1469890800', " | ||
"'1472569200', '1475161200', '1477839600'];") | ||
#assert expected_timestamps in out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you planning on un-commenting this part?
I am preparing a PR to your fork with some changes that will help you finish this PR. |
Thank you!
It's a better name, I agree :). |
I was not able to send #741 to your branch, so I created a new PR. I'll try to finish the docs tomorrow and get it merged so you can polish the details if you want. |
Alright, sounds good :)) |
OK. I'll try to finalize this later this week. Thanks! |
In reference to issue #722.
This is a pull request to merge in a plugin called
TimeDynamicGeoJson
.folium/plugins/timedynamic_geo_json.py
.folium/map.py
, but let me know if there's a better place to do this :).Cheers!