/
create_geopandas_from_pandas.py
91 lines (67 loc) · 3.22 KB
/
create_geopandas_from_pandas.py
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
"""
Creating a GeoDataFrame from a DataFrame with coordinates
---------------------------------------------------------
This example shows how to create a ``GeoDataFrame`` when starting from
a *regular* ``DataFrame`` that has coordinates either WKT
(`well-known text <https://en.wikipedia.org/wiki/Well-known_text>`_)
format, or in
two columns.
"""
import pandas as pd
import geopandas
import matplotlib.pyplot as plt
###############################################################################
# From longitudes and latitudes
# =============================
#
# First, let's consider a ``DataFrame`` containing cities and their respective
# longitudes and latitudes.
df = pd.DataFrame(
{'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],
'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],
'Latitude': [-34.58, -15.78, -33.45, 4.60, 10.48],
'Longitude': [-58.66, -47.91, -70.66, -74.08, -66.86]})
###############################################################################
# A ``GeoDataFrame`` needs a ``shapely`` object. We use geopandas
# ``points_from_xy()`` to transform **Longitude** and **Latitude** into a list
# of ``shapely.Point`` objects and set it as a ``geometry`` while creating the
# ``GeoDataFrame``. (note that ``points_from_xy()`` is an enhanced wrapper for
# ``[Point(x, y) for x, y in zip(df.Longitude, df.Latitude)]``)
gdf = geopandas.GeoDataFrame(
df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))
###############################################################################
# ``gdf`` looks like this :
print(gdf.head())
###############################################################################
# Finally, we plot the coordinates over a country-level map.
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
# We restrict to South America.
ax = world[world.continent == 'South America'].plot(
color='white', edgecolor='black')
# We can now plot our ``GeoDataFrame``.
gdf.plot(ax=ax, color='red')
plt.show()
###############################################################################
# From WKT format
# ===============
# Here, we consider a ``DataFrame`` having coordinates in WKT format.
df = pd.DataFrame(
{'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],
'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],
'Coordinates': ['POINT(-58.66 -34.58)', 'POINT(-47.91 -15.78)',
'POINT(-70.66 -33.45)', 'POINT(-74.08 4.60)',
'POINT(-66.86 10.48)']})
###############################################################################
# We use ``shapely.wkt`` sub-module to parse wkt format:
from shapely import wkt
df['Coordinates'] = df['Coordinates'].apply(wkt.loads)
###############################################################################
# The ``GeoDataFrame`` is constructed as follows :
gdf = geopandas.GeoDataFrame(df, geometry='Coordinates')
print(gdf.head())
#################################################################################
# Again, we can plot our ``GeoDataFrame``.
ax = world[world.continent == 'South America'].plot(
color='white', edgecolor='black')
gdf.plot(ax=ax, color='red')
plt.show()