{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "brasil = gpd.read_file('data/BRA_adm/BRA_adm2.shx')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "brasil.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "regions = brasil['NAME_1'].unique()\n", "regions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for r in regions:\n", " sample = brasil[brasil['NAME_1'] == r]\n", " print(r)\n", " print(sample.shape)\n", " sample.plot()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "rondonia = brasil[brasil['NAME_1'] == 'Rondônia']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# COMPUTE AREA" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/varaya/Projects/GeoTimePropagation/4_data_exploration/geoPropDataExploration/lib/python3.6/site-packages/pandas/core/indexing.py:362: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " self.obj[key] = _infer_fill_value(value)\n", "/home/varaya/Projects/GeoTimePropagation/4_data_exploration/geoPropDataExploration/lib/python3.6/site-packages/pandas/core/indexing.py:543: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " self.obj[item] = s\n" ] } ], "source": [ "from functools import partial\n", "\n", "# 3rd pary modules\n", "from shapely.geometry import Polygon\n", "from shapely.ops import transform\n", "import pyproj\n", "\n", "def assign_area(polygon):\n", " proj = partial(pyproj.transform, pyproj.Proj(init='epsg:4326'),\n", " pyproj.Proj(init='epsg:3857'))\n", " return transform(proj, polygon).area\n", "rondonia.loc[:, 'area'] = rondonia['geometry'].apply(assign_area)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "rondonia.loc[:, 'centroid'] = rondonia['geometry'].centroid" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def assign_x(centroid):\n", " return float(centroid.x)\n", "\n", "def assign_y(centroid):\n", " return float(centroid.y)\n", "rondonia.loc[:, 'centroid_x'] = rondonia['centroid'].apply(assign_x)\n", "rondonia.loc[:, 'centroid_y'] = rondonia['centroid'].apply(assign_y)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/varaya/Projects/GeoTimePropagation/4_data_exploration/geoPropDataExploration/lib/python3.6/site-packages/pandas/core/frame.py:3940: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " errors=errors)\n" ] } ], "source": [ "rondonia.drop('centroid', axis=1, inplace=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rondonia.head()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ID_0ISONAME_0ID_1NAME_1ID_2NAME_2TYPE_2ENGTYPE_2NL_NAME_2VARNAME_2geometryareacentroid_xcentroid_yid
033BRABrazil22Rondônia4290Alta Floresta d'OesteMunicípioMunicipalityNoneNonePOLYGON ((-62.88114166259766 -12.8631763458252...7.498797e+09-62.276634-12.4729500
133BRABrazil22Rondônia4291Alto Alegre dos ParecisMunicípioMunicipalityNoneNonePOLYGON ((-61.56608963012684 -12.4674110412596...4.191356e+09-61.881814-12.5949041
233BRABrazil22Rondônia4292Alto ParaísoMunicípioMunicipalityNoneNonePOLYGON ((-63.68526458740234 -9.83299446105957...2.742495e+09-63.394359-9.6492932
333BRABrazil22Rondônia4293Alvorada d'OesteMunicípioMunicipalityNoneNonePOLYGON ((-62.53446960449219 -11.4756612777708...3.168420e+09-62.544849-11.3114483
433BRABrazil22Rondônia4294AriquemesMunicípioMunicipalityNoneNonePOLYGON ((-63.209228515625 -10.16702175140375,...4.592228e+09-62.957336-9.9519804
\n", "
" ], "text/plain": [ " ID_0 ISO NAME_0 ID_1 NAME_1 ID_2 NAME_2 \\\n", "0 33 BRA Brazil 22 Rondônia 4290 Alta Floresta d'Oeste \n", "1 33 BRA Brazil 22 Rondônia 4291 Alto Alegre dos Parecis \n", "2 33 BRA Brazil 22 Rondônia 4292 Alto Paraíso \n", "3 33 BRA Brazil 22 Rondônia 4293 Alvorada d'Oeste \n", "4 33 BRA Brazil 22 Rondônia 4294 Ariquemes \n", "\n", " TYPE_2 ENGTYPE_2 NL_NAME_2 VARNAME_2 \\\n", "0 Município Municipality None None \n", "1 Município Municipality None None \n", "2 Município Municipality None None \n", "3 Município Municipality None None \n", "4 Município Municipality None None \n", "\n", " geometry area \\\n", "0 POLYGON ((-62.88114166259766 -12.8631763458252... 7.498797e+09 \n", "1 POLYGON ((-61.56608963012684 -12.4674110412596... 4.191356e+09 \n", "2 POLYGON ((-63.68526458740234 -9.83299446105957... 2.742495e+09 \n", "3 POLYGON ((-62.53446960449219 -11.4756612777708... 3.168420e+09 \n", "4 POLYGON ((-63.209228515625 -10.16702175140375,... 4.592228e+09 \n", "\n", " centroid_x centroid_y id \n", "0 -62.276634 -12.472950 0 \n", "1 -61.881814 -12.594904 1 \n", "2 -63.394359 -9.649293 2 \n", "3 -62.544849 -11.311448 3 \n", "4 -62.957336 -9.951980 4 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rondonia.reset_index(drop=True, inplace = True)\n", "rondonia.loc[:, 'id'] = rondonia.index.values\n", "rondonia.head()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/varaya/Projects/GeoTimePropagation/4_data_exploration/geoPropDataExploration/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " \"\"\"Entry point for launching an IPython kernel.\n", "/home/varaya/Projects/GeoTimePropagation/4_data_exploration/geoPropDataExploration/lib/python3.6/site-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " \n", "/home/varaya/Projects/GeoTimePropagation/4_data_exploration/geoPropDataExploration/lib/python3.6/site-packages/ipykernel_launcher.py:3: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", " This is separate from the ipykernel package so we can avoid doing imports until\n" ] }, { "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", " \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", " \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", "
ID_0ISONAME_0ID_1NAME_1ID_2NAME_2TYPE_2ENGTYPE_2NL_NAME_2VARNAME_2geometryareacentroid_xcentroid_yidcentroid_manual_xcentroid_manual_yadd_line
033BRABrazil22Rondônia4290Alta Floresta d'OesteMunicípioMunicipalityNoneNonePOLYGON ((-62.88114166259766 -12.8631763458252...7.498797e+09-62.276634-12.4729500-62.276634-12.472950False
133BRABrazil22Rondônia4291Alto Alegre dos ParecisMunicípioMunicipalityNoneNonePOLYGON ((-61.56608963012684 -12.4674110412596...4.191356e+09-61.881814-12.5949041-61.881814-12.594904False
233BRABrazil22Rondônia4292Alto ParaísoMunicípioMunicipalityNoneNonePOLYGON ((-63.68526458740234 -9.83299446105957...2.742495e+09-63.394359-9.6492932-63.394359-9.649293False
333BRABrazil22Rondônia4293Alvorada d'OesteMunicípioMunicipalityNoneNonePOLYGON ((-62.53446960449219 -11.4756612777708...3.168420e+09-62.544849-11.3114483-62.544849-11.311448False
433BRABrazil22Rondônia4294AriquemesMunicípioMunicipalityNoneNonePOLYGON ((-63.209228515625 -10.16702175140375,...4.592228e+09-62.957336-9.9519804-62.957336-9.951980False
\n", "
" ], "text/plain": [ " ID_0 ISO NAME_0 ID_1 NAME_1 ID_2 NAME_2 \\\n", "0 33 BRA Brazil 22 Rondônia 4290 Alta Floresta d'Oeste \n", "1 33 BRA Brazil 22 Rondônia 4291 Alto Alegre dos Parecis \n", "2 33 BRA Brazil 22 Rondônia 4292 Alto Paraíso \n", "3 33 BRA Brazil 22 Rondônia 4293 Alvorada d'Oeste \n", "4 33 BRA Brazil 22 Rondônia 4294 Ariquemes \n", "\n", " TYPE_2 ENGTYPE_2 NL_NAME_2 VARNAME_2 \\\n", "0 Município Municipality None None \n", "1 Município Municipality None None \n", "2 Município Municipality None None \n", "3 Município Municipality None None \n", "4 Município Municipality None None \n", "\n", " geometry area \\\n", "0 POLYGON ((-62.88114166259766 -12.8631763458252... 7.498797e+09 \n", "1 POLYGON ((-61.56608963012684 -12.4674110412596... 4.191356e+09 \n", "2 POLYGON ((-63.68526458740234 -9.83299446105957... 2.742495e+09 \n", "3 POLYGON ((-62.53446960449219 -11.4756612777708... 3.168420e+09 \n", "4 POLYGON ((-63.209228515625 -10.16702175140375,... 4.592228e+09 \n", "\n", " centroid_x centroid_y id centroid_manual_x centroid_manual_y add_line \n", "0 -62.276634 -12.472950 0 -62.276634 -12.472950 False \n", "1 -61.881814 -12.594904 1 -61.881814 -12.594904 False \n", "2 -63.394359 -9.649293 2 -63.394359 -9.649293 False \n", "3 -62.544849 -11.311448 3 -62.544849 -11.311448 False \n", "4 -62.957336 -9.951980 4 -62.957336 -9.951980 False " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rondonia['centroid_manual_x'] = rondonia['centroid_x']\n", "rondonia['centroid_manual_y'] = rondonia['centroid_y']\n", "rondonia['add_line'] = False\n", "rondonia.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# MANUAL CENTROIDS" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[2, 'centroid_manual_x'] = -75.1143399157413\n", "ica.loc[2, 'centroid_manual_y'] = -14.921044879838641" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[6, 'centroid_manual_x'] = -75.0782884238944\n", "ica.loc[6, 'centroid_manual_y'] = -14.467705760547148" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[9, 'centroid_manual_x'] = -75.11106250739158\n", "ica.loc[9, 'centroid_manual_y'] = -14.321677975592303" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[27, 'centroid_manual_x'] = -76.2927218459814\n", "ica.loc[27, 'centroid_manual_y'] = -13.714338235863679\n", "ica.loc[27, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[31, 'centroid_manual_x'] = -76.26977998753337\n", "ica.loc[31, 'centroid_manual_y'] = -13.56783013532487\n", "ica.loc[31, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[32, 'centroid_manual_x'] = -76.28944443763167\n", "ica.loc[32, 'centroid_manual_y'] = -13.462670717050226\n", "ica.loc[32, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[39, 'centroid_manual_x'] = -76.08445278725789\n", "ica.loc[39, 'centroid_manual_y'] = -13.175638341432746 " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[38, 'centroid_manual_x'] = -76.20571689619746\n", "ica.loc[38, 'centroid_manual_y'] = -13.264973736619528 " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[37, 'centroid_manual_x'] = -76.01234980356409\n", "ica.loc[37, 'centroid_manual_y'] = -13.280923037490457 " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[33, 'centroid_manual_x'] = -76.30255407103054\n", "ica.loc[33, 'centroid_manual_y'] = -13.360653800196141\n", "ica.loc[33, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[20, 'centroid_manual_x'] = -76.34516037957688\n", "ica.loc[20, 'centroid_manual_y'] = -14.05159452160894\n", "ica.loc[20, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[22, 'centroid_manual_x'] = -75.94680163656973\n", "ica.loc[22, 'centroid_manual_y'] = -13.937109738585168" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[12, 'centroid_manual_x'] = -75.11285484008283\n", "ica.loc[12, 'centroid_manual_y'] = -13.93392879298015\n", "ica.loc[12, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[10, 'centroid_manual_x'] = -76.03708399470337\n", "ica.loc[10, 'centroid_manual_y'] = -14.51212999240724\n", "ica.loc[10, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[16, 'centroid_manual_x'] = -75.30949934106593\n", "ica.loc[16, 'centroid_manual_y'] = -13.905298311937463\n", "ica.loc[16, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[19, 'centroid_manual_x'] = -75.43404085835522\n", "ica.loc[19, 'centroid_manual_y'] = -13.822568158715779\n", "ica.loc[19, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[14, 'centroid_manual_x'] = -76.3287733378283\n", "ica.loc[14, 'centroid_manual_y'] = -14.200974575219474\n", "ica.loc[14, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[13, 'centroid_manual_x'] = -76.17801255374125\n", "ica.loc[13, 'centroid_manual_y'] = -14.435968722682457\n", "ica.loc[13, 'add_line'] = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[21, 'centroid_manual_x'] = -75.40782159155748\n", "ica.loc[21, 'centroid_manual_y'] = -14.185087745040045" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ica.loc[23, 'centroid_manual_x'] = -76.18456737044069\n", "ica.loc[23, 'centroid_manual_y'] = -13.953013809054262" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rondonia.to_file('data/rondonia.geojson', driver='GeoJSON')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "rondonia_simple = rondonia.copy()\n", "rondonia_simple['geometry'] = rondonia_simple['geometry'].simplify(0.02, preserve_topology=True)\n", "rondonia_simple.to_file('data/rondonia_simplified.geojson', driver='GeoJSON')" ] } ], "metadata": { "kernelspec": { "display_name": "geoPropDataExploration", "language": "python", "name": "geopropdataexploration" }, "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }