Skip to content

Commit

Permalink
diff/show/apply: Simplify JSON diffs/patches
Browse files Browse the repository at this point in the history
Since #71 we haven't used GeoJSON for diffs or patches,
however we still have some GeoJSON artifacts that are now unnecessary
and unused.

This moves all feature attributes into the feature object, and
removes the `properties`, `geometry` and `id` keys.
  • Loading branch information
craigds committed Jun 13, 2020
1 parent f2dda8e commit eeba752
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 311 deletions.
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
Loading

0 comments on commit eeba752

Please sign in to comment.