-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetPopulation.m
48 lines (39 loc) · 1.94 KB
/
getPopulation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function [result] = getPopulation(place)
% Gets the population density of the tiles that encapsulate the given place
%
% DETAIL:
% Gets the population density of a geographical area confined within
% the geographical area referenced by the input place in terms of
% point location and the population density
% INPUT:
% place (String) - Name of an area polygon in OpenSteetMap
% OUTPUT:
% result(:,1:2) (Double) - Longitude and Latitude of the cell centroid
% result(:,3) (Double) - Population Density (persons/km^2)
% EXAMPLE:
% [result] = getPopulation('Bristol')
load('global');
table = 'pop_gpwv4_2015';
% Old query format
query = ['SELECT ST_RasterToWorldCoordX(p.rast, x) AS wx, ST_RasterToWorldCoordY(p.rast,y) AS wy, ST_Value(p.rast, x, y) as v '...
'FROM ' table ' AS p, '...
'(SELECT way FROM planet_osm_polygon WHERE name = ''' place ''' AND boundary=''administrative'' ORDER BY ST_NPoints(way) DESC LIMIT 1) AS f '...
'CROSS JOIN generate_series(1, 100) As x '...
'CROSS JOIN generate_series(1, 100) As y '...
'WHERE ST_Intersects(p.rast,f.way)'];
% New query format copied over from MassEvac v4
query = ['SELECT x, y, val FROM'...
'(SELECT (ST_PixelAsCentroids(ST_Clip(q.raster,q.clipper))).* FROM'...
'(SELECT p.rast AS raster, b.way as clipper'...
'FROM {} AS p,'...
'(SELECT way FROM planet_osm_polygon WHERE name = ''' place ''' AND boundary = ''administrative'' ORDER BY ST_NPoints(way) DESC LIMIT 1) AS b'...
'WHERE ST_Intersects(p.rast,b.way)) AS q) AS foo;'];
filePath = ['./cache/' table '/'];
if ~exist(filePath,'file')
mkdir(filePath);
end
rootPath = [filePath DBase '/']
if ~exist(rootPath,'file')
mkdir(rootPath);
end
result = getFileOrQuery([rootPath place], DBase, query);