mcpField
A
arcgis-portal-content

arcgis-portal-content

github.com/saschabrunnerch开发2363 次安装29 周前更新
代码仓库

关于 arcgis-portal-content

面向使用 ArcGIS Maps SDK for JavaScript 管理地图、场景及 ArcGIS 门户内容的开发者。涵盖保存 WebMap 和 WebScene、管理书签与幻灯片,以及查询或更新门户项目和群组。

主要功能

  1. 01加载并配置 WebMap 和 WebScene
  2. 02将地图和场景保存为门户项目
  3. 03创建和管理书签及场景幻灯片
  4. 04查询和更新门户项目与群组
  5. 05获取门户用户和文件夹信息

适用场景

  1. 01将地图视图保存为门户项目
  2. 02用幻灯片组织场景演示
  3. 03查找并更新门户中的项目

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/saschabrunnerch/arcgis-maps-sdk-js-ai-context --skill arcgis-portal-content

安装后在 Claude Code 中输入/arcgis-portal-content确认已加载。

详细介绍

ArcGIS Portal 内容

在保存地图、管理书签和幻灯片、处理门户项目以及配置 WebMap/WebScene 结构时使用此技能。

导入模式

直接 ESM 导入
import WebMap from "@arcgis/core/WebMap.js";
import WebScene from "@arcgis/core/WebScene.js";
import Portal from "@arcgis/core/portal/Portal.js";
import PortalItem from "@arcgis/core/portal/PortalItem.js";
import PortalQueryParams from "@arcgis/core/portal/PortalQueryParams.js";
动态导入(CDN)
const WebMap = await $arcgis.import("@arcgis/core/WebMap.js");
const Portal = await $arcgis.import("@arcgis/core/portal/Portal.js");
const [PortalItem, PortalQueryParams] = await $arcgis.import([
  "@arcgis/core/portal/PortalItem.js",
  "@arcgis/core/portal/PortalQueryParams.js",
]);

WebMap 结构

WebMap 属性
import WebMap from "@arcgis/core/WebMap.js";

const webMap = new WebMap({
  // From portal item
  portalItem: {
    id: "WEBMAP_ID",
    portal: { url: "https://www.arcgis.com" },
  },
});

// Or create from scratch
const webMap = new WebMap({
  basemap: "topo-vector",
  ground: "world-elevation",
  layers: [featureLayer, graphicsLayer],
  tables: [tableLayer],
  initialViewProperties: {
    center: [-118.805, 34.027],
    zoom: 13,
  },
  bookmarks: [bookmark1, bookmark2],
});
从 JSON 创建 WebMap
const webMap = WebMap.fromJSON({
  operationalLayers: [
    {
      id: "layer1",
      layerType: "ArcGISFeatureLayer",
      url: "https://services.arcgis.com/.../FeatureServer/0",
      title: "My Layer",
      visibility: true,
      opacity: 1,
    },
  ],
  baseMap: {
    baseMapLayers: [
      {
        id: "basemap",
        layerType: "VectorTileLayer",
        styleUrl:
          "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/v2/styles/arcgis/topographic",
      },
    ],
    title: "Topographic",
  },
  initialState: {
    viewpoint: {
      targetGeometry: {
        xmin: -118.9,
        ymin: 33.8,
        xmax: -118.1,
        ymax: 34.3,
        spatialReference: { wkid: 4326 },
      },
    },
  },
});

WebScene 结构

import WebScene from "@arcgis/core/WebScene.js";

const webScene = new WebScene({
  portalItem: { id: "WEBSCENE_ID" },
});

// Or create from scratch
const webScene = new WebScene({
  basemap: "satellite",
  ground: "world-elevation",
  layers: [sceneLayer, featureLayer],
  initialViewProperties: {
    viewpoint: {
      camera: {
        position: {
          x: -118.805,
          y: 34.027,
          z: 1500,
          spatialReference: { wkid: 4326 },
        },
        heading: 45,
        tilt: 65,
      },
    },
  },
  presentation: {
    slides: [slide1, slide2],
  },
});
本地场景与全球场景
// Global scene (default)
const globalScene = new WebScene({
  basemap: "satellite",
  ground: "world-elevation",
  viewingMode: "global",
});

