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!
import geopandas as gpd
fname = "./data/2013-04-29-Running.geojson"
df = gpd.read_file(fname)
print(type(df))
df
That is a GeoDataFrame
with only one column. Let's see if slicing it
we get GeoSeries.
df = df.ix[0]
print('{!r}'.format(type(df)))
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.
series = gpd.GeoSeries(df)
type(series)
Now let's explore what is in this GeoSeries
object.
series.total_bounds # Nice tuple that we can use for axis lims/bbox.
series.bounds # Same as above, but as a DataFrame.
deg2km = 111
series.length * deg2km
series.centroid
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!
ax = series.plot()
Better yet, let's plot it as UTM instead of WGS84.
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.
print('{!r}'.format(type(series.values[0])))
series.values[0]
HTML(html)