如何按县的字母顺序对GeoJson进行排序(一把钥匙)



我在组织geojson数据时遇到了一些问题。我需要json_countries['features']['properties']按郡名"name"排序。

输入:

with open('california-counties.geojson') as f:
json_counties = json.load(f);
json_counties['features']

输出:

{'type': 'Feature',
'properties': {'name': 'Alameda',
'cartodb_id': 1,
'created_at': '2015-07-04T21:04:58Z',
'updated_at': '2015-07-04T21:04:58Z'},
'geometry': {'type': 'MultiPolygon',
'coordinates': [[[[-122.312934, 37.897333],
[-122.28848, 37.897925],
[-122.27418, 37.905025],
[-122.263946, 37.903734],
[-122.249477, 37.893086],
[-122.248914, 37.886867],
[-122.223878, 37.878326],
[-122.216276, 37.868822],
[-122.220389, 37.864427],
[-122.204094, 37.851387],
[-122.196101, 37.842005]....}},
{'type': 'Feature',
'properties': {'name': 'Butte',
'cartodb_id': 4,
'created_at': '2015-07-04T21:04:58Z',
'updated_at': '2015-07-04T21:04:58Z'},
'geometry': {'type': 'Polygon',
'coordinates': [[[-121.879249, 39.303608],...

以上按随机顺序列出。例如,关于阿尔卑斯县的信息应该在巴特县之后。

我到了这里,

sorted(json_counties['features']['properties'], key = lambda x:x['name'])

但是我收到一个错误

TypeError: string indices must be integers

似乎'features'是一个字典列表。如果是,您需要:

sorted(json_counties["features"], key = lambda x:x["properties"]["name"]) 

最新更新