Skip to content

Commit

Permalink
Map bbox from config
Browse files Browse the repository at this point in the history
  • Loading branch information
alno committed May 22, 2012
1 parent da3d07f commit 24ee383
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: git://github.com/alno/Ruby-Mapnik.git
revision: 958910b17bce3d2c94227728d44578fb94b21da4
revision: e2ac6b6517c6fb8f7b0d8d4a1a8e7cd1f6d4795b
specs:
ruby_mapnik (0.1.2)
rice (>= 1.4.2)
Expand Down
6 changes: 3 additions & 3 deletions config/mapapp.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ development: &base
lng: 36.2597
zoom: 12
bbox:
min_lat: 54.3677585240684
min_lat: 54.367759
min_lng: 35.859375
max_lat: 54.7753458593645
max_lng: 36.5625
max_lat: 54.775345
max_lng: 36.562495
min_zoom: 10
max_zoom: 18
styles:
Expand Down
25 changes: 25 additions & 0 deletions lib/map_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module MapUtils
include Math
extend self

def zoom_x_y_to_lat_lng(zoom, x, y)
n = 2.0 ** zoom
lon_deg = x / n * 360.0 - 180.0
lat_rad = atan(sinh(Math::PI * (1 - 2 * y / n)))
lat_deg = lat_rad * 180.0 / Math::PI
[lon_deg, lat_deg]
end

def zoom_lat_lng_to_x_y(zoom, lat, lng)
n = 2.0 ** zoom

x = (lng + 180.0) * n / 360.0

lat_rad = lat / 180.0 * Math::PI
t = log((1 + sin(lat_rad)) / cos(lat_rad))
y = n * (1 - t / Math::PI) / 2.0

[x, y]
end

end
28 changes: 15 additions & 13 deletions lib/tasks/osm.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ namespace :osm do

desc "Prepare hillshade data"
task :hillshade do
app_conf_file = ENV['APP_CONFIG'] || Rails.root.join('config', 'mapapp.yml')
app = YAML.load(File.open app_conf_file)[Rails.env]

if File.exists? Rails.root.join('..', '..', 'shared')
heightdir = Rails.root.join('..', '..', 'shared', 'heights')
else
heightdir = Rails.root.join('tmp', 'heights')
end

min_lat = 53
max_lat = 56
min_lon = 33
max_lon = 38
min_lat = app['map']['bbox']['min_lat'].to_f.floor
max_lat = app['map']['bbox']['max_lat'].to_f.ceil
min_lon = app['map']['bbox']['min_lng'].to_f.floor
max_lon = app['map']['bbox']['max_lng'].to_f.ceil

puts "Downloading height data..."

Expand Down Expand Up @@ -73,6 +76,7 @@ namespace :osm do
desc "Render map"
task :render do
require 'ruby_mapnik'
require 'map_utils'

db_conf_file = ENV['DB_CONFIG'] || Rails.root.join('config', 'database.yml')
db = YAML.load(File.open db_conf_file)[Rails.env]
Expand Down Expand Up @@ -120,12 +124,15 @@ namespace :osm do

puts "Rendering..."

minx = 614
maxx = 615
miny = 325
maxy = 326
min_lat = app['map']['bbox']['min_lat'].to_f
max_lat = app['map']['bbox']['max_lat'].to_f
min_lng = app['map']['bbox']['min_lng'].to_f
max_lng = app['map']['bbox']['max_lng'].to_f

(minzoom..maxzoom).each do |z|
minx, maxy = MapUtils.zoom_lat_lng_to_x_y(z, min_lat, min_lng).map(&:floor)
maxx, miny = MapUtils.zoom_lat_lng_to_x_y(z, max_lat, max_lng).map(&:floor)

puts "Startinx zoom: #{[z, minx, miny, maxx, maxy].inspect}"

(minx..maxx).each do |x|
Expand All @@ -143,11 +150,6 @@ namespace :osm do

FileUtils.rm_rf "#{tiledir}/#{z}"
FileUtils.move "#{tmpdir}/tiles/#{z}", "#{tiledir}/#{z}"

minx = minx * 2
miny = miny * 2
maxx = maxx * 2 + 1
maxy = maxy * 2 + 1
end

puts "Rendering style '#{style}' completed!"
Expand Down

0 comments on commit 24ee383

Please sign in to comment.