GeoPandas - IO, projections, plotting

GeoPandas 1

In [1]:
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('bmh') # better for plotting geometries vs general plots.

from shapely.geometry import Point, Polygon, LineString
import pandas as pd
import geopandas as gpd
from geopandas import GeoSeries, GeoDataFrame

Geometry types

Geopandas uses shapely.geometry geometry objects. Geopandas has 6 types of geometry objects

  • Point
  • Line (LineString)
  • Polygon
  • Multi-Point
  • Multi-Line
  • Multi-Polygon

Gotchas

  • Geopandas is a growing project and its API could change over time
  • Geopandas does not restrict or check for consistency in geometry type of its series.

Constructing GeoSeries from Shapely Points

In [2]:
point_list = [Point(-120,45), Point(-121.2, 46), Point(-122.9, 47.5)]
gs = GeoSeries(point_list)
gs
Out[2]:
0        POINT (-120 45)
1      POINT (-121.2 46)
2    POINT (-122.9 47.5)
dtype: object
In [3]:
type(gs)
Out[3]:
geopandas.geoseries.GeoSeries
In [4]:
gs.geometry
Out[4]:
0        POINT (-120 45)
1      POINT (-121.2 46)
2    POINT (-122.9 47.5)
dtype: object
In [5]:
gs.geom_type
Out[5]:
0    Point
1    Point
2    Point
dtype: object
In [6]:
gs.iloc[0]
Out[6]:
In [8]:
gs.crs = {'init':'epsg:4326'}
Plot the geometries

This internally uses descartes and matplotlib

In [9]:
gs.plot()
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a0e833080>

Constructing GeoSeries from a Pandas DataFrame

In [16]:
data_dict = {'name':['a','b','c'],
            'lat':[45,46,47.5],
            'lon':[-120,-121.2,-123]}

df = pd.DataFrame(data_dict)
df.set_index(df['name'], inplace=True)
df
Out[16]:
lat lon name
name
a 45.0 -120.0 a
b 46.0 -121.2 b
c 47.5 -123.0 c
In [17]:
[xy for xy in zip(df['lon'], df['lat'])]
Out[17]:
[(-120.0, 45.0), (-121.2, 46.0), (-123.0, 47.5)]
In [18]:
point_list = [Point(xy) for xy in zip(df['lon'], df['lat'])]
point_list
Out[18]:
[<shapely.geometry.point.Point at 0x1a0e8af5c0>,
 <shapely.geometry.point.Point at 0x1a19123400>,
 <shapely.geometry.point.Point at 0x1a191237f0>]
In [20]:
gdf = GeoDataFrame(df, geometry=point_list)
gdf
Out[20]:
lat lon name geometry
name
a 45.0 -120.0 a POINT (-120 45)
b 46.0 -121.2 b POINT (-121.2 46)
c 47.5 -123.0 c POINT (-123 47.5)
In [21]:
gdf.plot()
Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a191bd9b0>

Reading a SHP file

In [22]:
fire_stations = gpd.read_file('./data/FireStations.shp')
type(fire_stations)
Out[22]:
geopandas.geodataframe.GeoDataFrame
In [23]:
fire_stations.head()
Out[23]:
OBJECTID post_id Name descriptio cat1 cat2 cat3 addrln1 addrln2 city source ext_id use_type dis_status latitude longitude date_updat POINT_X POINT_Y geometry
0 40223266 2022.0 Los Angeles County Fire Department - Battalion… The Battalion provides fire and rescue service… Public Safety Fire Stations None 7643 W. Santa Monica Blvd. None Los Angeles 211 None publish None 34.090940 -118.356388 2013-06-01 6.453766e+06 1.855666e+06 POINT (6453766.230470523 1855666.492027521)
1 40223440 2236.0 Los Angeles County Fire Department - Battalion… The Battalion provides fire and rescue service… Public Safety Fire Stations Battalion HQ 6031 Rickenbacker Rd. None Commerce 211 None publish None 33.988587 -118.154162 2013-06-01 6.514937e+06 1.818263e+06 POINT (6514937.276763365 1818262.767294437)
2 40223442 2237.0 Los Angeles County Fire Department - Battalion… The Battalion provides fire and rescue service… Public Safety Fire Stations None 1059 N. White Ave. None Pomona 211 None publish None 34.067555 -117.759366 2013-06-01 6.634548e+06 1.847052e+06 POINT (6634547.880061358 1847052.198560596)
3 40223459 2262.0 Los Angeles County Fire Department - Battalion… The Battalion provides fire and rescue service… Public Safety Fire Stations None 1260 Encinal Canyon Rd None Malibu 211 None publish None 34.084350 -118.866037 2013-06-01 6.299441e+06 1.854207e+06 POINT (6299441.115893021 1854206.637991846)
4 40223463 2269.0 Los Angeles County Fire Department - Battalion… The Battalion provides fire and rescue service… Public Safety Fire Stations Battalion HQ 6301 S. Santa Fe Ave. None Huntington Park 211 None publish None 33.983196 -118.230626 2013-06-01 6.491753e+06 1.816345e+06 POINT (6491753.321819022 1816345.240289599)

