Last active
August 1, 2016 18:45
-
-
Save ka7eh/7a296b0039a96e2d5a9d2cb293ba626f to your computer and use it in GitHub Desktop.
A simple tile maker, using mapnik and mercantile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
requires: | |
- mapnik 3.x and its python bindings (https://github.com/mapnik/mapnik) | |
- mercantile==0.9.0 (https://github.com/mapbox/mercantile) | |
""" | |
import os | |
import mapnik | |
import mercantile | |
TILE_WIDTH = 256 | |
class TileMaker(object): | |
def __init__(self, extent, zooms, xml_data, output=os.curdir, overwrite_existing_tiles=False): | |
""" | |
Creates 256x256 tiles in `output` for the passed extent and zoom levels | |
:param extent: is a list of coordinates for the working area [west, south, east, north] | |
:param zooms: a list of zoom levels to make tiles for | |
:param xml_data: mapnik xml styling; can be either inline data or the path to an xml file | |
:param output: is the folder to save the tiles in; default is the current working directory | |
:param overwrite_existing_tiles: whether to overwrite existing tile or not | |
""" | |
tiles = mercantile.tiles(*extent, zooms) | |
m = mapnik.Map(TILE_WIDTH, TILE_WIDTH) | |
m.aspect_fix_mode = mapnik.aspect_fix_mode.RESPECT | |
if xml_data.endswith('.xml'): | |
mapnik.load_map(m, xml_data) | |
else: | |
mapnik.load_map_from_string(m, xml_data) | |
for t in tiles: | |
bound = mercantile.bounds(t) | |
cwd = os.path.join(output, '{z}/{x}'.format(z=t.z, x=t.x)) | |
if not os.path.exists(cwd): | |
os.makedirs(cwd) | |
tile_path = os.path.join(cwd, '{y}.png'.format(y=t.y)) | |
if not overwrite_existing_tiles and os.path.exists(tile_path): | |
continue | |
box = mapnik.Box2d(*bound) | |
m.zoom_to_box(box) | |
mapnik.render_to_file(m, tile_path) | |
print('created {}'.format(tile_path)) | |
print('Your tiles are in {}!'.format(output)) | |
if __name__ == '__main__': | |
TileMaker([-125.865224875, 30.970478609, -112.681631125, 43.577740391], range(5, 10), '02_sample.xml', 'tiles', False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Map srs="+proj=longlat +ellps=WGS84 +datum=WGS84"> | |
<Style name="style"> | |
<Rule> | |
<Filter>[col_name] <= -0.75</Filter> | |
<PolygonSymbolizer fill="#444f89" gamma="0"/> | |
</Rule> | |
<Rule> | |
<Filter>[col_name] > -0.75 and [col_name] <= -0.5</Filter> | |
<PolygonSymbolizer fill="#9eaad7" gamma="0"/> | |
</Rule> | |
<Rule> | |
<Filter>[col_name] > -0.5 and [col_name] <= 0</Filter> | |
<PolygonSymbolizer fill="#ffffbe" gamma="0"/> | |
</Rule> | |
<Rule> | |
<Filter>[col_name] > 0 and [col_name] <= 0.5</Filter> | |
<PolygonSymbolizer fill="#bfd67b" gamma="0"/> | |
</Rule> | |
<Rule> | |
<Filter>[col_name] > 0.5 and [col_name] <= 0.75</Filter> | |
<PolygonSymbolizer fill="#7f9a51" gamma="0"/> | |
</Rule> | |
<Rule> | |
<Filter>[col_name] > 0.75</Filter> | |
<PolygonSymbolizer fill="#364d22" gamma="0"/> | |
</Rule> | |
</Style> | |
<Layer name="connectivity" srs="+proj=longlat +ellps=WGS84 +datum=WGS84"> | |
<StyleName>style</StyleName> | |
<Datasource> | |
<Parameter name="type">postgis</Parameter> | |
<Parameter name="host">localhost</Parameter> | |
<Parameter name="port">5432</Parameter> | |
<Parameter name="dbname">spatial_db</Parameter> | |
<Parameter name="user">db_user</Parameter> | |
<Parameter name="password">top_secret</Parameter> | |
<Parameter name="table">spatial_table</Parameter> | |
</Datasource> | |
</Layer> | |
</Map> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment