easyidp.jsonfile.read_geojson#

easyidp.jsonfile.read_geojson(geojson_path, name_field=-1, include_title=False, return_proj=False)#

Read geojson file to python dict

Parameters:
  • geojson_path (str) – The path to geojson file

  • name_field (str or int or list[ str|int ], optional) – by default None, the id or name of shp file fields as output dictionary keys

  • include_title (bool, optional) – by default False, whether add column name to roi key.

  • return_proj (bool, optional) – by default False, if given as true, will return extra pyproj.CRS object of current shp file.

Returns:

  • dict, – the dictionary with read numpy polygon coordinates

    {'id1': np.array([[x1,y1],[x2,y2],...]),
     'id2': np.array([[x1,y1],[x2,y2],...]),...}
    
  • pyproj.CRS, optional – once set return_proj=True

Example

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

Data prepare:

>>> import easyidp as idp
>>> test_data = idp.data.TestData()
>>> data_path = test_data.json.geojson_soy

Use this function to read 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]])

Caution

The 196 in previous code is the row 196, not the FID``=196. To use the FID as result key, Please use ``name_field introduced below

If want use other attributes as result keys, for example, the first column FID as the key:

>>> 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 ]])

You can also combine multiple columns as the key values, by passing a list to 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', ... ])

And you can also add column_names to id by include_title=True :

>>> 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', ... ])