// Local scene (with clipping)
const localScene = new WebScene({
  basemap: "satellite",
  ground: "world-elevation",
  viewingMode: "local",
  clippingArea: {
    type: "extent",
    xmin: -118.9,
    ymin: 33.8,
    xmax: -118.1,
    ymax: 34.3,
    spatialReference: { wkid: 4326 },
  },
  clippingEnabled: true,
});

保存 WebMap

另存为新项目
const map = new WebMap({
  portalItem: { id: "EXISTING_WEBMAP_ID" },
});

// Update map properties from view before saving
await map.updateFrom(view);

const savedItem = await map.saveAs({
  title: "My New WebMap",
  snippet: "Description of the map",
  tags: ["tag1", "tag2"],
});

console.log(
  "Saved to:",
  savedItem.portal.url + "/home/item.html?id=" + savedItem.id,
);
保存现有 WebMap
await map.updateFrom(view);
await map.save();
Map 组件保存示例
<arcgis-map item-id="YOUR_WEBMAP_ID">
  <arcgis-zoom slot="top-left"></arcgis-zoom>
</arcgis-map>

<script type="module">
  const viewElement = document.querySelector("arcgis-map");
  await viewElement.viewOnReady();

  document.getElementById("saveBtn").onclick = async () => {
    await viewElement.map.updateFrom(viewElement.view);
    await viewElement.map.save();
  };
</script>

保存 WebScene

const scene = new WebScene({
  portalItem: { id: "EXISTING_WEBSCENE_ID" },
});

await scene.updateFrom(sceneView);
await scene.saveAs({
  title: "My New WebScene",
  snippet: "3D scene description",
});

书签

书签组件
<arcgis-map item-id="YOUR_WEBMAP_ID">
  <arcgis-expand slot="top-right" expanded>
    <arcgis-bookmarks
      drag-enabled
      show-add-bookmark-button
      show-edit-bookmark-button
      hide-time
    >
    </arcgis-bookmarks>
  </arcgis-expand>
</arcgis-map>

<script type="module">
  const bookmarks = document.querySelector("arcgis-bookmarks");

  bookmarks.addEventListener("arcgisSelect", (event) => {
    console.log("Selected:", event.detail.bookmark.name);
  });
</script>
以编程方式创建书签
import Bookmark from "@arcgis/core/webmap/Bookmark.js";

const bookmark = new Bookmark({
  name: "My Location",
  viewpoint: view.viewpoint.clone(),
});

map.bookmarks.add(bookmark);

// Go to bookmark
view.goTo(bookmark.viewpoint);

WebScene 幻灯片

根据当前视图创建幻灯片
import Slide from "@arcgis/core/webscene/Slide.js";

const slide = await Slide.createFrom(sceneView);
slide.title = { text: "Downtown View" };

// Add to presentation
scene.presentation.slides.add(slide);
将幻灯片应用到视图
const slide = scene.presentation.slides.getItemAt(0);

slide.applyTo(sceneView, {
  maxDuration: 3000,
  easing: "in-out-coast-cubic",
});
移除幻灯片
scene.presentation.slides.remove(slide);

门户项目

加载门户项目
import PortalItem from "@arcgis/core/portal/PortalItem.js";

const item = new PortalItem({ id: "ITEM_ID" });
await item.load();

console.log("Title:", item.title);
console.log("Type:", item.type);
console.log("Owner:", item.owner);
console.log("Created:", item.created);
查询门户项目
import Portal from "@arcgis/core/portal/Portal.js";
import PortalQueryParams from "@arcgis/core/portal/PortalQueryParams.js";

const portal = new Portal({ authMode: "immediate" });
await portal.load();

const queryParams = new PortalQueryParams({
  query: `owner:${portal.user.username} type:"Web Map"`,
  sortField: "modified",
  sortOrder: "desc",
  num: 20,
});

const result = await portal.queryItems(queryParams);
result.results.forEach((item) => {
  console.log(item.title, item.id);
});
更新门户项目
const item = new PortalItem({ id: "ITEM_ID" });
await item.load();

