Example #1
0
    def _test_rgba_8bui_rendering(lbl, overview, rescale, clip):
        if rescale:
            lbl += ' Sc'
        if clip:
            lbl += ' Cl'
        ds = mapnik.PgRaster(dbname=MAPNIK_TEST_DBNAME, table='(select * from "River") foo',
                             use_overviews=1 if overview else 0,
                             prescale_rasters=rescale, clip_rasters=clip)
        fs = ds.featureset()
        feature = fs.next()
        eq_(feature['rid'], 1)
        lyr = mapnik.Layer('rgba_8bui')
        lyr.datasource = ds
        expenv = mapnik.Box2d(0, -210, 256, 0)
        env = lyr.envelope()
        # As the input size is a prime number both horizontally
        # and vertically, we expect the extent of the overview
        # tables to be a pixel wider than the original, whereas
        # the pixel size in geographical units depends on the
        # overview factor. So we start with the original pixel size
        # as base scale and multiply by the overview factor.
        # NOTE: the overview table extent only grows north and east
        pixsize = 1  # see gdalinfo river.tif
        tol = pixsize * max(overview.split(',')) if overview else 0
        assert_almost_equal(env.minx, expenv.minx)
        assert_almost_equal(env.miny, expenv.miny, delta=tol)
        assert_almost_equal(env.maxx, expenv.maxx, delta=tol)
        assert_almost_equal(env.maxy, expenv.maxy)
        mm = mapnik.Map(256, 256)
        style = mapnik.Style()
        sym = mapnik.RasterSymbolizer()
        rule = mapnik.Rule()
        rule.symbols.append(sym)
        style.rules.append(rule)
        mm.append_style('foo', style)
        lyr.styles.append('foo')
        mm.layers.append(lyr)
        mm.zoom_to_box(expenv)
        im = mapnik.Image(mm.width, mm.height)
        t0 = time.time()  # we want wall time to include IO waits
        mapnik.render(mm, im)
        lap = time.time() - t0
        log('T ' + str(lap) + ' -- ' + lbl + ' E:full')
        expected = 'images/support/pgraster/%s-%s-%s-%s-box1.png' % (
            lyr.name, lbl, overview, clip)
        compare_images(expected, im)
        # no data
        eq_(hexlify(im.view(3, 3, 1, 1).tostring()), b'00000000')
        eq_(hexlify(im.view(250, 250, 1, 1).tostring()), b'00000000')
        # full opaque river color
        eq_(hexlify(im.view(175, 118, 1, 1).tostring()), b'b9d8f8ff')
        # half-transparent pixel
        pxstr = hexlify(im.view(122, 138, 1, 1).tostring()).decode()
        apat = ".*(..)$"
        match = re.match(apat, pxstr)
        assert match, 'pixel ' + pxstr + ' does not match pattern ' + apat
        alpha = match.group(1)
        assert alpha != 'ff' and alpha != '00', \
            'unexpected full transparent/opaque pixel: ' + alpha

        # Now zoom over a portion of the env (1/10)
        newenv = mapnik.Box2d(166, -105, 191, -77)
        mm.zoom_to_box(newenv)
        t0 = time.time()  # we want wall time to include IO waits
        im = mapnik.Image(mm.width, mm.height)
        mapnik.render(mm, im)
        lap = time.time() - t0
        log('T ' + str(lap) + ' -- ' + lbl + ' E:1/10')
        expected = 'images/support/pgraster/%s-%s-%s-%s-box2.png' % (
            lyr.name, lbl, overview, clip)
        compare_images(expected, im)
        # no data
        eq_(hexlify(im.view(255, 255, 1, 1).tostring()), b'00000000')
        eq_(hexlify(im.view(200, 40, 1, 1).tostring()), b'00000000')
        # full opaque river color
        eq_(hexlify(im.view(100, 168, 1, 1).tostring()), b'b9d8f8ff')
        # half-transparent pixel
        pxstr = hexlify(im.view(122, 138, 1, 1).tostring()).decode()
        apat = ".*(..)$"
        match = re.match(apat, pxstr)
        assert match, 'pixel ' + pxstr + ' does not match pattern ' + apat
        alpha = match.group(1)
        assert alpha != 'ff' and alpha != '00', \
            'unexpected full transparent/opaque pixel: ' + alpha
