Skip to content

Instantly share code, notes, and snippets.

@rywall
Created November 30, 2012 00:59
Show Gist options
  • Save rywall/4173033 to your computer and use it in GitHub Desktop.
Save rywall/4173033 to your computer and use it in GitHub Desktop.
[50, 100, 200, 500, 1000, 5000].each do |buffer|
puts "Calculating population within #{buffer} m of each shape_geometry"
Shape.find_each do |s|
execute "UPDATE shapes SET pop#{buffer}m =
(SELECT SUM((ST_SummaryStats(ST_Clip(rast,1,ST_Buffer(geom::geography, #{buffer})::geometry))).sum)
FROM shapes, population_tiles
WHERE id = #{s.id} AND ST_Intersects(ST_Buffer(geom::geography, #{buffer})::geometry,rast)
GROUP BY id)
WHERE id = #{s.id}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment