Skip to content

Only the standard layer can be exported as an image, but it cannot be either #433

Open

Description

@1ec5

The Share panel’s Image option does not work when any layer is selected.

Problem

The Image option is only enabled if you have OpenStreetMap’s Standard layer selected. Otherwise, if you use another layer such as the Historical layer, this message appears:

Only the standard layer can be exported as an image

But if you switch to the Standard layer and try to share it, it fails with a server error:

Missing or invalid token

Diagnosis

In openstreetmap-website, the image sharing feature is limited to the Standard style because openstreetmap.org is only set up to run Mapnik locally against the openstreetmap-carto style; the other styles are hotlinked from other servers: openstreetmap/openstreetmap-website#1141.

ohm-website doesn’t have the openstreetmap-carto style to work with, so the option should be disabled for the Standard layer. On the other hand, the Historical and Woodblock style are rendered on the client side from vector tiles, so in principle it should be possible for site JavaScript to “export” an image without contacting the server. When setting up GL JS, we can set the preserveDrawingBuffer option and get the map canvas as PNG data when sharing. The option is disabled by default for performance reasons, but maybe we could reinitialize the map when sharing.

The mapbox-gl-export plugin claims to support JPEG, SVG, and PDF in addition to PNG. However, it doesn’t support runtime styling: watergis/maplibre-gl-export#16 (comment). leaflet-ohm-timeslider-v2 relies on runtime styling to filter the tiles by date, so we might need to forego these formats in favor of getting the PNG to look right. But it might be possible to cobble together an animated GIF or APNG that runs through the times specified by the time slider. A timelapse image would really differentiate OHM from OSM.

Impact

Enabling this feature for the Historical and Woodblock styles would make it much easier for a user to share their contributions on social media or tease OpenHistoricalMap’s potential when reaching out to a local historical society by e-mail.

Activity

  1. danrademacher commented on Nov 15, 2022

    @danrademacher

    Based on this part:

    leaflet-ohm-timeslider-v2 relies on runtime styling to filter the tiles by date, so we might need to forego these formats in favor of getting the PNG to look right.

    So we think that PNG format will include runtime styling but other formats would not, is that correct?

    Sound right based on this from the GL docs:

    If true , the map's canvas can be exported to a PNG using map.getCanvas().toDataURL() . This is false by default as a performance optimization.

    That file is here:
    https://github.com/OpenHistoricalMap/ohm-website/blob/staging/app/views/export/embed.html.erb

    I wonder if just implementing some of the same JS we have on the main page map would provide what we want here.

  2. 1ec5 commented on Nov 15, 2022

    @1ec5
    MemberAuthor

    This issue is about a static image export, rather than an interactive embed, which is #485. As things stand, the only image format that would definitely include the filtering we need is PNG, because GL JS can serve up PNG natively. I haven’t played with maplibre-gl-export yet, so I’m simply unsure about the other formats that plugin can export.

  3. danrademacher commented on Jul 12, 2023

    @danrademacher

    Pulling over from internal Slack convo -- a fix require only a few lines of code, but I'm not sure what those would be yet.

    Since we last touched this issue, we did some work on updating the embed.html, and js to include the OHM default layer and timeslider, but we never merged and deployed due to #495

    But that's the only view I see here:
    https://github.com/OpenHistoricalMap/ohm-website/tree/staging/app/views/export

    I wonder if the image export uses that same embed view, in which case maybe we can enable preserveDrawingBuffer there and the export would work?

  4. 1ec5 commented on Jul 12, 2023

    @1ec5
    MemberAuthor

    I wonder if the image export uses that same embed view, in which case maybe we can enable preserveDrawingBuffer there and the export would work?

    I had been thinking it would be more straightforward to hook up the existing image export functionality to GL JS:

    1. When we create the MapboxGL object, we should set preserveDrawingBuffer to true.
    2. Currently, clicking the Download button submits the form to the /export/finish API endpoint. But for these layers, we should intercept and cancel the form submission.
    3. Instead, it should get GL JS’s <canvas> element by querying the selector .mapboxgl-canvas, then take a screenshot of it by converting its 2D context to a PNG-formatted data: URI.
    4. We’d either need to disable the scale option or have it automatically zoom the map before screenshotting the canvas.

    Redirecting to an embed page and then screenshotting might be an alternative, but I was thinking it would be kind of awkward to orchestrate all that.

    /ref watergis/maplibre-gl-export#16 (comment)

  5. 1ec5 commented on Jul 12, 2023

    @1ec5
    MemberAuthor

    I don’t have a development environment set up for ohm-website, but I was able to get the mechanism above working in my browser locally:

    1. Simulate step (1) above:
      1. Set a breakpoint in application-11d5e4a2cbe139b2c49b6f9456bb8b2c0c65a337f52c14b73c2d025c13b6b83b.js line 199, within the baselayerchange event handler. (It’s easier to get there if you search the debugger for all files containing baselayerchange.)
      2. Switch to another vector layer, then run this in the console and continue:
        this.baseLayers[0].options.preserveDrawingBuffer = true;
      3. Disable the breakpoint, then switch to another vector layer.
    2. Simulate step (3) above:
      1. Run this in the console:
        canvas = $("#map .mapboxgl-canvas")[0];
        location = canvas.toDataURL("image/png");

    San José

  6. 1ec5 commented on Jul 12, 2023

    @1ec5
    MemberAuthor

    But it might be possible to cobble together an animated GIF or APNG that runs through the times specified by the time slider. A timelapse image would really differentiate OHM from OSM.

    I think a timelapse animation would be icing on top, not strictly required for fixing this issue, so I split it out as #569.

  7. added theissue type on Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions