dart 无法从GeoJson提取坐标未处理异常:类型“List< dynamic>”不是类型“double”的子类型

2lpgd968  于 2023-03-15  发布在  其他
关注(0)|答案(2)|浏览(122)

我尝试从geojson数据中获取坐标列表,当坐标不在数组中时一切正常,例如这里的最后4个坐标,我尝试这样做是为了从世界Map中获取世界Map的边界,简言之,没有任何坐标数组的国家工作正常,而那些有坐标数组的国家则会出错

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "ADMIN": "Anguilla",
                "ISO_A3": "AIA"
            },
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                -63.037668423999946,
                                18.212958075000031
                            ],
                            [
                                -63.099517381999959,
                                18.176174221000124
                            ],
                            [
                                -63.102365688999896,
                                18.180405992000161
                            ],
                            [
                                -63.109120245999918,
                                18.185044664000188
                            ],
                            [
                                -63.113840298999946,
                                18.189846096000068
                            ],
                            [
                                -63.136830206999917,
                                18.173407294000086
                            ],
                            [
                                -63.150502081999946,
                                18.169094143000095
                            ],
                            [
                                -63.167836066999939,
                                18.169338283000044
                            ],
                            [
                                -63.142079230999911,
                                18.198146877000156
                            ],
                            [
                                -63.134348110999895,
                                18.204087632
                            ],
                            [
                                -63.122792120999861,
                                18.20807526200015
                            ],
                            [
                                -63.09723873599998,
                                18.212469794000143
                            ],
                            [
                                -63.085845506999902,
                                18.217718817000119
                            ],
                            [
                                -63.0677791009999,
                                18.2364769550001
                            ],
                            [
                                -63.055083787999934,
                                18.254339911000059
                            ],
                            [
                                -63.038197394999941,
                                18.267726955000157
                            ],
                            [
                                -63.007394985999952,
                                18.273016669000029
                            ],
                            [
                                -62.983998175999886,
                                18.276434637000037
                            ],
                            [
                                -62.972645636999914,
                                18.275864976000079
                            ],
                            [
                                -62.972889777999853,
                                18.269273179
                            ],
                            [
                                -62.992909308999913,
                                18.236883856000091
                            ],
                            [
                                -63.000559048999946,
                                18.227362372000087
                            ],
                            [
                                -63.011545376999919,
                                18.220445054
                            ],
                            [
                                -63.037668423999946,
                                18.212958075000031
                            ]
                        ]
                    ],
                    [
                        [
                            [
                                -63.423573370999861,
                                18.600043036000059
                            ],
                            [
                                -63.427967902999939,
                                18.592840887000122
                            ],
                            [
                                -63.428822394999941,
                                18.601263739000061
                            ],
                            [
                                -63.423573370999861,
                                18.600043036000059
                            ]
                        ]
                    ]
                ]
            }
        }
       
    ]
}

这是我正在尝试使用的代码

List<LatLng> border = [];
  List<List<LatLng>> borders = [];
  Future<void> loadGeoJson() async {
    // Load GeoJSON data from asset file
    String data = await rootBundle.loadString('assets/nepal.geojson');
    Map<String, dynamic> geoJson = await json.decode(data);

    List<dynamic> features = geoJson['features'];
    for (int i = 0; i < features.length; i++) {
      List<dynamic> coordinates = features[i]['geometry']['coordinates'][0];
      border = coordinates
          .map((coordinate) => LatLng(coordinate[1], coordinate[0]))
          .toList();
      borders.add(border);
    }
  }

如果坐标不在数组中,它可以正常工作,但是当存在一个数组时,我会得到错误Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'double'

pkln4tw6

pkln4tw61#

您有3个嵌套数组,但您将其视为单个数组,因此应再添加两个[0]

List<dynamic> coordinates = features[i]['geometry']['coordinates'][0][0][0];

已编辑

我添加了新函数来读取所有坐标

List<LatLng> getBorder(List coordinates){
  final List<LatLng> borders = <LatLng>[];
  if(coordinates.isNotEmpty){
    if(coordinates.first is double){
      borders.add(LatLng(coordinates[0], coordinates[1]));
    }else{
      for(final item in coordinates){
        if(item is List){
          borders.addAll(getBorder(item));
        }
      }
    }
  }
  return borders;
}

并且在使用中

List<List<LatLng>> borders = [];
  Future<void> loadGeoJson() async {
    // Load GeoJSON data from asset file
    String data = await rootBundle.loadString('assets/nepal.geojson');
    Map<String, dynamic> geoJson = await json.decode(data);

    List<dynamic> features = geoJson['features'];
    for (int i = 0; i < features.length; i++) {
      final border = getBorder(features[i]['geometry']['coordinates'][0] as List);
      borders.add(border);
    }
  }
hfyxw5xn

hfyxw5xn2#

我想这是一种方法

List<LatLng> border = [];
  List<List<LatLng>> borders = [];
  Future<void> loadGeoJson() async {
    // Load GeoJSON data from asset file
    String data = await rootBundle.loadString('assets/countries.geojson');
    Map<String, dynamic> geoJson = await json.decode(data);

    List<dynamic> features = geoJson['features'];
    for (int i = 0; i < features.length; i++) {
      String geometryType = features[i]['geometry']['type'];
      List<dynamic> nestedCoords = features[i]['geometry']['coordinates'];
      List<List<LatLng>> bordersForFeature = [];

      if (geometryType == 'MultiPolygon') {
        for (int j = 0; j < nestedCoords.length; j++) {
          List<dynamic> coordinates = nestedCoords[j][0];
          List<LatLng> border = coordinates.map((coordinate) {
            double lat = coordinate[1];
            double lng = coordinate[0];
            if (lng > 180) lng = lng - 360;
            if (lng < -180) lng = lng + 360;
            if (lat > 90) lat = 90;
            if (lat < -90) lat = -90;
            return LatLng(lat, lng);
          }).toList();
          bordersForFeature.add(border);
        }
      } else if (geometryType == 'Polygon') {
        List<dynamic> coordinates = nestedCoords[0];
        List<LatLng> border = coordinates.map((coordinate) {
          double lat = coordinate[1];
          double lng = coordinate[0];
          if (lng > 180) lng = lng - 360;
          if (lng < -180) lng = lng + 360;
          if (lat > 90) lat = 90;
          if (lat < -90) lat = -90;
          return LatLng(lat, lng);
        }).toList();
        bordersForFeature.add(border);
      }

      borders.addAll(bordersForFeature);
    }
  }

相关问题