Skip to content

Instantly share code, notes, and snippets.

@tmthyjames
Created December 3, 2016 18:06
Show Gist options
  • Save tmthyjames/deffd7ab6244414707b0adaa8db27b53 to your computer and use it in GitHub Desktop.
Save tmthyjames/deffd7ab6244414707b0adaa8db27b53 to your computer and use it in GitHub Desktop.
Concentric Circles for same lat,lng
var newLatLng = function(olat, olng, r, degrees){
var lat1 = olat * Math.PI/180.0;
var lon1 = olng * Math.PI/180.0;
var d = (r/6371)/1000; //radius of the earth in km, accounts for curvature
var lon, loc;
var tc = degrees * Math.PI/180.0; // convert to radians to use trig functions
var lat = Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc));
lat = 180.0 * lat / Math.PI;
if (Math.cos(lat1) == 0){
lon = olng;
} else {
lon = ((lon1 - Math.asin(Math.sin(tc) * Math.sin(d)/Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI;
}
lon = 180.0 * lon / Math.PI;
return [lat, lon];
}
var featureKeys = Object.keys(features);
var geocontainer = featureKeys.reduce(function(output, npi, index){
var lat = features[npi].geometry.coordinates.lat;
var lng = features[npi].geometry.coordinates.lng;
var key = lat + '|' + lng;
if (!(key in output)){
output[key] = {
npis: [npi],
coords: {
lat: lat,
lng: lng
}
}
} else output[key].npis.push(npi);
return output;
}, {});
var geocontainerKeys = Object.keys(geocontainer);
for (var z in geocontainerKeys){
var latLonKey = geocontainer[geocontainerKeys[z]]
var npilist = latLonKey.npis;
var npilist = npilist.sort(function(a,b){
var entitytypea = features[a].type,
entitytypeb = features[b].type;
if (entitytypea == 'source' && entitytypeb == 'source'){
return features[a].charges - features[b].charges;
};
});
var radius = RADIUS;
var start = memberCount = 0;
var radiusObj = {};
for (var i=1;i<=npilist.length;i+=1){
var stop = start + INCREASE_BY*i;
radiusObj[radius] = npilist.slice(start, stop);
memberCount += radiusObj[radius].length
if (memberCount >= npilist.length) break;
start += INCREASE_BY*i;
radius += RADIUS; // + start; // to decrease distance between nodes as circles get more populous
};
if (!(isEmpty(radiusObj))){
for (var r in radiusObj){
var degrees = 0;
for (var n in radiusObj[r]){
var npi = radiusObj[r][n];
var lat = features[npi].geometry.coordinates.lat,
lng = features[npi].geometry.coordinates.lng;
var newCoords = newLatLng(lat, lng, r, degrees);
var entitytype = features[npi].type;
if (entitytype == 'source' || (entitytype == 'comp' && entitytypecode == '1')){
features[npi].geometry.coordinates.lat = newCoords[0];
features[npi].geometry.coordinates.lng = newCoords[1];
degrees += 360/radiusObj[r].length;
} else {
features[npi].geometry.coordinates.lat = lat;
features[npi].geometry.coordinates.lng = lng;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment