Skip to content

Commit

Permalink
read_geojson support -1 index
Browse files Browse the repository at this point in the history
fix #110
  • Loading branch information
HowcanoeWang committed Feb 3, 2024
1 parent 1f5ce0e commit 61d0d84
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
22 changes: 15 additions & 7 deletions easyidp/jsonfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _check_geojson_format(geojson_path):

return geojson_data

def read_geojson(geojson_path, name_field=None, include_title=False, return_proj=False):
def read_geojson(geojson_path, name_field=-1, include_title=False, return_proj=False):
"""Read geojson file to python dict
Parameters
Expand Down Expand Up @@ -240,13 +240,21 @@ def read_geojson(geojson_path, name_field=None, include_title=False, return_proj
# '試験区': 'SubBlk 2b',
# ...
# 'lineNum': 1}

if isinstance(field_id, list):
values = [feature['properties'][_key] for _key in keyring]
# list comprehension
values = [
feature['properties'][_key]
if fid != -1 else i
for fid, _key in zip(field_id, keyring)
]

plot_name = plot_name_template.format(*values)
elif field_id is None:
plot_name = plot_name_template.format(i)
else:
plot_name = plot_name_template.format(feature['properties'][keyring])
if field_id != -1:
plot_name = plot_name_template.format(feature['properties'][keyring])
else:
plot_name = plot_name_template.format(i)

plot_name = plot_name.replace(r'/', '_')
plot_name = plot_name.replace(r'\\', '_')
Expand Down Expand Up @@ -276,7 +284,7 @@ def read_geojson(geojson_path, name_field=None, include_title=False, return_proj

# check if has duplicated key, otherwise will cause override
if plot_name in geo_dict.keys():
raise KeyError(f"Meet with duplicated key [{plot_name}] for current shapefile, please specify another `name_field` from {geo_fields} or simple leave it blank `name_field=None`")
raise KeyError(f"Meet with duplicated key [{plot_name}] for current shapefile, please specify another `name_field` from {geo_fields} or using row id as key `name_field='#'`")

geo_dict[plot_name] = coord_np

Expand Down Expand Up @@ -321,7 +329,7 @@ def show_geojson_fields(geojson_path):
"""
geojson_data = _check_geojson_format(geojson_path)

head = ["[-1]"] + \
head = ["[-1] #"] + \
[f"[{i}] {k}" for i, k in enumerate(geojson_data.features[0]['properties'].keys())]
data = []

Expand Down
51 changes: 35 additions & 16 deletions tests/test_jsonfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_read_geojson_key_names():
gjs_path = test_data.json.geojson_soy

str_no_name_field_title_true= idp.jsonfile.read_geojson(gjs_path, include_title=True)
assert "line_1" in str_no_name_field_title_true.keys()
assert "# 1" in str_no_name_field_title_true.keys()

str_name_field_title_false = idp.jsonfile.read_geojson(gjs_path, name_field="FID")
assert "259" in str_name_field_title_false.keys()
Expand All @@ -65,10 +65,19 @@ def test_read_geojson_key_names():
assert "259" in int_name_field_title_false.keys()

str_name_field_title_true = idp.jsonfile.read_geojson(gjs_path, name_field="FID", include_title=True)
assert "FID_65" in str_name_field_title_true.keys()
assert "FID 65" in str_name_field_title_true.keys()

int_name_field_title_true = idp.jsonfile.read_geojson(gjs_path, name_field=0, include_title=True)
assert "FID_65" in int_name_field_title_true.keys()
assert "FID 65" in int_name_field_title_true.keys()

index_name_field_title_true = idp.jsonfile.read_geojson(gjs_path, name_field=-1, include_title=True)
assert "# 1" in index_name_field_title_true.keys()

str_index_name_field_title_true = idp.jsonfile.read_geojson(gjs_path, name_field='#', include_title=True)
assert "# 1" in str_index_name_field_title_true.keys()

str_index_name_field_title_false = idp.jsonfile.read_geojson(gjs_path, name_field='#', include_title=False)
assert "1" in str_index_name_field_title_false.keys()

def test_read_geojson_key_names_merge():
# merge several columns
Expand All @@ -77,33 +86,43 @@ def test_read_geojson_key_names_merge():
str_name_field_list_title_false = idp.jsonfile.read_geojson(
gjs_path, name_field=["FID", "plotName"]
)
assert "65_Enrei-10" in str_name_field_list_title_false.keys()
assert "65|Enrei-10" in str_name_field_list_title_false.keys()

str_name_field_list_title_true = idp.jsonfile.read_geojson(
gjs_path, name_field=["FID", "plotName"], include_title=True
)
assert "FID_65_plotName_Enrei-10" in str_name_field_list_title_true.keys()
assert "FID 65|plotName Enrei-10" in str_name_field_list_title_true.keys()

int_name_field_list_title_false = idp.jsonfile.read_geojson(
gjs_path, name_field=[0, 4]
)
assert "65_Enrei-10" in int_name_field_list_title_false.keys()
assert "65|Enrei-10" in int_name_field_list_title_false.keys()

int_name_field_list_title_true = idp.jsonfile.read_geojson(
gjs_path, name_field=[0, 4], include_title=True
)
assert "FID_65_plotName_Enrei-10" in int_name_field_list_title_true.keys()
assert "FID 65|plotName Enrei-10" in int_name_field_list_title_true.keys()

int_index_name_field_list_title_true = idp.jsonfile.read_geojson(
gjs_path, name_field=[-1, 4], include_title=True
)
assert "# 0|plotName Enrei-10" in int_index_name_field_list_title_true.keys()

str_index_name_field_list_title_false = idp.jsonfile.read_geojson(
gjs_path, name_field=["#", "FID"], include_title=False
)
assert "0|65" in str_index_name_field_list_title_false.keys()

geojson_table_preview = \
" [-1] [0] FID [1] 試験区 [2] ID [3] 除草剤 [4] plotName [5] lineNum\n"\
"------ --------- ------------ -------- ------------ -------------- -------------\n"\
" 0 65 SubBlk 2b 0 有 Enrei-10 1\n"\
" 1 97 SubBlk 2b 0 有 Enrei-20 1\n"\
" 2 147 SubBlk 2b 0 有 Nakasenri-10 1\n"\
" ... ... ... ... ... ... ...\n"\
" 257 259 SB 0a 0 Tachinagaha-10 3\n"\
" 258 4 SB 0a 0 Fukuyutaka-10 3\n"\
" 259 1 SubBlk 2a 0 無 Enrei-20 1\n"
" [-1] # [0] FID [1] 試験区 [2] ID [3] 除草剤 [4] plotName [5] lineNum\n"\
"-------- --------- ------------ -------- ------------ -------------- -------------\n"\
" 0 65 SubBlk 2b 0 有 Enrei-10 1\n"\
" 1 97 SubBlk 2b 0 有 Enrei-20 1\n"\
" 2 147 SubBlk 2b 0 有 Nakasenri-10 1\n"\
" ... ... ... ... ... ... ...\n"\
" 257 259 SB 0a 0 Tachinagaha-10 3\n"\
" 258 4 SB 0a 0 Fukuyutaka-10 3\n"\
" 259 1 SubBlk 2a 0 無 Enrei-20 1\n"
def test_show_geojson_fields(capfd):

idp.jsonfile.show_geojson_fields(test_data.json.geojson_soy)
Expand Down

0 comments on commit 61d0d84

Please sign in to comment.