如何从 geoJson 获取坐标?

Posted

技术标签:

【中文标题】如何从 geoJson 获取坐标?【英文标题】:How do I get coordinates from geoJson? 【发布时间】:2021-12-26 14:01:04 【问题描述】:

我很难从 GeoJson 文件中获取坐标。我设法在地图上画了一个多边形,但我需要一张坐标表来测量路线的长度并添加标记。这是我的 geoJson 文件


  "type": "FeatureCollection",
  "features": [
    
      "type": "Feature",
      "properties": "scalerank":0,"featurecla":"Admin-0 country","labelrank":2,"sovereignt":"Russia","sov_a3":"RUS","adm0_dif":0,"level":2,"type":"Sovereign country","admin":"Russia","adm0_a3":"RUS","geou_dif":0,"geounit":"Russia","gu_a3":"RUS","su_dif":0,"subunit":"Russia","su_a3":"RUS","brk_diff":0,"name":"Russia","name_long":"Russian Federation","brk_a3":"RUS","brk_name":"Russia","brk_group":null,"abbrev":"Rus.","postal":"RUS","formal_en":"Russian Federation","formal_fr":null,"note_adm0":null,"note_brk":null,"name_sort":"Russian Federation","name_alt":null,"mapcolor7":2,"mapcolor8":5,"mapcolor9":7,"mapcolor13":7,"pop_est":140041247,"gdp_md_est":2266000,"pop_year":-99,"lastcensus":2010,"gdp_year":-99,"economy":"3. Emerging region: BRIC","income_grp":"3. Upper middle income","wikipedia":-99,"fips_10_":"RS","iso_a2":"RU","iso_a3":"RUS","iso_n3":"643","un_a3":"643","wb_a2":"RU","wb_a3":"RUS","woe_id":23424936,"woe_id_eh":23424936,"woe_note":"Exact WOE match as country","adm0_a3_is":"RUS","adm0_a3_us":"RUS","adm0_a3_un":-99,"adm0_a3_wb":-99,"continent":"Europe","region_un":"Europe","subregion":"Eastern Europe","region_wb":"Europe & Central Asia","name_len":6,"long_len":18,"abbrev_len":4,"tiny":-99,"homepart":1,"filename":"RUS.geojson",
      "geometry": 
        "type": "MultiPolygon",
        "coordinates": [[[[132.44898522200018,42.845404364],[132.44988040500007,42.82811107000001],[132.4674585300002,42.81093984600007],[132.47771243600013,42.803900458],[132.47559655000012,42.80023834800015],[132.46713300900015,42.79682038000014],[132.458262566,42.798285223],[132.45337975400005,42.80097077000009],[132.44727623800017,42.80731842700011],[132.43946373800006,42.81753164300015],[132.4298608730002,42.82648346600017],[132.41749108200023,42.83002350500006],[132.4064233730002,42.82876211100013],[132

原来只适用于几何

【问题讨论】:

【参考方案1】:

有两种常见的方法来做到这一点。

    您可以使用 android Studio 中的“JSON To Kotlin 类”插件为您的响应数据创建一个模型 data class。然后您可以轻松地将 JSON 属性作为 Kotlin 对象访问。

    您将 JSON 数据作为字符串。将此行添加到您的模块级 build.gradle 文件中:

dependencies 
  //...

  implementation 'com.google.code.gson:gson:2.8.9'

然后您可以使用 Gson 解析您的 JSON 字符串并获取如下坐标:

val jsonData = "..."
val gson = Gson()
val coordinates = gson.fromJson(jsonData.getJSONArray("features")[0].getJSONObject("geometry").getJSONArray("coordinates").toString(), Array<Type /*your data type*/>::class.java).toList()

【讨论】:

以上是关于如何从 geoJson 获取坐标?的主要内容,如果未能解决你的问题,请参考以下文章

如何按坐标过滤 GeoJSON/TopoJSON 数据?

javascript 从点GeoJSON获取点坐标数组

ArcGIS风暴如何将矢量数据(点线面)折点坐标转为GeoJSON格式?

ArcGIS风暴如何将矢量数据(点线面)折点坐标转为GeoJSON格式?

如何从 GeoJson 创建 utfgrid 瓦片?

这个geojson例子的坐标是啥格式