{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 31 Obtaining City Locations\n", "\n", "Locations of cities for easy plotting can be obtained using GeoPy with GemGIS." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set File Paths and download Tutorial Data\n", "\n", "If you downloaded the latest `GemGIS` version from the Github repository, append the path so that the package can be imported successfully. Otherwise, it is recommended to install `GemGIS` via `pip install gemgis` and import `GemGIS` using `import gemgis as gg`. In addition, the file path to the folder where the data is being stored is set. The tutorial data is downloaded using Pooch (https://www.fatiando.org/pooch/latest/index.html) and stored in the specified folder. Use `pip install pooch` if Pooch is not installed on your system yet." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-03-17T11:52:38.178056Z", "start_time": "2021-03-17T11:52:35.281956Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`\n", "C:\\Users\\ale93371\\Anaconda3\\envs\\test_gempy\\lib\\site-packages\\theano\\configdefaults.py:560: UserWarning: DeprecationWarning: there is no c++ compiler.This is deprecated and with Theano 0.11 a c++ compiler will be mandatory\n", " warnings.warn(\"DeprecationWarning: there is no c++ compiler.\"\n", "WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.\n", "WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n" ] } ], "source": [ "import gemgis as gg\n", "\n", "file_path ='data/31_obtaining_location_information/'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Location Information\n", "\n", "Location information from different cities can be obtained using GeoPy. These contain a description of the location as well as longitude and latitude values." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-01-29T17:07:24.671966Z", "start_time": "2021-01-29T17:07:21.746686Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`\n", "C:\\Users\\ale93371\\Anaconda3\\envs\\test_gempy\\lib\\site-packages\\theano\\configdefaults.py:560: UserWarning: DeprecationWarning: there is no c++ compiler.This is deprecated and with Theano 0.11 a c++ compiler will be mandatory\n", " warnings.warn(\"DeprecationWarning: there is no c++ compiler.\"\n", "WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.\n", "WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n" ] }, { "data": { "text/plain": [ "Location(Aachen, Städteregion Aachen, Nordrhein-Westfalen, Deutschland, (50.776351, 6.083862, 0.0))" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aachen = gg.utils.get_location_coordinate(name='Aachen')\n", "aachen" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2021-01-29T17:07:25.203976Z", "start_time": "2021-01-29T17:07:24.671966Z" } }, "outputs": [ { "data": { "text/plain": [ "Location(Berlin, 10117, Deutschland, (52.5170365, 13.3888599, 0.0))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "berlin = gg.utils.get_location_coordinate(name='Berlin')\n", "berlin" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reprojecting Coordinates\n", "\n", "The WGS 84 coordinates can be reprojected using ``transform_location_coordinate(..)``." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2021-01-29T17:07:25.326753Z", "start_time": "2021-01-29T17:07:25.205980Z" } }, "outputs": [ { "data": { "text/plain": [ "{'Aachen, Städteregion Aachen, Nordrhein-Westfalen, Deutschland': (32294411.33488576,\n", " 5629009.357074926)}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aachen_repr = gg.utils.transform_location_coordinate(coordinates=aachen,\n", " crs='EPSG:4647')\n", "aachen_repr" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2021-01-29T17:07:25.452314Z", "start_time": "2021-01-29T17:07:25.328748Z" } }, "outputs": [ { "data": { "text/plain": [ "{'Berlin, 10117, Deutschland': (32797738.56053437, 5827603.740024588)}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "berlin_repr = gg.utils.transform_location_coordinate(coordinates=berlin,\n", " crs='EPSG:4647')\n", "berlin_repr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating Bounding Polygon from Location\n", "\n", "Each location also consists of a bounding box which can be used to create a Shapely Polygon from it using ``create_polygon_from_location(..)``." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2021-01-29T17:07:25.468156Z", "start_time": "2021-01-29T17:07:25.454326Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(50.6621373, 5.9748624, 50.8572449, 6.2180747)\n" ] }, { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aachen_bbox = gg.utils.create_polygon_from_location(coordinates=aachen)\n", "\n", "print(aachen_bbox.bounds)\n", "aachen_bbox" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2021-01-29T17:07:25.483175Z", "start_time": "2021-01-29T17:07:25.471178Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(52.3570365, 13.2288599, 52.6770365, 13.5488599)\n" ] }, { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "berlin_bbox = gg.utils.create_polygon_from_location(coordinates=berlin)\n", "\n", "print(berlin_bbox.bounds)\n", "berlin_bbox" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting the locations of multiple cities\n", "\n", "The function ``get_locations(..)`` allows to pass a list of cities and returns a dict with the city names and the corresponding coordinates." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2021-01-29T17:07:28.183115Z", "start_time": "2021-01-29T17:07:25.485169Z" } }, "outputs": [ { "data": { "text/plain": [ "{'Aachen, Städteregion Aachen, Nordrhein-Westfalen, Deutschland': (32294411.33488576,\n", " 5629009.357074926),\n", " 'Berlin, 10117, Deutschland': (32797738.56053437, 5827603.740024588),\n", " 'München, Bayern, Deutschland': (32691595.356409974, 5334747.274305081),\n", " 'Hamburg, Deutschland': (32566296.251301013, 5933959.964708338),\n", " 'Köln, Nordrhein-Westfalen, Deutschland': (32356668.818424627,\n", " 5644952.099932303)}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "coordinates_dict = gg.utils.get_locations(names = ['Aachen', 'Berlin', 'München', 'Hamburg', 'Köln'], crs='EPSG:4647')\n", "coordinates_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Converting Location Dict into GeoDataFrame\n", "\n", "In order to work with the coordinates and location names, the dict can be converted to a GeoDataFrame. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2021-01-29T17:07:28.213655Z", "start_time": "2021-01-29T17:07:28.185126Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CityXYgeometry
0Aachen32294411.335629009.36POINT (32294411.335 5629009.357)
1Berlin32797738.565827603.74POINT (32797738.561 5827603.740)
2München32691595.365334747.27POINT (32691595.356 5334747.274)
3Hamburg32566296.255933959.96POINT (32566296.251 5933959.965)
4Köln32356668.825644952.10POINT (32356668.818 5644952.100)
\n", "
" ], "text/plain": [ " City X Y geometry\n", "0 Aachen 32294411.33 5629009.36 POINT (32294411.335 5629009.357)\n", "1 Berlin 32797738.56 5827603.74 POINT (32797738.561 5827603.740)\n", "2 München 32691595.36 5334747.27 POINT (32691595.356 5334747.274)\n", "3 Hamburg 32566296.25 5933959.96 POINT (32566296.251 5933959.965)\n", "4 Köln 32356668.82 5644952.10 POINT (32356668.818 5644952.100)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import geopandas as gpd\n", "gdf = gg.utils.convert_location_dict_to_gdf(location_dict=coordinates_dict)\n", "gdf" ] } ], "metadata": { "hide_input": false, "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.10.8" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }