geopandas/geopandas — pull request #146 by jwass (2014-07-23 12:53:37+00:00)
Title: Explode collect

A couple of additional tools here:
explode() expands multi-part geometries into multiple rows, and into
their single part geometries. This returns a `GeoSeries` with a `MultiIndex`.

collect() - take multiple single geometries and combine them into their
Multi* counterparts.

`explode()` is a new member of `GeoPandasBase`, but could also be a separate function in `tools` like `collect()` is. These are similar to PostGIS `ST_Dump` and `ST_Collect` functions.

To use `MultiIndex` you can use `.loc` on the first level to get back:
```
>>> singles = df.explode()
0 0 POLYGON ((970217.0223999023 145643.3322143555,...
1 POLYGON ((969488.1658325195 149753.5946044922,...
2 POLYGON ((939997.0946044922 173013.5794067383,...
3 POLYGON ((961436.3049926758 175473.0296020508,...
1 0 POLYGON ((1021176.479003906 151374.7969970703,...
1 POLYGON ((1020482.590393066 157430.9501953125,...
2 POLYGON ((1006493.460205078 157737.2102050781,...
...

>>> singles.loc[0]
0 POLYGON ((970217.0223999023 145643.3322143555,...
1 POLYGON ((969488.1658325195 149753.5946044922,...
2 POLYGON ((939997.0946044922 173013.5794067383,...
3 POLYGON ((961436.3049926758 175473.0296020508,...
dtype: object
```

Using the NYC boros examples you can do some neat things quickly:

Take the polygon with the greatest area from each multipolygon, preserving the index:
```
>>> df.explode().groupby(level=0).apply(lambda x: x.loc[x.area.argmax()])
0 POLYGON ((961436.3049926758 175473.0296020508,...
1 POLYGON ((996887.8187866211 208559.3403930664,...
2 POLYGON ((1033946.682983398 231157.9963989258,...
3 POLYGON ((1004601.953430176 259027.5151977539,...
4 POLYGON ((1019370.870605469 268815.8876342773,...
dtype: object
```

Remove any polygons with small perimeters from each multipolygon, recombine to MultiPolygon:
```
>>> df.explode().groupby(level=0).apply(lambda x: collect(x[x.exterior.length >= 3000]))
0 (POLYGON ((969488.1658325195 149753.5946044922...
1 (POLYGON ((1021176.479003906 151374.7969970703...
2 (POLYGON ((1029606.076599121 156073.8142089844...
3 (POLYGON ((972081.7882080078 190733.4674072266...
4 (POLYGON ((1015023.713439941 230286.7592163086...
dtype: object
```