Last active
April 27, 2020 12:13
-
-
Save Hugheym/df9cbefc9416dce23637080cf48f1ca8 to your computer and use it in GitHub Desktop.
Converting pandas DataFrame with lat, lon coordinate lists to GeoJSON Features.
This file contains hidden or 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
| %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