javascript - 如何在javascript中将字符串数据转换为Geojson

标签 javascript json leaflet geojson

我在 javascript 中有一个字符串变量作为 statesData,当我在控制台中将其打印为 console.log(statesData) 它给我的输出为

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            74.987712893238,
                            16.4611124595779
                        ],
                        [
                            75.0794873366632,
                            16.4216041790002
                        ],
                        [
                            74.9914167945422,
                            16.5154363453721
                        ],
                        [
                            74.987712893238,
                            16.4611124595779
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Bagalkot District",
                "f2": 4,
                "f3": 0.0327269593968744
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            75.6708769115599,
                            14.7838624856797
                        ],
                        [
                            75.7046235678866,
                            14.7698699696418
                        ],
                        [
                            75.7497789666211,
                            14.9338893073175
                        ],
                        [
                            75.6988619436357,
                            14.9320185378459
                        ],
                        [
                            75.721037483182,
                            14.8603111097135
                        ],
                        [
                            75.6708769115599,
                            14.7838624856797
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Ballari District",
                "f2": 7,
                "f3": 0.0391301194948449
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            74.0400818803335,
                            15.6369930144092
                        ],
                        [
                            74.0643009308787,
                            15.629971642517
                        ],
                        [
                            74.0855287111498,
                            15.6477446840837
                        ],
                        [
                            74.0655222616442,
                            15.6769214138993
                        ],
                        [
                            74.0409889333752,
                            15.684796356228
                        ],
                        [
                            74.0205207356315,
                            15.6756395243798
                        ],
                        [
                            74.039428399187,
                            15.657777976182
                        ],
                        [
                            74.0400818803335,
                            15.6369930144092
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Belagavi District",
                "f2": 8,
                "f3": 0.0281856872997076
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            77.2207332227667,
                            13.1629012169715
                        ],
                        [
                            77.2411285265379,
                            13.1488604730127
                        ],
                        [
                            77.2001264894172,
                            13.1832957390292
                        ],
                        [
                            77.2083747838975,
                            13.179093776468
                        ],
                        [
                            77.2207332227667,
                            13.1629012169715
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Bengaluru Urban District",
                "f2": 86,
                "f3": 0.833530387188893
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            75.1765764664482,
                            13.2837112032172
                        ],
                        [
                            75.1542222218667,
                            13.2789357676158
                        ],
                        [
                            75.1772089066226,
                            13.2170710348114
                        ],
                        [
                            75.2039151953973,
                            13.3434386684308
                        ],
                        [
                            75.2020812594624,
                            13.3100521461702
                        ],
                        [
                            75.1765764664482,
                            13.2837112032172
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Chikkamagaluru District",
                "f2": 2,
                "f3": 0.0138095694326863
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            76.1116709003931,
                            13.9349464503589
                        ],
                        [
                            76.1038291497053,
                            13.9046996994902
                        ],
                        [
                            76.0847056753228,
                            14.056410903216
                        ],
                        [
                            76.0801950024902,
                            13.9465147806955
                        ],
                        [
                            76.1116709003931,
                            13.9349464503589
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Chitradurga District",
                "f2": 3,
                "f3": 0.0156783102632625
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            74.8653521851079,
                            13.0476877454595
                        ],
                        [
                            74.7786075719437,
                            13.0493299278648
                        ],
                        [
                            75.0699524730125,
                            13.0631902614285
                        ],
                        [
                            74.9642609277546,
                            13.0529854233513
                        ],
                        [
                            74.8653521851079,
                            13.0476877454595
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Dakshina Kannada District",
                "f2": 18,
                "f3": 0.197682627967718
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            75.5103745217132,
                            14.1179833401105
                        ],
                        [
                            75.5729292992944,
                            14.0397898681339
                        ],
                        [
                            75.6996850328144,
                            14.0570747408866
                        ],
                        [
                            75.7737630588975,
                            14.1146909833957
                        ],
                        [
                            75.6160317699427,
                            14.3742302168837
                        ],
                        [
                            75.5605851345746,
                            14.2633369461476
                        ],
                        [
                            75.5103745217132,
                            14.1179833401105
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Davangere District",
                "f2": 7,
                "f3": 0.0720749586059508
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            74.723340940517,
                            15.4748459289395
                        ],
                        [
                            74.7288856040538,
                            15.4055376347295
                        ],
                        [
                            74.8342342112531,
                            15.6106901855913
                        ],
                        [
                            74.7981938982639,
                            15.4665289336343
                        ],
                        [
                            74.723340940517,
                            15.4748459289395
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Dharwad District",
                "f2": 29,
                "f3": 0.294270499381753
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            75.2790864624982,
                            15.6291750722057
                        ],
                        [
                            75.409134552733,
                            15.5806128106624
                        ],
                        [
                            75.4033729284821,
                            15.4983038927923
                        ],
                        [
                            75.4280656038431,
                            15.8407089911319
                        ],
                        [
                            75.3251794565055,
                            15.8176624941283
                        ],
                        [
                            75.2790864624982,
                            15.6291750722057
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Gadag District",
                "f2": 3,
                "f3": 0.031949373198826
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            75.6220536370659,
                            12.8189890581004
                        ],
                        [
                            75.6220536370658,
                            12.8189890581003
                        ],
                        [
                            75.6759493105302,
                            12.9140842549367
                        ],
                        [
                            75.6519952502805,
                            12.9075942794764
                        ],
                        [
                            75.6220536370659,
                            12.8189890581004
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Hassan Distric",
                "f2": 2,
                "f3": 0.0144830182711203
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            75.0227527715045,
                            14.7263163514708
                        ],
                        [
                            75.0449314256517,
                            14.6570080572607
                        ],
                        [
                            75.1863203458402,
                            14.6431463984187
                        ],
                        [
                            75.3506952210452,
                            15.0102120098225
                        ],
                        [
                            75.1946373411455,
                            14.9952325330059
                        ],
                        [
                            75.0698824115673,
                            14.9148349117222
                        ],
                        [
                            75.0227527715045,
                            14.7263163514708
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Haveri District",
                "f2": 3,
                "f3": 0.0372722864393961
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            75.2703710228573,
                            16.6020450061215
                        ],
                        [
                            75.3249369342604,
                            16.5671446456902
                        ],
                        [
                            75.4441472003017,
                            16.5006086832485
                        ],
                        [
                            75.3554325837128,
                            16.7113058976472
                        ],
                        [
                            75.2887655722583,
                            16.7147116212027
                        ],
                        [
                            75.2703710228573,
                            16.6020450061215
                        ]
                    ]
                ]
            },
            "properties": {
                "f1": "Vijayapura District",
                "f2": 7,
                "f3": 0.0326766444387371
            }
        }
    ]
}

