Пример #1
0
    def get_one(self, tile):
        bbox = self.tilegrid.extent(tile.tilecoord, self.buffer)
        bbox2d = mapnik.Box2d(bbox[0], bbox[1], bbox[2], bbox[3])

        size = tile.tilecoord.n * self.tilegrid.tile_size + 2 * self.buffer
        self.mapnik.resize(size, size)
        self.mapnik.zoom_to_box(bbox2d)

        if self.output_format == 'grid':
            grid = mapnik.Grid(self.tilegrid.tile_size,
                               self.tilegrid.tile_size)
            for n, l in enumerate(self.mapnik.layers):
                if l.name in self.layers_fields:
                    mapnik.render_layer(self.mapnik,
                                        grid,
                                        layer=n,
                                        fields=self.layers_fields[l.name])

            encode = grid.encode('utf', resolution=self.resolution)
            if self.drop_empty_utfgrid and len(encode['data'].keys()) == 0:
                return None
            tile.data = dumps(encode)
        else:
            # Render image with default Agg renderer
            im = mapnik.Image(size, size)
            mapnik.render(self.mapnik, im)
            tile.data = im.tostring(self.output_format)

        return tile
Пример #2
0
    def renderTile(self, width, height, srs, coord):
        """
        """
        if self.mapnik is None:
            self.mapnik = mapnik.Map(0, 0)
            mapnik.load_map(self.mapnik, str(self.mapfile))

        nw = self.layer.projection.coordinateLocation(coord)
        se = self.layer.projection.coordinateLocation(coord.right().down())
        ul = self.mercator.locationProj(nw)
        lr = self.mercator.locationProj(se)

        self.mapnik.width = width
        self.mapnik.height = height
        self.mapnik.zoom_to_box(mapnik.Box2d(ul.x, ul.y, lr.x, lr.y))

        # create grid as same size as map/image
        grid = mapnik.Grid(width, height)
        # render a layer to that grid array
        mapnik.render_layer(self.mapnik,
                            grid,
                            layer=self.layer_index,
                            fields=self.fields)
        # then encode the grid array as utf, resample to 1/scale the size, and dump features
        grid_utf = grid.encode('utf', resolution=self.scale, features=True)

        if self.wrapper is None:
            return SaveableResponse(json.dumps(grid_utf))
        else:
            return SaveableResponse(self.wrapper + '(' + json.dumps(grid_utf) +
                                    ')')
    def render_tile(self, tile_uri, x, y, z):

        # Calculate pixel positions of bottom-left & top-right
        p0 = (x * 256, (y + 1) * 256)
        p1 = ((x + 1) * 256, y * 256)

        # Convert to LatLong (EPSG:4326)
        l0 = self.tileproj.fromPixelToLL(p0, z)
        l1 = self.tileproj.fromPixelToLL(p1, z)

        # Convert to map projection (e.g. mercator co-ords EPSG:900913)
        c0 = self.prj.forward(mapnik.Coord(l0[0], l0[1]))
        c1 = self.prj.forward(mapnik.Coord(l1[0], l1[1]))

        # Bounding box for the tile
        if hasattr(mapnik,
                   'mapnik_version') and mapnik.mapnik_version() >= 800:
            bbox = mapnik.Box2d(c0.x, c0.y, c1.x, c1.y)
        else:
            bbox = mapnik.Envelope(c0.x, c0.y, c1.x, c1.y)
        render_size = 256
        self.m.resize(render_size, render_size)
        self.m.zoom_to_box(bbox)
        self.m.buffer_size = 128

        # Render image with default Agg renderer
        im = mapnik.Image(render_size, render_size)
        mapnik.render(self.m, im)
        im.save(tile_uri, 'png256')
        grid_uri = tile_uri.replace('.png', '.grid.json')

        # new mapnik.Grid api, works like mapnik.Image
        # with the exception that you can only render one
        # layer to it with the mapnik.render_layer function

        # create grid as same size as map/image
        grid = mapnik.Grid(render_size, render_size)
        # render a layer to that grid array
        mapnik.render_layer(self.m, grid, layer=0, fields=['POP2005', 'NAME'])
        # then encode the grid array as utf, resample to 1/4 the size, and dump features
        grid_utf = grid.encode('utf', resolution=4, add_features=True)

        # below is the old grid api - will be removed soon, don't use
        #grid_utf = mapnik.render_grid(self.m,0,key='__id__',resolution=4,fields=['POP2005','NAME'])
        # client code uses jsonp, so fake by wrapping in grid() callback
        open(grid_uri, 'wb').write('grid(' + json.dumps(grid_utf) + ')')
