Skip to content

Instantly share code, notes, and snippets.

@onyxfish
Last active October 7, 2016 14:51
Show Gist options
  • Save onyxfish/605cef0a5a4f54ee9969fdba0199f21d to your computer and use it in GitHub Desktop.
Save onyxfish/605cef0a5a4f54ee9969fdba0199f21d to your computer and use it in GitHub Desktop.
An agate aggregation to generate geojson linestrings from sequential row data
import agate
import geojson
class LineString(agate.Aggregation):
def __init__(self, lat_column, lng_column):
self._lat_column_name = lat_column
self._lng_column_name = lng_column
def get_aggregate_data_type(self, table):
return agate.Text()
def validate(self, table):
lat_column = table.columns[self._lat_column_name]
lng_column = table.columns[self._lng_column_name]
if not isinstance(lat_column.data_type, agate.Number) or not isinstance(lng_column.data_type, agate.Number):
raise DataTypeError('LineString can only be applied to columns containing Number data.')
def run(self, table):
lat_column = table.columns[self._lat_column_name]
lng_column = table.columns[self._lng_column_name]
points = []
for lat, lng in zip(lat_column, lng_column):
points.append((float(lng), float(lat)))
return str(geojson.LineString(points))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment