Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
spatialthoughts committed Jun 21, 2023
1 parent b0b3495 commit 698725c
Showing 1 changed file with 75 additions and 120 deletions.
195 changes: 75 additions & 120 deletions code/python_foundation/solutions/13_creating_spatial_data_solution.ipynb
Original file line number Diff line number Diff line change
@@ -1,126 +1,81 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
"cells": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['../data/geonames/US.txt', '../data/geonames/MX.txt', '../data/geonames/CA.txt']\n"
]
}
],
"source": [
"import os\n",
"import pandas as pd\n",
"import geopandas as gpd\n",
"\n",
"data_pkg_path = '../data/geonames/'\n",
"files = os.listdir(data_pkg_path)\n",
"\n",
"filepaths = []\n",
"for file in files:\n",
" filepaths.append(os.path.join(data_pkg_path, file))\n",
"print(filepaths)\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "VpQxobY7eNvl"
},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd\n",
"import geopandas as gpd\n",
"\n",
"data_pkg_path = 'data/geonames/'"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(225328, 20)\n",
"(67134, 20)\n",
"(57266, 20)\n"
]
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "_Gg6n7SfeNvo"
},
"outputs": [],
"source": [
"column_names = [\n",
" 'geonameid', 'name', 'asciiname', 'alternatenames',\n",
" 'latitude', 'longitude', 'feature class', 'feature code',\n",
" 'country code', 'cc2', 'admin1 code', 'admin2 code',\n",
" 'admin3 code', 'admin4 code', 'population', 'elevation',\n",
" 'dem', 'timezone', 'modification date'\n",
"]\n",
"\n",
"dataframes = []\n",
"\n",
"files = os.listdir(data_pkg_path)\n",
"\n",
"for file in files:\n",
" path = os.path.join(data_pkg_path, file)\n",
" df = pd.read_csv(path, sep='\\t', names=column_names)\n",
" mountains = df[df['feature class']=='T']\n",
" dataframes.append(mountains)\n",
"\n",
"merged = pd.concat(dataframes)\n",
"geometry = gpd.points_from_xy(merged.longitude, merged.latitude)\n",
"gdf = gpd.GeoDataFrame(merged, crs='EPSG:4326', geometry=geometry)\n",
"\n",
"output_dir = 'output'\n",
"output_filename = 'merged.gpkg'\n",
"output_path = os.path.join(output_dir, output_filename)\n",
"\n",
"gdf.to_file(driver='GPKG', filename=output_path, encoding='utf-8')\n",
"print('Successfully written output file at {}'.format(output_path))"
]
}
],
"source": [
"column_names = [\n",
" 'geonameid', 'name', 'asciiname', 'alternatenames', \n",
" 'latitude', 'longitude', 'feature class', 'feature code',\n",
" 'country code', 'cc2', 'admin1 code', 'admin2 code',\n",
" 'admin3 code', 'admin4 code', 'population', 'elevation',\n",
" 'dem', 'timezone', 'modification date'\n",
"]\n",
"\n",
"dataframes = []\n",
"for filepath in filepaths:\n",
" df = pd.read_csv(filepath, sep='\\t', names=column_names)\n",
" mountains = df[df['feature class'] == 'T']\n",
" geometry = gpd.points_from_xy(mountains.longitude, mountains.latitude)\n",
" gdf = gpd.GeoDataFrame(mountains, crs='EPSG:4326', geometry=geometry)\n",
" print(gdf.shape)\n",
" dataframes.append(gdf)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(349728, 20)\n"
]
}
],
"source": [
"merged = pd.concat(dataframes)\n",
"print(merged.shape)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully written output file at ../output/merged.gpkg\n"
]
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.7"
},
"colab": {
"provenance": []
}
],
"source": [
"output_dir = '../output'\n",
"output_filename = 'merged.gpkg'\n",
"output_path = os.path.join(output_dir, output_filename)\n",
"\n",
"merged.to_file(driver='GPKG', filename=output_path, encoding='utf-8')\n",
"print('Successfully written output file at {}'.format(output_path))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 698725c

Please sign in to comment.