{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "14762a5e-0193-4de6-ac54-549b0309dd2c", "metadata": {}, "outputs": [], "source": [ "import asyncio\n", "import json\n", "import os\n", "import random\n", "import uuid\n", "from glob import glob\n", "\n", "import aiofiles\n", "import nest_asyncio\n", "from pymongo import MongoClient" ] }, { "cell_type": "code", "execution_count": null, "id": "a93723c1-0972-4f52-ae23-8e44e7861230", "metadata": {}, "outputs": [], "source": [ "nest_asyncio.apply()" ] }, { "cell_type": "code", "execution_count": null, "id": "12db86f4-0321-4f87-be6e-0088fe7aac3e", "metadata": {}, "outputs": [], "source": [ "output_dir = os.path.abspath(\"./output_tryagain\")\n", "\n", "jsonfiles = glob(os.path.join(output_dir, \"*.json\"))\n", "\n", "len(jsonfiles)" ] }, { "cell_type": "code", "execution_count": null, "id": "7ee6e661-9d22-4635-a912-92a15d21b5f0", "metadata": {}, "outputs": [], "source": [ "client = MongoClient(r'mongodb://root:example@mongo:27017/')\n", "\n", "db = client['govgis-nov2023']\n", "\n", "services_collection = db.services\n", "\n", "layers_collection = db.layers" ] }, { "cell_type": "code", "execution_count": null, "id": "5baaccb7-ef62-4d0c-b936-b6ef70befc44", "metadata": {}, "outputs": [], "source": [ "errors = []\n", "\n", "\n", "def create_uuid(input_str: str) -> str:\n", " # Consistent random UUIDs based on input string\n", " # https://nathanielknight.ca/articles/consistent_random_uuids_in_python.html\n", " random.seed(input_str)\n", " return str(uuid.UUID(bytes=bytes(random.getrandbits(8) for _ in range(16)), version=4))\n", "\n", "\n", "async def read_data():\n", " # Async generator to yield file content one by one\n", " for f in jsonfiles:\n", " async with aiofiles.open(f, 'r') as infile:\n", " content = await infile.read()\n", " yield json.loads(content)\n", "\n", "\n", "def process_metadata(metadata, additional_fields={}):\n", " # Process metadata and add any additional fields\n", " processed_md = {k: v for k, v in metadata.items() if k not in ['folders', 'services', 'layers']}\n", " processed_md.update(additional_fields)\n", " processed_md[\"original_id\"] = processed_md.get(\"id\", None)\n", " processed_md[\"id\"] = processed_md[\"hash\"]\n", " del processed_md[\"hash\"]\n", "\n", " return processed_md\n", "\n", "\n", "def get_type(layer: dict) -> str:\n", " return layer.get(\"type\", \"unknown\").lower().replace(\" \", \"_\").strip()\n", "\n", "\n", "async def main():\n", " async for server in read_data():\n", " server_md = process_metadata(server[\"metadata\"],\n", " {\"url\": server['metadata']['url'], \"hash\": create_uuid(server['metadata']['url'])})\n", " for service in server['services']:\n", " service_md = process_metadata(service[\"metadata\"],\n", " {\"url\": service['url'], \"hash\": create_uuid(service['url']),\n", " \"server\": server_md})\n", " service_md['layers'] = []\n", "\n", " layer_dict = {}\n", "\n", " for layer in service['metadata']['layers']:\n", " layer_md = process_metadata(layer, {\"url\": layer['url'], \"hash\": create_uuid(layer['url']),\n", " \"service\": service_md[\"id\"]})\n", " layer_type = get_type(layer)\n", " service_md['layers'].append(dict(type=layer_type, layer_id=layer_md['id']))\n", " if layer_type not in layer_dict:\n", " layer_dict[layer_type] = []\n", " layer_dict[layer_type].append(layer_md)\n", "\n", " if len(service_md) > 0:\n", " services_collection.insert_one(service_md)\n", "\n", " for k, layers in layer_dict.items():\n", " if len(layers) > 0:\n", " try:\n", " db[k].insert_many(layers)\n", " except OverflowError:\n", " for layer in layers:\n", " try:\n", " db[k].insert_one(layer)\n", " except OverflowError:\n", " for c in ['drawingInfo', 'classBreakInfos']:\n", " if c in layer:\n", " del layer[c]\n", " try:\n", " db[k].insert_one(layer)\n", " except OverflowError:\n", " errors.append(layer)" ] }, { "cell_type": "code", "execution_count": 64, "id": "67190800-bbfd-4bd3-a616-ec6b57050c96", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4min 22s, sys: 25.6 s, total: 4min 48s\n", "Wall time: 6min 25s\n" ] } ], "source": [ "%%time\n", "\n", "# Run the async main function\n", "asyncio.run(main())" ] }, { "cell_type": "code", "execution_count": 65, "id": "56bc67d5-fd8d-4dda-9939-111ce484bb3f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(errors)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }