Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diff/show/apply: Simplify JSON diffs/patches #117

Merged
merged 1 commit into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sno/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
def unjson_feature(dataset, d):
if d is None:
return d
r = copy.deepcopy(d['properties'])
r = copy.deepcopy(d)
if dataset.geom_column_name:
# add geometry in
r[dataset.geom_column_name] = hex_wkb_to_gpkg_geom(d['geometry'])
r[dataset.geom_column_name] = hex_wkb_to_gpkg_geom(r[dataset.geom_column_name])
return r


Expand Down
32 changes: 13 additions & 19 deletions sno/diff_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from . import gpkg
from .output_util import dump_json_output, resolve_output_path
from .utils import ungenerator


@contextlib.contextmanager
Expand Down Expand Up @@ -272,12 +273,15 @@ def _out(dataset, diff):
)

# sort for reproducibility
d["featureChanges"].sort(
key=lambda fc: (
fc['-']["id"] if '-' in fc else "",
fc['+']["id"] if '+' in fc else "",
)
)
def sort_key(fc):
if '-' in fc and '+' in fc:
return (fc['-'][pk_field], fc['+'][pk_field])
elif '-' in fc:
return (fc['-'][pk_field],)
else:
return (fc['+'][pk_field],)

d["featureChanges"].sort(key=sort_key)
accumulated[dataset.path] = d

yield _out
Expand All @@ -287,26 +291,16 @@ def _out(dataset, diff):
)


@ungenerator(dict)
def json_row(row, pk_field, change=None):
"""
Turns a row into a dict for serialization as JSON.
The geometry is serialized as hexWKB.
"""
raw_id = row[pk_field]
change_id = f"{change}::{raw_id}" if change else raw_id
f = {
"geometry": None,
"properties": {},
"id": change_id,
}

for k, v in row.items():
if isinstance(v, bytes):
f["geometry"] = gpkg.gpkg_geom_to_hex_wkb(v)
else:
f["properties"][k] = v

return f
v = gpkg.gpkg_geom_to_hex_wkb(v)
yield k, v


def geojson_row(row, pk_field, change=None):
Expand Down
60 changes: 24 additions & 36 deletions tests/data/patches/points-1U-1D-1I.snopatch
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,40 @@
"featureChanges": [
{
"+": {
"geometry": "01010000005788C5255BEA6540CB85AC5AC54242C0",
"id": "U+::9999",
"properties": {
"fid": 9999,
"macronated": null,
"name": null,
"name_ascii": "new",
"t50_fid": null
}
"fid": 9999,
"geom": "01010000005788C5255BEA6540CB85AC5AC54242C0",
"macronated": null,
"name": null,
"name_ascii": "new",
"t50_fid": null
}
},
{
"-": {
"geometry": "0101000000492A45C30AEB6540551507C3663C42C0",
"id": "U-::1241",
"properties": {
"fid": 1241,
"macronated": "N",
"name": null,
"name_ascii": null,
"t50_fid": 2427486
}
"fid": 1241,
"geom": "0101000000492A45C30AEB6540551507C3663C42C0",
"macronated": "N",
"name": null,
"name_ascii": null,
"t50_fid": 2427486
}
},
{
"+": {
"geometry": "010100000091C8DB9402C765403DDEF1BAB99F41C0",
"id": "U+::1795",
"properties": {
"fid": 1795,
"macronated": "N",
"name": null,
"name_ascii": "updated",
"t50_fid": 2428041
}
"fid": 1795,
"geom": "010100000091C8DB9402C765403DDEF1BAB99F41C0",
"macronated": "N",
"name": null,
"name_ascii": "updated",
"t50_fid": 2428041
},
"-": {
"geometry": "010100000091C8DB9402C765403DDEF1BAB99F41C0",
"id": "U-::1795",
"properties": {
"fid": 1795,
"macronated": "N",
"name": null,
"name_ascii": null,
"t50_fid": 2428041
}
"fid": 1795,
"geom": "010100000091C8DB9402C765403DDEF1BAB99F41C0",
"macronated": "N",
"name": null,
"name_ascii": null,
"t50_fid": 2428041
}
}
],
Expand Down
30 changes: 12 additions & 18 deletions tests/data/patches/updates-only.snopatch
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@
"featureChanges": [
{
"-": {
"geometry": "0101000000DD1A926B95F16540D5B4783B715642C0",
"id": "U-::1095",
"properties": {
"fid": 1095,
"macronated": "Y",
"name": "Harataunga (Rākairoa)\n",
"name_ascii": "Harataunga (Rakairoa)\n",
"t50_fid": 2427339
}
"geom": "0101000000DD1A926B95F16540D5B4783B715642C0",
"fid": 1095,
"macronated": "Y",
"name": "Harataunga (Rākairoa)\n",
"name_ascii": "Harataunga (Rakairoa)\n",
"t50_fid": 2427339
},
"+": {
"geometry": "0101000000DD1A926B95F16540D5B4783B715642C0",
"id": "U+::1095",
"properties": {
"fid": 1095,
"macronated": "N",
"name": null,
"name_ascii": null,
"t50_fid": 2427339
}
"geom": "0101000000DD1A926B95F16540D5B4783B715642C0",
"fid": 1095,
"macronated": "N",
"name": null,
"name_ascii": null,
"t50_fid": 2427339
}
}
],
Expand Down
45 changes: 18 additions & 27 deletions tests/test_conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,37 +141,28 @@ def test_list_conflicts(create_conflicts, cli_runner):
"feature": {
"4": {
"ancestor": {
"geometry": "0101000000E699C7FE092966404E7743C1B50B43C0",
"properties": {
"fid": 4,
"macronated": "N",
"name": None,
"name_ascii": None,
"t50_fid": 2426274,
},
"id": 4,
"geom": "0101000000E699C7FE092966404E7743C1B50B43C0",
"fid": 4,
"macronated": "N",
"name": None,
"name_ascii": None,
"t50_fid": 2426274,
},
"ours": {
"geometry": "0101000000E699C7FE092966404E7743C1B50B43C0",
"properties": {
"fid": 4,
"t50_fid": 2426274,
"name_ascii": None,
"macronated": "N",
"name": "ours_version",
},
"id": 4,
"geom": "0101000000E699C7FE092966404E7743C1B50B43C0",
"fid": 4,
"t50_fid": 2426274,
"name_ascii": None,
"macronated": "N",
"name": "ours_version",
},
"theirs": {
"geometry": "0101000000E699C7FE092966404E7743C1B50B43C0",
"properties": {
"fid": 4,
"t50_fid": 2426274,
"name_ascii": None,
"macronated": "N",
"name": "theirs_version",
},
"id": 4,
"geom": "0101000000E699C7FE092966404E7743C1B50B43C0",
"fid": 4,
"t50_fid": 2426274,
"name_ascii": None,
"macronated": "N",
"name": "theirs_version",
},
}
}
Expand Down
Loading