Skip to content

Commit

Permalink
feat(arcgis): convert link to rest format (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
mspivak-actionengine authored Jan 9, 2024
1 parent f7d49a5 commit 5b8d178
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/components/layers-panel/layers-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { useClickOutside } from "../../utils/hooks/use-click-outside-hook";
import { ArcGISWebSceneLoader } from "@loaders.gl/i3s";
import { ActiveSublayer } from "../../utils/active-sublayer";
import { useAppLayout } from "../../utils/hooks/layout";
import { getTilesetType } from "../../utils/url-utils";
import { getTilesetType, convertUrlToRestFormat } from "../../utils/url-utils";
import { convertArcGisSlidesToBookmars } from "../../utils/bookmarks-utils";
import { useAppDispatch } from "../../redux/hooks";
import { addBaseMap } from "../../redux/slices/base-maps-slice";
Expand Down Expand Up @@ -253,6 +253,8 @@ export const LayersPanel = ({
url: string;
token?: string;
}) => {
scene.url = convertUrlToRestFormat(scene.url);

const existedScene = layers.some(
(exisLayer) => exisLayer.url.trim() === scene.url.trim()
);
Expand Down
14 changes: 14 additions & 0 deletions src/utils/url-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
parseTilesetUrlParams,
urlParamsToViewState,
viewStateToUrlParams,
convertUrlToRestFormat,
} from "./url-utils";

const mockResponse = jest.fn();
Expand Down Expand Up @@ -179,3 +180,16 @@ describe("Url Utils - urlParamsToViewState", () => {
});
});
});

describe("Url Utils - convertUrlToRestFormat", () => {
test("Should convert to the format required", () => {
const urlExpected = "https://www.arcgis.com/sharing/rest/content/items/ae34b234d390148/data";

const urlItem = "https://some.maps.arcgis.com/home/item.html?id=ae34b234d390148";
const urlViewer = "https://some.maps.arcgis.com/home/webscene/viewer.html?webscene=ae34b234d390148";
expect(convertUrlToRestFormat(urlItem)).toEqual(urlExpected);
expect(convertUrlToRestFormat(urlViewer)).toEqual(urlExpected);

expect(convertUrlToRestFormat(urlExpected)).toEqual(urlExpected);
});
});
26 changes: 26 additions & 0 deletions src/utils/url-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,29 @@ export const urlParamsToViewState = (viewState: ViewStateSet) => {
}
return urlViewStateParams;
};

/**
* Converts the link of a webscene that can be copied from ArcGIS
* to the format required by i3s-explorer to insert a webscene.
* @param url - Url copied from ArcGIS.
* @returns Url converted.
*/
export const convertUrlToRestFormat = (url: string): string => {
let urlRest = "https://www.arcgis.com/sharing/rest/content/items/";
const urlObject = new URL(url);

let param: string | null = null;
for (const paramName of ["id", "webscene"]) {
param = urlObject.searchParams.get(paramName);
if (param) {
break;
}
}
if (param) {
urlRest += param + "/data";
} else {
// The url cannot be converted. Use it "as is".
return url;
}
return urlRest;
};

0 comments on commit 5b8d178

Please sign in to comment.