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

Fix Broken rendering - No handling of local GeoJSON files #1324 #1326

Merged
merged 3 commits into from
Sep 1, 2024

Conversation

okimiko
Copy link
Contributor

@okimiko okimiko commented Jul 20, 2024

Add general missing handling of local geojson files during rendering (#1324).

@acalcutt
Copy link
Collaborator

I'm not sure I would want to take this approach. I kind of liked you other idea of adding a "file://" protocol, where you could set up path for "file" paths like we do for others paths https://tileserver.readthedocs.io/en/latest/config.html#paths

For that to work we would need a path set up similar to
https://github.com/maptiler/tileserver-gl/blob/master/src/server.js#L94
and then we would set up serve_rendered.js to handle the file:// protocol.
In your style config you would then prefix your geojson files with file://, similar to how mbtiles:// or pmtiles://

@okimiko
Copy link
Contributor Author

okimiko commented Jul 23, 2024

Yes, that behaviour would have been a bit hacky, because it would require a matching environment to work in rendered and vector mode.

I extended the code a bit to support a file:// urls, but stumpled over the spec a bit: geojson uses "data" instead of "url" because it seems to be inteded, to write inline geojson.

In the current implementation it may be a change with a "bigger" impact, because with /files/ the public interface is extended, but this works without any need of a reverse proxy in both modes.

@okimiko okimiko marked this pull request as ready for review July 23, 2024 15:20
src/serve_rendered.js Fixed Show resolved Hide resolved
@acalcutt
Copy link
Collaborator

acalcutt commented Jul 23, 2024

I like how this looks better now and will try to test it soon.

One thing I was thinking about was an issue we have with the 'icons' path we added. Right now, if you don't specify a path for that it defaults to the tileserver-gl root directory, and in that case allows the user to try and load files in the root directory that are not images and causes a crash. In my own instance I specifically set it to the folder public/resources/images which avoids that issue, but really it should be hardcoded to a path like that by default, not the root folder like we use now.

I was wondering if we should do the same thing with 'files' to prevent inadvertently exposing all source files and maybe make a default folder specifically for that like public/resources/files and use that as the default path if one doesn't exists. If it was specified in the config it would use the alternative path given, but not set it would default to public/resources/files (or possibly just files would be fine)

@acalcutt
Copy link
Collaborator

acalcutt commented Jul 23, 2024

For the comment on geojson sources, it seems to accect inline code or a url
https://maplibre.org/maplibre-style-spec/sources/#geojson

In my own project I have used dynamically generated geojson sources by url on pages like
https://wifidb.net/wifidb/opt/map.php?func=user_list&labeled=0&id=65596&sig_label=none

I guess the only question is does maplibre-native seem to work with this new file:// protocol ? the handler for file:// has been added so I assume it should

src/serve_rendered.js Outdated Show resolved Hide resolved
@acalcutt
Copy link
Collaborator

acalcutt commented Jul 24, 2024

I am testing locally with this config

test_data_geojson.zip

on windows I am starting this with
node . -c C:\Users\andrew.EIRI\Desktop\test_data_geojson\config.json

vector map
image

raster map
image

Note, I did make a few changes locally. I changed 'file://' to 'files://' and updated anywhere in the code that just used 'file' to 'files'. This was for consistency with the other protocol and to get it working. I also made the other path change I commented above.

@acalcutt
Copy link
Collaborator

acalcutt commented Jul 24, 2024

One thing to note is depending on your geojson file, this is likely going to affect performance in raster tiles because every tile it makes is basically loading the full geojson file. Were if instead you did like I mentioned before and converted your geojson to mbtiles/pmtiles vector files with tippecanoe, it would only need to download the region you are looking at and would be faster, since tippcannoe has already broken your data down into tiles.

src/serve_style.js Outdated Show resolved Hide resolved
docs/config.rst Outdated Show resolved Hide resolved
src/main.js Outdated Show resolved Hide resolved
@okimiko
Copy link
Contributor Author

okimiko commented Jul 24, 2024

One thing to note is depending on your geojson file, this is likely going to affect performance in raster tiles because every tile it makes is basically loading the full geojson file. Were if instead you did like I mentioned before and converted your geojson to mbtiles/pmtiles vector files with tippecanoe, it would only need to download the region you are looking at and would be faster, since tippcannoe has already broken your data down into tiles.

Good point! For my current files it seems not to be a problem, but that should be added as note/kept in mind.

I will apply some of the above changes later.

Edit: I added the changes and tested my setup, maybe you can test on Windows again?

src/serve_rendered.js Fixed Show fixed Hide fixed
@okimiko
Copy link
Contributor Author

okimiko commented Aug 2, 2024

@okimiko okimiko requested a review from acalcutt August 2, 2024 22:37
Dockerfile Outdated
@@ -107,8 +107,8 @@ COPY --from=builder /usr/src/app /usr/src/app

COPY . /usr/src/app

RUN mkdir -p /data && chown node:node /data
VOLUME /data
RUN mkdir -p /data /usr/src/app/public/files && chown node:node /data /usr/src/app/public/files
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only thing I don't really like is making this folder with the dockerfile . since tileserver-gl can run directly without docker, it would need that folder there in that use case also. So maybe we should either make the directory with a default file inside, of maybe find a way to create it with the package.json npm prepare..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the folder to the repository (and excluded .gitignore files for docker build).

Copy link
Collaborator

@acalcutt acalcutt Aug 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me. this can be removed now right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant can we remove the changes in this file. Since the folder exists we don't need to create it here anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was a bit confused.

In fact the mkdir for public/files may be removed, but due to the '-p' it does not hurt and ensures, that the path, which will be chown'ed and exposed as volume is really there.
If I remove/change it, I would revert the whole file, because the volume depends on the configuration and everything will still work with the default configuration, because the mount may be applied without a exposed volume of an empty directory. (This may be different, if there was a default config file included in the image.)

In short: It's up to you ;-)

Copy link
Collaborator

@acalcutt acalcutt Aug 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep it as it was before since it shouldn't really be needed now,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@okimiko
Copy link
Contributor Author

okimiko commented Aug 12, 2024

@acalcutt I merged the latest changes, applied the prettier fixes and finally updated the tests to be executed in the main directory, so no further stubbing should be required.

src/serve_rendered.js Dismissed Show dismissed Hide dismissed
src/serve_rendered.js Fixed Show fixed Hide fixed
src/serve_rendered.js Fixed Show fixed Hide fixed
@okimiko
Copy link
Contributor Author

okimiko commented Aug 26, 2024

@acalcutt: I tidied up a little bit (merge master, lint:*:fix, squash - okimiko@222d6c1). If you want to merge, this may be a bit cleaner than this here. If wanted, I can force push it or we close this here and I create a new, clean PR.

@@ -893,13 +893,13 @@ export const serve_rendered = {
// console.log('Handling request:', req);
if (protocol === 'sprites') {
const dir = options.paths[protocol];
const file = unescape(req.url).substring(protocol.length + 3);
const file = decodeURI(req.url).substring(protocol.length + 3).replaceAll('%2c', ',');
Copy link
Collaborator

@acalcutt acalcutt Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this replacement added for?
replaceAll('%2c', ',')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I did not check the code before. Correct would be to use decodeURIComponent as counterpart of encodeURIComponent.
decodeURI / encodeURI does not escape commas, so I added the extra replace after using a style with two fonts configured and receiving ERROR: Font not found: Noto Sans Regular%2cNoto Sans Bold.

I will fix that later today an force push the squashed commit.

@acalcutt
Copy link
Collaborator

force push or a new PR would be fine with me

- use 'file://' as indicator for local files
- add  directory as default directory
- serve local files at
- add documentation for static file serving
- add some minor fixes (icon directory, directory checking, decodeURIComponent, extend error message)
@acalcutt acalcutt merged commit 44cf365 into maptiler:master Sep 1, 2024
6 checks passed
acalcutt added a commit that referenced this pull request Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants