Files
geopandas/aggregation_with_dissolve.html

294 lines
17 KiB
HTML

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aggregation with dissolve &mdash; GeoPandas 0.2-py3.5.egg documentation</title>
<link href='https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Roboto+Slab:400,700|Inconsolata:400,700&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="author" title="About these documents"
href="about.html"/>
<link rel="top" title="GeoPandas 0.2-py3.5.egg documentation" href="index.html"/>
<link rel="next" title="Merging Data" href="mergingdata.html"/>
<link rel="prev" title="Set-Operations with Overlay" href="set_operations.html"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-nav-search">
<a href="index.html" class="fa fa-home"> GeoPandas</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="install.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="install.html#installing-geopandas">Installing GeoPandas</a></li>
<li class="toctree-l2"><a class="reference internal" href="install.html#dependencies">Dependencies</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="data_structures.html">Data Structures</a><ul>
<li class="toctree-l2"><a class="reference internal" href="data_structures.html#geoseries">GeoSeries</a></li>
<li class="toctree-l2"><a class="reference internal" href="data_structures.html#geodataframe">GeoDataFrame</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="io.html">Reading and Writing Files</a><ul>
<li class="toctree-l2"><a class="reference internal" href="io.html#reading-spatial-data">Reading Spatial Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="io.html#writing-spatial-data">Writing Spatial Data</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="mapping.html">Making Maps</a><ul>
<li class="toctree-l2"><a class="reference internal" href="mapping.html#chloropleth-maps">Chloropleth Maps</a></li>
<li class="toctree-l2"><a class="reference internal" href="mapping.html#maps-with-layers">Maps with Layers</a></li>
<li class="toctree-l2"><a class="reference internal" href="mapping.html#other-resources">Other Resources</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="projections.html">Managing Projections</a><ul>
<li class="toctree-l2"><a class="reference internal" href="projections.html#coordinate-reference-systems">Coordinate Reference Systems</a></li>
<li class="toctree-l2"><a class="reference internal" href="projections.html#setting-a-projection">Setting a Projection</a></li>
<li class="toctree-l2"><a class="reference internal" href="projections.html#re-projecting">Re-Projecting</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="geometric_manipulations.html">Geometric Manipulations</a><ul>
<li class="toctree-l2"><a class="reference internal" href="geometric_manipulations.html#constructive-methods">Constructive Methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="geometric_manipulations.html#affine-transformations">Affine transformations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="set_operations.html">Set Operations with overlay</a><ul>
<li class="toctree-l2"><a class="reference internal" href="set_operations.html#the-different-overlay-operations">The different Overlay operations</a></li>
<li class="toctree-l2"><a class="reference internal" href="set_operations.html#overlay-countries-example">Overlay Countries Example</a></li>
<li class="toctree-l2"><a class="reference internal" href="set_operations.html#more-examples">More Examples</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Aggregation with dissolve</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#dissolve-example"><code class="docutils literal"><span class="pre">dissolve</span></code> Example</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="mergingdata.html">Merging Data</a><ul>
<li class="toctree-l2"><a class="reference internal" href="mergingdata.html#attribute-joins">Attribute Joins</a></li>
<li class="toctree-l2"><a class="reference internal" href="mergingdata.html#spatial-joins">Spatial Joins</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="geocoding.html">Geocoding</a></li>
<li class="toctree-l1"><a class="reference internal" href="reference.html">Reference to All Attributes and Methods</a><ul>
<li class="toctree-l2"><a class="reference internal" href="reference.html#geodataframe">GeoDataFrame</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="contributing.html">Contributing to GeoPandas</a><ul>
<li class="toctree-l2"><a class="reference internal" href="contributing.html#overview">Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="contributing.html#forking-the-geopandas-repository-using-git">1) Forking the <em>geopandas</em> repository using Git</a></li>
<li class="toctree-l2"><a class="reference internal" href="contributing.html#creating-a-development-environment">2) Creating a development environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="contributing.html#installing-dependencies">3) Installing Dependencies</a></li>
<li class="toctree-l2"><a class="reference internal" href="contributing.html#making-a-development-build">4) Making a development build</a></li>
<li class="toctree-l2"><a class="reference internal" href="contributing.html#making-changes-and-writing-tests">5) Making changes and writing tests</a></li>
<li class="toctree-l2"><a class="reference internal" href="contributing.html#updating-the-documentation">6) Updating the Documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="contributing.html#submitting-a-pull-request">7) Submitting a Pull Request</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="about.html">About</a><ul>
<li class="toctree-l2"><a class="reference internal" href="about.html#known-issues">Known issues</a></li>
</ul>
</li>
</ul>
</div>
&nbsp;
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">GeoPandas</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> &raquo;</li>
<li>Aggregation with dissolve</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/aggregation_with_dissolve.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document">
<div class="section" id="aggregation-with-dissolve">
<h1>Aggregation with dissolve<a class="headerlink" href="#aggregation-with-dissolve" title="Permalink to this headline"></a></h1>
<p>It is often the case that we find ourselves working with spatial data that is more granular than we need. For example, we might have data on sub-national units, but we&#8217;re actually interested in studying patterns at the level of countries.</p>
<p>In a non-spatial setting, we aggregate our data using the <code class="docutils literal"><span class="pre">groupby</span></code> function. But when working with spatial data, we need a special tool that can also aggregate geometric features. In the <em>geopandas</em> library, that functionality is provided by the <code class="docutils literal"><span class="pre">dissolve</span></code> function.</p>
<p><code class="docutils literal"><span class="pre">dissolve</span></code> can be thought of as doing three things: (a) it dissolves all the geometries within a given group together into a single geometric feature (using the <code class="docutils literal"><span class="pre">unary_union</span></code> method), and (b) it aggregates all the rows of data in a group using <code class="docutils literal"><span class="pre">groupby.aggregate()</span></code>, and (c) it combines those two results.</p>
<div class="section" id="dissolve-example">
<h2><code class="docutils literal"><span class="pre">dissolve</span></code> Example<a class="headerlink" href="#dissolve-example" title="Permalink to this headline"></a></h2>
<p>Suppose we are interested in studying continents, but we only have country-level data like the country dataset included in <em>geopandas</em>. We can easily convert this to a continent-level dataset.</p>
<p>First, let&#8217;s look at the most simple case where we just want continent shapes and names. By default, <code class="docutils literal"><span class="pre">dissolve</span></code> will pass <code class="docutils literal"><span class="pre">'first'</span></code> to <code class="docutils literal"><span class="pre">groupby.aggregate</span></code>.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="n">world</span> <span class="o">=</span> <span class="n">gpd</span><span class="o">.</span><span class="n">read_file</span><span class="p">(</span><span class="n">gpd</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s">&#39;naturalearth_lowres&#39;</span><span class="p">))</span>
<span class="gp">In [2]: </span><span class="n">world</span> <span class="o">=</span> <span class="n">world</span><span class="p">[[</span><span class="s">&#39;continent&#39;</span><span class="p">,</span> <span class="s">&#39;geometry&#39;</span><span class="p">]]</span>
<span class="gp">In [3]: </span><span class="n">continents</span> <span class="o">=</span> <span class="n">world</span><span class="o">.</span><span class="n">dissolve</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="s">&#39;continent&#39;</span><span class="p">)</span>
<span class="gp">In [4]: </span><span class="n">continents</span><span class="o">.</span><span class="n">plot</span><span class="p">();</span>
<span class="gp">In [5]: </span><span class="n">continents</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gh">Out[5]: </span><span class="go"></span>
<span class="go"> geometry</span>
<span class="go">continent </span>
<span class="go">Africa (POLYGON ((49.54351891459575 -12.4698328589405...</span>
<span class="go">Antarctica (POLYGON ((-159.2081835601977 -79.497059421708...</span>
<span class="go">Asia (POLYGON ((120.7156087586305 -10.2395813940878...</span>
<span class="go">Europe (POLYGON ((-52.55642473001839 2.50470530843705...</span>
<span class="go">North America (POLYGON ((-61.68000000000001 10.76, -61.105 1...</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/continents.png"><img alt="_images/continents.png" src="_images/continents.png" style="width: 5in;" /></a>
<p>If we are interested in aggregate populations, however, we can pass different functions to the <code class="docutils literal"><span class="pre">dissolve</span></code> method to aggregate populations:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [6]: </span><span class="n">world</span> <span class="o">=</span> <span class="n">gpd</span><span class="o">.</span><span class="n">read_file</span><span class="p">(</span><span class="n">gpd</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s">&#39;naturalearth_lowres&#39;</span><span class="p">))</span>
<span class="gp">In [7]: </span><span class="n">world</span> <span class="o">=</span> <span class="n">world</span><span class="p">[[</span><span class="s">&#39;continent&#39;</span><span class="p">,</span> <span class="s">&#39;geometry&#39;</span><span class="p">,</span> <span class="s">&#39;pop_est&#39;</span><span class="p">]]</span>
<span class="gp">In [8]: </span><span class="n">continents</span> <span class="o">=</span> <span class="n">world</span><span class="o">.</span><span class="n">dissolve</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="s">&#39;continent&#39;</span><span class="p">,</span> <span class="n">aggfunc</span><span class="o">=</span><span class="s">&#39;sum&#39;</span><span class="p">)</span>
<span class="gp">In [9]: </span><span class="n">continents</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">column</span> <span class="o">=</span> <span class="s">&#39;pop_est&#39;</span><span class="p">,</span> <span class="n">scheme</span><span class="o">=</span><span class="s">&#39;quantiles&#39;</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="s">&#39;YlOrRd&#39;</span><span class="p">);</span>
<span class="gp">In [10]: </span><span class="n">continents</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gh">Out[10]: </span><span class="go"></span>
<span class="go"> geometry pop_est</span>
<span class="go">continent </span>
<span class="go">Africa (POLYGON ((49.54351891459575 -12.4698328589405... 993281878</span>
<span class="go">Antarctica (POLYGON ((-159.2081835601977 -79.497059421708... 3802</span>
<span class="go">Asia (POLYGON ((120.7156087586305 -10.2395813940878... 4085852698</span>
<span class="go">Europe (POLYGON ((-52.55642473001839 2.50470530843705... 728131201</span>
<span class="go">North America (POLYGON ((-61.68000000000001 10.76, -61.105 1... 539350981</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/continents.png"><img alt="_images/continents.png" src="_images/continents.png" style="width: 5in;" /></a>
<div class="toctree-wrapper compound">
<ul class="simple">
</ul>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="mergingdata.html" class="btn btn-neutral float-right" title="Merging Data">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="set_operations.html" class="btn btn-neutral" title="Set-Operations with Overlay"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2013-2016, GeoPandas developers.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'0.2-py3.5.egg',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>