Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 34 additions & 5 deletions ohmg/extensions/atlascope.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import json

from django.conf import settings
import topojson
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon
from ninja import Schema

from ..core.models import LayerSet
from ..places.models import Place


def generate_atlascope_properties(layerset: LayerSet):
return {
"identifier": layerset.map.identifier,
"publisherShort": "Sanborn",
"publisherShort": layerset.map.publisher
if layerset.map.publisher
else layerset.map.creator,
"year": layerset.map.year,
"bibliographicEntry": "_Richards standard atlas of the town of Greenfield_ (Richards Map Company, 1918)",
"bibliographicEntry": f"Fire Insurance Map of {layerset.map.title} (Sanborn Map Company)",
"source": {
"type": "tilejson",
"url": f"{settings.SITEURL.rstrip('/')}/map/{layerset.map.identifier}/{layerset.category.slug}/tilejson",
"type": "xyz",
"url": f"{layerset.xyz_tiles_url}/{{z}}/{{x}}/{{y}}.png",
},
"catalogPermalink": f"https://loc.gov/item/{layerset.map.identifier}",
"heldBy": ["Library of Congress"],
Expand All @@ -36,6 +39,32 @@ def generate_atlascope_geometry(layerset: LayerSet):
return {"type": "MultiPolygon", "coordinates": []}


def generate_atlascope_footprints(place: Place, override_data_name: str = None):
maps = sorted(place.map_set.all().exclude(hidden=True), key=lambda x: x.year)

features = []
for map in maps:
ls = map.get_layerset("main-content")
if ls and ls.xyz_tiles_url:
feature = AtlascopeLayersetFeature.from_orm(ls).dict()
features.append(feature)

topo = topojson.Topology(
{
"type": "FeatureCollection",
"features": features,
}
)
topo_json = json.loads(topo.to_json())

## allow override of the data property name in the topojson
if override_data_name:
topo_json["objects"][override_data_name] = topo_json["objects"]["data"]
del topo_json["objects"]["data"]

return topo_json


class AtlascopeLayersetFeature(Schema):
type: str = "Feature"
properties: dict
Expand Down
1 change: 1 addition & 0 deletions ohmg/extensions/loc_sanborn.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def parse(self):
"identifier": identifier,
"title": parsed_item.title,
"creator": "Sanborn Map Company",
"publisher": "Sanborn Map Company",
"year": parsed_item.year,
"month": parsed_item.month,
"locale": self.input_data["locale"],
Expand Down
46 changes: 16 additions & 30 deletions ohmg/extensions/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import json

import topojson
from django.http import JsonResponse
from django.views import View

from ohmg.conf.http import JsonResponseNotFound
from ohmg.core.models import Map
from ohmg.core.utils import full_reverse

from .atlascope import AtlascopeLayersetFeature
from .atlascope import generate_atlascope_footprints
from .iiif import IIIFResource


Expand Down Expand Up @@ -53,29 +50,18 @@ def get(self, request, mapid, layerset_category):

class AtlascopeDataView(View):
def get(self, request, place, operation):
if operation == "footprints":
maps = sorted(place.map_set.all().exclude(hidden=True), key=lambda x: x.year)
ls = [i.get_layerset("main-content") for i in maps]

features = [
AtlascopeLayersetFeature.from_orm(i).dict() for i in ls if i and i.mosaic_geotiff
]

feature_collection = {
"type": "FeatureCollection",
"name": f"{place.slug}-volume-extents",
"features": features,
}
topo = topojson.Topology(feature_collection)
topo_json = json.loads(topo.to_json())

## extra key needed for atlascope detroit
if place.slug == "detroit-mi":
topo_json["objects"]["detroit-volume-extents"] = topo_json["objects"]["data"]
return JsonResponse(topo_json)

elif operation == "coverages":
return JsonResponse([{"name": str(place), "center": place.get_center()}], safe=False)

else:
return JsonResponseNotFound("invalid operation. must be 'footprints' or 'coverages'")
data_name = request.GET.get("data-name")
match operation:
case "footprints":
topo_json = generate_atlascope_footprints(place, override_data_name=data_name)
return JsonResponse(topo_json)

case "coverages":
return JsonResponse(
[{"name": str(place), "center": place.get_center()}], safe=False
)

case _:
return JsonResponseNotFound(
"invalid operation. must be 'footprints' or 'coverages'"
)
Loading