Search code examples
javajsonmapsgisgeojson

Mapfish Print Vector layer GeoJSON example?


I'm using MapFish Print to generate PDFs of maps, creating the map images in the pdf when I only use a WMS layer for base tiles is completely fine. Works as expected.

However, when I add a vector layer and a geoJSON object within it, mapfish print throws the following error:

Exception in thread "tilesReader3" java.lang.NullPointerException
        at org.mapfish.print.map.renderers.vector.PointRenderer.renderImpl(PointRenderer.java:113)
        at org.mapfish.print.map.renderers.vector.PointRenderer.renderImpl(PointRenderer.java:49)
        at org.mapfish.print.map.renderers.vector.GeometriesRenderer.render(GeometriesRenderer.java:69)
        at org.mapfish.print.map.renderers.vector.FeaturesRenderer$FeatureRenderer.renderImpl(FeaturesRenderer.java:62)
        at org.mapfish.print.map.renderers.vector.FeaturesRenderer$FeatureRenderer.renderImpl(FeaturesRenderer.java:58)
        at org.mapfish.print.map.renderers.vector.FeaturesRenderer.render(FeaturesRenderer.java:53)
        at org.mapfish.print.map.renderers.vector.FeaturesRenderer$FeatureCollectionRenderer.renderImpl(FeaturesRenderer.java:70)
        at org.mapfish.print.map.renderers.vector.FeaturesRenderer$FeatureCollectionRenderer.renderImpl(FeaturesRenderer.java:67)
        at org.mapfish.print.map.renderers.vector.FeaturesRenderer.render(FeaturesRenderer.java:53)
        at org.mapfish.print.map.readers.VectorMapReader$1.renderOnPdf(VectorMapReader.java:85)
        at org.mapfish.print.map.ParallelMapTileLoader.handle(ParallelMapTileLoader.java:97)
        at org.mapfish.print.map.ParallelMapTileLoader.handle(ParallelMapTileLoader.java:41)
        at org.pvalsecc.concurrent.OrderedResultsExecutor.addOutput(OrderedResultsExecutor.java:148)
        at org.pvalsecc.concurrent.OrderedResultsExecutor.access$600(OrderedResultsExecutor.java:23)
        at org.pvalsecc.concurrent.OrderedResultsExecutor$Runner.run(OrderedResultsExecutor.java:181)
        at java.lang.Thread.run(Unknown Source)

My query that I am sending to Mapfish print is a slightly modified version of the default sample (to use a different WMS as the default for the example is down), with the Vector layer added in, as follows:

          {
             layout: 'A4 portrait',
             title: 'A simple example',
             srs: 'EPSG:4326',
             units: 'dd',
             outputFilename: 'mapfish-print',
             outputFormat: 'pdf',
             layers: [
                 {
                     type: 'WMS',
                     format: 'image/png',
                     layers: ['OSM-GB:__all__'],
                     baseURL: 'http://www.osmgb.org.uk/ogc/wms'
                 },
                 {
                     type: 'Vector',
                     geoJson: {
                        "type": "FeatureCollection",
                        "features": [
                            {
                                "type": "Feature",
                                "geometry": {
                                    "type": "Point",
                                    "coordinates": [
                                        -0.0996621,
                                        51.5059247
                                    ]
                                },
                                "properties": {
                                }
                            },
                            {
                                "type": "Feature",
                                "geometry": {
                                    "type": "Point",
                                    "coordinates": [
                                        -0.099332,
                                        51.5072000
                                    ]
                                },
                                "properties": {
                                }
                            },
                            {
                                "type": "Feature",
                                "geometry": {
                                    "type": "Point",
                                    "coordinates": [
                                        -0.0945508,
                                        51.50240484
                                    ]
                                },
                                "properties": {
                                }
                            }
                        ]
                    }
                 }
             ],
             pages: [
                 {
                     center: [0.09, 51.50],
                     scale: 4000000,
                     dpi: 190,
                     mapTitle: "First map",
                     comment: "The \"routes\" layer is not shown (the scale is too small)",
                     data: [
                         {id:1, name: 'blah', icon: 'icon_pan'},
                         {id:2, name: 'blip', icon: 'icon_zoomin'}
                     ]
                 },
                 {
                     center: [0.09, 51.50],
                     scale: 500000,
                     dpi: 190,
                     mapTitle: "Second map",
                     comment: "This is a zoomed in version of the first map. Since the scale is more appropriate, we show the \"routes\" layer.",
                     data: [
                         {id:1, name: 'blah', icon: 'icon_pan'},
                         {id:2, name: 'blip', icon: 'icon_zoomin'}
                     ]
                 }
             ]
          }

The GeoJSON validates and displays on geojsonlint.com, just fine. Having looked through the source for mapfish print I cannot figure out why exactly this is occurring. Though it seems to be something to do with styles, I have had no luck adding one into the request to fix this issue.

-- Update:

If anybody else ever comes across this issue, the problem was that (undocumented), you have to define within the vector layer, a set of styles, and make sure each feature in the geoJSON uses a style.


Solution

  • The problem was that (undocumented), you have to define within the vector layer, a set of styles and a styleproperty, and make sure each feature in the geoJSON uses a style.

    The styles themselves are SVG styles, although many of the tags are renamed, so rather than being fill-color, they will be fillColor.

    See here for a bit more detail: http://www.mapfish.org/doc/print/protocol.html#layers-params