Skip to content

Commit

Permalink
Removing geometry when huge and providing download instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ssadhu-sl committed Jan 8, 2025
1 parent 9cc146e commit 20369db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
5 changes: 4 additions & 1 deletion application/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def get_entity_name(entity):


def digital_land_to_json(dict):
filtered_dict = {k: v for k, v in dict.items() if k != "geometry"}
filtered_dict = dict.get("row", {})
is_truncated = dict.get("is_truncated", False)
if is_truncated:
filtered_dict = {k: v for k, v in filtered_dict.items() if k != "geometry"}
# dict["geometry"] = dict["geometry"][:1000]
return json.dumps(
filtered_dict, default=str, indent=4, cls=NoneToEmptyStringEncoder
Expand Down
38 changes: 33 additions & 5 deletions application/templates/entity.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
{%- from "components/entity-field/macro.jinja" import entityField %}
{%- from "components/back-button/macro.jinja" import dlBackButton %}

{% set is_truncated = false %}
{% if row['geometry'] %}
{% set truncated_geometry = row['geometry'][:10000] %}
{% set full_geometry = row['geometry'] %}
{% if row['geometry']|length > 10000 %}
{% set is_truncated = true %}
{% set truncated_geometry = row['geometry'][:10000] %}
{% else %}
{% set truncated_geometry = row['geometry'] %}
{% endif %}
{% else %}
{% set truncated_geometry = "" %}
{% set full_geometry = "" %}
{% endif %}


Expand All @@ -29,6 +33,7 @@
{%- endif %}

{% set geometry_url = "/entity/" + row["entity"] | string + ".geojson" if row['typology'] == "geography" else None %}
{% set geometry_json_url = "/entity/" + row["entity"] | string + ".json" if row['typology'] == "geography" else None %}
{% set geojson_data = entity %}

{% if geometry_url or geojson_features %}
Expand Down Expand Up @@ -93,7 +98,7 @@ <h1 class="govuk-heading-xl">{{ row_name }}</h1>
{% if field == 'geometry' %}
<code class ="app-code-block" id="geometry-content">
{% if row[field] is not none %}
{{ row[field][:10000] }}{% if row[field]|length > 10000 %}... <a href="javascript:void(0);" onclick="expandGeometry()">Load More</a>{% endif %}
{{ truncated_geometry }}{% if row[field]|length > 10000 %}... <a href="javascript:void(0);" onclick="expandGeometry()">Load More</a>{% endif %}
{% endif %}
</code>
<code class ="app-code-block" id="geometry-full-content" style="display: none;">
Expand Down Expand Up @@ -125,8 +130,23 @@ <h1 class="govuk-heading-xl">{{ row_name }}</h1>
</table>
{% endcall %}

{% set data_to_send = {
'row': row,
'is_truncated': is_truncated
} %}
{% set jsonHTML %}
<pre class="govuk-!-margin-0"><code class="language-json app-code-block app-code-block-overflow" tabindex="0">{{ row | digital_land_to_json | safe }}</code></pre>
{% if is_truncated %}
<a href="{{geometry_json_url}}"
class="govuk-button govuk-button--secondary"
style="margin-bottom: 10px;"
download>
Download JSON
</a>
<pre class="govuk-!-margin-0"><code class="language-json app-code-block app-code-block-overflow" tabindex="0">{{ data_to_send | digital_land_to_json | safe }}
</code></pre>
{% else %}
<pre class="govuk-!-margin-0"><code class="language-json app-code-block app-code-block-overflow" tabindex="0">{{ data_to_send | digital_land_to_json | safe }}</code></pre>
{% endif %}
{% endset %}
{% set geojsonHTML %}
<pre class="govuk-!-margin-0"><code class="language-json app-code-block app-code-block-overflow" id="geojson-content" tabindex="0">Loading...</code></pre>
Expand Down Expand Up @@ -387,6 +407,14 @@ <h3 class="govuk-heading-s">{{ type|capitalize }}</h3>
.catch(error => {
console.error('Error loading GeoJSON:', error);
geojsonContentElement.innerHTML = 'Error loading GeoJSON content';
// Create and append the download button
const downloadButton = document.createElement('a');
downloadButton.href = '{{ geometry_url }}';
downloadButton.className = 'govuk-button govuk-button--secondary';
downloadButton.style.marginTop = '10px';
downloadButton.textContent = 'Download GeoJSON';

geojsonContentElement.parentElement.appendChild(downloadButton);
});
}
}
Expand Down

0 comments on commit 20369db

Please sign in to comment.