donderdag 29 november 2018

Geopandas en folium

Hoe maak je een colormap

from branca.colormap import linear

colormap = linear.YlGn_09.scale(
    geo_api_df['waarde'].min(),
    geo_api_df['waarde'].max())
print(colormap(5.0))
colormap


Maak een dict met kleuren afhankelijk van de colormap

color_dict = geo_api_df.set_index('code')['waarde'].to_dict()
type(color_dict)
colormap(color_dict['DX21'])
color_dict = {key: colormap(color_dict[key]) for key in color_dict.keys()}


Voeg de kleuren van de color_dict toe aan de geopandas dataframe als kolom

geo_api_df['feature']=geo_api_df['code'].map(color_dict)


Geopandas

import json
import pandas as pd
import geopandas as gpd
import requests
from shapely.geometry import shape
import matplotlib.pyplot as plt
import folium
%matplotlib inline

gebied_api_url = 'https://api.data.amsterdam.nl/gebieden/gebiedsgerichtwerken/'
#buurt_api_url = 'https://api.data.amsterdam.nl/gebieden/buurt/'

resp = requests.get(gebied_api_url)
json_data= requests.get(gebied_api_url).json()


geb_codes = pd.DataFrame(json_data['results']).drop(columns=['_display', '_links', 'dataset'])


geo_api_raw = []
for code in geb_codes['code']:
    geo_api_raw.append(  requests.get(gebied_api_url + code).json())

geo_api_df = pd.DataFrame(geo_api_raw)
geo_api_df.drop(columns=['_display', '_links', 'bbox', 'dataset', 'buurten'], inplace=True)
geo_api_df['geometry'] = geo_api_df['geometrie'].map(shape)
geo_api_df['stadsdeel_code'] = geo_api_df['stadsdeel'].map(lambda f: f['code'])
geo_api_df['stadsdeel_naam'] = geo_api_df['stadsdeel'].map(lambda f: f['naam'])

geo_api_df.drop(columns=['stadsdeel', 'geometrie'], inplace=True)
geo_api_df['waarde']=geo_api_df['code'].str[-1].astype(int)*100

crs = '+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.2369,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +units=m +no_defs'

gebieden_geo=gpd.GeoDataFrame(geo_api_df, crs=crs)


gebieden_geo.plot()


voorbeeld 1

map= folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
map.add_child(folium.GeoJson(gebieden_geo),popup=)


Voorbeeld 2

map=folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
map.choropleth(geo_data=gebieden_geo,data=gebieden_geo, columns=['code','waarde'],key_on='feature.properties.name',fill_color='YlGn',
              fill_opacity=10, line_opacity=0.2,threshold_scale=[0, 20, 30, 40, 50, 60],
             legend_name='Participation Rate (%))')
folium.LayerControl().add_to(map)
map

Voorbeeld 3

map=folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
folium.GeoJson(
    gebieden_geo,
    style_function=lambda feature: {
        'fillColor': '#ffff00',
        'color': 'black',
        'weight': 2,
        'dashArray': '5, 5'
    }
).add_to(map)
map


Voorbeeld 4

map=folium.Map(location=[52.37, 4.90], tiles='Stamen Terrain', zoom_start=12)
folium.GeoJson(
    gebieden_geo,
    style_function=lambda feature: {
        'fillColor': '#ffff00',
        'color': 'black',
        'weight': 2,
        'dashArray': '5, 5'
    }
).add_to(map)
map

woensdag 14 november 2018

json en rest-api

roep rest-api aan

ophalen data


import json
import pandas as pd
import requests
gebied_api_url = 'https://api.data.amsterdam.nl/gebieden/gebiedsgerichtwerken/'
buurt_api_url = 'https://api.data.amsterdam.nl/gebieden/buurt/'
resp = requests.get(gebied_api_url)
json_data= requests.get(gebied_api_url).json()


json_data is nu een DICT object.

==> wil je de keys weten

json_data.keys()
Out[54]: dict_keys(['_links', 'count', 'results'])



haal alleen de key results op en stop in een pandas dataframe

geb_codes = pd.DataFrame(json_data['results'])

Wil je nu weten welke keys (columnamen) er zijn van het dataframe
geb_codes.keys()Out[61]: Index(['_display', '_links', 'code', 'dataset', 'naam'], dtype='object')

Als je van dit pandas object alleen de code en de naam wil hebben dan drop je de andere

geb_codes.drop(columns=['_display', '_links', 'dataset'],inplace=True)

Datums bepalen adhv begin en einddatum in Dataframe

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