mirror of
https://github.com/wassname/geopandas.git
synced 2026-09-20 12:50:47 +08:00
* DOC: added API links to intersphinx mapping * DOC: updated mergingdata.rst links * DOC: updated aggregation_with_dissolve.rst links * DOC: updated data_structures.rst links * DOC: updated geocoding.rst links * DOC: updated geometric_manipulations.rst links * DOC: updated indexing.rst links * DOC: updated io.rst links * DOC: updated projections.rst links * DOC: updated set_operations.rst links * DOC: updated mapping.rst links * DOC: updated missing_empty.rst links * DOC: updated geoplot intersphinx links * DOC: make 'unary_union' attr instead of method Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> * DOC: remove link to GeoDataFrame.geometry Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> * DOC: convert pandas indexers to attrs instead of methods Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> * DOC: convert indexer 'cx' to attr instead of method Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> * DOC: refer GeoSeries.buffer instead of shapely buffer Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> * DOC: make 'unary_union' attr instead of method on missing_empty.rst Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> * DOC: refer to DataFrame.merge instead of pandas.merge Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> * DOC: link pyproj.CRS * DOC: changed API links to 'stable' from 'latest' * DOC: fixed separator length * DOC: uppercase CRS in pyproj.crs Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
78 lines
3.0 KiB
ReStructuredText
78 lines
3.0 KiB
ReStructuredText
.. ipython:: python
|
|
:suppress:
|
|
|
|
import geopandas
|
|
import matplotlib
|
|
orig = matplotlib.rcParams['figure.figsize']
|
|
matplotlib.rcParams['figure.figsize'] = [orig[0] * 1.5, orig[1]]
|
|
|
|
|
|
Aggregation with dissolve
|
|
=============================
|
|
|
|
Spatial data are often more granular than we need. For example, we might have data on sub-national units, but we're actually interested in studying patterns at the level of countries.
|
|
|
|
In a non-spatial setting, when all we need are summary statistics of the data, we aggregate our data using the :meth:`~pandas.DataFrame.groupby` function. But for spatial data, we sometimes also need to aggregate geometric features. In the *geopandas* library, we can aggregate geometric features using the :meth:`~geopandas.GeoDataFrame.dissolve` function.
|
|
|
|
:meth:`~geopandas.GeoDataFrame.dissolve` 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 :attr:`~geopandas.GeoSeries.unary_union` method), and
|
|
(b) it aggregates all the rows of data in a group using :ref:`groupby.aggregate <groupby.aggregate>`, and
|
|
(c) it combines those two results.
|
|
|
|
:meth:`~geopandas.GeoDataFrame.dissolve` Example
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Suppose we are interested in studying continents, but we only have country-level data like the country dataset included in *geopandas*. We can easily convert this to a continent-level dataset.
|
|
|
|
|
|
First, let's look at the most simple case where we just want continent shapes and names. By default, :meth:`~geopandas.GeoDataFrame.dissolve` will pass ``'first'`` to :ref:`groupby.aggregate <groupby.aggregate>`.
|
|
|
|
.. ipython:: python
|
|
|
|
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
|
|
world = world[['continent', 'geometry']]
|
|
continents = world.dissolve(by='continent')
|
|
|
|
@savefig continents1.png
|
|
continents.plot();
|
|
|
|
continents.head()
|
|
|
|
If we are interested in aggregate populations, however, we can pass different functions to the :meth:`~geopandas.GeoDataFrame.dissolve` method to aggregate populations using the ``aggfunc =`` argument:
|
|
|
|
.. ipython:: python
|
|
|
|
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
|
|
world = world[['continent', 'geometry', 'pop_est']]
|
|
continents = world.dissolve(by='continent', aggfunc='sum')
|
|
|
|
@savefig continents2.png
|
|
continents.plot(column = 'pop_est', scheme='quantiles', cmap='YlOrRd');
|
|
|
|
continents.head()
|
|
|
|
|
|
.. ipython:: python
|
|
:suppress:
|
|
|
|
matplotlib.rcParams['figure.figsize'] = orig
|
|
|
|
|
|
.. toctree::
|
|
:maxdepth: 2
|
|
|
|
Dissolve Arguments
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
The ``aggfunc =`` argument defaults to 'first' which means that the first row of attributes values found in the dissolve routine will be assigned to the resultant dissolved geodataframe.
|
|
However it also accepts other summary statistic options as allowed by :meth:`pandas.groupby <pandas.DataFrame.groupby>` including:
|
|
|
|
* 'first'
|
|
* 'last'
|
|
* 'min'
|
|
* 'max'
|
|
* 'sum'
|
|
* 'mean'
|
|
* 'median'
|