python-visualization / folium

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

Option to obfuscate coordinates when rendering to html #1993

Closed lukefullard closed 2 months ago

lukefullard commented 2 months ago

Is your feature request related to a problem? Please describe. When rendering folium maps to html the location data is available to view in the html text. This is usually OK, but occasionally I will make a map with markers and limit the max zoom so users can get a general but not specific idea of a location. I would like to somehow have the location data obfuscated in the html text.

Describe the solution you'd like Ideally there would be an option when exporting a map to html to obfuscate the locations.

Describe alternatives you've considered Have considered serving the map data server side, but this is an additional complexity that I don't want. I like being able to simply host static html.

Implementation I am not a programmer, but can help where I can as a data analyst with relatively good Python skills.

hansthen commented 2 months ago

You could add a little randomness to the data. That way users can have a general idea, but not the specific locations. This is in fact how GPS works for non-military users.

I am not an encryption specialist, but I don't think obfuscation or encryption would work for your purpose. Somewhere in the chain, the data would need to be made available for the computer to read. Users could just use browser tools to inspect the decrypted data.

lukefullard commented 2 months ago

I managed to get this done with the code below:

import base64
with open('test.html', 'rb') as file:
    html_content = file.read()
    encoded_content = base64.b64encode(html_content).decode('utf-8')
html_template = f"""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <script>
        document.write(atob("{encoded_content}"));
    </script>
</body>
</html>
"""

with open('encoded_file.html', 'w') as encoded_file:
    encoded_file.write(html_template)

If it is useful to anyone else then I am happy, or if this type of idea can be integrated into folium at some point that would be cool too.

Limitations of encoding the html in base64 include a) It is only obfuscated, not encoded. If someone tries hard enough they can decode, but for me the extra layer of security is enough. b) The file size becomes slightly larger. In my case it went from ~1 Mb to ~1.4 Mb.

I'm sure the template I use could be modified too, for example, to include a nicer title... but for me it is sufficient :)

Conengmo commented 2 months ago

I think @hansthen's answer is spot on, you'd have to mangle your data before rendering it with Folium. I think that responsibility is outside of the scope of Folium, so I'll go ahead and close this issue. Thanks for sharing your code snippet though @lukefullard! It's useful for others who might want to make it a bit harder for regular folks to see their data. But not something for in the Folium library though. But I appreciate you sharing your use case!