python4oceanographers

Turning ripples into waves

Loading GeoJSON with geopandas

Geopandas is an awesome project that brings the power of pandas to geospatial data. I am not sure if we can load GPX data directly, so for this notebook I will use a GeoJSON that I previously converted from a GPX. (In a future post I will try to write a GPX reader for geopandas.)

Let's load the data!

In [2]:
import geopandas as gpd

fname = "./data/2013-04-29-Running.geojson"

df = gpd.read_file(fname)

print(type(df))

df
<class 'geopandas.geodataframe.GeoDataFrame'>

Out[2]:
geometry
0 (LINESTRING (-46.7350076045841 -23.55963248759...

That is a GeoDataFrame with only one column. Let's see if slicing it we get GeoSeries.

In [3]:
df = df.ix[0]

print('{!r}'.format(type(df)))
<class 'pandas.core.series.Series'>

Apparently not. As you can see the "Geo" type was not preserved. To fix that we can just call the GeoSeries method on the pandas Series object.

In [4]:
series = gpd.GeoSeries(df)
type(series)
Out[4]:
geopandas.geoseries.GeoSeries

Now let's explore what is in this GeoSeries object.

In [5]:
series.total_bounds  # Nice tuple that we can use for axis lims/bbox.
Out[5]:
(-46.737991729751229,
 -23.564499272033572,
 -46.713051293045282,
 -23.551835222169757)
In [6]:
series.bounds  # Same as above, but as a DataFrame.
Out[6]:
minx miny maxx maxy
geometry -46.737992 -23.564499 -46.713051 -23.551835
In [7]:
deg2km = 111

series.length * deg2km
Out[7]:
geometry    7.278114
dtype: float64
In [8]:
series.centroid
Out[8]:
geometry    POINT (-46.72445652998366 -23.55928803359001)
dtype: object

The GeoSeries and GeoDataFrame have plenty more useful method and properties that I did not show here. I will address some of them in another post.

Let's finish this with my favorite feature, plotting!

In [9]:
ax = series.plot()

Better yet, let's plot it as UTM instead of WGS84.

In [10]:
original = dict(ellps='WGS84', datum='WGS84', proj='longlat')

series.crs = original

target = dict(ellps='WGS84', datum='WGS84', proj='utm', zone=23, units='km', no_defs=True)

ax = series.to_crs(crs=target).plot()

PS: Here is an "eastern egg" that I found out while writing this post. IPython notebook rich display renders Shapely objects.

In [11]:
print('{!r}'.format(type(series.values[0])))

series.values[0]
<class 'shapely.geometry.multilinestring.MultiLineString'>

Out[11]:
In [12]:
HTML(html)
Out[12]:

This post was written as an IPython notebook. It is available for download or as a static html.

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://ocefpaf.github.io/.

Comments