vrijdag 13 september 2019

Geomapping Folium

# -*- coding: utf-8 -*-
"""
Created on Thu Aug 29 09:42:55 2019

@author: wagenerj
"""
import pandas as pd
import folium
import geojson
from shapely.geometry import shape


shapefile2 = 'C://Users//wagenerj//Documents//data//amsterdamgeo//GEBIEDEN22.json'#Read shapefile using Geopandas

# =============================================================================
# https://github.com/python-visualization/folium/blob/master/examples/GeoJSON_and_choropleth.ipynb
# =============================================================================

#Lees GEOJSON file in
with open(shapefile2) as f:
    geo_json_data = geojson.load(f)



# =============================================================================
# Probeer de structuur van de GEOJSON file te begrijpen
#
# GEOJSON ___type (string)
#         ___features (list)
#       
# features is een lijst van  Feature objecten
#
# een Feature is een collectie met de volgende keys
# geometry   (geometry.Polygon)
# id         (int)
# properties (dict)
# type       (str)
#
# EVT TE bekijken via
# for key, value in geo_json_data.features[2].items() :
#     print (key)
#
# Belangrijk is om te weten wat de properties zijn van het feature die kunnen nl elke keer weer een beetje verschillen
# dit kan je opvragen door bijv
# geo_json_data.features[2]['properties']
#
#
# for feature in geo_json_data['features']:
#     print(feature['properties'])
#
# geo_json_data.features[2].properties
# geo_json_data.features[2].properties['Gebied']
# geo_json_data.features[2].properties['Gebied_code']
# =============================================================================


for feature in geo_json_data['features']:
     print(feature['id'])
#    print(feature['properties']['Gebied_code'])
     feature['properties']['centroid_x']=shape(feature['geometry']).centroid.x
     feature['properties']['centroid_y']=shape(feature['geometry']).centroid.y
     print(shape(feature['geometry']).centroid)
# Essentieel we maken hier het feature ID gelijk aan Gebied_code omdat er op gematsch kan worden 
#    feature['id']=feature['properties']['Gebied_code']




m = folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
#simpel
folium.GeoJson(geo_json_data).add_to(m)
m.save("C://Users//wagenerj//Documents//data//amsterdamgeo//m.html" )


m = folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
folium.GeoJson(
    geo_json_data,
    style_function=lambda feature: {
       'fillColor': 'green' if 'e' in feature['properties']['Gebied'].lower() else '#ffff00',
        'color': 'black',
        'weight': 2,
        'dashArray': '5, 5'
    }
).add_to(m)
m.save("C://Users//wagenerj//Documents//data//amsterdamgeo//m.html" )



# we moeten een een kleur voor elk gebied berekenen
gbdata = pd.read_csv('C://Users//wagenerj//Documents//data//amsterdamgeo//datapergebied.csv',sep=';')
gbdata.columns =gbdata.columns.str.strip()


#create een functiie die op basis van de waarden een colormap aanmaakt
from branca.colormap import linear
colormap = linear.YlGn_09.scale(gbdata['aantal'].min(),gbdata['aantal'].max())
print(colormap(5.0))
colormap
print(colormap)

#convert de tabel in a dictionary, zodat we een featere aan de waarde van het gebeid kunnen koppelen
gbdata_dict= gbdata.set_index('Gebied_code')['aantal']


#Essentie is hier dat je bij fillcoolor de juiste key doorgeeft aan de gbdata_dict. Hierin moet de gebiedscode staan van het Feature :feature['properties']['Gebied_code']
#WERKEND add Popup to polygon shapes in chloropleth
#https://github.com/python-visualization/folium/issues/1020
m = folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
gj=folium.GeoJson(
    geo_json_data,
    name='unemployment',
    tooltip=folium.GeoJsonTooltip(fields=['centroid_x']),
    style_function=lambda feature: {
        'fillColor': colormap(gbdata_dict[feature['properties']['Gebied_code']]),
        'color': 'black',
        'weight': 1,
        'dashArray': '5, 5',
        'fillOpacity': 0.9,})
#gj.add_child(folium.Popup('outline Popup on GeoJSON'))
gj.add_to(m)
m.save("C://Users//wagenerj//Documents//data//amsterdamgeo//m.html" )







   
   








#https://github.com/python-visualization/folium/issues/1020
m = folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
gj=folium.GeoJson(
    geo_json_data,
    name='unemployment',
    tooltip=folium.GeoJsonTooltip(fields=['Gebied']),
    style_function=lambda feature: {
        'fillColor': colormap(gbdata_dict[feature['properties']['Gebied_code']]),
        'color': 'black',
        'weight': 1,
        'dashArray': '5, 5',
        'fillOpacity': 0.9,})
#gj.add_child(folium.Popup('outline Popup on GeoJSON'))
gj.add_to(m)
m.save("C://Users//wagenerj//Documents//data//amsterdamgeo//m.html" )


# Let op dat de Xen Y  in de juiste volgorde staan.
for feature in geo_json_data['features']:
    folium.Marker(
        location=[feature['properties']['centroid_y'], feature['properties']['centroid_x']],
        popup=folium.Popup("Let's try quotes", parse_html=True, max_width=100)
    ).add_to(m)

m.save("C://Users//wagenerj//Documents//data//amsterdamgeo//m.html" )











































>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
There is a work-around for this. You need to iterate over the each geoJson feature and create a new geojson for each one.
Then, add a popup for each geoJson feature. Then combine all features in a layer. In my code, the full geoJson is data_geojson_dict

m = folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
list_tooltip_vars=['Gebied_code']

layer_geom = folium.FeatureGroup(name='layer',control=False)

for i in range(len(geo_json_data["features"])):
    temp_geojson = {"features":[geo_json_data["features"][i]],"type":"FeatureCollection"}
    temp_geojson_layer = folium.GeoJson(temp_geojson,
                   highlight_function=lambda x: {'weight':3, 'color':'black'},
                    control=False,
                    style_function=lambda feature: {
                   'color': 'black',
                   'weight': 1},
                    tooltip=folium.features.GeoJsonTooltip(fields=list_tooltip_vars,
                                        aliases=[x.capitalize()+":" for x in list_tooltip_vars],
                                          labels=True,
                                          sticky=False))
#    folium.Popup(temp_geojson["features"][0]["properties"]["Gebied"]).add_to(temp_geojson_layer)
    temp_geojson_layer.add_to(layer_geom)

layer_geom.add_to(m)
folium.LayerControl(autoZIndex=False, collapsed=True).add_to(m)
m.save("C://Users//wagenerj//Documents//data//amsterdamgeo//m.html" )





# =============================================================================
# # Using Choropleth class
# # Now if you want to get faster, you can use the Choropleth class. Have a look at it's docstring, it has several styling options.
# # You can use it in providing a file name (geo_path) :
# # =============================================================================
m = folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
folium.Choropleth(geo_data=geo_json_data).add_to(m)
m.save("C://Users//wagenerj//Documents//data//amsterdamgeo//m.html" )


# werkend folium.chloropleth
m = folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
folium.Choropleth(
    geo_data=geo_json_data,
    data=gbdata,
    columns=['Gebied_code', 'aantal'],
    key_on='feature.properties.Gebied_code',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='Gebieden  (%)',
#    highlight=True,
    tooltip=folium.GeoJsonTooltip(fields=['Gebied'])
).add_to(m)
m.save("C://Users//wagenerj//Documents//data//amsterdamgeo//m.html" )




Geen opmerkingen:

Een reactie posten

Datums bepalen adhv begin en einddatum in Dataframe

Voorbeeld op losse velden  ####################################################################### # import necessary packages from datetime...