无法访问 geojson 特征集合的 'coordinates' 成员

can't access 'coordinates' member of geojson feature collection

我的 Angular 应用程序中有一个 GeoJSON 要素集合,它是一个要素数组,每个要素都包含一个几何对象和属性对象。结构如下所示:

import { FeatureCollection, Feature } from 'geojson';

staticBreadcrumbs: GeoJSON.FeatureCollection<GeoJSON.Geometry>;

this.staticBreadcrumbs = {
    type : 'FeatureCollection',
    features: [
    {
        type: 'Feature',
        properties: {
            property1:  'property1',
            property2:  'property2'
        },
        geometry: {
            type: 'Point',
            coordinates: [-117.14024305343628, 32.81294345855713]
        }
    },
    {
        type: 'Feature',
        properties: {
        ...

我正在尝试为集合中的每个要素创建一个 mapboxgl 标记,并且需要从每个 GeoJSON 对象获取坐标,编译器告诉我坐标不是要素的一部分。具体错误是:属性 'coordinates' does not exist on type 'Geometry'。 属性 'coordinates' 在类型 'GeometryCollection' 上不存在。

console.log('this.staticBreadcrumbs.features[0]', this.staticBreadcrumbs.features[0]);

var marker = new Marker(el)
    .setLngLat([
        this.staticBreadcrumbs.features[0].geometry.coordinates[0],
        this.staticBreadcrumbs.features[0].geometry.coordinates[1]
    ])
    .addTo(this.map);

我的 console.log 显示器

 this.staticBreadcrumbs.features[0]
 {type: "Feature", properties: {…}, geometry: {…}}
 1.  geometry:
     1.  coordinates: Array(2)
         1.  0: -117.14024305343628
         2.  1: 32.81294345855713
         3.  length: 2
         4.  __proto__: Array(0)
     2.  type: "Point"
   2. __proto__: Object
2. properties: {deviceID: "CAP498", altitude: 401.6721913312, autonomous: 0, azimuth: 0, batteryStatusLabel: "Nominal", …}
3. type: "Feature"
4. __proto__: Object

坐标正是我期望的位置,但我无法到达它们。我尝试了各种不同的方法来声明 Feature 集合,但我找不到合适的组合。

我需要做什么才能访问坐标?

谢谢......

What do I need to do in order to access the coordinates?

简答

在尝试访问其 coordinates 属性 之前检查 geometry 是否为 Point

if (staticBreadcrumbs.features[0].geometry.type === 'Point') {

    var marker = new Marker(el)
        .setLngLat([
            this.staticBreadcrumbs.features[0].geometry.coordinates[0],
            this.staticBreadcrumbs.features[0].geometry.coordinates[1]
        ])
        .addTo(this.map);

}

说明

您遇到的问题是因为 Geometry is a union type.

A union type describes a value that can be one of several types. We use the vertical bar (|) to separate each type...

这是 Geometry 联合类型定义:

export type Geometry = 
    Point |
    MultiPoint | 
    LineString | 
    MultiLineString | 
    Polygon | 
    MultiPolygon | 
    GeometryCollection;

如您所见,Geometry 类型是七种类型的联合。不幸的是,并非所有这些类型都包含 coordinates 属性.

If we have a value that has a union type, we can only access members that are common to all types in the union.

这就是为什么我们只有在缩小类型后才能访问 coordinates 属性。

一些其他选项

如果您在 FeatureCollection 中仅使用 Point 类型,则使用 Point 类型作为通用参数:

let staticBreadcrumbs: FeatureCollection<Point>;

如果您在 FeatureCollection 中使用多种类型,则使用强制转换告诉类型检查器您确定有 Point:

(staticBreadcrumbs.features[0].geometry as Point).coordinates[0]

简答所示,您可以使用条件语句来缩小类型,而不是使用强制转换:

const geometry = staticBreadcrumbs.features[0].geometry;

if (geometry.type === 'Point') {
    const coordinates00 = geometry.coordinates[0];
    const coordinates01 = geometry.coordinates[1];
}

有一个简短的演示 of that last example here