You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过代码获取Autodesk Forge中自定义点的经纬高坐标?

How to Retrieve Real-World Coordinates (lat/lon/z) from 3D Rectangle Corners in Forge Viewer

Hey there! I totally get the frustration when you can see data in the properties panel but can't seem to pull it via code—let's fix that. Since you have basic JS experience and are new to React, I'll stick to plain Viewer JavaScript examples that you can drop into your Node.js/JS setup.

There are two common scenarios for these coordinates, so let's cover both:

Scenario 1: Coordinates are stored as custom properties (what you see in the properties panel)

If the lat/lon/z values show up directly in the Viewer's properties panel for your rectangle, you can fetch them using the viewer.getProperties() method. Here's how:

  1. First, get the dbId of your rectangle. You can either:

    • Use viewer.getSelection() to get the dbId of a selected object
    • Or traverse the model's object tree to find the right element (if you know its name or other identifiers)
  2. Fetch the properties and extract the coordinates:

// Get the first selected object's dbId (adjust if you need a specific element)
const targetDbId = viewer.getSelection()[0];

if (targetDbId) {
  viewer.getProperties(targetDbId, (props) => {
    // Loop through all properties to find your lat/lon/z entries
    props.properties.forEach(prop => {
      // Match the display names exactly as they appear in the properties panel
      switch(prop.displayName) {
        case '纬度': // or 'Latitude', 'lat'—use whatever you see in the panel
          console.log('Latitude:', prop.displayValue);
          break;
        case '经度': // or 'Longitude', 'lon'
          console.log('Longitude:', prop.displayValue);
          break;
        case '高程': // or 'Elevation', 'z'
          console.log('Elevation:', prop.displayValue);
          break;
      }
    });
  });
}

Scenario 2: Coordinates are derived from the model's georeferencing

If the model is georeferenced (aligned to real-world coordinates), you'll need to convert the rectangle's local 3D coordinates to lat/lon/z using the Viewer's georeferencing API. Here's how:

// Get the dbId of your rectangle (same as above)
const targetDbId = viewer.getSelection()[0];

if (targetDbId) {
  // Get the bounding box of the rectangle to access its corner points
  viewer.getBoundingBox(targetDbId, (bbox) => {
    if (!bbox) return;

    // Define all four corner points of the 2D rectangle (adjust for 3D if needed)
    const corners = [
      new THREE.Vector3(bbox.min.x, bbox.min.y, bbox.min.z),
      new THREE.Vector3(bbox.max.x, bbox.min.y, bbox.min.z),
      new THREE.Vector3(bbox.max.x, bbox.max.y, bbox.min.z),
      new THREE.Vector3(bbox.min.x, bbox.max.y, bbox.min.z)
    ];

    // Get the model's georeferencing transformation
    const geoRef = viewer.model.getGeoreferencing();
    if (geoRef) {
      corners.forEach((corner, index) => {
        // Convert local model coordinates to real-world lat/lon/z
        const worldCoords = geoRef.modelToWorld(corner);
        // Note: Depending on the model's CRS, worldCoords.x is usually longitude, y is latitude, z is elevation
        console.log(`Corner ${index + 1} real-world coords:`, {
          lon: worldCoords.x,
          lat: worldCoords.y,
          z: worldCoords.z
        });
      });
    } else {
      console.log('This model does not have georeferencing data.');
    }
  });
}

Quick Tips for Your Setup

  • Since you're new to React, you don't need to wrap this in React components right now—you can run this code after the Viewer finishes initializing (hook into the viewer.initialize() promise or the GEOMETRY_LOADED_EVENT).
  • Double-check the property names in the properties panel to make sure your switch case matches exactly (capitalization, spaces, and language matter!).

Let me know if you run into issues—start with verifying which scenario applies to your model, and you'll be pulling those coordinates in no time!

内容的提问来源于stack exchange,提问作者Fred Coud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:09:47