basemaps icon indicating copy to clipboard operation
basemaps copied to clipboard

Add support for easy custom layer generation in generate-layers

Open benjaminpreiss opened this issue 1 year ago • 6 comments
trafficstars

Description It should be easily possible to generate custom style layers with generate-layers. I will create a pull request that allows the following syntax: npx protomaps-themes-base protomaps.

Notice the missing theme key at the end! The pull request will then look for a file named protomapsStyles.ts in the current directory and then generate the layers based on the specs from this file.

benjaminpreiss avatar Jun 20 '24 18:06 benjaminpreiss

Somehow I can't link the pull request I created :(

benjaminpreiss avatar Jun 20 '24 20:06 benjaminpreiss

Weird that it won't auto link.

Alternatively, can you paste a link to the PR url here?

nvkelso avatar Jun 20 '24 20:06 nvkelso

Found it! https://github.com/protomaps/basemaps/pull/262

nvkelso avatar Jun 20 '24 20:06 nvkelso

look for a file named protomapsStyles.ts

Is your feature request simply to call generate_layers on a .ts file with a defined theme (instead of one of the pre-built ones), and output a JSON style on stdout?

We should be able to accomplish this without any additional dependency.

bdon avatar Jun 22 '24 17:06 bdon

Yes, that is correct - more specifically, I want to execute generate_layers from NPM and not by cloning the whole repo... Do you know a better way how a .ts file can be executed from within JS?

benjaminpreiss avatar Jun 23 '24 08:06 benjaminpreiss

We removed generate-layers in favor of generate-style, which creates a full MapLibre style object with sensible defaults for glyphs, sprite etc instead of requiring you to construct these yourself.

The 4.x version of the protomaps-themes-base package also requires passing a language code like en or de when generating a style. This isn't released in NPM yet.

For generating a custom style on the command line here is a proposal of what it would look like:

https://github.com/protomaps/basemaps/commit/83bfb9bf01ae86e5fe6681a0304eff925dd6737d

We can use the existing dependency tsx for importing a .ts module so don't need jiti

However I'm not sure if a command like npm run generate-style https://example.com/tiles.json myStyle.ts en out.json is the most intuitive compared to storing a custom theme as a JSON object.

For a custom theme to be a .ts it requires a default export, it is more convenient to have type-checking but it really depends on what TypeScript environment the .ts lives in. Any suggestions?

bdon avatar Sep 03 '24 08:09 bdon

See https://github.com/protomaps/basemaps/pull/472#issuecomment-2899029158 for the solution

This does bring in tsx as a dependency ,but means you can now run generate_style on a .ts, .js or JSON, example .ts files in https://github.com/protomaps/basemaps-flavors/tree/main/flavors

@benjaminpreiss please try it out and LMK if any issues

bdon avatar May 21 '25 19:05 bdon