python-visualization / folium

Python Data. Leaflet.js Maps.
https://python-visualization.github.io/folium/
MIT License
6.91k stars 2.22k forks source link

geo_json data_out path issue #235

Closed oztalha closed 8 years ago

oztalha commented 9 years ago

I would like both the create_map's html and the data_out's json file to be created in maps/ folder instead of the current working directory. Currently this is not possible because when I data_out = 'maps/xyz.json' and also create_map(path='maps/xyz.html'), then the produced html source has .defer(d3.json, 'maps/xyz.json') (and the html file cannot find 'maps/xyz.json because itself is inside maps/ too). Instead that part of the javascript in the html file should've been .defer(d3.json, 'xyz.json').

So, my suggestion to fix this issue is that folium should consider the data_out's path, relative to create_html's path. To achieve this, the path passed to create_html needs to be parsed and the generated json file should be saved relative to this path. So, if the data_out is only a filename (without path/directory) then it should be placed in the same directory where html file is generated.

ocefpaf commented 8 years ago

Hi @oztalha,

We are focused on getting folium 0.2.0 ready. So we don't have the manpower to investigate this.

Can you:

  1. provide a sscce showing the problem;
  2. check if latest master does not fix that already;
  3. start a PR with your ideas above.

Thanks!

jthornbrue commented 8 years ago

I'm also having issues with the data_out path. The problem with the current implementation is that I need to run the webserver and the python script from the same directory so that they both use the same relative path. This is not how I have my project set up, so it's restrictive.

I recommend solving this problem by eliminating the json data file and embedding the data in the html file. In conjunction with geo_str, this embeds all the necessary data in the html file and eliminates the need to run a webserver.

BibMartin commented 8 years ago

@jthornbrue Thank you for you comment.

I recommend solving this problem by eliminating the json data file and embedding the data in the html file.

This is what we've been doing in the coming v0.2, while keeping the possibility not to embed the data. Here's a copy of GeoJson dcostring:

    Creates a GeoJson plugin to append into a map with
    Map.add_plugin.
    Parameters
    ----------
    data: file, dict or str.
        The GeoJSON data you want to plot.
        * If file, then data will be read in the file and fully
          embedded in Leaflet's JavaScript.
        * If dict, then data will be converted to JSON and embedded
          in the JavaScript.
        * If str, then data will be passed to the JavaScript as-is.
    style_function: function, default None
        A function mapping a GeoJson Feature to a style dict.
    Examples
    --------
    >>> # Providing file that shall be embeded.
    >>> GeoJson(open('foo.json'))
    >>> # Providing filename that shall not be embeded.
    >>> GeoJson('foo.json')
    >>> # Providing dict.
    >>> GeoJson(json.load(open('foo.json')))
    >>> # Providing string.
    >>> GeoJson(open('foo.json').read())
    >>> # Providing a style_function that put all states in green, but Alabama in blue.
    >>> style_function=lambda x: {'fillColor': '#0000ff' if x['properties']['name']=='Alabama' else '#00ff00'}
    >>> GeoJson(geojson, style_function=style_function)

I hope v0.2 will be out soon ; but you can clone the master in case of hurry.

ocefpaf commented 8 years ago

Since this is implemented in the next release I am closing this. Please reopen if the problem re-surfaces.