Example #2
0
    def _test_dataraster_16bsi_rendering(lbl, overview, rescale, clip):
        if rescale:
            lbl += ' Sc'
        if clip:
            lbl += ' Cl'
        ds = mapnik.PgRaster(dbname=MAPNIK_TEST_DBNAME, table='"dataRaster"',
                             band=1, use_overviews=1 if overview else 0,
                             prescale_rasters=rescale, clip_rasters=clip)
        fs = ds.featureset()
        feature = fs.next()
        eq_(feature['rid'], 1)
        lyr = mapnik.Layer('dataraster_16bsi')
        lyr.datasource = ds
        expenv = mapnik.Box2d(-14637, 3903178, 1126863, 4859678)
        env = lyr.envelope()
        # As the input size is a prime number both horizontally
        # and vertically, we expect the extent of the overview
        # tables to be a pixel wider than the original, whereas
        # the pixel size in geographical units depends on the
        # overview factor. So we start with the original pixel size
        # as base scale and multiply by the overview factor.
        # NOTE: the overview table extent only grows north and east
        pixsize = 500  # see gdalinfo dataraster.tif
        pixsize = 2497  # see gdalinfo dataraster-small.tif
        tol = pixsize * max(overview.split(',')) if overview else 0
        assert_almost_equal(env.minx, expenv.minx)
        assert_almost_equal(env.miny, expenv.miny, delta=tol)
        assert_almost_equal(env.maxx, expenv.maxx, delta=tol)
        assert_almost_equal(env.maxy, expenv.maxy)
        mm = mapnik.Map(256, 256)
        style = mapnik.Style()
        col = mapnik.RasterColorizer()
        col.default_mode = mapnik.COLORIZER_DISCRETE
        col.add_stop(0, mapnik.Color(0x40, 0x40, 0x40, 255))
        col.add_stop(10, mapnik.Color(0x80, 0x80, 0x80, 255))
        col.add_stop(20, mapnik.Color(0xa0, 0xa0, 0xa0, 255))
        sym = mapnik.RasterSymbolizer()
        sym.colorizer = col
        rule = mapnik.Rule()
        rule.symbols.append(sym)
        style.rules.append(rule)
        mm.append_style('foo', style)
        lyr.styles.append('foo')
        mm.layers.append(lyr)
        mm.zoom_to_box(expenv)
        im = mapnik.Image(mm.width, mm.height)
        t0 = time.time()  # we want wall time to include IO waits
        mapnik.render(mm, im)
        lap = time.time() - t0
        log('T ' + str(lap) + ' -- ' + lbl + ' E:full')
        # no data
        eq_(im.view(1, 1, 1, 1).tostring(), b'\x00\x00\x00\x00')
        eq_(im.view(255, 255, 1, 1).tostring(), b'\x00\x00\x00\x00')
        eq_(im.view(195, 116, 1, 1).tostring(), b'\x00\x00\x00\x00')
        # A0A0A0
        eq_(im.view(100, 120, 1, 1).tostring(), b'\xa0\xa0\xa0\xff')
        eq_(im.view(75, 80, 1, 1).tostring(), b'\xa0\xa0\xa0\xff')
        # 808080
        eq_(im.view(74, 170, 1, 1).tostring(), b'\x80\x80\x80\xff')
        eq_(im.view(30, 50, 1, 1).tostring(), b'\x80\x80\x80\xff')
        # 404040
        eq_(im.view(190, 70, 1, 1).tostring(), b'\x40\x40\x40\xff')
        eq_(im.view(140, 170, 1, 1).tostring(), b'\x40\x40\x40\xff')

        # Now zoom over a portion of the env (1/10)
        newenv = mapnik.Box2d(273663, 4024478, 330738, 4072303)
        mm.zoom_to_box(newenv)
        t0 = time.time()  # we want wall time to include IO waits
        mapnik.render(mm, im)
        lap = time.time() - t0
        log('T ' + str(lap) + ' -- ' + lbl + ' E:1/10')
        # nodata
        eq_(hexlify(im.view(255, 255, 1, 1).tostring()), b'00000000')
        eq_(hexlify(im.view(200, 254, 1, 1).tostring()), b'00000000')
        # A0A0A0
        eq_(hexlify(im.view(90, 232, 1, 1).tostring()), b'a0a0a0ff')
        eq_(hexlify(im.view(96, 245, 1, 1).tostring()), b'a0a0a0ff')
        # 808080
        eq_(hexlify(im.view(1, 1, 1, 1).tostring()), b'808080ff')
        eq_(hexlify(im.view(128, 128, 1, 1).tostring()), b'808080ff')
        # 404040
        eq_(hexlify(im.view(255, 0, 1, 1).tostring()), b'404040ff')
