[GIS] Plot points on map using Fiona and/or Shapely

coordinatesfionamappingpythonshapely

I have written a parser in Python that extracts the coordinates out of a GPX file and I would like to view those points on a map. I have done a little research and it seems like either Fiona or Shapely is capable of doing this, but all of the examples I find handle shape files.

My coordinates are stored as tuples in a list. I was wondering if there was an easy way to use either of these packages to plot my coordinates.

Best Answer

Given you're already familiar with Python I'd recommend taking a look at the cartopy library for plotting spatial data with matplotlib. It's a much nicer replacement to matplotlib-basemap.

The basic idea of cartopy is that you can apply a projection to both your matplotlib frame as well as everything you're plotting to the frame. It also includes a lot of helper methods for adding base data, displaying shapely geometries, etc (even in v0.12 released earlier this month, reading in mapbox tiles as a basemap!).

In your case you've got a set of points that are (presumably) in WGS84 longitude, latitude pairs. You could build a shapely geometry and plot that, but the easiest method would be to use the standard matplotlib scatter to see the data. In which case I'm going to assume that your points rather than being a list of tuples, are in a 2d numpy array called points. For my example I grabbed a trail from the OpenStreetMap gpx trail store.

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

First you should define some projections. See the cartopy projection list for a whole stack of different options.

data_crs = ccrs.PlateCarree()
map_crs = ccrs.UTM(zone=32)

Then build the axis and plot some points with a bit of basemap information.

ax = plt.axes(projection=map_crs)

ax.add_feature(cartopy.feature.OCEAN, zorder=0)
ax.add_feature(cartopy.feature.LAND, zorder=0, edgecolor='black')
ax.gridlines()

Finally add your data (and apply the transform)

ax.scatter(points[:, 0], points[:, 1], transform=data_crs)

plt.show()

Plot of gpx trail