Skip to content

Commit

Permalink
Merge pull request #9 from Open-Telecoms-Data/2022-10-26
Browse files Browse the repository at this point in the history
geojson: Convert JSON to geojson and offer to user to download
  • Loading branch information
odscjames authored Oct 26, 2022
2 parents aace5c7 + 83438be commit 5a55eb4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cove_ofds/templates/cove_ofds/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ <h4 class="panel-title">
{% endif %}


{% if can_download_geojson %}
<ul class="list-unstyled">
<li>
<span class="glyphicon glyphicon-download" aria-hidden="true"></span><a href="{{download_geojson_nodes_url}}">Nodes GeoJSON</a>
</li>
<li>
<span class="glyphicon glyphicon-download" aria-hidden="true"></span><a href="{{download_geojson_spans_url}}">Spans GeoJSON</a>
</li>
</ul>
{% endif %}

</div>
</div>
</div>
Expand Down
38 changes: 38 additions & 0 deletions cove_ofds/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools
import json
import logging
import os
from decimal import Decimal

from cove.views import explore_data_context
Expand All @@ -12,12 +13,20 @@
from libcoveofds.common_checks import common_checks_ofds
from libcoveofds.config import LibCoveOFDSConfig
from libcoveofds.schema import SchemaOFDS
from ofdskit.lib.geojson import JSONToGeoJSONConverter

from cove_project import settings

logger = logging.getLogger(__name__)


class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return str(obj)
return json.JSONEncoder.default(self, obj)


def cove_web_input_error(func):
@functools.wraps(func)
def wrapper(request, *args, **kwargs):
Expand Down Expand Up @@ -135,6 +144,35 @@ def explore_ofds(request, pk):
db_data.rendered = True
db_data.save()

# Make GeoJSON
context["can_download_geojson"] = False
geojson_nodes_path = os.path.join(upload_dir, "nodes.geojson.json")
geojson_spans_path = os.path.join(upload_dir, "spans.geojson.json")
context["download_geojson_nodes_url"] = "{}{}nodes.geojson.json".format(
upload_url, "" if upload_url.endswith("/") else "/"
)
context["download_geojson_spans_url"] = "{}{}spans.geojson.json".format(
upload_url, "" if upload_url.endswith("/") else "/"
)
if not os.path.exists(geojson_nodes_path) or not os.path.exists(geojson_spans_path):
try:
converter = JSONToGeoJSONConverter()
converter.process_package(json_data)
with open(geojson_nodes_path, "w") as fp:
json.dump(
converter.get_nodes_geojson(), fp, indent=2, cls=DecimalEncoder
)
with open(geojson_spans_path, "w") as fp:
json.dump(
converter.get_spans_geojson(), fp, indent=2, cls=DecimalEncoder
)

context["can_download_geojson"] = True
except Exception as err:
print(err)
else:
context["can_download_geojson"] = True

# Some extra info from the Schema
context["schema_version_used"] = schema_ofds.schema_version

Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sentry-sdk
Django>3.2,<3.3
jsonschema
libcoveofds
ofdskit
libcoveweb>=0.19.0
gunicorn
whitenoise
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ lxml==4.9.1
# via flattentool
odfpy==1.4.1
# via flattentool
ofdskit==0.0.0
# via -r requirements.in
openpyxl==3.0.10
# via flattentool
persistent==4.9.1
Expand Down
2 changes: 2 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ odfpy==1.4.1
# via
# -r requirements.txt
# flattentool
ofdskit==0.0.0
# via -r requirements.txt
openpyxl==3.0.10
# via
# -r requirements.txt
Expand Down

0 comments on commit 5a55eb4

Please sign in to comment.