easyidp.jsonfile.read_geojson¶
- easyidp.jsonfile.read_geojson(geojson_path, name_field=-1, include_title=False, return_proj=False)¶
将 geojson 文件读取为 Python 字典
- 参数:
geojson_path (str) -- The path to geojson file
name_field (str or int or list[ str|int ], optional) -- 默认值为 None,shp 文件字段的 ID 或名称作为输出字典的键
include_title (bool, optional) -- 默认值为 False,是否将列名添加到 roi 键。
return_proj (bool, optional) -- 默认值为 False,如果设置为 true,将返回当前 shp 文件的额外 pyproj.CRS 对象。
- 返回:
dict, -- 包含读取的 numpy 多边形坐标的字典
{'id1': np.array([[x1,y1],[x2,y2],...]), 'id2': np.array([[x1,y1],[x2,y2],...]),...}
pyproj.CRS, 可选 -- 一旦设置 return_proj=True
示例
The example geojons file has the following columns:
[0] FID [1] 試験区 [2] ID [3] 除草剤 [4] plotName [5] lineNum --------- ------------ -------- ------------ -------------- ------------- 65 SubBlk 2b 0 有 Enrei-10 1 97 SubBlk 2b 0 有 Enrei-20 1 147 SubBlk 2b 0 有 Nakasenri-10 1 ... ... ... ... ... ... 259 SB 0a 0 Tachinagaha-10 3 4 SB 0a 0 Fukuyutaka-10 3 1 SubBlk 2a 0 無 Enrei-20 1数据准备:
>>> import easyidp as idp >>> test_data = idp.data.TestData() >>> data_path = test_data.json.geojson_soy
使用此函数读取 geojson:
>>> out = idp.jsonfile.read_geojson(data_path) >>> out['196'] array([[-26403.247163, -28847.161464], [-26404.137868, -28843.261904], [-26404.820258, -28843.417749], [-26403.929552, -28847.317309], [-26403.247163, -28847.161464]])
小心
前面代码中的
196是第 196 行,不是FID``=196。要使用 FID 作为结果键,请使用下面介绍的 ``name_field如果想使用其他属性作为结果键,例如,将第一列
FID作为键:>>> out = idp.jsonfile.read_geojson(data_path, name_field="FID") >>> # or >>> out = idp.jsonfile.read_geojson(data_path, name_field="0") >>> out['196'] array([[-26391.733164, -28862.99536 ], [-26392.623851, -28859.09578 ], [-26393.306263, -28859.25163 ], [-26392.415575, -28863.15121 ], [-26391.733164, -28862.99536 ]])
您还可以通过传递一个列表给
name_field来组合多个列作为键值:>>> out = idp.jsonfile.read_geojson(data_path, name_field=["FID", "plotName"]) >>> # or >>> out = idp.jsonfile.read_geojson(data_path, name_field=[0, 4]) >>> out.keys() dict_keys(['65_Enrei-10', '97_Enrei-20', '147_Nakasenri-10', ... ])
您还可以通过设置
include_title=True将列名添加到 id:>>> out = idp.jsonfile.read_geojson(data_path, name_field=["FID", "plotName"], include_title=True) >>> # or >>> out = idp.jsonfile.read_geojson(data_path, name_field=[0, 4], include_title=True) >>> out.keys() dict_keys(['FID_65_plotName_Enrei-10', 'FID_97_plotName_Enrei-20', ... ])