skip to Main Content

i am trying to get a list of coordinates from a geojson data , everything works fine when the coordinates are not in array for example here the last 4 coordinates, I am trying to do this to get borders of the world map from world map, in short countries without any coordinate array works fine and those having one is giving errors

{
    "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
                            ]
                        ]
                    ]
                ]
            }
        }
       
    ]
}

This is the code I am trying to use


  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);
    }
  }

it works fine if the coordinates are not in an array, but when one is present i get the error Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'double'

2

Answers


  1. Chosen as BEST ANSWER

    This is one way I guess

    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);
        }
      }
    

  2. You have 3 nested array but you are treating as single array, So you should add two more [0]

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

    Edited

    I added new function to read all coordinates

    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;
    }
    

    and in usage

      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);
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search