Skip to content

Commit

Permalink
jsontogeojson: Add meta output, with output field coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
odscjames committed Nov 7, 2022
1 parent 0767e54 commit 9faaa1d
Show file tree
Hide file tree
Showing 9 changed files with 639 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add meta output, with output field coverage
- Fix bug that meant get_json() could not be called twice
- Include contracts
- Improved in JSON to GeoJSON:
- Add meta output, with output field coverage

### Changed

Expand Down
9 changes: 9 additions & 0 deletions libcoveofds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def main():
json_to_geojson_parser.add_argument(
"outputspansfilename", help="Output filename to write Spans GeoJSON data to"
)
json_to_geojson_parser.add_argument(
"--outputmetafilename",
help="Output filename to write meta JSON data to",
required=False,
)

geojson_to_json_parser = subparsers.add_parser("geojsontojson", aliases=["gjtoj"])
geojson_to_json_parser.add_argument(
Expand Down Expand Up @@ -110,6 +115,10 @@ def main():
with open(args.outputspansfilename, "w") as fp:
json.dump(converter.get_spans_geojson(), fp, indent=4)

if args.outputmetafilename:
with open(args.outputmetafilename, "w") as fp:
json.dump(converter.get_meta_json(), fp, indent=4)

elif args.subparser_name == "geojsontojson" or args.subparser_name == "gjtoj":

with open(args.inputnodesfilename) as fp:
Expand Down
20 changes: 20 additions & 0 deletions libcoveofds/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ def get_nodes_geojson(self) -> dict:
def get_spans_geojson(self) -> dict:
return {"type": "FeatureCollection", "features": self._spans_geojson_features}

def get_meta_json(self) -> dict:
out: dict = {
"nodes_output_field_coverage": {},
"spans_output_field_coverage": {},
}
# nodes field coverage
for key, value in fields_present_generator(self.get_nodes_geojson()):
if key not in out["nodes_output_field_coverage"]:
out["nodes_output_field_coverage"][key] = {"count": 1}
else:
out["nodes_output_field_coverage"][key]["count"] += 1
# spans field coverage
for key, value in fields_present_generator(self.get_spans_geojson()):
if key not in out["spans_output_field_coverage"]:
out["spans_output_field_coverage"][key] = {"count": 1}
else:
out["spans_output_field_coverage"][key]["count"] += 1
# return
return out

def _dereference_object(self, ref, list):
"""
Return from list the object referenced by ref. Otherwise, return ref.
Expand Down
8 changes: 4 additions & 4 deletions make_expected_test_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ libcoveofds gjtoj --outputmetafilename tests/fixtures/geojson_to_json/phases_1.m
libcoveofds gjtoj --outputmetafilename tests/fixtures/geojson_to_json/organisations_1.meta.expected.json tests/fixtures/geojson_to_json/organisations_1.nodes.geo.json tests/fixtures/geojson_to_json/organisations_1.spans.geo.json tests/fixtures/geojson_to_json/organisations_1.expected.json

# JSON to GeoJSON
libcoveofds jtogj tests/fixtures/json_to_geojson/basic_1.json tests/fixtures/json_to_geojson/basic_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/basic_1.expected.spans.geo.json
libcoveofds jtogj tests/fixtures/json_to_geojson/phases_1.json tests/fixtures/json_to_geojson/phases_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/phases_1.expected.spans.geo.json
libcoveofds jtogj tests/fixtures/json_to_geojson/organisations_1.json tests/fixtures/json_to_geojson/organisations_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/organisations_1.expected.spans.geo.json
libcoveofds jtogj tests/fixtures/json_to_geojson/no_geometry_1.json tests/fixtures/json_to_geojson/no_geometry_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/no_geometry_1.expected.spans.geo.json
libcoveofds jtogj --outputmetafilename tests/fixtures/json_to_geojson/basic_1.expected.meta.json tests/fixtures/json_to_geojson/basic_1.json tests/fixtures/json_to_geojson/basic_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/basic_1.expected.spans.geo.json
libcoveofds jtogj --outputmetafilename tests/fixtures/json_to_geojson/phases_1.expected.meta.json tests/fixtures/json_to_geojson/phases_1.json tests/fixtures/json_to_geojson/phases_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/phases_1.expected.spans.geo.json
libcoveofds jtogj --outputmetafilename tests/fixtures/json_to_geojson/organisations_1.expected.meta.json tests/fixtures/json_to_geojson/organisations_1.json tests/fixtures/json_to_geojson/organisations_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/organisations_1.expected.spans.geo.json
libcoveofds jtogj --outputmetafilename tests/fixtures/json_to_geojson/no_geometry_1.expected.meta.json tests/fixtures/json_to_geojson/no_geometry_1.json tests/fixtures/json_to_geojson/no_geometry_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/no_geometry_1.expected.spans.geo.json

# JSON Schema validate
libcoveofds jsv tests/fixtures/jsonschemavalidate/basic_1.input.json > tests/fixtures/jsonschemavalidate/basic_1.expected.json
Expand Down
120 changes: 120 additions & 0 deletions tests/fixtures/json_to_geojson/basic_1.expected.meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"nodes_output_field_coverage": {
"/type": {
"count": 1
},
"/features": {
"count": 1
},
"/features/type": {
"count": 2
},
"/features/geometry": {
"count": 2
},
"/features/geometry/type": {
"count": 2
},
"/features/geometry/coordinates": {
"count": 2
},
"/features/properties": {
"count": 2
},
"/features/properties/id": {
"count": 2
},
"/features/properties/name": {
"count": 2
},
"/features/properties/network": {
"count": 2
},
"/features/properties/network/id": {
"count": 2
},
"/features/properties/network/name": {
"count": 2
},
"/features/properties/status": {
"count": 1
}
},
"spans_output_field_coverage": {
"/type": {
"count": 1
},
"/features": {
"count": 1
},
"/features/type": {
"count": 1
},
"/features/geometry": {
"count": 1
},
"/features/geometry/type": {
"count": 1
},
"/features/geometry/coordinates": {
"count": 1
},
"/features/properties": {
"count": 1
},
"/features/properties/id": {
"count": 1
},
"/features/properties/name": {
"count": 1
},
"/features/properties/start": {
"count": 1
},
"/features/properties/start/id": {
"count": 1
},
"/features/properties/start/name": {
"count": 1
},
"/features/properties/start/location": {
"count": 1
},
"/features/properties/start/location/type": {
"count": 1
},
"/features/properties/start/location/coordinates": {
"count": 1
},
"/features/properties/end": {
"count": 1
},
"/features/properties/end/id": {
"count": 1
},
"/features/properties/end/name": {
"count": 1
},
"/features/properties/end/status": {
"count": 1
},
"/features/properties/end/location": {
"count": 1
},
"/features/properties/end/location/type": {
"count": 1
},
"/features/properties/end/location/coordinates": {
"count": 1
},
"/features/properties/network": {
"count": 1
},
"/features/properties/network/id": {
"count": 1
},
"/features/properties/network/name": {
"count": 1
}
}
}
90 changes: 90 additions & 0 deletions tests/fixtures/json_to_geojson/no_geometry_1.expected.meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"nodes_output_field_coverage": {
"/type": {
"count": 1
},
"/features": {
"count": 1
},
"/features/type": {
"count": 2
},
"/features/geometry": {
"count": 2
},
"/features/properties": {
"count": 2
},
"/features/properties/id": {
"count": 2
},
"/features/properties/name": {
"count": 2
},
"/features/properties/network": {
"count": 2
},
"/features/properties/network/id": {
"count": 2
},
"/features/properties/network/name": {
"count": 2
},
"/features/properties/status": {
"count": 1
}
},
"spans_output_field_coverage": {
"/type": {
"count": 1
},
"/features": {
"count": 1
},
"/features/type": {
"count": 1
},
"/features/geometry": {
"count": 1
},
"/features/properties": {
"count": 1
},
"/features/properties/id": {
"count": 1
},
"/features/properties/name": {
"count": 1
},
"/features/properties/start": {
"count": 1
},
"/features/properties/start/id": {
"count": 1
},
"/features/properties/start/name": {
"count": 1
},
"/features/properties/end": {
"count": 1
},
"/features/properties/end/id": {
"count": 1
},
"/features/properties/end/name": {
"count": 1
},
"/features/properties/end/status": {
"count": 1
},
"/features/properties/network": {
"count": 1
},
"/features/properties/network/id": {
"count": 1
},
"/features/properties/network/name": {
"count": 1
}
}
}
Loading

0 comments on commit 9faaa1d

Please sign in to comment.