我复制了这些数据并使用链接 http://geojsonlint.com/ 进行了测试 它在其中创建 Geojson 对象层。但是当我在我的 map 脚本中使用它作为

geojson = L.geoJson(statesData, {
                        style: style,
                        onEachFeature: onEachFeature
                }).addTo(map);

它给我一个错误作为 Uncaught Error :无效的 GeoJSON 对象。

我没有得到错误的原因。我是否需要将我的字符串变量 statesData 解析为 Geojson。如果是,那么我如何将字符串数据解析为 GEOJSON。

请帮帮我...

最佳答案

GeoJSON 数据就是简单的 JSON,因此您只需要将 String 转换为 JSON:

const parsedGeoJson = JSON.parse(geoJsonString);

关于javascript - 如何在javascript中将字符串数据转换为Geojson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39058045/

相关文章:

javascript - Leaflet.js - 使用哪种方法从 GeoJson 添加标记

javascript - 为传单风图格式化json

php - 如果我的表单使用的是 ajax 提交方法(无需重新加载),我该如何阻止垃圾邮件?

javascript - 如何在 Node.js 中使用 async/await 编写依赖项

python - 如何从python中的文件中读取多个词典?

javascript - 在 Leaflet 上检测用户发起的平移/缩放操作

javascript - 使用 javascript/jQuery 捕获已知字符串旁边的字符串

javascript - 修改 s3.putObject 函数内的变量

Javascript Typeerror 变量未定义。

jquery - 使用 jQuery 检索包含换行符的 JSON 格式文本时出现问题