master
wfct0p 2 years ago
commit ebf9336f72

1
.gitignore vendored

@ -0,0 +1 @@
.ipynb_checkpoints/

@ -0,0 +1,296 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "4d098df7-6998-4e10-99fd-5e68aa01a997",
"metadata": {},
"outputs": [],
"source": [
"# data = \"data/data-3d9Ex.csv\"\n",
"import plotly.express as px\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f49def17-f64f-46bf-a056-1d8a063ce3a6",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"data/data-3d9Ex.csv\")\n",
"df = df.fillna(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17975a6c-686e-43c3-a644-18004f74880b",
"metadata": {},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d41f39d0-36fb-45d4-8d34-bd9dfd9545ad",
"metadata": {},
"outputs": [],
"source": [
"fig = px.scatter_geo(df.head(), lat=\"Lat\", lon=\"Lon\", scope=\"europe\", fitbounds='locations',\n",
" size=\"Lakosság\")\n",
"fig.show(width=1000, height=800)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2940dc7-1221-4545-bcee-9eecf7b75fdb",
"metadata": {},
"outputs": [],
"source": [
"import plotly.express as px\n",
"\n",
"colorscales = px.colors.named_colorscales()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc5fe5a3-ba73-49ca-8c3d-24c5404b4ad5",
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objects as go\n",
"\n",
"import pandas as pd\n",
"\n",
"df['text'] = df['Name'] + ': ' + df['Elhunytak száma 100000 főre'].astype('str')\n",
"tdf = df.head(100)\n",
"\n",
"\n",
"fig = go.Figure(data=go.Scattergeo(\n",
" #locationmode = 'USA-states',\n",
" lon = tdf['Lon'],\n",
" lat = tdf['Lat'],\n",
" text = tdf['text'],\n",
" mode = 'markers',\n",
" marker = dict(\n",
" size = 5,\n",
" opacity = 0.6,\n",
" #color_continuous_scale=px.colors.sequential.Viridis,\n",
" #reversescale = True,\n",
" #autocolorscale = False,\n",
" symbol = 'square',\n",
" line = dict(\n",
" width=1,\n",
" color='rgba(102, 102, 102)'\n",
" ),\n",
" cmin = 0,\n",
" color = np.log(df['Elhunytak száma 100000 főre']),\n",
" cmax = np.log(df['Elhunytak száma 100000 főre'].max()),\n",
" colorbar_title=\"Population\"\n",
" )))\n",
"\n",
"fig.update_layout(\n",
" title = 'Halálesetek száma',\n",
" height=1000,\n",
" width=1200,\n",
" geo = dict(\n",
" fitbounds='locations',\n",
" scope='europe',\n",
" #projection_type='albers usa',\n",
" showland = True,\n",
" landcolor = \"rgb(250, 250, 250)\",\n",
" subunitcolor = \"rgb(217, 217, 217)\",\n",
" countrycolor = \"rgb(217, 217, 217)\",\n",
" #countrywidth = 0.5,\n",
" #subunitwidth = 0.5\n",
" ),\n",
" )\n",
"fig.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "109c8725-0e01-4ad3-9185-813d5db115b7",
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objects as go\n",
"\n",
"import pandas as pd\n",
"\n",
"df['text'] = df['Name'] + ': ' + df['Elhuyntak száma'].astype('str')\n",
"tdf = df[df['Elhuyntak száma']>0]#.head(100)\n",
"\n",
"\n",
"fig = go.Figure(data=go.Scattergeo(\n",
" #locationmode = 'USA-states',\n",
" lon = tdf['Lon'],\n",
" lat = tdf['Lat'],\n",
" text = tdf['text'],\n",
" mode = 'markers',\n",
" marker = dict(\n",
" size = np.log(tdf['Elhuyntak száma'])*5,\n",
" opacity = 0.6,\n",
" #color_continuous_scale=px.colors.sequential.Viridis,\n",
" #reversescale = True,\n",
" #autocolorscale = False,\n",
" symbol = 'circle',\n",
" line = dict(\n",
" width=1,\n",
" color='rgba(102, 102, 102)'\n",
" ),\n",
" cmin = 0,\n",
" color = np.log(tdf['Elhuyntak száma']),\n",
" cmax = np.log(tdf['Elhuyntak száma'].max()),\n",
" colorbar_title=\"Population\"\n",
" )))\n",
"\n",
"fig.update_layout(\n",
" title = 'Halálesetek száma',\n",
" height=1000,\n",
" width=1200,\n",
" geo = dict(\n",
" fitbounds='locations',\n",
" scope='europe',\n",
" #projection_type='albers usa',\n",
" showland = True,\n",
" landcolor = \"rgb(250, 250, 250)\",\n",
" subunitcolor = \"rgb(217, 217, 217)\",\n",
" countrycolor = \"rgb(217, 217, 217)\",\n",
" #countrywidth = 0.5,\n",
" #subunitwidth = 0.5\n",
" ),\n",
" )\n",
"fig.show()\n"
]
},
{
"cell_type": "markdown",
"id": "bb6b0eda-51f1-4bf9-b161-487db05e4ebc",
"metadata": {},
"source": [
"## Heti adatok"
]
},
{
"cell_type": "markdown",
"id": "770e6ba1-e48a-4fde-aa77-5b898e9853e5",
"metadata": {},
"source": [
"### Letoltes\n",
"https://atlo.team/koronaterkep/#megyeibovebb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40f6a53f-c929-4b86-b355-20997ca6adbe",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://docs.google.com/spreadsheets/d/1djH-yUHLPwuEExCjiXS__6-8W2Yp_msFvShpL4bBcuM/export?format=xlsx&gid=1283792994\"\n",
"heti_df = pd.read_excel(url) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e89ed3a-3afe-42ee-b923-cd7e659ae9a8",
"metadata": {},
"outputs": [],
"source": [
"heti_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c319fee-1e1b-4b8a-bea0-690b5e3d67d3",
"metadata": {},
"outputs": [],
"source": [
"df['text'] = df['Name'] + ': ' + df['Elhuyntak száma'].astype('str')\n",
"tdf = df[df['Elhuyntak száma']>0]#.head(100)\n",
"\n",
"\n",
"fig = go.Figure(data=go.Scattergeo(\n",
" #locationmode = 'USA-states',\n",
" lon = tdf['Lon'],\n",
" lat = tdf['Lat'],\n",
" text = tdf['text'],\n",
" mode = 'markers',\n",
" marker = dict(\n",
" size = np.log(tdf['Elhuyntak száma'])*5,\n",
" opacity = 0.6,\n",
" #color_continuous_scale=px.colors.sequential.Viridis,\n",
" #reversescale = True,\n",
" #autocolorscale = False,\n",
" symbol = 'circle',\n",
" line = dict(\n",
" width=1,\n",
" color='rgba(102, 102, 102)'\n",
" ),\n",
" cmin = 0,\n",
" color = np.log(tdf['Elhuyntak száma']),\n",
" cmax = np.log(tdf['Elhuyntak száma'].max()),\n",
" colorbar_title=\"Population\"\n",
" )))\n",
"\n",
"fig.update_layout(\n",
" title = 'Halálesetek száma',\n",
" height=1000,\n",
" width=1200,\n",
" geo = dict(\n",
" fitbounds='locations',\n",
" scope='europe',\n",
" #projection_type='albers usa',\n",
" showland = True,\n",
" landcolor = \"rgb(250, 250, 250)\",\n",
" subunitcolor = \"rgb(217, 217, 217)\",\n",
" countrycolor = \"rgb(217, 217, 217)\",\n",
" #countrywidth = 0.5,\n",
" #subunitwidth = 0.5\n",
" ),\n",
" )\n",
"fig.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a60442e-74fa-45a3-a50c-6808c52c7ca9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -0,0 +1,286 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "2b8d4789-78b3-4f31-9207-4ba6fcf90ad4",
"metadata": {},
"outputs": [],
"source": [
"# data = \"data/data-3d9Ex.csv\"\n",
"import plotly.express as px\n",
"import pandas as pd\n",
"import geopandas as gpd\n",
"import numpy as np\n",
"import json\n",
"\n",
"colorscales = px.colors.named_colorscales()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ef312bf-3c18-4417-806b-3d07830d96d3",
"metadata": {},
"outputs": [],
"source": [
"# Hungary counties shapefile\n",
"url = \"https://maps.princeton.edu/download/file/stanford-dt251rh6351-shapefile.zip\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6822dbd-d897-445f-9743-f7267d66537c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#!cd data && wget https://maps.princeton.edu/download/file/stanford-dt251rh6351-shapefile.zip && unzip stanford-dt251rh6351-shapefile.zip"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb150ab8-aa94-4b76-849f-554f32a30fde",
"metadata": {},
"outputs": [],
"source": [
"# read shp data into geopandas\n",
"gg = gpd.read_file(\"data/dt251rh6351.shp\")\n",
"ggjson = gg.to_json()\n",
"gg.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63e6b826-8563-4c3e-bc81-6141bf6d08f3",
"metadata": {},
"outputs": [],
"source": [
"gg.iloc[1].geometry"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a29a6736-1cf5-485e-abcc-dd5970773d3d",
"metadata": {},
"outputs": [],
"source": [
"# We will need gejson\n",
"dggjson = json.loads(ggjson)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0bf4ed85-0f56-4ede-b75a-cbc7a8bdbd5f",
"metadata": {},
"outputs": [],
"source": [
"# Turns out one of the county's name is misspelled so we rename it for now in the dataframe\n",
"[f['properties']['name_1'] for f in dggjson['features']]"
]
},
{
"cell_type": "markdown",
"id": "bb6b0eda-51f1-4bf9-b161-487db05e4ebc",
"metadata": {},
"source": [
"## Heti adatok"
]
},
{
"cell_type": "markdown",
"id": "770e6ba1-e48a-4fde-aa77-5b898e9853e5",
"metadata": {},
"source": [
"### Letoltes\n",
"https://atlo.team/koronaterkep/#megyeibovebb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e5eeb9f-847c-4e73-8b73-9fc6a97ba56b",
"metadata": {},
"outputs": [],
"source": [
"# https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/hungary-shapefile\n",
"url = \"https://docs.google.com/spreadsheets/d/1djH-yUHLPwuEExCjiXS__6-8W2Yp_msFvShpL4bBcuM/export?format=xlsx&gid=1283792994\"\n",
"heti_df = pd.read_excel(url) \n",
"\n",
"heti_df['date'] = pd.to_datetime(heti_df['Dátum']) #, format='%y-%m-%d')\n",
"heti_df = heti_df.drop(columns=['Dátum', 'Összesen'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e89ed3a-3afe-42ee-b923-cd7e659ae9a8",
"metadata": {},
"outputs": [],
"source": [
"heti_df.head(7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4290d58d-5dfc-46ee-afd7-c46148c62e7c",
"metadata": {},
"outputs": [],
"source": [
"#Rename Győr to Gyor\n",
"heti_df = heti_df.rename(columns={'Győr-Moson-Sopron':'Gyor-Moson-Sopron'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33c27409-bebd-4cd6-a1a6-14935565e94b",
"metadata": {},
"outputs": [],
"source": [
"import datetime as dt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "153128f5-3e5b-4176-aebf-5bf40ddb703f",
"metadata": {},
"outputs": [],
"source": [
"start_date = heti_df.loc[0,'date']\n",
"end_date = start_date + dt.timedelta(days=7)\n",
"\n",
"mask = (heti_df['date'] >= start_date) & (heti_df['date'] < end_date)\n",
"\n",
"hhdf = heti_df.loc[mask].drop(columns=['date']).transpose()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d13aee1a-6702-44fb-8382-985ff582a012",
"metadata": {},
"outputs": [],
"source": [
"hhdf['sum'] = hhdf.sum(axis=1)\n",
"hhdf = hhdf.reset_index().rename(columns={'index':'name_1'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b4fe7f4-796b-4b1c-bcdf-6b440b4ef3f8",
"metadata": {},
"outputs": [],
"source": [
"hhdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d50af1bc-b8d4-4fee-9992-f9939071945b",
"metadata": {},
"outputs": [],
"source": [
"# Get the mean of the county's centroids\n",
"cc = gg.centroid\n",
"\n",
"clon = cc.apply(lambda x: x.x).mean()\n",
"clat = cc.apply(lambda x: x.y).mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c319fee-1e1b-4b8a-bea0-690b5e3d67d3",
"metadata": {},
"outputs": [],
"source": [
"fig = px.choropleth_mapbox(hhdf.drop(columns=[0,1,2,3,4,5,6]), geojson=dggjson, locations='name_1', color='sum',\n",
" color_continuous_scale=\"Viridis\",\n",
" #locationmode='geojson-id',\n",
" featureidkey='properties.name_1',\n",
" range_color=(0, 1800),\n",
" mapbox_style=\"carto-positron\",\n",
" zoom=5.7, center = {\"lat\": clat, \"lon\": clon},\n",
" opacity=0.5,\n",
" labels={'sum':'Megfertozodesek szama'}\n",
" )\n",
"fig.update_layout(margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0})\n",
"fig.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a757a20-9c59-4e31-b1db-bc187f1e3aee",
"metadata": {},
"outputs": [],
"source": [
"thhdf = hhdf.drop(columns=[0,1,2,3,4,5,6])"
]
},
{
"cell_type": "markdown",
"id": "a2294fa4-d611-40e7-a768-a39943b9020b",
"metadata": {},
"source": [
"## Using the built-in plotly choropleth maps\n",
"This is not very decorative"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76163eb4-4587-4561-9dc2-02a321a4e27f",
"metadata": {},
"outputs": [],
"source": [
"fig = px.choropleth(thhdf, geojson=dggjson, locations='name_1',#, color='sum',\n",
" color_continuous_scale=\"Viridis\",\n",
" range_color=(0, 1800),\n",
" locationmode='geojson-id',\n",
" featureidkey='properties.name_1',\n",
" color=thhdf['sum'],\n",
" labels={'sum' : 'Megfertozodesek szama'}\n",
" )\n",
"fig.update_layout(margin={\"r\":0,\"t\":0,\"l\": 0,\"b\":0}, geo_scope='europe'\n",
" )\n",
"fig.layout.geo.center.lat = clat\n",
"fig.layout.geo.center.lon = clon\n",
"fig.layout.geo.projection.scale = 9\n",
"fig.show()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1 @@
ISO-8859-1

Binary file not shown.

@ -0,0 +1 @@
GEOGCS["WGS 84", DATUM["World Geodetic System 1984", SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4326"]]

Binary file not shown.

Binary file not shown.

@ -0,0 +1 @@
https://geowebservices.stanford.edu/geoserver/wfs?outputformat=SHAPE-ZIP&request=GetFeature&service=wfs&srsName=EPSG%3A4326&typeName=druid%3Adt251rh6351&version=2.0.0
Loading…
Cancel
Save