Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rutgerhofste/3b3c84089b051919d34aaa2f2f3cc747 to your computer and use it in GitHub Desktop.
Save rutgerhofste/3b3c84089b051919d34aaa2f2f3cc747 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import geopandas as gpd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'init': 'epsg:4326'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdf.crs"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"gdf[\"perimeter\"] = gdf[\"geometry\"].length"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>pop_est</th>\n",
" <th>continent</th>\n",
" <th>name</th>\n",
" <th>iso_a3</th>\n",
" <th>gdp_md_est</th>\n",
" <th>geometry</th>\n",
" <th>perimeter</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>28400000.0</td>\n",
" <td>Asia</td>\n",
" <td>Afghanistan</td>\n",
" <td>AFG</td>\n",
" <td>22270.0</td>\n",
" <td>POLYGON ((61.21081709172574 35.65007233330923,...</td>\n",
" <td>46.043309</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>12799293.0</td>\n",
" <td>Africa</td>\n",
" <td>Angola</td>\n",
" <td>AGO</td>\n",
" <td>110300.0</td>\n",
" <td>(POLYGON ((16.32652835456705 -5.87747039146621...</td>\n",
" <td>54.986811</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3639453.0</td>\n",
" <td>Europe</td>\n",
" <td>Albania</td>\n",
" <td>ALB</td>\n",
" <td>21810.0</td>\n",
" <td>POLYGON ((20.59024743010491 41.85540416113361,...</td>\n",
" <td>8.146598</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4798491.0</td>\n",
" <td>Asia</td>\n",
" <td>United Arab Emirates</td>\n",
" <td>ARE</td>\n",
" <td>184300.0</td>\n",
" <td>POLYGON ((51.57951867046327 24.24549713795111,...</td>\n",
" <td>14.574017</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>40913584.0</td>\n",
" <td>South America</td>\n",
" <td>Argentina</td>\n",
" <td>ARG</td>\n",
" <td>573900.0</td>\n",
" <td>(POLYGON ((-65.50000000000003 -55.199999999999...</td>\n",
" <td>113.624468</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" pop_est continent name iso_a3 gdp_md_est \\\n",
"0 28400000.0 Asia Afghanistan AFG 22270.0 \n",
"1 12799293.0 Africa Angola AGO 110300.0 \n",
"2 3639453.0 Europe Albania ALB 21810.0 \n",
"3 4798491.0 Asia United Arab Emirates ARE 184300.0 \n",
"4 40913584.0 South America Argentina ARG 573900.0 \n",
"\n",
" geometry perimeter \n",
"0 POLYGON ((61.21081709172574 35.65007233330923,... 46.043309 \n",
"1 (POLYGON ((16.32652835456705 -5.87747039146621... 54.986811 \n",
"2 POLYGON ((20.59024743010491 41.85540416113361,... 8.146598 \n",
"3 POLYGON ((51.57951867046327 24.24549713795111,... 14.574017 \n",
"4 (POLYGON ((-65.50000000000003 -55.199999999999... 113.624468 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdf.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, the perimeter is calculated in the unit of the crs e.g. degrees length. not very meaningful."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"lets reproject and try again"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ECKERT_IV_PROJ4_STRING = \"+proj=eck4 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gdf_eckert4 = gdf.to_crs(ECKERT_IV_PROJ4_STRING)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gdf_eckert4[\"perimeter\"] = gdf_eckert4[\"geometry\"].length"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>pop_est</th>\n",
" <th>continent</th>\n",
" <th>name</th>\n",
" <th>iso_a3</th>\n",
" <th>gdp_md_est</th>\n",
" <th>geometry</th>\n",
" <th>perimeter</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>28400000.0</td>\n",
" <td>Asia</td>\n",
" <td>Afghanistan</td>\n",
" <td>AFG</td>\n",
" <td>22270.0</td>\n",
" <td>POLYGON ((5310471.251038463 4514215.669201106,...</td>\n",
" <td>4.480149e+06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>12799293.0</td>\n",
" <td>Africa</td>\n",
" <td>Angola</td>\n",
" <td>AGO</td>\n",
" <td>110300.0</td>\n",
" <td>(POLYGON ((1531585.16979237 -773954.702031967,...</td>\n",
" <td>6.120479e+06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3639453.0</td>\n",
" <td>Europe</td>\n",
" <td>Albania</td>\n",
" <td>ALB</td>\n",
" <td>21810.0</td>\n",
" <td>POLYGON ((1729835.41690117 5215796.736088942, ...</td>\n",
" <td>8.322338e+05</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4798491.0</td>\n",
" <td>Asia</td>\n",
" <td>United Arab Emirates</td>\n",
" <td>ARE</td>\n",
" <td>184300.0</td>\n",
" <td>POLYGON ((4675864.307638651 3138288.872889439,...</td>\n",
" <td>1.528442e+06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>40913584.0</td>\n",
" <td>South America</td>\n",
" <td>Argentina</td>\n",
" <td>ARG</td>\n",
" <td>573900.0</td>\n",
" <td>(POLYGON ((-5017766.464975084 -6571677.2590154...</td>\n",
" <td>1.144272e+07</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" pop_est continent name iso_a3 gdp_md_est \\\n",
"0 28400000.0 Asia Afghanistan AFG 22270.0 \n",
"1 12799293.0 Africa Angola AGO 110300.0 \n",
"2 3639453.0 Europe Albania ALB 21810.0 \n",
"3 4798491.0 Asia United Arab Emirates ARE 184300.0 \n",
"4 40913584.0 South America Argentina ARG 573900.0 \n",
"\n",
" geometry perimeter \n",
"0 POLYGON ((5310471.251038463 4514215.669201106,... 4.480149e+06 \n",
"1 (POLYGON ((1531585.16979237 -773954.702031967,... 6.120479e+06 \n",
"2 POLYGON ((1729835.41690117 5215796.736088942, ... 8.322338e+05 \n",
"3 POLYGON ((4675864.307638651 3138288.872889439,... 1.528442e+06 \n",
"4 (POLYGON ((-5017766.464975084 -6571677.2590154... 1.144272e+07 "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdf_eckert4.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the length of a polygon (perimeter) is arbitrary depending on the resolution of your polygon. \n",
"It is also known as the [coastline paradox](https://en.wikipedia.org/wiki/Coastline_paradox)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 35",
"language": "python",
"name": "python35"
},
"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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment