Skip to content

Instantly share code, notes, and snippets.

@blissdev
Last active August 29, 2015 13:58
Show Gist options
  • Save blissdev/9928329 to your computer and use it in GitHub Desktop.
Save blissdev/9928329 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION public.rotate()
RETURNS boolean
LANGUAGE plv8
AS $function$
var toDegrees = function toDegrees (angle) {
return angle * (180 / Math.PI);
}
var rooms = plv8.execute(
"select room_id, layout from room where label is not null and label <> ''"
);
var roomCount = rooms.length;
var update, points, calculatedRotation;
for (var i = 0; i < roomCount; i++) {
points = rooms[i]['layout'].slice(1).split(" ").slice(0, 4).map(function(n) { return +n; })
calculatedRotation = toDegrees(Math.atan2(points[3] - points[1], points[2] - points[0]));
if(calculatedRotation >= 5 && calculatedRotation < 90) {
update = "update room set rotation = round(" + calculatedRotation + ", 2)"
+ " where room_id = " + rooms[i]['room_id'];
plv8.execute(update);
}
}
return 1;
$function$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment