Skip to content

Instantly share code, notes, and snippets.

@timwaters
Created September 28, 2011 18:33
Show Gist options
  • Save timwaters/1248798 to your computer and use it in GitHub Desktop.
Save timwaters/1248798 to your computer and use it in GitHub Desktop.
ruby mapnik displaying a raster?
CURRENT_PATH = File.expand_path(File.dirname(__FILE__))
require File.join(CURRENT_PATH, '..', 'lib', 'ruby_mapnik')
map = Mapnik::Map.new do |m|
m.width = 2024
m.height = 1768
m.background = Mapnik::Color.new("#fff")
m.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"
nd83_srs = "+proj=lcc +ellps=GRS80 +lat_0=49 +lon_0=-95 +lat+1=49 +lat_2=77 +datum=NAD83 +units=m +no_defs"
wgs84_srs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
m.layer "provinces", nd83_srs do |prov|
prov.datasource = Mapnik::Datasource.create(:type => "shape", :file => File.join(CURRENT_PATH, 'data/boundaries'))
prov.style do |s|
s.rule "[NAME_EN] = 'Ontario'" do |ontario|
ontario.fill = Mapnik::Color.new('#FABEB7')
end
s.rule "[NAME_EN] = 'Quebec'" do |quebec|
quebec.fill = Mapnik::Color.new('#D9EBCB')
end
end
end
m.style 'drainage' do |style|
style.rule '[HYC] = 8' do |drainage|
drainage.fill = Mapnik::Color.new('#99CCFF')
end
end
m.layer "quebec_hydro", nd83_srs do |q_hydro|
q_hydro.datasource = Mapnik::Datasource.create(:type => "shape", :file => File.join(CURRENT_PATH, 'data/qcdrainage'))
q_hydro.styles << 'drainage'
end
m.layer "ontario_hydro", nd83_srs do |o_hydro|
o_hydro.datasource = Mapnik::Datasource.create(:type => "shape", :file => File.join(CURRENT_PATH, 'data/ontdrainage'))
o_hydro.styles << 'drainage'
end
m.layer "provincial_borders", nd83_srs do |borders|
borders.datasource = Mapnik::Datasource.create(:type => "shape", :file => File.join(CURRENT_PATH, 'data/boundaries_l'))
borders.style do |s|
s.rule do |default|
default.line do |stroke|
stroke.color = Mapnik::Color.new('#000')
stroke.dashes << {:gap => 8, :length => 4}
stroke.dashes << {:gap => 2, :length => 2}
stroke.dashes << {:gap => 2, :length => 2}
end
end
end
end
roads_datasource = Mapnik::Datasource.create(:type => "shape", :file => File.join(CURRENT_PATH, 'data/roads'))
m.layer 'roads_3_and_4', nd83_srs do |layer|
layer.datasource = roads_datasource
layer.style do |s|
s.rule '([CLASS] = 3) or ([CLASS] = 4)' do |road|
road.line do |stroke|
stroke.color = Mapnik::Color.new('#AB9E89')
stroke.width = 2.0
end
end
end
end
m.layer 'roads_2', nd83_srs do |layer|
layer.datasource = roads_datasource
layer.style 'road_fill' do |s|
s.rule '[CLASS] = 2' do |road|
road.line do |stroke|
stroke.color = Mapnik::Color.new('#AB9E89')
stroke.width = 4.0
end
end
end
layer.style 'road_border' do |s|
s.rule '[CLASS] = 2' do |road|
road.line do |stroke|
stroke.color = Mapnik::Color.new('#FFFA73')
stroke.width = 2.0
end
end
end
end
m.layer 'roads_1', nd83_srs do |layer|
layer.datasource = roads_datasource
layer.style 'highway_fill' do |s|
s.rule '[CLASS] = 1' do |road|
road.line do |stroke|
stroke.color = Mapnik::Color.new('#BC951C')
stroke.width = 7.0
stroke.line_cap = Mapnik::LineCap::ROUND_CAP
end
end
end
layer.style 'highway_border' do |s|
s.rule '[CLASS] = 1' do |road|
road.line do |stroke|
stroke.color = Mapnik::Color.new('#F2BF24')
stroke.width = 5.0
stroke.line_cap = Mapnik::LineCap::ROUND_CAP
end
end
end
end
m.layer "peter_fox", m.srs do | pfox |
pfox.datasource = Mapnik::Datasource.create(:type => "gdal",
:file => File.join(CURRENT_PATH, 'data/peterfox_merc_big.tif')
)
pfox.style do | style |
style.rule do | default |
default.symbols << Mapnik::RasterSymbolizer.new
end
end
end
m.layer 'populated_places', nd83_srs do |layer|
layer.datasource = Mapnik::Datasource.create(:type => "shape", :file => File.join(CURRENT_PATH, 'data/popplaces'))
layer.style do |style|
style.rule do |default|
color = Mapnik::Color.new('#000')
default.text "[GEONAME]" do |text|
text.label_placement = Mapnik::LABEL_PLACEMENT::POINT_PLACEMENT
text.fill = Mapnik::Color.new('#000')
text.halo_fill = Mapnik::Color.new("#fff")
text.halo_radius = 1
text.avoid_edges = true
end
end
end
end
end
# Rendering...
filename = File.join(CURRENT_PATH, '..', 'tmp', 'rundemo_chippy.png')
map.zoom_to_box(Mapnik::Envelope.new(-9235202.17783,5411904.0521,-7365246.7182,6023400.2783))
#map.zoom_all
map.render_to_file(filename)
`gnome-open #{filename}`
# gdalwarp -t_srs '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs' pf.tif demo/data/peterfox_merc_big.tif
#gdalinfo demo/data/peterfox_merc_big.tif
#Driver: GTiff/GeoTIFF
#Files: demo/data/peterfox_merc_big.tif
#Size is 663, 389
#Coordinate System is:
#PROJCS["unnamed",
# GEOGCS["unnamed ellipse",
# DATUM["unknown",
# SPHEROID["unnamed",6378137,0]],
# PRIMEM["Greenwich",0],
# UNIT["degree",0.0174532925199433]],
# PROJECTION["Mercator_1SP"],
# PARAMETER["central_meridian",0],
# PARAMETER["scale_factor",1],
# PARAMETER["false_easting",0],
# PARAMETER["false_northing",0],
# UNIT["metre",1,
# AUTHORITY["EPSG","9001"]]]
#Origin = (-8359563.993268110789359,5802027.664030012674630)
#Pixel Size = (433.663829985368125,-433.663829985368125)
#Metadata:
# AREA_OR_POINT=Area
#Image Structure Metadata:
# INTERLEAVE=PIXEL
#Corner Coordinates:
#Upper Left (-8359563.993, 5802027.664) ( 75d 5'42.87"W, 46d 8' 6.41"N)
#Lower Left (-8359563.993, 5633332.434) ( 75d 5'42.87"W, 45d 4'29.91"N)
#Upper Right (-8072044.874, 5802027.664) ( 72d30'44.69"W, 46d 8' 6.41"N)
#Lower Right (-8072044.874, 5633332.434) ( 72d30'44.69"W, 45d 4'29.91"N)
#Center (-8215804.434, 5717680.049) ( 73d48'13.78"W, 45d36'27.17"N)
#Band 1 Block=663x3 Type=Byte, ColorInterp=Red
# Mask Flags: PER_DATASET ALPHA
#Band 2 Block=663x3 Type=Byte, ColorInterp=Green
# Mask Flags: PER_DATASET ALPHA
#Band 3 Block=663x3 Type=Byte, ColorInterp=Blue
# Mask Flags: PER_DATASET ALPHA
#Band 4 Block=663x3 Type=Byte, ColorInterp=Alpha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment