Building Shadow Analysis Program

Building Shadow Analysis Program

Earlier this week I wrote a small PHP script for analysis of the shadow cast by buildings on the surrounding landscape. To obtain sun position and altitude, it relies on SunCalc PHP which must be included in the script. It goes throughout the year, churning out how much of a shadow a building will produce each hour, in feet and direction. Additionally, the script will calculate the maximum point the shadow will be cast as a set of coordinates.

01<?php
02// The current values entered into this script currently are for the Empire Plaza.
03$latitude =  42.64915;
04$longitude = -73.75950;
05$height =  610;
06 
07$start = strtotime("January 1, 2020 12 AM");
08$end = strtotime("December 31, 2020 11 PM");
09 
10include("suncalc.php");
11 
12// Compass as a Reference
13$compass = array(
14            'north', 'north-northeast', 'northeast',
15            'east-northeast', 'east', 'east-southeast',
16            'southeast', 'south-southeast', 'south',
17            'south-southwest', 'southwest',
18            'west-southwest', 'west', 'west-northwest',
19            'northwest', 'north-northwest');
20 
21function newPos($lat, $lng, $dist, $dir) {
22    // Earth Radius in KM
23    $R = 6378.14;
24     
25    // distance in feet to KM
26    $d = (($dist * 0.3048) / 1000);
27 
28    // Degree to Radian
29    $lat1 = $lat * (M_PI/180);
30    $lng1 = $lng * (M_PI/180);
31    $brng = $dir * (M_PI/180);
32 
33    // Really Complicated Math (TM) that works based on the HAVERSINE formula from
35    $lat2 = asin(sin($lat1)*cos($d/$R) + cos($lat1)*sin($d/$R)*cos($brng));
36    $lng2 = $lng1 + atan2(sin($brng)*sin($d/$R)*cos($lat1),cos($d/$R)-sin($lat1)*sin($lat2));
37 
38    # back to degrees
39    $lat2 = $lat2 * (180/M_PI);
40    $lng2 = $lng2 * (180/M_PI);
41 
42  return "$lat2,$lng2";
43}
44 
45$output = "Month,Day,Hour,Sun Angle,Sun Position,Sun Pos Name,Shadow Length,Shadow Direction,Shadow Dir Name,Orginal Lat, Original Lng, Maximum Shadow Lat, Maximum Shadow Lng\n";
46for ($time = $start; $time &amp;lt; $end; $time += 60*60) { // get sun position for time $timeObj = new DateTime(); $timeObj-&amp;gt;setTimestamp($time);
47     
48    $sc = new AurorasLive\SunCalc($timeObj, $latitude, $longitude);
49    $sunPos = $sc-&amp;gt;getSunPosition($timeObj);
50 
51    // don't include any time when sun is below the horizon
52    if ($sunPos-&amp;gt;altitude*(180/M_PI) &amp;lt; 0) continue; // add month, day, hour $output .= date('n,j,G,',$time); // get sun altitude $output .= $sunPos-&amp;gt;altitude*(180/M_PI).',';
53     
54    // get sun position
55    $output .= 180+$sunPos-&amp;gt;azimuth*(180/M_PI).',';
56    $output .= $compass[round((180+$sunPos-&amp;gt;azimuth*(180/M_PI))/ 22.5) % 16].',';   
57     
58    // shadow length
59    $output .= $height/tan(deg2rad($sunPos-&amp;gt;altitude*(180/M_PI))).',';
60     
61    // shadow direction
62    $shadowDir = $sunPos-&amp;gt;azimuth*(180/M_PI) + 360;
63    if ($shadowDir &amp;gt; 360) $shadowDir -= 360;
64     
65    $output .= $shadowDir.',';
66    $output .= $compass[round($shadowDir/ 22.5) % 16].',';
67 
68    // building latitude and longitude
69    $output .= "$latitude, $longitude,";
70     
71    // maximum extent of shadow
72    $output .= newPos($latitude, $longitude, $height/tan(deg2rad($sunPos-&amp;gt;altitude*(180/M_PI))), $shadowDir).",";   
73     
74    $output .= "\n";   
75}
76 
77echo $output;

Here is an example output:

01Month,Day,Hour,Sun Angle,Sun Position,Sun Pos Name,Shadow Length,Shadow Direction,Shadow Dir Name,Orginal Lat, Original Lng, Maximum Shadow Lat, Maximum Shadow Lng
021,1,8,4.370562595888,127.16997476055,southeast,7981.2630997694,307.16997476055,northwest,42.64915, -73.7595,42.662350867593,-73.783180446551,
031,1,9,12.464234785715,138.47001937172,southeast,2759.6833421566,318.47001937172,northwest,42.64915, -73.7595,42.654806428356,-73.766311948646,
041,1,10,18.826346377511,151.18871989968,south-southeast,1789.1660233981,331.18871989968,north-northwest,42.64915, -73.7595,42.653442385927,-73.762710054908,
051,1,11,22.92597731207,165.29270254371,south-southeast,1442.2476816852,345.29270254371,north-northwest,42.64915, -73.7595,42.652969571748,-73.76086316453,
061,1,12,24.317973645092,180.27827074558,south,1349.8710413416,0.27827074557518,north,42.64915, -73.7595,42.652845989302,-73.7594755931,
071,1,13,22.827705402895,195.24737437252,south-southwest,1449.1706789769,15.247374372522,north-northeast,42.64915, -73.7595,42.652978239559,-73.758081165062,
081,1,14,18.641667869753,209.31025239405,south-southwest,1808.2272087538,29.310252394054,north-northeast,42.64915, -73.7595,42.653467170112,-73.756204500047,
091,1,15,12.211118961179,221.98033545934,southwest,2818.7133569306,41.980335459338,northeast,42.64915, -73.7595,42.654887013398,-73.752480800429,
101,1,16,4.0670753820534,233.23837174035,southwest,8579.0650125467,53.238371740354,northeast,42.64915, -73.7595,42.663205632648,-73.733908619093,
111,2,8,4.3645441749857,127.03353849744,southeast,7992.3115188513,307.03353849744,northwest,42.64915, -73.7595,42.662327567958,-73.783255965565,
121,2,9,12.476443037614,138.33095911417,southeast,2756.8958218911,318.33095911417,northwest,42.64915, -73.7595,42.654788550435,-73.766323694576,
131,2,10,18.860737296743,151.04916237459,south-southeast,1785.6561891219,331.04916237459,north-northwest,42.64915, -73.7595,42.653428213262,-73.762717935368,
141,2,11,22.985140372821,165.15894624042,south-southeast,1438.1068136859,345.15894624042,north-northwest,42.64915, -73.7595,42.652956261035,-73.760871335783,
151,2,12,24.401639825801,180.15947237405,south,1344.6352402431,0.15947237405339,north,42.64915, -73.7595,42.652831682677,-73.759486067027,
161,2,13,22.932215921968,195.15131682951,south-southwest,1441.810083379,15.151316829514,north-northeast,42.64915, -73.7595,42.652960530629,-73.7580970562,
171,2,14,18.761308368827,209.23956034287,south-southwest,1795.8375075952,29.239560342872,north-northeast,42.64915, -73.7595,42.653440556697,-73.756234277101,
181,2,15,12.340240887742,221.93268535805,southwest,2788.302504508,41.932685358049,northeast,42.64915, -73.7595,42.654829364851,-73.752562956502,
191,2,16,4.2013827926665,233.20922777897,southwest,8303.8756666826,53.209227778967,northeast,42.64915, -73.7595,42.662764126188,-73.734739101558,

Egg Highlighted

Leave a Reply

Your email address will not be published. Required fields are marked *