Home > qgis > Implementing day night functionality

# Implementing day night functionality

November 23Hits:0
• I have a daytime raster of my study area.
• I have 20+ counters in the study area that count "stuff" with a timestamp.
• Using PyEphem I determined the type of ambient light for each collected data point.

What I'd like to do:

• Add a grey/semi transparent overlay to reflect the ambient light (Dusk, Sunrise, day, sunset, and dark).
• The end goal: produce a time-based visualization of the data collection that highlights the time of the day. Or be able to produce a snapshot at a certain time with the proper shaded overlay.
• Think of this as this the image:

Given the sun's position, you can easily create a grid of the surface light intensity (ignoring atmospheric and topographic effects) in lat-lon coordinates. Reproject that grid as desired and overlay it on the map.

The formulas are the standard ones for spherical coordinates: the unit vector corresponding to a spherical coordinate (lon, lat) is

(cos(lon)cos(lat), sin(lon)cos(lat), sin(lat)).

The intensity depends on the cosine between the unit outward normal ("up") direction and the direction to the sun. This cosine is just the dot product of those two unit vectors. That is, let (x,y,z) be the unit vector pointing towards the sun (in geocentric coordinates), convert the geodetic (lat, lon) of each grid cell's center point into a unit vector (xi, eta, zeta), and compute

(x,y,z) . (xi, eta, zeta) = x*xi + y*eta + z*zeta.

Any results that are less than 0 correspond to an invisible sun, so set these to zero.

