Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ace5ee0
Changes for testing warnings with js
BharatVe Mar 24, 2025
e987387
Update APi to API&Data
BharatVe Mar 26, 2025
7c145c0
Merge branch 'main' into enhancement/Download_all_geometries_and_meta…
nuest Mar 31, 2025
ba487d4
Addition of GeoPackage + Dynamic Size Calculation ( GeoPackage needs …
Apr 2, 2025
af50835
Upadted implemntation for Geopackage download
Apr 7, 2025
83cc473
Updated test file
Apr 9, 2025
699e78b
Merge remote-tracking branch 'origin/main' into enhancement/Download_…
Apr 9, 2025
1cc2193
Merge remote-tracking branch 'origin/main' into enhancement/Download_…
Apr 9, 2025
b21233d
update test( with pygdal)
Apr 9, 2025
823ad18
Update test_geo_data.py
BharatVe Apr 9, 2025
bcec9a1
Update requirements.txt
BharatVe Apr 9, 2025
aca2962
updated views.py, requirements.txt using fiona and shapely (vs osgeo)
Apr 10, 2025
7869a82
Changes for updated pull request. (Work in progress)
Apr 20, 2025
17965b0
Update tasks.py, minor updates
BharatVe Apr 20, 2025
988b5e1
Completed implemeentation with recommeded changes(final check needed)
Apr 22, 2025
c4cc194
Minor corrections tasks.py
BharatVe Apr 23, 2025
f135ef3
updated test
Apr 23, 2025
052c42f
Update data.html
BharatVe Apr 23, 2025
acfe536
Updated data message
BharatVe Apr 23, 2025
30cb5f1
now to timezone (Fix unittest issue)
Apr 23, 2025
9d24d63
add logos and colours to README, closes #33
nuest Apr 9, 2025
fc02e9b
Updated scripts- changed time fomats, modified test added humanize time
Apr 28, 2025
daed800
Merge branch 'main' into enhancement/Download_all_geometries_and_meta…
BharatVe Apr 28, 2025
a9f7a8d
fixed tests, removed fiona and updated requirements.txt
Apr 28, 2025
939e0b8
install GDAL package form PyPI
nuest Apr 29, 2025
e7d9701
fix test
nuest Apr 29, 2025
a2f829f
Updated links, changed URLs, corrected footer, added automated cache …
May 5, 2025
479e10e
Updated apps and tests
May 6, 2025
0e4b16b
Update apps.py
BharatVe May 6, 2025
6c072ed
Use Humanize, added checks for link validity.
May 12, 2025
100dc2a
added humanize
May 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions optimap/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'django.contrib.staticfiles',
'django.contrib.gis',
'django.contrib.sitemaps',
'django.contrib.humanize',
'publications',
'rest_framework',
'rest_framework_gis',
Expand Down
3 changes: 1 addition & 2 deletions optimap/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@

urlpatterns = [
path('admin/', admin.site.urls),
# Updated inclusion with namespace
path('', include(('publications.urls', 'publications'), namespace='publications')),
path('', include(('publications.urls', 'optimap'), namespace='optimap')),
Comment thread
nuest marked this conversation as resolved.
path(
"sitemap.xml",
sitemaps_views.index,
Expand Down
2 changes: 1 addition & 1 deletion publications/sitemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StaticViewSitemap(Sitemap):

def items(self):
return ["main",
"data",
"data_and_api",
"api",
"privacy"]

Expand Down
10 changes: 10 additions & 0 deletions publications/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
from django.contrib.auth import get_user_model
User = get_user_model()
from django.urls import reverse
from urllib.parse import quote
from datetime import datetime
from django_q.tasks import schedule
from django.utils import timezone
from django_q.tasks import schedule
from django_q.models import Schedule
import time
import calendar
import re
from django.contrib.gis.geos import GeometryCollection

BASE_URL = settings.BASE_URL

Expand Down
12 changes: 6 additions & 6 deletions publications/templates/data.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "main.html" %}
{% load optimap_extras %}

{% load humanize %}
{% block title %}Data & API | {% endblock %}

{% block content %}
Expand All @@ -21,7 +21,7 @@ <h2 class="py-2">API Endpoint</h2>
<a href="https://stedolan.github.io/jq/" title="Link to jq project website" target="_blank"><code>jq</code></a> for formatting):
</p>
<pre>
curl -X GET {{ site|addstr:"/api" }}/api/publications/ | jq
curl -X GET {{ site|addstr:"/api" }}/api/optimap/ | jq
</pre>
<h2 class="py-2">OpenAPI Schema</h2>
<p>
Expand All @@ -38,15 +38,15 @@ <h2 class="py-2">Download Publication Data</h2>
</p>
<ul>
<li class="text-nowrap">
<a class="btn btn-primary" href="{% url 'publications:download_geojson' %}">
<a class="btn btn-primary" href="{% url 'optimap:download_geojson' %}">
Download GeoJSON
</a>
<span class="text-nowrap">
(<a href="https://geojson.org/" target="_blank">GeoJSON spec</a>) {{ geojson_size }}
</span>
</li>
<li class="text-nowrap">
<a class="btn btn-primary" href="{% url 'publications:download_geopackage' %}">
<a class="btn btn-primary" href="{% url 'optimap:download_geopackage' %}">
Download GeoPackage
</a>
<span class="text-nowrap">
Expand All @@ -56,8 +56,8 @@ <h2 class="py-2">Download Publication Data</h2>
</ul>

<p class="small text-muted">
Data dumps are recreated every 6 hours.&nbsp;Last updated: {{ last_updated }}
</p>
Data dumps are recreated every 6 hours. Last updated: {{ last_updated|naturaltime }}
Comment thread
BharatVe marked this conversation as resolved.
Outdated
</p>
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion publications/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
path("api/v1/", include("publications.api")),
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
path('api/schema/ui/sitemap', SpectacularRedocView.as_view(url_name='optimap:schema'), name='redoc'),
path("data/", views.data, name="data_and_api"),
path("data_and_api/", views.data, name="data_and_api"),
Comment thread
BharatVe marked this conversation as resolved.
Outdated
path('feed/georss/', GeoFeed(feed_type_variant="georss"), name='georss_feed'),
path('feed/geoatom/', GeoFeed(feed_type_variant="geoatom"), name='geoatom_feed'),
path('feed/w3cgeo/', GeoFeed(feed_type_variant="w3cgeo"), name='w3cgeo_feed'),
Expand Down
144 changes: 63 additions & 81 deletions publications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.views.decorators.cache import never_cache
from django.urls import reverse
import uuid
from django.utils.timezone import now
from django.utils.timezone import now, get_default_timezone
from datetime import datetime
import imaplib
import time
Expand All @@ -26,14 +26,12 @@
from publications.models import BlockedEmail, BlockedDomain, Subscription, UserProfile, Publication
from django.contrib.auth import get_user_model
User = get_user_model()

import tempfile, os
import fiona
from fiona.crs import from_epsg
from shapely import wkt
from shapely.geometry import mapping
from django.contrib.gis.geos import GEOSGeometry
from publications.tasks import regenerate_geojson_cache
from osgeo import ogr, osr

ogr.UseExceptions()
osr.UseExceptions()

LOGIN_TOKEN_LENGTH = 32
LOGIN_TOKEN_TIMEOUT_SECONDS = 10 * 60
Expand Down Expand Up @@ -83,47 +81,50 @@ def download_geojson(request):
return response

def generate_geopackage():
Comment thread
BharatVe marked this conversation as resolved.
"""
Generates a GeoPackage file from Publication data using Fiona.
Geometry is converted using Shapely. The file is written to a persistent
cache directory and its filename is returned.
"""
schema = {
'geometry': 'Unknown',
'properties': {
'title': 'str',
'abstract': 'str',
'doi': 'str',
'source': 'str',
},
}
cache_dir = os.path.join(tempfile.gettempdir(), "optimap_cache")
os.makedirs(cache_dir, exist_ok=True)
filename = os.path.join(cache_dir, "publications.gpkg")
with fiona.open(filename, 'w', driver='GPKG', schema=schema, crs=from_epsg(4326)) as collection:
for pub in Publication.objects.all():
geom = None
if pub.geometry:
try:
geom_wkt = pub.geometry.wkt if hasattr(pub.geometry, 'wkt') else str(pub.geometry)
if geom_wkt.startswith("SRID="):
geom_wkt = geom_wkt.split(";", 1)[1]
geom_obj = wkt.loads(geom_wkt)
geom = mapping(geom_obj)
except Exception as e:
logger.error("Error converting geometry for publication %s: %s", pub.id, e)
geom = None
feature = {
'geometry': geom,
'properties': {
'title': pub.title or "",
'abstract': pub.abstract or "",
'doi': pub.doi or "",
'source': pub.source or "",
},
}
collection.write(feature)
return filename
gpkg_path = os.path.join(cache_dir, "publications.gpkg")

# Delete old file so driver can re-create it
driver = ogr.GetDriverByName("GPKG")
if os.path.exists(gpkg_path):
driver.DeleteDataSource(gpkg_path)

# Create data source & layer
ds = driver.CreateDataSource(gpkg_path)
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
layer = ds.CreateLayer("publications", srs, ogr.wkbUnknown)

# Define fields
for name in ("title", "abstract", "doi", "source"):
field_defn = ogr.FieldDefn(name, ogr.OFTString)
field_defn.SetWidth(255)
layer.CreateField(field_defn)

# Populate features
layer_defn = layer.GetLayerDefn()
for pub in Publication.objects.all():
feat = ogr.Feature(layer_defn)
feat.SetField("title", pub.title or "")
feat.SetField("abstract", pub.abstract or "")
feat.SetField("doi", pub.doi or "")
feat.SetField("source", pub.source or "")

if pub.geometry:
# Use the GEOSGeometry WKB directly
wkb = pub.geometry.wkb # bytes
geom = ogr.CreateGeometryFromWkb(wkb)
geom.AssignSpatialReference(srs)
feat.SetGeometry(geom)

layer.CreateFeature(feat)
feat = None

# Clean up
ds = None
return gpkg_path


@require_GET
def download_geopackage(request):
Expand Down Expand Up @@ -203,21 +204,27 @@ def privacy(request):
def data(request):
cache_dir = os.path.join(tempfile.gettempdir(), "optimap_cache")
os.makedirs(cache_dir, exist_ok=True)
json_path = os.path.join(cache_dir, 'geojson_cache.json')
json_path = os.path.join(cache_dir, "geojson_cache.json")
if not os.path.exists(json_path):
json_path = regenerate_geojson_cache()
geojson_size = format_file_size(os.path.getsize(json_path))
last_updated = datetime.fromtimestamp(os.path.getmtime(json_path)).strftime('%Y-%m-%d %H:%M:%S')
logger.info("GeoJSON cache last updated at %s", last_updated)

# Build a timezone-aware datetime from the file’s modification time
mtime = os.path.getmtime(json_path)
tz = get_default_timezone()

last_updated = datetime.fromtimestamp(mtime, tz)
gpkg_filename = generate_geopackage()
geopackage_size = format_file_size(os.path.getsize(gpkg_filename)) if os.path.exists(gpkg_filename) else "0 B"
context = {
'geojson_size': geojson_size,
'geopackage_size': geopackage_size,
'last_updated': last_updated
}
return render(request, 'data.html', context)
if os.path.exists(gpkg_filename):
geopackage_size = format_file_size(os.path.getsize(gpkg_filename))
else:
geopackage_size = "0 B"

return render(request, "data.html", {
"geojson_size": geojson_size,
"geopackage_size": geopackage_size,
"last_updated": last_updated, # pass the aware datetime
})

def Confirmationlogin(request):
return render(request, 'confirmation_login.html')
Expand Down Expand Up @@ -505,31 +512,6 @@ def finalize_account_deletion(request):
del request.session[USER_DELETE_TOKEN_PREFIX]
request.session.modified = True

@require_GET
def download_geojson(request):
"""
Returns the cached GeoJSON file as a downloadable file.
"""
cache_dir = os.path.join(tempfile.gettempdir(), "optimap_cache")
os.makedirs(cache_dir, exist_ok=True)
json_path = os.path.join(cache_dir, 'geojson_cache.json')
if not os.path.exists(json_path):
json_path = regenerate_geojson_cache()
return FileResponse(open(json_path, 'rb'), content_type="application/json",
as_attachment=True, filename="publications.geojson")

# New Functionality: Download as GeoPackage using Fiona and Shapely
@require_GET
def download_geopackage(request):
"""
Generates a GeoPackage file from Publication data and returns it as a downloadable file.
"""
filename = generate_geopackage()
if not os.path.exists(filename):
return HttpResponse("Error generating GeoPackage.", status=500)
return FileResponse(open(filename, 'rb'), content_type="application/geopackage+sqlite3",
as_attachment=True, filename="publications.gpkg")

class RobotsView(View):
http_method_names = ['get']
def get(self, request):
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@ whitenoise==6.8.2
psycopg2-binary==2.9.10
packaging==21.3
pycryptodome==3.21.0
Fiona>=1.8.20
Shapely>=1.8.0
4 changes: 2 additions & 2 deletions tests/test_geo_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def test_data_endpoint(self):
response = self.client.get(reverse('optimap:data_and_api'))
self.assertEqual(response.status_code, 200, "Data endpoint should return status 200")
content = response.content.decode()
self.assertIn("Data dumps are recreated each night.", content,
"The data page should mention nightly recreation")
+ self.assertIn("Data dumps are recreated every 6 hours.", content,
"The data page should mention the six-hour schedule")
match = re.search(r'Last updated:\s*(\S+)', content)
self.assertIsNotNone(match, "Data page should display a Last updated timestamp")
self.assertTrue(match.group(1).strip() != "", "Last updated timestamp should not be empty")
Expand Down
Loading