【问题标题】:how to access or get value from geojson response given a key给定键如何从geojson响应中访问或获取值
【发布时间】:2021-04-16 05:12:39
【问题描述】:

在下面的 geojson 响应中,我想访问以下内容

type and segments

为了实现我做了以下事情

    return data["type"] //does not work i receive an error
    return data["features"][0]["properties"]["segments"] //does not work i receive an error

请告诉我如何在给定密钥的情况下从 geojson 响应中访问或获取值 geojson

{"type":"FeatureCollection","features":[{"bbox":[29.924118,31.205141,29.924728,31.20539],"type":"Feature","properties":{"segments":[{"distance":64.4,"duration":3.9,"steps":[{"distance":64.4,"duration":3.9,"type":11,"instruction":"Head southwest on شارع جمال عبد الناصر","name":"شارع جمال عبد الناصر","way_points":[0,3]},{"distance":0.0,"duration":0.0,"type":10,"instruction":"Arrive at شارع جمال عبد الناصر, straight ahead","name":"-","way_points":[3,3]}]}],"summary":{"distance":64.4,"duration":3.9},"way_points":[0,3]},"geometry":{"coordinates":[[29.924728,31.20539],[29.924568,31.205325],[29.924493,31.205294],[29.924118,31.205141]],"type":"LineString"}}],"bbox":[29.924118,31.205141,29.924728,31.20539],"metadata":{"attribution":"openrouteservice.org | OpenStreetMap contributors","service":"routing","timestamp":1618549441274,"query":{"coordinates":[[29.924728,31.205391],[29.924118,31.205141]],"profile":"driving-car","format":"json"},"engine":{"version":"6.4.1","build_date":"2021-04-12T07:11:51Z","graph_date":"1970-01-01T00:00:00Z"}}}

代码

@app.route("/getRouteFromOriginToDestination/<string:originAndDestinationGPSCoords>", methods=['GET'] )
def getRouteFromOriginToDestination(originAndDestinationGPSCoords):
#api_key = request.args.get('api_key')
# startLat, startLng= request.args.get('start').split(',')
# end1Lat, endLng = request.args.get('end').split(',')
global gpsCoordinates
gpsCoordinates = originAndDestinationGPSCoords
isCohesiveData = checkDataCohesion(gpsCoordinates)
if (isCohesiveData):
    logging.info("data is cohesive")
    data = fetchDataForURL(getBaseURL(gpsCoordinates))
else:
    print("data is not cohesive")
    abort(400, 'Record not found')   

#dist = float(distance)
#dist = dist/1000
return data

【问题讨论】:

  • “返回错误”没有帮助。请下次告诉我们什么错误。

标签: javascript python json angular geojson


【解决方案1】:

需要先解析json:

>>> import json
>>> data = json.loads('{"type":"FeatureCollection"}')
>>> data['type']
'FeatureCollection'

【讨论】:

  • 执行我,我对 dataAsJSON["features"][0]["geometry"]["coordinates"] 尝试了同样的方法,但它返回一个错误,请你帮帮我
  • 告诉我你得到了什么 dataAsJSON['features']
  • 您知道,Python 的一大优点是能够在命令行上处理数据。然后,每个操作的结果都可以在特殊变量 _ 中获得。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
相关资源
最近更新 更多