5

My goal is to get a so-called "choropleth map" (I guess) of the zip code areas in Germany. I have found the python package "folium" but it seems like it takes a .json file as input:

https://github.com/python-visualization/folium

On OpenStreetMap I only see shp.zip and .osm.pbf files. Inside the shp.zip archive I find all sorts of file endings which I have never heard of but no .json file. How do I use the data from OpenStreetMap to feed folium? Am I running into the wrong direction?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
user3182532
  • 1,097
  • 5
  • 22
  • 37
  • Have not done this before, but am going to need it soon as well. In case it helps have a look at [this](https://www.twilio.com/blog/2017/08/geospatial-analysis-python-geojson-geopandas.html) post on a similar topic. – cardamom Aug 18 '17 at 09:10

3 Answers3

1

If you want to create a choropleth map you must follow these steps:

  1. First you need a file containing info about the regions of that country. A sample .json file has been supplied with this answer, however, there are actually many file formats commonly used for maps. In your case, you need to convert your OSM shape file (.shp) into a more modern file type like .geojson. Thankfully we have ogr2ogr to do this last part:

ogr2ogr -f GeoJSON -t_srs EPSG:4326 -simplify 1000 [name].geojson [name].shp

Advice: You can also extract the administrative borders from these web sites:

* [OSM Boundaries Map 4.2][2]
* [Mapzen][3]
* [Geofabrik][4]  
  1. Download data based on it (a .csv file, for example). Obviously, the file must have a column with the ZIP Codes of that country.

  2. Once you get these files the rest is straightforward, Follium will create the choropleth map automatically.

I wrote a simple example of this about the unemployment rate in the US:

Code:

import folium
import pandas as pd

osm = folium.Map([43, -100], zoom_start=4)

osm.choropleth(
    geo_str = open('US_states.json').read(),
    data = pd.read_csv("US_unemployment.csv"),
    columns = ['State', 'Unemployment'],
    key_on = 'feature.id',
    fill_color = 'YlGn',
)

Output:

enter image description here

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
mforpe
  • 1,549
  • 1
  • 12
  • 22
  • "A sample .json file has been supplied with this answer, but you can create this file from the shape files (.shp) as well." -> that's basically my question: how do you process the openstreetmap data in order to feed it to folium? can you give more details on this part? – user3182532 Aug 18 '17 at 10:58
  • The maps are automatically pulled from OpenStreetMap. Files like `.json` just contain info about borders. Would you like to know about how to convert a `.shp` file into a `.json` file? – mforpe Aug 18 '17 at 11:03
  • yes, I think that is the step that I need to get done. Using folium itself seems very straight forward. I need to get the zip code border information from openstreetmap (whatever file that might be), convert it to .json and then use the code from the tutroial / your post. – user3182532 Aug 18 '17 at 11:15
  • You can extract the administrative border lines with the applet i've posted. There are many ways to do this but most of them are not trivial. – mforpe Aug 18 '17 at 13:32
0

I haven't done this myself but there are various solutions for converting OSM files (.osm or .pbf) to (geo)json. For example osmtogeojson. More tools can be found at the GeoJSON page in the OSM wiki.

scai
  • 20,297
  • 4
  • 56
  • 72
  • so which file do I need to download from openstreetmap? I need one that contains the boundaries of the zip code areas. I can't find it. After that I guess I can try those conversion tools. – user3182532 Aug 18 '17 at 11:41
  • This is a different question. Usually an OSM file contains *all* information available in OSM. If you just want zip codes then either extract them from your OSM file (for example via osmium) or use Overpass API to download only zip codes in the first place. Either way, a comment is not really suited to explain these steps in detail. Also, question about downloading specific OSM data only have already been answered several times (here, at http://gis.stackexchange.com and at help.openstreetmap.org). – scai Aug 18 '17 at 12:33
  • @user3182532: @scai is right, that's a different question. You asked about how to use `.shp` files to feed folium. Extract the administrative border lines from OSM is not trivial. – mforpe Aug 18 '17 at 13:05
0

I went to https://overpass-turbo.eu/ (which retrieves data from openstreetmap via a specific Query Language QL) and hit run on the following code:

[timeout:900];
area[name="Deutschland"][admin_level=2][boundary=administrative]->.myarea;
rel(area.myarea)["boundary"="postal_code"];
out geom;

You can "export to geojson" but in my case that didn't work because it's too much data which cannot be processed inside the browser. But exporting the "raw data" works. So I did that and then I used "osmtogeojson" to get the right format. After that I was able to feed my openstreetmap data to folium as described in the tutorial of folium.


This answer was posted as an edit to the question Choropleth map with OpenStreetMap data by the OP user3182532 under CC BY-SA 3.0.

vvvvv
  • 25,404
  • 19
  • 49
  • 81