These calculations are straightforward to perform with map algebra, starting either with a grid of (lat, lon) values (that's how I did it) or with a pair of grids, one for latitude and another for longitude (which is how you would have to do it with ESRI software, for example). (To create a latitude or longitude grid, form the y-coordinate or x-coordinate grid, respectively, in geographic coordinates, then project it.) From this initial information compute, once and for all, three grids [xi], [eta], and [zeta] (or a single vector-valued grid [xi, eta, zeta]) to represent the upward directions at the grid cells. For any given sun position, the calculation becomes the simple linear combination of [xi], [eta], and [zeta] given above; the sun determines the coefficients x, y, and z (which are just numbers, of course).

Here's a world map in Plate Carree projection of such an intensity grid (216 by 432 cells).

The prime meridian is in the center and north is to the top, as usual. For this calculation I set the sun vector proportional to (0.6, -0.8, 0.5), to approximate a summer's day at noon over the mid-Atlantic ocean. (Actually, a z-value of 0.4 would be more realistic; the sun never gets quite this far north.)

An alpha-overlay (which makes the brighter areas slightly more transparent, too) on an Aster colorized map produces this image.

As time changes, the sun's vector (x,y,z) gets recalculated, requiring a recalculation of the intensity grid. This is a fast calculation. You don't need high resolution, either: I used just one-tenth the resolution of the original grid (2160 by 4320 cells) for this illustration. The interpolation used to display the coarser intensity grid works just fine to fill in the gaps. This should enable you to modify the display almost instantaneously if you like, creating a smooth animation.

## Related Articles

• ### How to implement password reset functionality without becoming susceptible to cross-domain referer leakage?October 6

I have typically implemented password reset functionality by sending a link that included something like this: http://example.com/pwreset?id=userId&resetToken=superSecretResetToken On my pwreset page I will typically request a few resources from thir

• ### Is There A Way To Implement WINS-like Functionality Without WINS?May 8

A company 'somecompany' is retiring WINS (Windows Internet Name Server) support. This means that where the IT staff used to be able to type: ssh somehost They now have to type: ssh somehost.somecompany.com WINS has been deprecated for years. Is there

• ### ReactiveCocoa implementation of async functionAugust 15

I would like a review of the RAC implementation for the function setGist() I've posted the code on GitHub for easier reading. Description: You create an instance of GistService() let gistService = GistService() Then use public functions .resetGist()

• ### What are the security implication of implementing an IVR functionality in my application?October 11

I am planning to implement an Interactive Voice Recognition (IVR) functionality in my python-based application. I will be using IVR as a backup verification method or when a user is going to setup her 2FA for the first time. What are the security con

• ### Implementing a Boolean function using NOR gatesDecember 14

The task is to implement a Boolean function using NOR gates only. After minimization what I end up with is the following equation: $$f(a,b,c,d) = {\overline {b} \overline {c}} + {a \overline {c}} + {c \overline {d}}$$ I tried to do something like th

• ### Matlab Implementation of Round FunctionJanuary 15

I trying to implement the round function present in MatLab without using round or roundn. This is simply as a practice for an interview. My thinking process was as so: I know I would need to use the functions floor and ceil. I figured let me not jump

• ### How to implement waterfall serialize function in core javascriptJanuary 21

I am new to javascript developer. Just few days days ago i little bit know about Asynchronize Waterfall function in NodeJs which enforcing to serial executing function. Now i need to implement like Waterfall function in core javascript. Somethings li

• ### Efficient faithfully-rounded implementation of error function erff()February 2

The error function is closely related to the standard normal distribution and occurs frequently in the natural sciences as well as other fields. It is used in finance when pricing options, for example. While support for it was added first to ISO C99

• ### Implementing String replacement function February 4

I am trying to implement string replacement function however I was able to get to do with character replace(code below). For the string replacement I looked up for Microsoft reference source which has below comment.Although I looked up for the string

• ### Is there any way or tool to check whether a particular iOS app is implementing SSL Pinning functionality?February 11

I'm just learning the iOS security. This is completely a beginner question. I'm trying to figure out whether there is any particular way to check if an iOS app is implementing SSL Pinning functionality. I've currently checked the SSL traffic of some

• ### How to invoke my own implementation of tan function, rather than the one defined in math.h? February 15

This question already has an answer here: Gcc uses sqrt without including math.h 1 answer I try to invoke my own implementation of tan function, but my gcc only invokes the one defined in math.h (i.e, libm.a's ). Suppose I have main.c that invokes ta

• ### Implementation of Recursive Function f(i) = 2f(i-1) + 4f(i-2) ; f(1) = 3; f(0) = 1 in MIPS AssemblyFebruary 17

How can I implement the Recursive Function f(i) = 2f(i-1) + 4f(i-2) ; f(1) = 3; f(0) = 1 in MIPS Assembly Simulator QTSpim. Thanks in Advance. I tried in this way: The source File is in here: program.asm f: addi $sp,$sp,-8 # Argument sw $a0,4($sp) sw

• ### What is needed to implement a usable functional languageFebruary 28

I've done some programming in a more-or-less functional style, but I've never really studied pure functional programming. What is the bare minimum needed to implement a functional language? As far as I can tell, you need: The ability to define a func

add_action() and add_filter() are major functions. However in some scenarios add one more function and hook it somewhere approach gets bulky and inconvenient. I had determined for myself several use cases that can streamline code with wrappers on top

• ### How do I implement the autocomplete functionality in Webforms?September 14

I have a drop down box with college names that has 9000 options to choose from. I am unable to use the drop-down field in Webforms. Is there a way to use an autocomplete in a text field for Drupal 6? --------------Solutions------------- Autocomplete

• ### How to implement question/answer functionality with following feature?October 19

I would like to create a question/answer website using Drupal7, where a user can ask a question and other users can answer that question. This one is very simple to implement in Drupal 7. But what additional feature i want is when a user asks a quest

• ### Custom implementation of ArcGIS functionNovember 28

I would like to find out what it takes to create a custom implementation of an ArcGIS function. In particular, I would like to implement GeoAnalyst.ISurfaceOp2.Visibility() in order to get it to run faster. Right now, it is taking ~3 seconds per call

• ### When should I add a new tpl.php file, and when should I implement a theme function?January 13

Two part question, both highly related. Can all templates be implemented both as a function in template.php, and as a separate tpl.php file? In the cases where you can do both, are there advantages to either of the approaches? --------------Solutions

• ### Implementation of language_attributes functionAugust 1

I am reverse engineering how to make wordpress themes. For that, I opened its files and found language_attributes() function. I went to function.php to find it but it was not there. Any ideas where can I find its implementation? --------------Solutio

• ### Implementing C++ boolean function objects with logical operator combinationsOctober 24

I want to make a C++ object hierarchy of "classifiers", which can be composed together via logical operators into a single classifier that implements the whole logical combination. This is actually for a particle physics analysis library, but I'