Пример #4
0
    def render_tile(self, tile_uri, x, y, z):
        # Calculate pixel positions of bottom-left & top-right
        p0 = (x * TILES_SIZE, (y + 1) * TILES_SIZE)
        p1 = ((x + 1) * TILES_SIZE, y * TILES_SIZE)

        # Convert to LatLong (EPSG:4326)
        l0 = self.tileproj.fromPixelToLL(p0, z)
        l1 = self.tileproj.fromPixelToLL(p1, z)

        # Convert to map projection (e.g. mercator co-ords EPSG:900913)
        c0 = self.prj.forward(mapnik.Coord(l0[0], l0[1]))
        c1 = self.prj.forward(mapnik.Coord(l1[0], l1[1]))

        # Bounding box for the tile
        if hasattr(mapnik,
                   'mapnik_version') and mapnik.mapnik_version() >= 800:
            bbox = mapnik.Box2d(c0.x, c0.y, c1.x, c1.y)
        else:
            bbox = mapnik.Envelope(c0.x, c0.y, c1.x, c1.y)
        render_size = TILES_SIZE
        self.m.resize(render_size, render_size)
        self.m.zoom_to_box(bbox)
        self.m.buffer_size = 128

        if FORMAT == 'grid':
            grid = mapnik.Grid(render_size, render_size)
            #            mapnik.render_layer(self.m, grid, layer=64, fields=['name'])
            for n, l in enumerate(self.m.layers):
                if l.name != 'admin-012345678':
                    if 'name' in l.datasource.fields():
                        mapnik.render_layer(self.m,
                                            grid,
                                            layer=n,
                                            fields=['name'])
            utfgrid = grid.encode('utf', resolution=4)
            f = open(tile_uri + '.' + FILE_EXTENSION, 'w')
            f.write(json.dumps(utfgrid))
            f.close()
        else:
            # Render image with default Agg renderer
            im = mapnik.Image(render_size, render_size)
            mapnik.render(self.m, im)
            im.save(tile_uri + '.' + FILE_EXTENSION, FORMAT)
Пример #5
0
def test_render_grid2():
    """ test old against new"""
    width,height = 256,256
    m = create_grid_map(width,height)
    ul_lonlat = mapnik2.Coord(142.30,-38.20)
    lr_lonlat = mapnik2.Coord(143.40,-38.80)
    m.zoom_to_box(mapnik2.Box2d(ul_lonlat,lr_lonlat))

    # new method
    grid = mapnik2.Grid(m.width,m.height,key='Name')
    mapnik2.render_layer(m,grid,layer=0,fields=['Name'])
    utf1 = grid.encode('utf',resolution=4)
    eq_(utf1,grid_correct_new)

    # old method - to be removed
    utf2 = mapnik2.render_grid(m,0,key='Name',resolution=4,fields=['Name'])
    eq_(utf2,grid_correct)

    # for complex polygons these will not be true
    eq_(len(utf2['grid']),len(utf1['grid']))
    eq_(len(utf2['keys']),len(utf1['keys']))
    eq_(len(utf2['data']),len(utf1['data']))
    
    # check a full view is the same as a full image
    grid_view = grid.view(0,0,width,height)
    # for kicks check at full res too
    utf3 = grid.encode('utf',resolution=1)
    utf4 = grid_view.encode('utf',resolution=1)
    eq_(utf3['grid'],utf4['grid'])
    eq_(utf3['keys'],utf4['keys'])
    eq_(utf3['data'],utf4['data'])
    
    eq_(resolve(utf4,0,0),None)
    
    # resolve some center points in the
    # resampled view
    utf5 = grid_view.encode('utf',resolution=4)
    eq_(resolve(utf5,25,10),{"Name": "North West"})
    eq_(resolve(utf5,25,46),{"Name": "North East"})
    eq_(resolve(utf5,38,10),{"Name": "South West"})
    eq_(resolve(utf5,38,46),{"Name": "South East"})
Пример #6
0
    def renderTile(self, width, height, srs, coord):
        """
        """
        if self.mapnik is None:
            self.mapnik = mapnik.Map(0, 0)
            mapnik.load_map(self.mapnik, str(self.mapfile))

        # buffer as fraction of tile size
        buffer = float(self.buffer) / 256

        nw = self.layer.projection.coordinateLocation(
            coord.left(buffer).up(buffer))
        se = self.layer.projection.coordinateLocation(
            coord.right(1 + buffer).down(1 + buffer))
        ul = self.mercator.locationProj(nw)
        lr = self.mercator.locationProj(se)

        self.mapnik.width = width + 2 * self.buffer
        self.mapnik.height = height + 2 * self.buffer
        self.mapnik.zoom_to_box(mapnik.Box2d(ul.x, ul.y, lr.x, lr.y))

        # create grid as same size as map/image
        grid = mapnik.Grid(width + 2 * self.buffer, height + 2 * self.buffer)
        # render a layer to that grid array
        mapnik.render_layer(self.mapnik,
                            grid,
                            layer=self.layer_index,
                            fields=self.fields)
        # extract a gridview excluding the buffer
        grid_view = grid.view(self.buffer, self.buffer, width, height)
        # then encode the grid array as utf, resample to 1/scale the size, and dump features
        grid_utf = grid_view.encode('utf',
                                    resolution=self.scale,
                                    add_features=True)

        if self.wrapper is None:
            return SaveableResponse(json.dumps(grid_utf))
        else:
            return SaveableResponse(self.wrapper + '(' + json.dumps(grid_utf) +
                                    ')')
Пример #7
0
#!/usr/bin/env python

import os
import json
import mapnik2

m = mapnik2.Map(256,256)
mapnik2.load_map(m,'stylesheet.xml')

# tile with south america
tile = mapnik2.Box2d(-10018754.171394622,-10018754.171394622,0,0)
m.zoom_to_box(tile)

# render image
im = mapnik2.Image(m.width, m.height)
mapnik2.render(m,im)
im.save("test.png")
# render grid
grid = mapnik2.Grid(m.width, m.height)
mapnik2.render_layer(m,grid,layer=0,fields=['POP2005','NAME'])
grid_utf = grid.encode()
open('test.json','w').write(json.dumps(grid_utf))