Exemple #1
0
    def assign_item(self, item, origin):
        """
        Assigns an item from a given cluster to the closest located cluster.

        :param item: the item to be moved.
        :param origin: the originating cluster.
        """
        closest_cluster = origin
        for cluster in self.__clusters:
            if self.distance(item, centroid(cluster)) < self.distance(
                    item, centroid(closest_cluster)):
                closest_cluster = cluster

        if id(closest_cluster) != id(origin):
            self.move_item(item, origin, closest_cluster)
            return True
        else:
            return False
    def assign_item(self, item, origin):
        """
        Assigns an item from a given cluster to the closest located cluster.

        :param item: the item to be moved.
        :param origin: the originating cluster.
        """
        closest_cluster = origin
        for cluster in self.__clusters:
            if self.distance(item, centroid(cluster)) < self.distance(
                    item, centroid(closest_cluster)):
                closest_cluster = cluster

        if id(closest_cluster) != id(origin):
            self.move_item(item, origin, closest_cluster)
            return True
        else:
            return False
    kml_items = [{'label': label, 'coords': '%s,%s' % coords[0]} for (label,
                                                                      coords) in expanded_coords]

    # It would also be helpful to include names of your contacts on the map

    for item in kml_items:
        item['contacts'] = '\n'.join(['%s %s.' % (c['firstName'], c['lastName'])
                                      for c in connections if c.has_key('location') and
                                      c['location']['name'] == item['label']])

    # Step 3 - Cluster locations and extend the KML data structure with centroids

    cl = KMeansClustering([coords for (label, coords_list) in expanded_coords
                           for coords in coords_list])
    centroids = [{'label': 'CENTROID', 'coords': '%s,%s' % centroid(c)} for c in
                 cl.getclusters(K)]
    kml_items.extend(centroids)

    # Step 4 - Create the final KML output and write it to a file
    kml = createKML(kml_items)
    f = open(OUT_FILE, 'w')
    f.write(kml)
    f.close()
    print 'Data written to ' + OUT_FILE


def main():
    # company_distribution()
    # job_role_distribution()
    # g = geocoders.Bing(GEO_APP_KEY)
CLUSTER_COUNT = 3
i = 0
for trend in mainTrend:
    location_list = list()
    for tweet in tweepy.Cursor(api.search,
                               q=trend['query']).items(TWEET_SAMPLE_SIZE):
        if tweet.user.location:
            print(i)
            i += 1
            try:
                location = geocoder.geocode(tweet.user.location)
                location_list.append({
                    "lat": location.latitude,
                    "lon": location.longitude
                })
            except Exception as e:
                print("An exception occurred: ")
                print(e)
                pass

with open('out.txt', 'w') as f:
    print(location_list, file=f)

cluster = KMeansClustering([(l['lat'], l['lon']) for l in location_list])
centroids = [centroid(c) for c in cluster.getclusters(CLUSTER_COUNT)]

kml_clusters = simplekml.Kml()
for i, c in enumerate(centroids):
    kml_clusters.newpoint(name='Cluster {}'.format(i), coords=[(c[1], c[0])])
    kml_clusters.save('{}.kml'.format(trend['query']))