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

96-url-and-style-formatting #100

Merged
merged 5 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

* Updated peerDependencies to be more flexible for using v3 of `esri-leaflet`. [#99](https://github.com/Esri/esri-leaflet-vector/pull/99)

### Fixed

* Utility functions used by `L.esri.Vector.vectorTileLayer` have been improved to be more friendly with URL structures and style reformatting assumptions. [#100](https://github.com/Esri/esri-leaflet-vector/pull/100)

## [3.0.1] - 2021-06-03

### Fixed
Expand Down
47 changes: 32 additions & 15 deletions src/Util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { latLng, latLngBounds } from 'leaflet';
import { request, Support, Util } from 'esri-leaflet';

const WEB_MERCATOR_WKIDS = [3857, 102100, 102113];

/*
utility to establish a URL for the basemap styles API
used primarily by VectorBasemapLayer.js
Expand Down Expand Up @@ -82,15 +80,20 @@ function loadStyleFromService (serviceUrl, options, callback) {
console.error(error);
}

var sanitizedServiceUrl = serviceUrl;
// a trailing "/" may create invalid paths
if (serviceUrl.charAt(serviceUrl.length - 1) === '/') {
sanitizedServiceUrl = serviceUrl.slice(0, serviceUrl.length - 1);
}

var defaultStylesUrl;
if (
serviceUrl.charAt(0) !== '/' &&
service.defaultStyles.charAt(service.defaultStyles.length - 1) !== '/'
) {
// inadvertently inserting more than 1 adjacent "/" may create invalid paths
if (service.defaultStyles.charAt(0) === '/') {
defaultStylesUrl =
serviceUrl + '/' + service.defaultStyles + '/root.json';
sanitizedServiceUrl + service.defaultStyles + '/root.json';
} else {
defaultStylesUrl = serviceUrl + service.defaultStyles + '/root.json';
defaultStylesUrl =
sanitizedServiceUrl + '/' + service.defaultStyles + '/root.json';
}

loadStyleFromUrl(defaultStylesUrl, options, function (error, style) {
Expand Down Expand Up @@ -120,6 +123,11 @@ export function formatStyle (style, styleUrl, metadata, token) {
source.url = styleUrl.replace('/resources/styles/root.json', '');
}

// a trailing "/" may create invalid paths
if (source.url.charAt(source.url.length - 1) === '/') {
source.url = source.url.slice(0, source.url.length - 1);
}

// add tiles property if missing
if (!source.tiles) {
// right now ArcGIS Pro published vector services have a slightly different signature
Expand Down Expand Up @@ -162,24 +170,27 @@ export function formatStyle (style, styleUrl, metadata, token) {
}
}

// resolve absolute URLs for style.sprite and style.glyphs
if (style.sprite.indexOf('http') === -1) {
if (style.sprite && style.sprite.indexOf('http') === -1) {
// resolve absolute URL for style.sprite
style.sprite = styleUrl.replace(
'styles/root.json',
style.sprite.replace('../', '')
);

// add the token to the style.sprite property as a query param
style.sprite += token ? '?token=' + token : '';
}

if (style.glyphs.indexOf('http') === -1) {
if (style.glyphs && style.glyphs.indexOf('http') === -1) {
// resolve absolute URL for style.glyphs
style.glyphs = styleUrl.replace(
'styles/root.json',
style.glyphs.replace('../', '')
);
}

// add the token to the style.sprite and style.glyphs properties as a query param
style.sprite += token ? '?token=' + token : '';
style.glyphs += token ? '?token=' + token : '';
// add the token to the style.glyphs property as a query param
style.glyphs += token ? '?token=' + token : '';
}

return style;
}
Expand Down Expand Up @@ -223,6 +234,12 @@ export function getAttributionData (url, map) {
}
}

/*
utility to check if a service's tileInfo spatial reference is in Web Mercator
used primarily by VectorTileLayer.js
*/
const WEB_MERCATOR_WKIDS = [3857, 102100, 102113];

export function isWebMercator (wkid) {
return WEB_MERCATOR_WKIDS.indexOf(wkid) >= 0;
}