5 rows × 30 columns

In [24]:
fire_stations.crs
Out[24]:
{'proj': 'lcc',
 'lat_1': 34.03333333333333,
 'lat_2': 35.46666666666667,
 'lat_0': 33.5,
 'lon_0': -118,
 'x_0': 2000000,
 'y_0': 500000.0000000001,
 'datum': 'NAD83',
 'units': 'us-ft',
 'no_defs': True}

Data exploration using pandas

In [28]:
fire_stations.columns
Out[28]:
Index(['OBJECTID', 'post_id', 'Name', 'descriptio', 'cat1', 'cat2', 'cat3',
       'addrln1', 'addrln2', 'city', 'state', 'zip', 'hours', 'phones', 'url',
       'email', 'info1', 'info2', 'link', 'org_name', 'source', 'ext_id',
       'use_type', 'dis_status', 'latitude', 'longitude', 'date_updat',
       'POINT_X', 'POINT_Y', 'geometry'],
      dtype='object')
In [67]:
fire_stations['city'].value_counts().head()
Out[67]:
Los Angeles      65
Long Beach       25
Santa Clarita    10
Pasadena         10
Pomona            9
Name: city, dtype: int64

Plotting

In [39]:
fire_stations.plot(figsize=(10,10))
Out[39]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a19347048>
Overlay USA map

Read the bundled dataset and overlay it on top of this