item.title = "Updated Title";
item.snippet = "Updated description";
item.tags = ["new", "tags"];

await item.update();

门户群组

查询群组
const portal = new Portal();
await portal.load();

const groups = await portal.queryGroups({ query: "title:GIS" });
groups.results.forEach((group) => {
  console.log(group.title, group.id);
});
查询群组内容
const group = (await portal.queryGroups({ query: `id:GROUP_ID` })).results[0];
const content = await group.queryItems();
content.results.forEach((item) => {
  console.log(item.title);
});

门户用户信息

const portal = new Portal({ authMode: "immediate" });
await portal.load();

console.log("Username:", portal.user.username);
console.log("Full name:", portal.user.fullName);
console.log("Email:", portal.user.email);
console.log("Role:", portal.user.role);
console.log("Org name:", portal.name);

门户文件夹

import PortalFolder from "@arcgis/core/portal/PortalFolder.js";

// Get user folders
const folders = await portal.user.fetchFolders();
folders.forEach((folder) => {
  console.log(folder.title, folder.id);
});

// Query items in a specific folder
const queryParams = new PortalQueryParams({
  query: `owner:${portal.user.username}`,
  sortField: "modified",
  num: 20,
});
queryParams.folder = folder;

const result = await portal.queryItems(queryParams);

常见问题

  1. 保存需要身份验证:保存需要用户身份验证。调用 save() 或 saveAs() 前,请确保已设置 OAuth。

  2. 保存前忘记调用 updateFrom:保存前务必调用 updateFrom(view),以捕获当前视图状态:

    // Anti-pattern: saving without updating
    await map.save(); // May save stale viewpoint
    
    // Correct: update from view first
    await map.updateFrom(view);
    await map.save();
    
  3. Enterprise 门户 URL:Enterprise 门户需要显式指定门户 URL:

    const portal = new Portal({
      url: "https://your-portal.com/portal",
    });
    
  4. 所有权限制:你只能更新自己拥有的门户项目。使用 saveAs() 创建自己拥有的副本。

  5. 幻灯片缩略图:缩略图会通过 Slide.createFrom() 自动生成,但渲染可能需要一些时间。

完整保存示例

<!DOCTYPE html>
<html>
  <head>
    <script src="https://js.arcgis.com/5.0/"></script>
    <script
      type="module"
      src="https://js.arcgis.com/5.0/map-components/"
    ></script>
    <style>
      html,
      body {
        height: 100%;
        margin: 0;
      }
    </style>
  </head>
  <body>
    <arcgis-map item-id="YOUR_WEBMAP_ID">
      <arcgis-zoom slot="top-left"></arcgis-zoom>
      <arcgis-expand slot="top-right" expanded>
        <arcgis-bookmarks
          drag-enabled
          show-add-bookmark-button
          show-edit-bookmark-button
        >
        </arcgis-bookmarks>
      </arcgis-expand>
    </arcgis-map>

    <script type="module">
      const viewElement = document.querySelector("arcgis-map");
      await viewElement.viewOnReady();

      // Save map on button click
      document.getElementById("saveBtn").onclick = async () => {
        await viewElement.map.updateFrom(viewElement.view);
        await viewElement.map.save();
      };
    </script>
  </body>
</html>

参考示例

  • webmap-save - 将 WebMap 保存到门户
  • webmap-basic - 从门户加载 WebMap
  • webmap-swap - 在不同 WebMap 之间切换
  • webscene-save - 将 WebScene 保存到门户
  • webscene-basic - 从门户加载 WebScene
  • webscene-slides - 管理 WebScene 幻灯片
  • webscene-slide-tour - 浏览 WebScene 幻灯片
  • bookmarks - 使用地图书签
  • widgets-bookmarks - 书签微件用法
  • basemaps-portal - 从门户加载底图
  • layers-portal - 从门户项目加载图层
  • portalitem-dragndrop - 拖放门户项目

相关技能

  • OAuth 和 API 密钥设置请参阅 arcgis-authentication。
  • 地图和视图创建请参阅 arcgis-core-maps。
  • 图层配置请参阅 arcgis-layers。

评论

支持 Markdown