Created
May 28, 2020 20:26
-
-
Save kurasaiteja/36d06b59a98e67b66f585118c8101d3a to your computer and use it in GitHub Desktop.
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
def calculateCentroid(place): | |
""" Calculates the centroid from a bounding box.""" | |
# Obtain the coordinates from the bounding box. | |
coordinates = place['bounding_box']['coordinates'][0] | |
longs = np.unique( [x[0] for x in coordinates] ) | |
lats = np.unique( [x[1] for x in coordinates] ) | |
if len(longs) == 1 and len(lats) == 1: | |
# return a single coordinate | |
return (longs[0], lats[0]) | |
elif len(longs) == 2 and len(lats) == 2: | |
# If we have two longs and lats, we have a box. | |
central_long = np.sum(longs) / 2 | |
central_lat = np.sum(lats) / 2 | |
else: | |
raise ValueError("Non-rectangular polygon not supported.") | |
return (central_long, central_lat) | |
# Calculate the centroids of place | |
centroids = tweets_sotu['place'].apply(calculateCentroid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment