1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
.. _using_the_shapereader:
Using the cartopy shapereader
=============================
Cartopy provides an object oriented shapefile reader based on top of the
`pyshp`_ module to provide easy, programmatic, access to standard vector datasets.
Cartopy's wrapping of pyshp has the benefit of being pure python, and is therefore
easy to install and extremely portable. However, for heavy duty shapefile I/O `Fiona`_ and
`GeoPandas`_ are highly recommended.
.. _pyshp: https://github.com/GeospatialPython/pyshp
.. _Fiona: https://fiona.readthedocs.io/
.. _GeoPandas: http://geopandas.org/
Detailed API for the shapereader functionality can be found in the :ref:`documentation <api.io.shapereader>`
.. currentmodule:: cartopy.io.shapereader
Helper functions for shapefile acquisition
-------------------------------------------
Cartopy provides an interface for access to frequently used data such as the
`GSHHS <https://www.ngdc.noaa.gov/mgg/shorelines/gshhs.html>`_ dataset and from
the `NaturalEarthData <https://www.naturalearthdata.com/>`_ website.
These interfaces allow the user to define the data programmatically, and if the data does not exist
on disk, it will be retrieved from the appropriate source (normally by
downloading the data from the internet). Currently the interfaces available are
:func:`natural_earth` and :func:`gshhs`.
Using the shapereader
---------------------
We can acquire the countries dataset from Natural Earth found at
https://www.naturalearthdata.com/downloads/110m-cultural-vectors/110m-admin-0-countries/
by using the :func:`natural_earth` function:
.. testcode:: countries
import cartopy.io.shapereader as shpreader
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
From here, we can make use of the :class:`Reader` to get the first country
in the shapefile:
.. testcode:: countries
reader = shpreader.Reader(shpfilename)
countries = reader.records()
country = next(countries)
We can get the country's attributes dictionary with the
:data:`Record.attributes` attribute:
.. doctest:: countries
:options: +ELLIPSIS
>>> print type(country.attributes)
<type 'dict'>
>>> print sorted(country.attributes.keys())
['abbrev', ..., 'name_long', ... 'pop_est', ...]
We could now find the 5 least populated countries with:
.. testcode:: countries
reader = shpreader.Reader(shpfilename)
# define a function which returns the population given the country
population = lambda country: country.attributes['pop_est']
# sort the countries by population and get the first 5
countries_by_pop = sorted(reader.records(), key=population)[:5]
Which we can print with
.. doctest:: countries
>>> ', '.join([country.attributes['name_long']
... for country in countries_by_pop])
'Western Sahara, French Southern and Antarctic Lands, Falkland Islands, Antarctica, Greenland'
**Exercises**:
* **SHP.1**: Repeat the last example to show the 4 most populated African countries in to the shapefile.
Hint: Look at the possible attributes to find out which continent a country belongs.
Answer:
.. testcode:: countries
:hide:
reader = shpreader.Reader(shpfilename)
# define a function which can return the population of a given country
population = lambda country: country.attributes['pop_est']
# sort the countries by population
countries_by_pop = sorted(reader.records(), key=population)
# define a function which can return whether a country belongs to Africa
is_african = lambda country: country.attributes['continent'] == 'Africa'
# remove non-African countries
african_countries = filter(is_african, countries_by_pop)
print ', '.join([country.attributes['name_long']
for country in african_countries[-4:]])
.. testoutput:: countries
Democratic Republic of the Congo, Egypt, Ethiopia, Nigeria
* **SHP.2**: Using the countries shapefile, find the most populated country grouped
by the first letter of the "name_long".
Hint: :func:`itertools.groupby` can help with the grouping.
Answer:
.. testcode:: countries
:hide:
import itertools
reader = shpreader.Reader(shpfilename)
# define a function which returns the first letter of a country's name
first_letter = lambda country: country.attributes['name_long'][0]
# define a function which returns the population of a country
population = lambda country: country.attributes['pop_est']
# sort the countries so that the groups come out alphabetically
countries = sorted(reader.records(), key=first_letter)
# group the countries by first letter
for letter, countries in itertools.groupby(countries, key=first_letter):
# print the letter and least populated country
print letter, sorted(countries, key=population)[-1].attributes['name_long']
.. testoutput:: countries
A Argentina
B Brazil
C China
D Democratic Republic of the Congo
E Ethiopia
F France
G Germany
H Hungary
I India
J Japan
K Kenya
L Lao PDR
M Mexico
N Nigeria
O Oman
P Pakistan
Q Qatar
R Russian Federation
S South Africa
T Turkey
U United States
V Vietnam
W Western Sahara
Y Yemen
Z Zimbabwe
|