簡體   English   中英

如何在javascript中將字符串數據轉換為Geojson

[英]How to convert a string data to Geojson in javascript

我在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 對象層。 但是當我在我的地圖腳本中使用它時

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

它給了我一個錯誤,如未捕獲的錯誤:無效的 GeoJSON 對象。

我沒有得到錯誤的原因。 我是否需要將我的字符串變量 statesData 解析為 Geojson。 如果是,那么我如何將字符串數據解析為 GEOJSON。

請幫幫我...

GeoJSON 數據只是 JSON,因此您只需要將 String 轉換為 JSON:

const parsedGeoJson = JSON.parse(geoJsonString);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM