Skip to content

Instantly share code, notes, and snippets.

@john-sanders
Created January 4, 2015 13:39
Show Gist options
  • Save john-sanders/800b973858bc13e4417c to your computer and use it in GitHub Desktop.
Save john-sanders/800b973858bc13e4417c to your computer and use it in GitHub Desktop.
import mapnik
#datasource rasters for land and sea, plus world vector
landDatasource = mapnik.Gdal(file="land/land.vrt")
seaDatasource = mapnik.Gdal(file="sea/sea.vrt")
vectorDatasource = mapnik.GeoJSON(file="ne_10m_admin_0_countries_lakes/ne_10m_admin_0_countries_lakes.geojson")
# create our three layers: sea, land, vector mask
landLayer = mapnik.Layer("land")
landLayer.datasource = landDatasource
seaLayer = mapnik.Layer("sea");
seaLayer.datasource = seaDatasource
borderLayer = mapnik.Layer("Border")
borderLayer.datasource = borderDatasource
#land symbolizer, rule, style
landSym = mapnik.RasterSymbolizer()
landRule = mapnik.Rule()
landRule.symbols.append( landSym )
landStyle = mapnik.Style()
landStyle.rules.append(landRule)
#sea symbolizer, rule, style
seaSym = mapnik.RasterSymbolizer()
seaRule = mapnik.Rule()
seaRule.symbols.append( seaSym )
seaStyle = mapnik.Style()
seaStyle.rules.append(seaRule)
# vector mask symbolizer, rule style
vectorSym = mapnik.PolygonSymbolizer(mapnik.Color("rgb(255,255,255)"))
vectorSym.comp_op = mapnik.CompositeOp.dst_in
vectorStyle = mapnik.Style()
vectorRule = mapnik.Rule()
vectorRule.symbols.append(vectorSym)
vectorStyle.rules.append(vectorRule)
#creating the map
map = mapnik.Map(MAP_WIDTH, MAP_HEIGHT, "+proj=longlat +datum=WGS84")
map.background = mapnik.Color("#000000")
map.append_style("Land Style", landStyle)
map.append_style("Sea Style", seaStyle)
map.append_style('Vector Style', vectorStyle)
landLayer.styles.append('Land Style')
seaLayer.styles.append("Sea Style")
vectorLayer.styles.append("Vector Style")
map.layers.append(seaLayer)
map.layers.append(vectorLayer);
map.layers.append(landLayer)
#zoom to somewhere random and render
MIN_X = -10
MAX_X = 10
MIN_Y = 45
MAX_Y = 55
MAP_WIDTH = 1920
MAP_HEIGHT = 1080
map.zoom_to_box(mapnik.Envelope(MIN_X, MIN_Y, MAX_X, MAX_Y))
mapnik.render_to_file(map, "test.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment