Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mapping Facebook Friend Locations and Connections
# Script to strip longitude and latitude data from friend list and coorelate with mutual friends
# Intentionally remove any identifying info before writing to file.
# Author: achornbake , 7/13/2013
# Facebook Data exported using "Give me my Data App" by Ownen Mundy http://owenmundy.com/site/give-me-my-data
import sys
import json
from pprint import pprint
f_data=open('friend_data.json')
friends = json.load(f_data)
m_data=open('mutual_friend.json')
mutual = json.load(m_data)
locations = {};
links = [];
nodes = [];
for i in friends:
if i['current_location'] != None :
locations[i['name']] = i['current_location']
nodes.append({'longitude' : i['current_location']['longitude'] , 'latitude' : i['current_location']['latitude']})
elif i['current_location'] != None :
locations[i['name']] = i['hometown_location']
nodes.append({'longitude' : i['hometown_location']['longitude'] , 'latitude' : i['hometown_location']['latitude']})
for i in mutual:
if i['uid1'] in locations and i['uid2'] in locations :
links.append({'source':{'longitude' : locations[i['uid1']]['longitude'] , 'latitude' : locations[i['uid1']]['latitude']} , 'target':{'longitude' : locations[i['uid2']]['longitude'] , 'latitude' : locations[i['uid2']]['latitude']}})
with open('friend_nodes.json', 'w') as outfile:
json.dump(nodes, outfile)
with open('friend_links.json', 'w') as outfile:
json.dump(links, outfile)
f_data.close()
m_data.close()
[{"latitude": "39.7392", "longitude": "-104.985"}, {"latitude": "37.7793", "longitude": "-122.419"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "34.1561", "longitude": "-118.132"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "39.2833", "longitude": "-76.6167"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "34.05", "longitude": "-118.25"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "27.9709", "longitude": "-82.4646"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "37.7793", "longitude": "-122.419"}, {"latitude": "52.3731", "longitude": "4.8922"}, {"latitude": "37.7793", "longitude": "-122.419"}, {"latitude": "51.5072", "longitude": "-0.1275"}, {"latitude": "34.05", "longitude": "-118.25"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "38.8951", "longitude": "-77.0367"}, {"latitude": "42.3578", "longitude": "-71.0617"}, {"latitude": "38.8951", "longitude": "-77.0367"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "38.8951", "longitude": "-77.0367"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "51.5072", "longitude": "-0.1275"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "34.05", "longitude": "-118.25"}, {"latitude": "42.3578", "longitude": "-71.0617"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "41.0235", "longitude": "-73.7618"}, {"latitude": "39.9587", "longitude": "-75.605"}, {"latitude": "38.8951", "longitude": "-77.0367"}, {"latitude": "40.85", "longitude": "-73.8667"}, {"latitude": "39.2833", "longitude": "-76.6167"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "32.2989", "longitude": "-90.1847"}, {"latitude": "40.8778", "longitude": "-73.4137"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "40.26", "longitude": "-74.7888"}, {"latitude": "35.9259", "longitude": "-79.0395"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "39.2833", "longitude": "-76.6167"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "38.8951", "longitude": "-77.0367"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "43.0753", "longitude": "-73.7824"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "30.4942", "longitude": "47.8192"}, {"latitude": "34.0252", "longitude": "-84.6159"}, {"latitude": "35.2269", "longitude": "-80.8433"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "39.2833", "longitude": "-76.6167"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.8719", "longitude": "-84.5184"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "32.7893", "longitude": "-79.9863"}, {"latitude": "33.9534", "longitude": "-84.5406"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "41.9831", "longitude": "-91.6685"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "32.0811", "longitude": "-81.0911"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "37.7793", "longitude": "-122.419"}, {"latitude": "41.8236", "longitude": "-71.4221"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "40.7161", "longitude": "-73.8506"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "41.8236", "longitude": "-71.4221"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.8628", "longitude": "-73.4946"}, {"latitude": "40.7449", "longitude": "-74.0329"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.8778", "longitude": "-73.4137"}, {"latitude": "35.2269", "longitude": "-80.8433"}, {"latitude": "41.8369", "longitude": "-87.6844"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "33.9534", "longitude": "-84.5406"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "33.415", "longitude": "-111.831"}, {"latitude": "34.05", "longitude": "-118.25"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.8671", "longitude": "-73.532"}, {"latitude": "47.4719", "longitude": "19.0503"}, {"latitude": "36.1658", "longitude": "-86.7844"}, {"latitude": "34.05", "longitude": "-118.25"}, {"latitude": "33.9608", "longitude": "-83.3781"}, {"latitude": "32.0811", "longitude": "-81.0911"}, {"latitude": "40.7719", "longitude": "-73.9306"}, {"latitude": "33.9608", "longitude": "-83.3781"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "32.2217", "longitude": "-110.926"}, {"latitude": "39.998", "longitude": "-75.1448"}, {"latitude": "41.8369", "longitude": "-87.6844"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "37.7793", "longitude": "-122.419"}, {"latitude": "34.05", "longitude": "-118.25"}, {"latitude": "51.5072", "longitude": "-0.1275"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.9259", "longitude": "-79.0395"}, {"latitude": "48.8567", "longitude": "2.3508"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "37.9781", "longitude": "-122.031"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "38.8951", "longitude": "-77.0367"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "34.2711", "longitude": "-118.739"}, {"latitude": "32.9901", "longitude": "-96.8933"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "36.1028", "longitude": "-80.2605"}, {"latitude": "33.9489", "longitude": "-118.445"}, {"latitude": "34.736", "longitude": "-92.3311"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.1819", "longitude": "-79.3983"}, {"latitude": "33.4483", "longitude": "-112.074"}, {"latitude": "35.9259", "longitude": "-79.0395"}, {"latitude": "35.9204", "longitude": "-79.0839"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "33.9908", "longitude": "-118.459"}, {"latitude": "34.736", "longitude": "-92.3311"}, {"latitude": "36.1028", "longitude": "-80.2605"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "38.8634", "longitude": "-104.792"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.9886", "longitude": "-78.9072"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.7719", "longitude": "-73.9306"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "29.9728", "longitude": "-90.059"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.4414", "longitude": "-79.9773"}, {"latitude": "40.8634", "longitude": "-73.4431"}, {"latitude": "34.05", "longitude": "-118.25"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.9204", "longitude": "-79.0839"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.6593", "longitude": "-111.5"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.9729", "longitude": "-83.9422"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "41.8369", "longitude": "-87.6844"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "45.6779", "longitude": "-111.047"}, {"latitude": "35.2269", "longitude": "-80.8433"}, {"latitude": "35.855", "longitude": "-82.1981"}, {"latitude": "51.5072", "longitude": "-0.1275"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "34.0221", "longitude": "-118.481"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "29.9728", "longitude": "-90.059"}, {"latitude": "45.4642", "longitude": "9.1903"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "47.6097", "longitude": "-122.333"}, {"latitude": "35.9886", "longitude": "-78.9072"}, {"latitude": "35.5778", "longitude": "-82.5839"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.9259", "longitude": "-79.0395"}, {"latitude": "55.6667", "longitude": "12.5833"}, {"latitude": "53.3478", "longitude": "-6.2597"}, {"latitude": "21.309", "longitude": "-157.826"}, {"latitude": "33.9608", "longitude": "-83.3781"}, {"latitude": "35.2269", "longitude": "-80.8433"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "36.1658", "longitude": "-86.7844"}, {"latitude": "38.8951", "longitude": "-77.0367"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "42.7358", "longitude": "-83.4189"}, {"latitude": "40.4414", "longitude": "-79.9773"}, {"latitude": "37.9772", "longitude": "-87.5506"}, {"latitude": "42.0955", "longitude": "-79.2386"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "39.7392", "longitude": "-104.985"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "55.396", "longitude": "10.3885"}, {"latitude": "42.2512", "longitude": "-73.7851"}, {"latitude": "29.5543", "longitude": "-95.296"}, {"latitude": "41.0336", "longitude": "-73.5786"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "40.5003", "longitude": "-74.4257"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "34.7162", "longitude": "-111.91"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "47.6589", "longitude": "-117.425"}, {"latitude": "45.6779", "longitude": "-111.047"}, {"latitude": "33.4483", "longitude": "-112.074"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.8188", "longitude": "-78.6446"}, {"latitude": "40.8778", "longitude": "-73.4137"}, {"latitude": "35.9259", "longitude": "-79.0395"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "57.15", "longitude": "9.88333"}, {"latitude": "40.5107", "longitude": "-78.3998"}, {"latitude": "38.9599", "longitude": "-95.2532"}, {"latitude": "43.7954", "longitude": "-123.061"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "33.755", "longitude": "-84.39"}, {"latitude": "35.9204", "longitude": "-79.0839"}, {"latitude": "32.0811", "longitude": "-81.0911"}, {"latitude": "27.9709", "longitude": "-82.4646"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "32.8765", "longitude": "-85.1739"}, {"latitude": "43.7954", "longitude": "-123.061"}, {"latitude": "41.8369", "longitude": "-87.6844"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "27.9709", "longitude": "-82.4646"}, {"latitude": "28.5335", "longitude": "-81.3758"}, {"latitude": "35.8188", "longitude": "-78.6446"}, {"latitude": "39.998", "longitude": "-75.1448"}, {"latitude": "42.7622", "longitude": "-78.7414"}, {"latitude": "27.9709", "longitude": "-82.4646"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.0566", "longitude": "-77.7445"}, {"latitude": "57.05", "longitude": "9.9167"}, {"latitude": "40.7114", "longitude": "-74.0647"}, {"latitude": "56.1572", "longitude": "10.2107"}, {"latitude": "40.65", "longitude": "-73.95"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "32.0811", "longitude": "-81.0911"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "47.3667", "longitude": "8.55"}, {"latitude": "40.7167", "longitude": "-74"}, {"latitude": "38.6272", "longitude": "-90.1978"}, {"latitude": "35.5778", "longitude": "-82.5839"}, {"latitude": "40.4414", "longitude": "-79.9773"}, {"latitude": "35.5799", "longitude": "-82.5558"}, {"latitude": "39.2833", "longitude": "-76.6167"}, {"latitude": "35.5799", "longitude": "-82.5558"}]
<html>
<title>Mapping Facebook Friends</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<style>
body {
background: #0F151A;
text-align: center;
}
.nodes {
fill: none;
stroke: #FFFFFF;
stroke-width: .5px;
stroke-opacity: 20%;
}
.links {
stroke: #CCFFFF;
stroke-opacity: 12%;
fill: none;
}
</style>
<body>
<script type="text/javascript">
var w = 962,
h = 502,
nodeSize = 5;
var nodes = [],
links = [];
var nBound = 50,
sBound = 25,
wBound = -132,
eBound = -62;
// Read in cleaned Data, no friends names or info, just map coordinates
d3.json("friend_nodes.json", function(error,root) {
nodes = root;
d3.json("friend_links.json", function(error,root){
links = root;
drawData();
});
});
function drawData()
{
var x = d3.scale.linear().domain([wBound,eBound]).range([0,w]);
var y = d3.scale.linear().domain([sBound,nBound]).range([h,0]);
var svg = d3.select("body").append("svg")
.attr("width",w)
.attr("height",h);
var circleNodes = d3.select("body").select("svg").selectAll("circle").data(nodes);
var lineLinks = d3.select("body").select("svg").selectAll("line").data(links);
circleNodes.enter().append("circle")
.attr("class","nodes")
.attr("cx",function(d) { return x(d.longitude); })
.attr("cy",function(d) { return y(d.latitude); })
.attr("r",nodeSize + "px");
lineLinks.enter().append("path")
.attr("class","links")
.attr("d",function(d) {return "M " + x(d.source.longitude) + " " + y(d.source.latitude) +
" A800,800 0 0,1 " + x(d.target.longitude) + " " + y(d.target.latitude);});
circleNodes.exit().remove();
lineLinks.exit().remove();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.