Last active
November 19, 2019 18:46
-
-
Save davepape/41917d4715d5aba313a95db29abb2ed4 to your computer and use it in GitHub Desktop.
Convert SEDAC global population data into a texture.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Visualize data from SEDAC's Gridded Population of the World data set - https://sedac.ciesin.columbia.edu/data/set/gpw-v4-population-count-rev11 | |
Parses the population counts and converts the data into a greyscale texture. | |
The data file must be in ASCII format, with its extension changed to ".txt" in order for Unity to recognize it as a TextAsset. | |
*/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
using UnityEngine; | |
public class gpw : MonoBehaviour | |
{ | |
public float max=100000f; | |
public TextAsset datafile; | |
private int IntHeaderVal(string s) | |
{ | |
string[] tokens = s.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries); | |
return int.Parse(tokens[1]); | |
} | |
private float FloatHeaderVal(string s) | |
{ | |
string[] tokens = s.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries); | |
return float.Parse(tokens[1]); | |
} | |
private Texture2D InitTexture(int width, int height) | |
{ | |
Texture2D texture = new Texture2D(width,height); | |
var initColors = new Color32[width*height]; | |
for (int i=0; i < width*height; i++) | |
initColors[i] = Color.black; | |
texture.SetPixels32(initColors); | |
GetComponent<Renderer>().material.mainTexture = texture; | |
return texture; | |
} | |
private Color TransferFunction(float data) | |
{ | |
Color color; | |
if (data > max) | |
color = Color.white; | |
else if (data > 0) | |
color = Color.white * data/max; | |
else | |
color = Color.black; | |
return color; | |
} | |
void Start() | |
{ | |
var separators = new string[] { "\n", "\r\n", "\r" }; | |
string[] lines = datafile.text.Split(separators, StringSplitOptions.None); | |
int ncols = IntHeaderVal(lines[0]); | |
int nrows = IntHeaderVal(lines[1]); | |
float xllcorner = FloatHeaderVal(lines[2]); | |
float yllcorner = FloatHeaderVal(lines[3]); | |
float cellsize = FloatHeaderVal(lines[4]); | |
int width = (int)(360.0f/cellsize); | |
int height = (int)(180.0f/cellsize); | |
Texture2D texture = InitTexture(width,height); | |
for (int i=0; (i < nrows); i++) | |
{ | |
int texRow = (nrows-1)-i + (int)((90+yllcorner)/cellsize); | |
string[] vals = lines[i+6].Split(' '); | |
for (int j=0; (j < ncols); j++) | |
{ | |
int texCol = j+(int)((180+xllcorner)/cellsize); | |
float pop = float.Parse(vals[j]); | |
texture.SetPixel(texCol, texRow, TransferFunction(pop)); | |
} | |
} | |
texture.Apply(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment