Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PolygonPatternSymbolizer question #4007

Closed
gitdevnull opened this issue Oct 31, 2018 · 7 comments
Closed

PolygonPatternSymbolizer question #4007

gitdevnull opened this issue Oct 31, 2018 · 7 comments

Comments

@gitdevnull
Copy link

gitdevnull commented Oct 31, 2018

Hi,

Could someone please explain why would the PolygonPatternSymbolizer example below not work?
It works as expected using PolygonSymbolizer, highlighting the necessary polygons, but not when I try to use a .svg pattern instead.

Any suggestions much appreciated.

#!/usr/bin/env python

import mapnik
import cairo
import json

surface = cairo.PDFSurface('test.pdf',800,600)
context = cairo.Context(surface)

m = mapnik.Map(1280,1810)
m.background = mapnik.Color('lightblue')

s_main = mapnik.Style()
r_main = mapnik.Rule()
polygon_symbolizer = mapnik.PolygonSymbolizer()
polygon_symbolizer.fill = mapnik.Color('#ccc')
r_main.symbols.append(polygon_symbolizer)

s_data = mapnik.Style()
# PolygonSymbolizer below works as desired
# data_polygon_symbolizer = mapnik.PolygonSymbolizer()
# data_polygon_symbolizer.fill = mapnik.Color('rgb(0%,0%,75%)')
# data_polygon_symbolizer.fill_opacity = 1.0
data_polygon_symbolizer = mapnik.PolygonPatternSymbolizer('patterns/pattern.svg','svg',8,8)
r_data = mapnik.Rule()
r_data.filter = mapnik.Expression("[value]=1")
r_data.symbols.append(data_polygon_symbolizer)

s_main.rules.append(r_main)
s_data.rules.append(r_data)
m.append_style('Main Style', s_main)
m.append_style('Pattern', s_data)
w = mapnik.Shapefile(file='maps/ne_110m_admin_0_countries.shp')
layer = mapnik.Layer('World')
layer.datasource = w
layer.styles.append('Main Style')
m.layers.append(layer)
datalayer = mapnik.Layer('Data')
data = mapnik.GeoJSON(file='data.json')
datalayer.datasource = data
datalayer.styles.append('Main Style')
datalayer.styles.append('Pattern')
dbox = datalayer.envelope()
m.layers.append(datalayer)
m.zoom_to_box(dbox)
m.zoom(-1.1)

mapnik.render(m, context)

surface.finish()

P.S. I'm using mapnik 3.0.12

@talaj
Copy link
Member

talaj commented Oct 31, 2018

There is no constructor such as

mapnik.PolygonPatternSymbolizer('patterns/pattern.svg','svg',8,8)

I would try it this way, but I haven't tested if it really works:

sym = mapnik.PolygonPatternSymbolizer()
sym.file = 'patterns/pattern.svg'

Why not to define the style as XML?

There is a drawback concerning rendering PolygonPatternSymbolizer with an SVG into a PDF now. Mapnik is rasterizing the SVG first so the pattern in resulting PDF is not rendered as vectors. I have solved this in downstream version of Mapnik, but haven't merged it to the upstream yet.

@gitdevnull
Copy link
Author

Thank you for pointing me in the right direction - defining the style in a .xml worked for a .png pattern (however not .svg)

I've also tried the following before:

data_polygon_symbolizer = mapnik.PolygonPatternSymbolizer()
data_polygon_symbolizer.file = '../patterns/pattern.png' # As well as .svg

But neither raster, not vector version, have produced any fill.

P.S. Really useful info regarding .svg patterns being rasterized btw... however sad it may be

@talaj
Copy link
Member

talaj commented Oct 31, 2018

defining the style in a .xml worked for a .png pattern (however not .svg)

That's strange, there should not be much difference. Maybe Mapnik has problem with interpreting that SVG. Can you, please, try some SVG from Mapnik test data which is tested to render correctly:
https://github.com/mapnik/test-data/blob/master/svg/octocat.svg

@gitdevnull
Copy link
Author

Indeed, a faulty .svg

@talaj
Copy link
Member

talaj commented Oct 31, 2018

There was some work on SVG processing since Mapnik 3.0.12. The latest version of Mapnik could handle that SVG better.

If the SVG seems correct, you can fill a new issue with that SVG file.

@gitdevnull
Copy link
Author

It was an InkScape .svg that was not compressed/optimized

@talaj
Copy link
Member

talaj commented Nov 2, 2018

@gitdevnull Just for information, I have created a patch for rendering vector patterns in #4009.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants