Last active
October 7, 2016 14:51
-
-
Save onyxfish/605cef0a5a4f54ee9969fdba0199f21d to your computer and use it in GitHub Desktop.
An agate aggregation to generate geojson linestrings from sequential row data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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