Example #3
0
 def _test_rgba_subquery(lbl, pixtype, r, g, b, a, g1, b1):
     #
     #      3   8   13
     #    +---+---+---+
     #  3 | v | v | h |  NOTE: writes different alpha
     #    +---+---+---+        in 13,8 and 8,13
     #  8 | v | v | a |
     #    +---+---+---+
     # 13 | v | b | v |
     #    +---+---+---+
     #
     sql = "(select 3 as i, " \
           " ST_SetValues(" \
           "  ST_SetValues(" \
           "   ST_AddBand(" \
           "    ST_AddBand(" \
           "     ST_AddBand(" \
           "      ST_AsRaster(" \
           "       ST_MakeEnvelope(0,0,14,14), " \
           "       1.0, -1.0, '%s', %s" \
           "      )," \
           "      '%s', %d::float" \
           "     ), " \
           "     '%s', %d::float" \
           "    ), " \
           "    '%s', %d::float" \
           "   ), " \
           "   2, 11, 6, 4, 5, %s::float8" \
           "  )," \
           "  3, 6, 11, 5, 4, %s::float8" \
           " ) as r" \
           ") as foo" % (
               pixtype,
               r,
               pixtype,
               g,
               pixtype,
               b,
               pixtype,
               a,
               g1,
               b1)
     overview = ''
     rescale = 0
     clip = 0
     if rescale:
         lbl += ' Sc'
     if clip:
         lbl += ' Cl'
     ds = mapnik.PgRaster(dbname=MAPNIK_TEST_DBNAME,
                          table=sql,
                          raster_field='r',
                          use_overviews=0 if overview else 0,
                          prescale_rasters=rescale,
                          clip_rasters=clip)
     fs = ds.featureset()
     feature = fs.next()
     eq_(feature['i'], 3)
     lyr = mapnik.Layer('rgba_subquery')
     lyr.datasource = ds
     expenv = mapnik.Box2d(0, 0, 14, 14)
     env = lyr.envelope()
     assert_almost_equal(env.minx, expenv.minx, places=0)
     assert_almost_equal(env.miny, expenv.miny, places=0)
     assert_almost_equal(env.maxx, expenv.maxx, places=0)
     assert_almost_equal(env.maxy, expenv.maxy, places=0)
     mm = mapnik.Map(15, 15)
     style = mapnik.Style()
     sym = mapnik.RasterSymbolizer()
     rule = mapnik.Rule()
     rule.symbols.append(sym)
     style.rules.append(rule)
     mm.append_style('foo', style)
     lyr.styles.append('foo')
     mm.layers.append(lyr)
     mm.zoom_to_box(expenv)
     im = mapnik.Image(mm.width, mm.height)
     t0 = time.time()  # we want wall time to include IO waits
     mapnik.render(mm, im)
     lap = time.time() - t0
     log('T ' + str(lap) + ' -- ' + lbl + ' E:full')
     expected = 'images/support/pgraster/%s-%s-%s-%s-%s-%s-%s-%s-%s.png' % (
         lyr.name, lbl, pixtype, r, g, b, a, g1, b1)
     compare_images(expected, im)
     hex_v = format(r << 24 | g << 16 | b << 8 | a, '08x').encode()
     hex_a = format(r << 24 | g1 << 16 | b << 8 | a, '08x').encode()
     hex_b = format(r << 24 | g << 16 | b1 << 8 | a, '08x').encode()
     eq_(hexlify(im.view(3, 3, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(8, 3, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(13, 3, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(3, 8, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(8, 8, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(13, 8, 1, 1).tostring()), hex_a)
     eq_(hexlify(im.view(3, 13, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(8, 13, 1, 1).tostring()), hex_b)
     eq_(hexlify(im.view(13, 13, 1, 1).tostring()), hex_v)
Example #4
0
 def _test_data_subquery(lbl, pixtype, value):
     #
     #      3   8   13
     #    +---+---+---+
     #  3 | v | v | v |  NOTE: writes different values
     #    +---+---+---+        in 13,8 and 8,13
     #  8 | v | v | a |
     #    +---+---+---+
     # 13 | v | b | v |
     #    +---+---+---+
     #
     val_a = value / 3
     val_b = val_a * 2
     sql = "(select 3 as i, " \
           " ST_SetValues(" \
           "  ST_SetValues(" \
           "   ST_AsRaster(" \
           "    ST_MakeEnvelope(0,0,14,14), " \
           "    1.0, -1.0, '%s', %s" \
           "   ), " \
           "   11, 6, 5, 5, %s::float8" \
           "  )," \
           "  6, 11, 5, 5, %s::float8" \
           " ) as \"R\"" \
           ") as foo" % (pixtype, value, val_a, val_b)
     overview = ''
     rescale = 0
     clip = 0
     if rescale:
         lbl += ' Sc'
     if clip:
         lbl += ' Cl'
     ds = mapnik.PgRaster(dbname=MAPNIK_TEST_DBNAME,
                          table=sql,
                          raster_field='R',
                          use_overviews=0 if overview else 0,
                          band=1,
                          prescale_rasters=rescale,
                          clip_rasters=clip)
     fs = ds.featureset()
     feature = fs.next()
     eq_(feature['i'], 3)
     lyr = mapnik.Layer('data_subquery')
     lyr.datasource = ds
     expenv = mapnik.Box2d(0, 0, 14, 14)
     env = lyr.envelope()
     assert_almost_equal(env.minx, expenv.minx, places=0)
     assert_almost_equal(env.miny, expenv.miny, places=0)
     assert_almost_equal(env.maxx, expenv.maxx, places=0)
     assert_almost_equal(env.maxy, expenv.maxy, places=0)
     mm = mapnik.Map(15, 15)
     style = mapnik.Style()
     col = mapnik.RasterColorizer()
     col.default_mode = mapnik.COLORIZER_DISCRETE
     col.add_stop(val_a, mapnik.Color(0xff, 0x00, 0x00, 255))
     col.add_stop(val_b, mapnik.Color(0x00, 0xff, 0x00, 255))
     col.add_stop(value, mapnik.Color(0x00, 0x00, 0xff, 255))
     sym = mapnik.RasterSymbolizer()
     sym.colorizer = col
     rule = mapnik.Rule()
     rule.symbols.append(sym)
     style.rules.append(rule)
     mm.append_style('foo', style)
     lyr.styles.append('foo')
     mm.layers.append(lyr)
     mm.zoom_to_box(expenv)
     im = mapnik.Image(mm.width, mm.height)
     t0 = time.time()  # we want wall time to include IO waits
     mapnik.render(mm, im)
     lap = time.time() - t0
     log('T ' + str(lap) + ' -- ' + lbl + ' E:full')
     expected = 'images/support/pgraster/%s-%s-%s-%s.png' % (lyr.name, lbl,
                                                             pixtype, value)
     compare_images(expected, im)
Example #5
0
 def _test_grayscale_subquery(lbl, pixtype, value):
     #
     #      3   8   13
     #    +---+---+---+
     #  3 | v | v | v |  NOTE: writes different color
     #    +---+---+---+        in 13,8 and 8,13
     #  8 | v | v | a |
     #    +---+---+---+
     # 13 | v | b | v |
     #    +---+---+---+
     #
     val_a = int(value / 3)
     val_b = val_a * 2
     sql = "(select 3 as i, " \
           " ST_SetValues(" \
           "  ST_SetValues(" \
           "   ST_AsRaster(" \
           "    ST_MakeEnvelope(0,0,14,14), " \
           "    1.0, -1.0, '%s', %s" \
           "   ), " \
           "   11, 6, 4, 5, %s::float8" \
           "  )," \
           "  6, 11, 5, 4, %s::float8" \
           " ) as \"R\"" \
           ") as foo" % (pixtype, value, val_a, val_b)
     rescale = 0
     clip = 0
     if rescale:
         lbl += ' Sc'
     if clip:
         lbl += ' Cl'
     ds = mapnik.PgRaster(dbname=MAPNIK_TEST_DBNAME,
                          table=sql,
                          raster_field='"R"',
                          use_overviews=1,
                          prescale_rasters=rescale,
                          clip_rasters=clip)
     fs = ds.featureset()
     feature = fs.next()
     eq_(feature['i'], 3)
     lyr = mapnik.Layer('grayscale_subquery')
     lyr.datasource = ds
     expenv = mapnik.Box2d(0, 0, 14, 14)
     env = lyr.envelope()
     assert_almost_equal(env.minx, expenv.minx, places=0)
     assert_almost_equal(env.miny, expenv.miny, places=0)
     assert_almost_equal(env.maxx, expenv.maxx, places=0)
     assert_almost_equal(env.maxy, expenv.maxy, places=0)
     mm = mapnik.Map(15, 15)
     style = mapnik.Style()
     sym = mapnik.RasterSymbolizer()
     rule = mapnik.Rule()
     rule.symbols.append(sym)
     style.rules.append(rule)
     mm.append_style('foo', style)
     lyr.styles.append('foo')
     mm.layers.append(lyr)
     mm.zoom_to_box(expenv)
     im = mapnik.Image(mm.width, mm.height)
     t0 = time.time()  # we want wall time to include IO waits
     mapnik.render(mm, im)
     lap = time.time() - t0
     log('T ' + str(lap) + ' -- ' + lbl + ' E:full')
     expected = 'images/support/pgraster/%s-%s-%s-%s.png' % (lyr.name, lbl,
                                                             pixtype, value)
     compare_images(expected, im)
     h = format(value, '02x')
     hex_v = h + h + h + 'ff'
     hex_v = hex_v.encode()
     h = format(val_a, '02x')
     hex_a = h + h + h + 'ff'
     hex_a = hex_a.encode()
     h = format(val_b, '02x')
     hex_b = h + h + h + 'ff'
     hex_b = hex_b.encode()
     eq_(hexlify(im.view(3, 3, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(8, 3, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(13, 3, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(3, 8, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(8, 8, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(13, 8, 1, 1).tostring()), hex_a)
     eq_(hexlify(im.view(3, 13, 1, 1).tostring()), hex_v)
     eq_(hexlify(im.view(8, 13, 1, 1).tostring()), hex_b)
     eq_(hexlify(im.view(13, 13, 1, 1).tostring()), hex_v)
Example #6
0
 def _test_rgb_8bui_rendering(lbl, tnam, overview, rescale, clip):
     if rescale:
         lbl += ' Sc'
     if clip:
         lbl += ' Cl'
     ds = mapnik.PgRaster(dbname=MAPNIK_TEST_DBNAME,
                          table=tnam,
                          use_overviews=1 if overview else 0,
                          prescale_rasters=rescale,
                          clip_rasters=clip)
     fs = ds.featureset()
     feature = fs.next()
     eq_(feature['rid'], 1)
     lyr = mapnik.Layer('rgba_8bui')
     lyr.datasource = ds
     expenv = mapnik.Box2d(-12329035.7652168, 4508650.39854396,
                           -12328653.0279471, 4508957.34625536)
     env = lyr.envelope()
     # As the input size is a prime number both horizontally
     # and vertically, we expect the extent of the overview
     # tables to be a pixel wider than the original, whereas
     # the pixel size in geographical units depends on the
     # overview factor. So we start with the original pixel size
     # as base scale and multiply by the overview factor.
     # NOTE: the overview table extent only grows north and east
     pixsize = 2  # see gdalinfo nodata-edge.tif
     tol = pixsize * max(overview.split(',')) if overview else 0
     assert_almost_equal(env.minx, expenv.minx, places=0)
     assert_almost_equal(env.miny, expenv.miny, delta=tol)
     assert_almost_equal(env.maxx, expenv.maxx, delta=tol)
     assert_almost_equal(env.maxy, expenv.maxy, places=0)
     mm = mapnik.Map(256, 256)
     style = mapnik.Style()
     sym = mapnik.RasterSymbolizer()
     rule = mapnik.Rule()
     rule.symbols.append(sym)
     style.rules.append(rule)
     mm.append_style('foo', style)
     lyr.styles.append('foo')
     mm.layers.append(lyr)
     mm.zoom_to_box(expenv)
     im = mapnik.Image(mm.width, mm.height)
     t0 = time.time()  # we want wall time to include IO waits
     mapnik.render(mm, im)
     lap = time.time() - t0
     log('T ' + str(lap) + ' -- ' + lbl + ' E:full')
     expected = 'images/support/pgraster/%s-%s-%s-%s-%s-box1.png' % (
         lyr.name, tnam, lbl, overview, clip)
     compare_images(expected, im)
     # no data
     eq_(hexlify(im.view(3, 16, 1, 1).tostring()), b'00000000')
     eq_(hexlify(im.view(128, 16, 1, 1).tostring()), b'00000000')
     eq_(hexlify(im.view(250, 16, 1, 1).tostring()), b'00000000')
     eq_(hexlify(im.view(3, 240, 1, 1).tostring()), b'00000000')
     eq_(hexlify(im.view(128, 240, 1, 1).tostring()), b'00000000')
     eq_(hexlify(im.view(250, 240, 1, 1).tostring()), b'00000000')
     # dark brown
     eq_(hexlify(im.view(174, 39, 1, 1).tostring()), b'c3a698ff')
     # dark gray
     eq_(hexlify(im.view(195, 132, 1, 1).tostring()), b'575f62ff')
     # Now zoom over a portion of the env (1/10)
     newenv = mapnik.Box2d(-12329035.7652168, 4508926.651484220,
                           -12328997.49148983, 4508957.34625536)
     mm.zoom_to_box(newenv)
     t0 = time.time()  # we want wall time to include IO waits
     im = mapnik.Image(mm.width, mm.height)
     mapnik.render(mm, im)
     lap = time.time() - t0
     log('T ' + str(lap) + ' -- ' + lbl + ' E:1/10')
     expected = 'images/support/pgraster/%s-%s-%s-%s-%s-box2.png' % (
         lyr.name, tnam, lbl, overview, clip)
     compare_images(expected, im)
     # no data
     eq_(hexlify(im.view(3, 16, 1, 1).tostring()), b'00000000')
     eq_(hexlify(im.view(128, 16, 1, 1).tostring()), b'00000000')
     eq_(hexlify(im.view(250, 16, 1, 1).tostring()), b'00000000')
     # black
     eq_(hexlify(im.view(3, 42, 1, 1).tostring()), b'000000ff')
     eq_(hexlify(im.view(3, 134, 1, 1).tostring()), b'000000ff')
     eq_(hexlify(im.view(3, 244, 1, 1).tostring()), b'000000ff')
     # gray
     eq_(hexlify(im.view(135, 157, 1, 1).tostring()), b'4e555bff')
     # brown
     eq_(hexlify(im.view(195, 223, 1, 1).tostring()), b'f2cdbaff')