Skip to content

Commit

Permalink
geojsontojson: Start
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Oct 26, 2022
1 parent 9fbd24e commit a308459
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ofdskit/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def main():
json_to_geojson_parser.add_argument("outputnodesfilename")
json_to_geojson_parser.add_argument("outputspansfilename")

geojson_to_json_parser = subparsers.add_parser("geojsontojson")
geojson_to_json_parser.add_argument("inputnodesfilename")
geojson_to_json_parser.add_argument("inputspansfilename")
geojson_to_json_parser.add_argument("outputfilename")

args = parser.parse_args()

if args.subparser_name == "jsontogeojson":
Expand All @@ -29,6 +34,20 @@ def main():
with open(args.outputspansfilename, "w") as fp:
json.dump(converter.get_spans_geojson(), fp, indent=4)

elif args.subparser_name == "geojsontojson":

with open(args.inputnodesfilename) as fp:
nodes_data = json.load(fp)

with open(args.inputspansfilename) as fp:
spans_data = json.load(fp)

converter = ofdskit.lib.geojson.GeoJSONToJSONConverter()
converter.process_data(nodes_data, spans_data)

with open(args.outputfilename, "w") as fp:
json.dump(converter.get_json(), fp, indent=4)


if __name__ == "__main__":
main()
72 changes: 72 additions & 0 deletions ofdskit/lib/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,75 @@ def _convert_span_to_feature(
feature["properties"]["network"] = reduced_network_data

return feature


class GeoJSONToJSONConverter:
def __init__(self):
self._networks: dict = {}

def process_data(self, nodes_data: dict, spans_data: dict) -> None:
# Network
for geojson_feature in nodes_data.get("features", []):
self._process_network(geojson_feature)
for geojson_feature in spans_data.get("features", []):
self._process_network(geojson_feature)

# Nodes
for geojson_feature in nodes_data.get("features", []):
self._process_node(geojson_feature)

# Spans
for geojson_feature in spans_data.get("features", []):
self._process_span(geojson_feature)

def _process_network(self, geojson_feature_node_or_span: dict) -> None:
if (
"properties" in geojson_feature_node_or_span
and "network" in geojson_feature_node_or_span["properties"]
):
network = geojson_feature_node_or_span["properties"]["network"]
if network.get("id"):
# TODO check for inconsistent data here!
self._networks[network.get("id")] = copy.deepcopy(network)
self._networks[network.get("id")]["nodes"] = []
self._networks[network.get("id")]["spans"] = []
self._networks[network.get("id")]["phases"] = []
self._networks[network.get("id")]["organisations"] = []

def _process_node(self, geojson_feature_node: dict) -> None:
node = copy.deepcopy(geojson_feature_node.get("properties", {}))
for key_to_remove in ["network"]:
if key_to_remove in node:
del node[key_to_remove]
network_id = (
geojson_feature_node.get("properties", {}).get("network", {}).get("id")
)
if network_id not in self._networks.keys():
# TODO log error
return

node["location"] = geojson_feature_node.get("geometry")

self._networks[network_id]["nodes"].append(node)

def _process_span(self, geojson_feature_span: dict) -> None:
span = copy.deepcopy(geojson_feature_span.get("properties", {}))
for key_to_remove in ["network"]:
if key_to_remove in span:
del span[key_to_remove]
network_id = (
geojson_feature_span.get("properties", {}).get("network", {}).get("id")
)
if network_id not in self._networks.keys():
# TODO log error
return

span["route"] = geojson_feature_span.get("geometry")

span["start"] = span.get("start", {}).get("id")
span["end"] = span.get("end", {}).get("id")

self._networks[network_id]["spans"].append(span)

def get_json(self):
return {"networks": [v for v in self._networks.values()]}
100 changes: 100 additions & 0 deletions tests/fixtures/geojson_to_json/basic_1.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"networks": [
{
"id": "a096d627-72e1-4f9b-b129-951b1737bff4",
"name": "Ghana Fibre Network",
"nodes": [
{
"id": "1",
"name": "Accra",
"location": {
"type": "Point",
"coordinates": [
-0.174,
5.625
]
}
},
{
"id": "2",
"name": "Kumasi",
"status": "operational",
"location": {
"type": "Point",
"coordinates": [
-1.628,
6.711
]
}
}
],
"spans": [
{
"id": "1",
"name": "Accra to Kumasi",
"start": "1",
"end": "2",
"route": {
"type": "LineString",
"coordinates": [
[
-0.173,
5.626
],
[
-0.178,
5.807
],
[
-0.112,
5.971
],
[
-0.211,
5.963
],
[
-0.321,
6.17
],
[
-0.488,
6.29
],
[
-0.56,
6.421
],
[
-0.752,
6.533
],
[
-0.867,
6.607
],
[
-1.101,
6.585
],
[
-1.304,
6.623
],
[
-1.461,
6.727
],
[
-1.628,
6.713
]
]
}
}
],
"phases": [],
"organisations": []
}
]
}
42 changes: 42 additions & 0 deletions tests/fixtures/geojson_to_json/basic_1.nodes.geo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-0.174,
5.625
]
},
"properties": {
"id": "1",
"name": "Accra",
"network": {
"id": "a096d627-72e1-4f9b-b129-951b1737bff4",
"name": "Ghana Fibre Network"
}
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-1.628,
6.711
]
},
"properties": {
"id": "2",
"name": "Kumasi",
"status": "operational",
"network": {
"id": "a096d627-72e1-4f9b-b129-951b1737bff4",
"name": "Ghana Fibre Network"
}
}
}
]
}
96 changes: 96 additions & 0 deletions tests/fixtures/geojson_to_json/basic_1.spans.geo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[
-0.173,
5.626
],
[
-0.178,
5.807
],
[
-0.112,
5.971
],
[
-0.211,
5.963
],
[
-0.321,
6.17
],
[
-0.488,
6.29
],
[
-0.56,
6.421
],
[
-0.752,
6.533
],
[
-0.867,
6.607
],
[
-1.101,
6.585
],
[
-1.304,
6.623
],
[
-1.461,
6.727
],
[
-1.628,
6.713
]
]
},
"properties": {
"id": "1",
"name": "Accra to Kumasi",
"start": {
"id": "1",
"name": "Accra",
"location": {
"type": "Point",
"coordinates": [
-0.174,
5.625
]
}
},
"end": {
"id": "2",
"name": "Kumasi",
"status": "operational",
"location": {
"type": "Point",
"coordinates": [
-1.628,
6.711
]
}
},
"network": {
"id": "a096d627-72e1-4f9b-b129-951b1737bff4",
"name": "Ghana Fibre Network"
}
}
}
]
}
Loading

0 comments on commit a308459

Please sign in to comment.