In [41]:
gpd.datasets.available
Out[41]:
['naturalearth_cities', 'naturalearth_lowres', 'nybb']
In [42]:
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.head()
Out[42]:
pop_est continent name iso_a3 gdp_md_est geometry
0 28400000.0 Asia Afghanistan AFG 22270.0 POLYGON ((61.21081709172574 35.65007233330923,…
1 12799293.0 Africa Angola AGO 110300.0 (POLYGON ((16.32652835456705 -5.87747039146621…
2 3639453.0 Europe Albania ALB 21810.0 POLYGON ((20.59024743010491 41.85540416113361,…
3 4798491.0 Asia United Arab Emirates ARE 184300.0 POLYGON ((51.57951867046327 24.24549713795111,…
4 40913584.0 South America Argentina ARG 573900.0 (POLYGON ((-65.50000000000003 -55.199999999999…
In [45]:
world.shape
Out[45]:
(177, 6)
In [46]:
world['name'].unique()
Out[46]:
array(['Afghanistan', 'Angola', 'Albania', 'United Arab Emirates',
       'Argentina', 'Armenia', 'Antarctica', 'Fr. S. Antarctic Lands',
       'Australia', 'Austria', 'Azerbaijan', 'Burundi', 'Belgium',
       'Benin', 'Burkina Faso', 'Bangladesh', 'Bulgaria', 'Bahamas',
       'Bosnia and Herz.', 'Belarus', 'Belize', 'Bolivia', 'Brazil',
       'Brunei', 'Bhutan', 'Botswana', 'Central African Rep.', 'Canada',
       'Switzerland', 'Chile', 'China', "Côte d'Ivoire", 'Cameroon',
       'Dem. Rep. Congo', 'Congo', 'Colombia', 'Costa Rica', 'Cuba',
       'N. Cyprus', 'Cyprus', 'Czech Rep.', 'Germany', 'Djibouti',
       'Denmark', 'Dominican Rep.', 'Algeria', 'Ecuador', 'Egypt',
       'Eritrea', 'Spain', 'Estonia', 'Ethiopia', 'Finland', 'Fiji',
       'Falkland Is.', 'France', 'Gabon', 'United Kingdom', 'Georgia',
       'Ghana', 'Guinea', 'Gambia', 'Guinea-Bissau', 'Eq. Guinea',
       'Greece', 'Greenland', 'Guatemala', 'Guyana', 'Honduras',
       'Croatia', 'Haiti', 'Hungary', 'Indonesia', 'India', 'Ireland',
       'Iran', 'Iraq', 'Iceland', 'Israel', 'Italy', 'Jamaica', 'Jordan',
       'Japan', 'Kazakhstan', 'Kenya', 'Kyrgyzstan', 'Cambodia', 'Korea',
       'Kosovo', 'Kuwait', 'Lao PDR', 'Lebanon', 'Liberia', 'Libya',
       'Sri Lanka', 'Lesotho', 'Lithuania', 'Luxembourg', 'Latvia',
       'Morocco', 'Moldova', 'Madagascar', 'Mexico', 'Macedonia', 'Mali',
       'Myanmar', 'Montenegro', 'Mongolia', 'Mozambique', 'Mauritania',
       'Malawi', 'Malaysia', 'Namibia', 'New Caledonia', 'Niger',
       'Nigeria', 'Nicaragua', 'Netherlands', 'Norway', 'Nepal',
       'New Zealand', 'Oman', 'Pakistan', 'Panama', 'Peru', 'Philippines',
       'Papua New Guinea', 'Poland', 'Puerto Rico', 'Dem. Rep. Korea',
       'Portugal', 'Paraguay', 'Palestine', 'Qatar', 'Romania', 'Russia',
       'Rwanda', 'W. Sahara', 'Saudi Arabia', 'Sudan', 'S. Sudan',
       'Senegal', 'Solomon Is.', 'Sierra Leone', 'El Salvador',
       'Somaliland', 'Somalia', 'Serbia', 'Suriname', 'Slovakia',
       'Slovenia', 'Sweden', 'Swaziland', 'Syria', 'Chad', 'Togo',
       'Thailand', 'Tajikistan', 'Turkmenistan', 'Timor-Leste',
       'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Taiwan', 'Tanzania',
       'Uganda', 'Ukraine', 'Uruguay', 'United States', 'Uzbekistan',
       'Venezuela', 'Vietnam', 'Vanuatu', 'Yemen', 'South Africa',
       'Zambia', 'Zimbabwe'], dtype=object)
In [47]:
usa = world[world['name']=='United States']
usa
Out[47]:
pop_est continent name iso_a3 gdp_md_est geometry
168 313973000.0 North America United States USA 15094000.0 (POLYGON ((-155.54211 19.08348000000001, -155….
In [49]:
ax = fire_stations.plot(figsize=(10,10))
usa.plot(ax = ax)
Out[49]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a1939fc18>
In [50]:
usa.plot()
Out[50]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a193be9e8>
In [51]:
usa.crs
Out[51]:
{'init': 'epsg:4326'}

Snap! They are in different projections and geopandas does not handle that. SO lets try to reproject the fire stations to match usa

Reprojection

Projecting the fire stations layer to match the world dataset from geopandas

In [52]:
fire_stations_gcs = fire_stations.to_crs(epsg=4326)
In [53]:
fire_stations_gcs.crs
Out[53]:
{'init': 'epsg:4326', 'no_defs': True}
In [54]:
fire_stations_gcs.plot()
Out[54]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a193ec0f0>
In [60]:
fire_stations_gcs.total_bounds
Out[60]:
array([-118.88357856,   33.32376846, -117.70773665,   34.75793147])

Overlay plot

In [66]:
f, ax = plt.subplots(1, figsize=(12, 12))
ax.set_title('Fire stations in Southern California')
usa.plot(ax=ax, facecolor='lightgray', edgecolor='gray')
fire_stations_gcs.plot(ax=ax)

#this plots the whole US. To limit the scale to just SoCal, limit the axes
ax.set_xlim([-120, -118])
ax.set_ylim([33, 35])
Out[66]:
(33, 35)

Filtering and writing data

In this part, we will read the Damselfish shape file, do some filtering, column calculations and split the shape file into many smaller ones grouped by a column.

Read from Shape file

In [2]:
damsel_gdf = gpd.read_file('./data/damselfish/DAMSELFISH_distributions.shp')
damsel_gdf.head(3)
Out[2]:
ID_NO BINOMIAL ORIGIN COMPILER YEAR CITATION SOURCE DIST_COMM ISLAND SUBSPECIES RL_UPDATE KINGDOM_NA PHYLUM_NAM CLASS_NAME ORDER_NAME FAMILY_NAM GENUS_NAME SPECIES_NA CATEGORY geometry
0 183963.0 Stegastes leucorus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes leucorus VU POLYGON ((-115.6437454219999 29.71392059300007…
1 183963.0 Stegastes leucorus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes leucorus VU POLYGON ((-105.589950704 21.89339825500002, -1…
2 183963.0 Stegastes leucorus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes leucorus VU POLYGON ((-111.159618439 19.01535626700007, -1…

3 rows × 24 columns

In [3]:
damsel_gdf.columns
Out[3]:
Index(['ID_NO', 'BINOMIAL', 'ORIGIN', 'COMPILER', 'YEAR', 'CITATION', 'SOURCE',
       'DIST_COMM', 'ISLAND', 'SUBSPECIES', 'SUBPOP', 'LEGEND', 'SEASONAL',
       'TAX_COMM', 'RL_UPDATE', 'KINGDOM_NA', 'PHYLUM_NAM', 'CLASS_NAME',
       'ORDER_NAME', 'FAMILY_NAM', 'GENUS_NAME', 'SPECIES_NA', 'CATEGORY',
       'geometry'],
      dtype='object')
In [4]:
damsel_gdf.shape
Out[4]:
(231, 24)

Plot a map by category

In [17]:
damsel_gdf.plot(column='BINOMIAL', categorical=True, 
                figsize=(12, 4))
Out[17]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a1d4a9eb8>

Groupby a column

The BINOMIAL column contains the full genus and species names of the fishes. We could split the shape file by each fish type

In [5]:
damsel_gdf['BINOMIAL'].value_counts()
Out[5]:
Amphiprion sandaracinos    51
Chromis cyanea             15
Chromis alpha              14
Chromis alta               11
Microspathodon bairdii     10
Abudefduf troschelii       10
Stegastes acapulcoensis     9
Chromis atrilobata          9
Microspathodon dorsalis     9
Stegastes arcifrons         8
Stegastes flavilatus        8
Stegastes beebei            8
Chromis limbaughi           7
Teixeirichthys jordani      7
Nexilosus latifrons         7
Abudefduf concolor          6
Chrysiptera flavipinnis     5
Azurina hirundo             5
Hypsypops rubicundus        4
Chromis pembae              4
Stegastes leucorus          3
Chromis intercrusma         3
Abudefduf declivifrons      3
Stegastes rectifraenum      3
Azurina eupalama            3
Chromis crusma              3
Chromis flavicauda          2
Stegastes redemptus         2
Stegastes baldwini          1
Chromis punctipinnis        1
Name: BINOMIAL, dtype: int64
In [6]:
grouped = damsel_gdf.groupby(by='BINOMIAL')
grouped.head()
Out[6]:
ID_NO BINOMIAL ORIGIN COMPILER YEAR CITATION SOURCE DIST_COMM ISLAND SUBSPECIES RL_UPDATE KINGDOM_NA PHYLUM_NAM CLASS_NAME ORDER_NAME FAMILY_NAM GENUS_NAME SPECIES_NA CATEGORY geometry
0 183963.0 Stegastes leucorus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes leucorus VU POLYGON ((-115.6437454219999 29.71392059300007…
1 183963.0 Stegastes leucorus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes leucorus VU POLYGON ((-105.589950704 21.89339825500002, -1…
2 183963.0 Stegastes leucorus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes leucorus VU POLYGON ((-111.159618439 19.01535626700007, -1…
3 183793.0 Chromis intercrusma 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis intercrusma LC POLYGON ((-80.86500229899997 -0.77894492099994…
4 183793.0 Chromis intercrusma 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis intercrusma LC POLYGON ((-67.33922225599997 -55.6761029239999…
5 183793.0 Chromis intercrusma 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis intercrusma LC POLYGON ((-74.81822204599996 -51.4608230589999…
6 183462.0 Stegastes beebei 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes beebei VU POLYGON ((-86.13105102199995 5.598867493000057…
7 183462.0 Stegastes beebei 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes beebei VU POLYGON ((-80.68215272899994 4.037426486000072…
8 183462.0 Stegastes beebei 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes beebei VU POLYGON ((-90.22271068899994 -0.27334342499995…
9 183462.0 Stegastes beebei 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes beebei VU POLYGON ((-83.93571481699996 9.296388679000074…
10 183462.0 Stegastes beebei 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes beebei VU POLYGON ((-79.15961456299993 9.011326790000055…
14 183586.0 Stegastes rectifraenum 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes rectifraenum LC POLYGON ((-114.129066467 31.49156189000007, -1…
15 183586.0 Stegastes rectifraenum 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes rectifraenum LC POLYGON ((-106.7327677389999 23.60688894500004…
16 183586.0 Stegastes rectifraenum 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes rectifraenum LC POLYGON ((-111.159618439 19.01535626700007, -1…
17 154831.0 Chromis punctipinnis 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis punctipinnis LC POLYGON ((-121.799003601 36.75037002600004, -1…
18 183240.0 Chromis crusma 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis crusma LC POLYGON ((-80.82860578399999 -1.22984210599997…
19 183240.0 Chromis crusma 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis crusma LC POLYGON ((-67.33922225599997 -55.6761029239999…
20 183240.0 Chromis crusma 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis crusma LC POLYGON ((-74.81822204599996 -51.4608230589999…
21 154856.0 Chromis pembae 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis pembae LC POLYGON ((32.56497192400008 29.96966743400003,…
22 154856.0 Chromis pembae 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis pembae LC POLYGON ((55.90029544000004 -3.630027441999971…
23 154856.0 Chromis pembae 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis pembae LC POLYGON ((73.86925448500006 7.187452539000049,…
24 154856.0 Chromis pembae 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis pembae LC POLYGON ((39.89708328200004 16.32163810700007,…
25 183567.0 Stegastes redemptus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes redemptus VU POLYGON ((-112.128707413 25.07043759500004, -1…
26 183567.0 Stegastes redemptus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes redemptus VU POLYGON ((-111.159618439 19.01535626700007, -1…
27 154915.0 Teixeirichthys jordani 1 None 2012 Red List Index (Sampled Approach), Zoological … None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Teixeirichthys jordani LC POLYGON ((121.6300326400001 33.04248618400004,…
28 154915.0 Teixeirichthys jordani 1 None 2012 Red List Index (Sampled Approach), Zoological … None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Teixeirichthys jordani LC POLYGON ((32.56219482400007 29.97488975500005,…
29 154915.0 Teixeirichthys jordani 1 None 2012 Red List Index (Sampled Approach), Zoological … None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Teixeirichthys jordani LC POLYGON ((130.9052090560001 34.02498196400006,…
30 154915.0 Teixeirichthys jordani 1 None 2012 Red List Index (Sampled Approach), Zoological … None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Teixeirichthys jordani LC POLYGON ((56.32233070000007 -3.707270205999976…
31 154915.0 Teixeirichthys jordani 1 None 2012 Red List Index (Sampled Approach), Zoological … None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Teixeirichthys jordani LC POLYGON ((40.64476131800006 -10.85502363999996…
34 183452.0 Chromis limbaughi 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis limbaughi LC POLYGON ((-112.417701721 29.37484550400006, -1…
187 183483.0 Abudefduf concolor 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf concolor LC POLYGON ((-91.09391784599995 -0.58096068999992…
189 183397.0 Abudefduf troschelii 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf troschelii LC POLYGON ((-90.09187344499998 13.72481517500006…
190 183397.0 Abudefduf troschelii 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf troschelii LC POLYGON ((-121.284812927 35.67465591400003, -1…
191 183397.0 Abudefduf troschelii 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf troschelii LC POLYGON ((-106.72576642 23.59774532600005, -10…
192 183397.0 Abudefduf troschelii 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf troschelii LC POLYGON ((-86.13105102199995 5.598867493000057…
193 183397.0 Abudefduf troschelii 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf troschelii LC POLYGON ((-111.159618439 19.01535626700007, -1…
199 154920.0 Chrysiptera flavipinnis 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chrysiptera flavipinnis LC POLYGON ((144.147216637 -7.778888740999946, 14…
200 154920.0 Chrysiptera flavipinnis 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chrysiptera flavipinnis LC POLYGON ((156.671737653 -6.481586890999949, 15…
201 154920.0 Chrysiptera flavipinnis 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chrysiptera flavipinnis LC POLYGON ((164.5374754960001 -20.08774569199994…
202 154920.0 Chrysiptera flavipinnis 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chrysiptera flavipinnis LC POLYGON ((151.2978177690001 -16.88691080299998…
203 154920.0 Chrysiptera flavipinnis 1 None 2012 International Union for Conservation of Nature… None None None None 2012.2 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chrysiptera flavipinnis LC POLYGON ((159.58249191 -21.61306719099997, 159…
204 183653.0 Chromis atrilobata 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis atrilobata LC POLYGON ((-90.09187344499998 13.72481517500006…
205 183653.0 Chromis atrilobata 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis atrilobata LC POLYGON ((-113.422749196 30.12412492800007, -1…
206 183653.0 Chromis atrilobata 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis atrilobata LC POLYGON ((-106.72576642 23.59774532600005, -10…
207 183653.0 Chromis atrilobata 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis atrilobata LC POLYGON ((-86.13105102199995 5.598867493000057…
208 183653.0 Chromis atrilobata 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Chromis atrilobata LC POLYGON ((-111.159618439 19.01535626700007, -1…
213 183431.0 Stegastes acapulcoensis 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes acapulcoensis LC POLYGON ((-90.09187344499998 13.72481517500006…
214 183431.0 Stegastes acapulcoensis 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes acapulcoensis LC POLYGON ((-106.726318976 23.59928014100007, -1…
215 183431.0 Stegastes acapulcoensis 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes acapulcoensis LC POLYGON ((-109.2706279609999 26.29294047400003…
216 183431.0 Stegastes acapulcoensis 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes acapulcoensis LC POLYGON ((-86.13105102199995 5.598867493000057…
217 183431.0 Stegastes acapulcoensis 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Stegastes acapulcoensis LC POLYGON ((-111.170232026 19.01577707400003, -1…
222 183367.0 Hypsypops rubicundus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Hypsypops rubicundus LC POLYGON ((-124.1523361 41.05706766400004, -124…
223 183367.0 Hypsypops rubicundus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Hypsypops rubicundus LC POLYGON ((-117.4136322419999 29.18823463600006…
224 183367.0 Hypsypops rubicundus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Hypsypops rubicundus LC POLYGON ((-117.330311632 21.41966897300006, -1…
225 183367.0 Hypsypops rubicundus 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Hypsypops rubicundus LC POLYGON ((-110.714172363 25.30718421900008, -1…
226 183774.0 Azurina hirundo 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Azurina hirundo NT POLYGON ((-120.118286133 34.47283172600004, -1…
227 183774.0 Azurina hirundo 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Azurina hirundo NT POLYGON ((-117.4136322419999 29.18823463600006…
228 183774.0 Azurina hirundo 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Azurina hirundo NT POLYGON ((-114.638387688 28.39007998000005, -1…
229 183774.0 Azurina hirundo 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Azurina hirundo NT POLYGON ((-111.159618439 19.01535626700007, -1…
230 183774.0 Azurina hirundo 1 IUCN 2010 International Union for Conservation of Nature… None None None None 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Azurina hirundo NT POLYGON ((-114.244282766 22.71134688500007, -1…

122 rows × 24 columns

In [9]:
type(grouped), type(grouped['BINOMIAL'])
Out[9]:
(pandas.core.groupby.DataFrameGroupBy, pandas.core.groupby.SeriesGroupBy)

Split by column value and write to disk

Create output folder is not available

In [10]:
!mkdir './data/fish_split'

We can iterate over the DataFrameGroupBy object and perform aggregations like std, mean, sum etc. However, here, we can split by column and write each individual fish species to its own shape file.

In [12]:
import os
for key, value in grouped:
    # create output name using key. Replace space with _
    out_name = '{}.shp'.format(key.replace(" ","_"))
    print("Writing: " + out_name)
    
    #write to disk using geopandas
    value.to_file(os.path.join("./data/fish_split", out_name))
Writing: Abudefduf_concolor.shp
Writing: Abudefduf_declivifrons.shp
Writing: Abudefduf_troschelii.shp
Writing: Amphiprion_sandaracinos.shp
Writing: Azurina_eupalama.shp
Writing: Azurina_hirundo.shp
Writing: Chromis_alpha.shp
Writing: Chromis_alta.shp
Writing: Chromis_atrilobata.shp
Writing: Chromis_crusma.shp
Writing: Chromis_cyanea.shp
Writing: Chromis_flavicauda.shp
Writing: Chromis_intercrusma.shp
Writing: Chromis_limbaughi.shp
Writing: Chromis_pembae.shp
Writing: Chromis_punctipinnis.shp
Writing: Chrysiptera_flavipinnis.shp
Writing: Hypsypops_rubicundus.shp
Writing: Microspathodon_bairdii.shp
Writing: Microspathodon_dorsalis.shp
Writing: Nexilosus_latifrons.shp
Writing: Stegastes_acapulcoensis.shp
Writing: Stegastes_arcifrons.shp
Writing: Stegastes_baldwini.shp
Writing: Stegastes_beebei.shp
Writing: Stegastes_flavilatus.shp
Writing: Stegastes_leucorus.shp
Writing: Stegastes_rectifraenum.shp
Writing: Stegastes_redemptus.shp
Writing: Teixeirichthys_jordani.shp

Plot the last fish to see if its a smaller area than original:

In [14]:
print(key)
value.plot()
Teixeirichthys jordani
Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x110808eb8>

Dissolve polygons

An option is to dissolve each of the individual polygons into one for each fish. This way we can measure the area of each species. This is a proper geoprocessing implemented from Shapely lib.

In [ ]:
grouped.aggregate()
In [18]:
damsel_gdf_dissolved = damsel_gdf.dissolve(by='BINOMIAL', aggfunc='first')
damsel_gdf_dissolved.shape
Out[18]:
(30, 23)

This took a while, but we managed to shrink 231 features to 30 features. Let us plot and write it to disk for future processing.

In [20]:
damsel_gdf_dissolved.head(3)
Out[20]:
geometry ID_NO ORIGIN COMPILER YEAR CITATION SOURCE DIST_COMM ISLAND SUBSPECIES TAX_COMM RL_UPDATE KINGDOM_NA PHYLUM_NAM CLASS_NAME ORDER_NAME FAMILY_NAM GENUS_NAME SPECIES_NA CATEGORY
BINOMIAL
Abudefduf concolor (POLYGON ((-86.13105102199995 5.59886749300005… 183483.0 1 IUCN 2010 International Union for Conservation of Nature… NaN NaN NaN NaN NaN 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf concolor LC
Abudefduf declivifrons (POLYGON ((-90.09187344499998 13.7248151750000… 183460.0 1 IUCN 2010 International Union for Conservation of Nature… NaN NaN NaN NaN NaN 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf declivifrons LC
Abudefduf troschelii (POLYGON ((-91.24864959699994 -0.6798474789999… 183397.0 1 IUCN 2010 International Union for Conservation of Nature… NaN NaN NaN NaN NaN 2012.1 ANIMALIA CHORDATA ACTINOPTERYGII PERCIFORMES POMACENTRIDAE Abudefduf troschelii LC

3 rows × 23 columns

Make a choropleth map

Since the dataframe is reshaped by applying the categorical column as the index, I just create a simple choropleth map. Simply specify a cmap and that does the trick

In [24]:
damsel_gdf_dissolved.plot(cmap='Paired', figsize=(12,4))
Out[24]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a1eb3feb8>
Export to disk
In [25]:
damsel_gdf_dissolved.to_file('./data/damselfish/damselfish_dissolved.shp')
In [ ]: