Skip to content

Instantly share code, notes, and snippets.

@Hugheym
Last active April 27, 2020 12:13
Show Gist options
  • Select an option

  • Save Hugheym/df9cbefc9416dce23637080cf48f1ca8 to your computer and use it in GitHub Desktop.

Select an option

Save Hugheym/df9cbefc9416dce23637080cf48f1ca8 to your computer and use it in GitHub Desktop.
Converting pandas DataFrame with lat, lon coordinate lists to GeoJSON Features.
%python
import numpy as np
paramToVisualize = "numPickups"
def to_hex_geojson(dataLat, dataLon):
coords = list(zip(dataLon, dataLat))
# Because the geojson polygon requires the last and first element to match.
coords.append(coords[0])
return {"type": "Polygon",
"coordinates": [coords]}
hexify = np.vectorize(to_hex_geojson, otypes=[object])
df["hex"] = hexify(df.pickupH3Lats[:], df.pickupH3Lons[:])
features = []
#We could put all the columns as properties of the feature, but it would slightly impact performance.
for i, row in df.iterrows():
features.append(Feature(geometry=row['hex'], properties={'param': row[paramToVisualize],
'id': row['h3_pickup']}))
feature_collection = FeatureCollection(features)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment