joshuasundance
commited on
Commit
•
3a9b866
1
Parent(s):
cc8c26e
mongodb
Browse files- govgis-nov2023-mongodb.gz +3 -0
- to_mongodb.ipynb +209 -0
govgis-nov2023-mongodb.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c603abb7838d15686f3f8c4e6f210c1af2f81981ee3e2767c119f05dbc16983a
|
3 |
+
size 1129192237
|
to_mongodb.ipynb
ADDED
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "14762a5e-0193-4de6-ac54-549b0309dd2c",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import asyncio\n",
|
11 |
+
"import json\n",
|
12 |
+
"import os\n",
|
13 |
+
"import random\n",
|
14 |
+
"import uuid\n",
|
15 |
+
"from glob import glob\n",
|
16 |
+
"\n",
|
17 |
+
"import aiofiles\n",
|
18 |
+
"import nest_asyncio\n",
|
19 |
+
"from pymongo import MongoClient"
|
20 |
+
]
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"cell_type": "code",
|
24 |
+
"execution_count": null,
|
25 |
+
"id": "a93723c1-0972-4f52-ae23-8e44e7861230",
|
26 |
+
"metadata": {},
|
27 |
+
"outputs": [],
|
28 |
+
"source": [
|
29 |
+
"nest_asyncio.apply()"
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": null,
|
35 |
+
"id": "12db86f4-0321-4f87-be6e-0088fe7aac3e",
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [],
|
38 |
+
"source": [
|
39 |
+
"output_dir = os.path.abspath(\"./output_tryagain\")\n",
|
40 |
+
"\n",
|
41 |
+
"jsonfiles = glob(os.path.join(output_dir, \"*.json\"))\n",
|
42 |
+
"\n",
|
43 |
+
"len(jsonfiles)"
|
44 |
+
]
|
45 |
+
},
|
46 |
+
{
|
47 |
+
"cell_type": "code",
|
48 |
+
"execution_count": null,
|
49 |
+
"id": "7ee6e661-9d22-4635-a912-92a15d21b5f0",
|
50 |
+
"metadata": {},
|
51 |
+
"outputs": [],
|
52 |
+
"source": [
|
53 |
+
"client = MongoClient(r'mongodb://root:example@mongo:27017/')\n",
|
54 |
+
"\n",
|
55 |
+
"db = client['govgis-nov2023']\n",
|
56 |
+
"\n",
|
57 |
+
"services_collection = db.services\n",
|
58 |
+
"\n",
|
59 |
+
"layers_collection = db.layers"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": null,
|
65 |
+
"id": "5baaccb7-ef62-4d0c-b936-b6ef70befc44",
|
66 |
+
"metadata": {},
|
67 |
+
"outputs": [],
|
68 |
+
"source": [
|
69 |
+
"errors = []\n",
|
70 |
+
"\n",
|
71 |
+
"\n",
|
72 |
+
"def create_uuid(input_str: str) -> str:\n",
|
73 |
+
" # Consistent random UUIDs based on input string\n",
|
74 |
+
" # https://nathanielknight.ca/articles/consistent_random_uuids_in_python.html\n",
|
75 |
+
" random.seed(input_str)\n",
|
76 |
+
" return str(uuid.UUID(bytes=bytes(random.getrandbits(8) for _ in range(16)), version=4))\n",
|
77 |
+
"\n",
|
78 |
+
"\n",
|
79 |
+
"async def read_data():\n",
|
80 |
+
" # Async generator to yield file content one by one\n",
|
81 |
+
" for f in jsonfiles:\n",
|
82 |
+
" async with aiofiles.open(f, 'r') as infile:\n",
|
83 |
+
" content = await infile.read()\n",
|
84 |
+
" yield json.loads(content)\n",
|
85 |
+
"\n",
|
86 |
+
"\n",
|
87 |
+
"def process_metadata(metadata, additional_fields={}):\n",
|
88 |
+
" # Process metadata and add any additional fields\n",
|
89 |
+
" processed_md = {k: v for k, v in metadata.items() if k not in ['folders', 'services', 'layers']}\n",
|
90 |
+
" processed_md.update(additional_fields)\n",
|
91 |
+
" processed_md[\"original_id\"] = processed_md.get(\"id\", None)\n",
|
92 |
+
" processed_md[\"id\"] = processed_md[\"hash\"]\n",
|
93 |
+
" del processed_md[\"hash\"]\n",
|
94 |
+
"\n",
|
95 |
+
" return processed_md\n",
|
96 |
+
"\n",
|
97 |
+
"\n",
|
98 |
+
"def get_type(layer: dict) -> str:\n",
|
99 |
+
" return layer.get(\"type\", \"unknown\").lower().replace(\" \", \"_\").strip()\n",
|
100 |
+
"\n",
|
101 |
+
"\n",
|
102 |
+
"async def main():\n",
|
103 |
+
" async for server in read_data():\n",
|
104 |
+
" server_md = process_metadata(server[\"metadata\"],\n",
|
105 |
+
" {\"url\": server['metadata']['url'], \"hash\": create_uuid(server['metadata']['url'])})\n",
|
106 |
+
" for service in server['services']:\n",
|
107 |
+
" service_md = process_metadata(service[\"metadata\"],\n",
|
108 |
+
" {\"url\": service['url'], \"hash\": create_uuid(service['url']),\n",
|
109 |
+
" \"server\": server_md})\n",
|
110 |
+
" service_md['layers'] = []\n",
|
111 |
+
"\n",
|
112 |
+
" layer_dict = {}\n",
|
113 |
+
"\n",
|
114 |
+
" for layer in service['metadata']['layers']:\n",
|
115 |
+
" layer_md = process_metadata(layer, {\"url\": layer['url'], \"hash\": create_uuid(layer['url']),\n",
|
116 |
+
" \"service\": service_md[\"id\"]})\n",
|
117 |
+
" layer_type = get_type(layer)\n",
|
118 |
+
" service_md['layers'].append(dict(type=layer_type, layer_id=layer_md['id']))\n",
|
119 |
+
" if layer_type not in layer_dict:\n",
|
120 |
+
" layer_dict[layer_type] = []\n",
|
121 |
+
" layer_dict[layer_type].append(layer_md)\n",
|
122 |
+
"\n",
|
123 |
+
" if len(service_md) > 0:\n",
|
124 |
+
" services_collection.insert_one(service_md)\n",
|
125 |
+
"\n",
|
126 |
+
" for k, layers in layer_dict.items():\n",
|
127 |
+
" if len(layers) > 0:\n",
|
128 |
+
" try:\n",
|
129 |
+
" db[k].insert_many(layers)\n",
|
130 |
+
" except OverflowError:\n",
|
131 |
+
" for layer in layers:\n",
|
132 |
+
" try:\n",
|
133 |
+
" db[k].insert_one(layer)\n",
|
134 |
+
" except OverflowError:\n",
|
135 |
+
" for c in ['drawingInfo', 'classBreakInfos']:\n",
|
136 |
+
" if c in layer:\n",
|
137 |
+
" del layer[c]\n",
|
138 |
+
" try:\n",
|
139 |
+
" db[k].insert_one(layer)\n",
|
140 |
+
" except OverflowError:\n",
|
141 |
+
" errors.append(layer)"
|
142 |
+
]
|
143 |
+
},
|
144 |
+
{
|
145 |
+
"cell_type": "code",
|
146 |
+
"execution_count": 64,
|
147 |
+
"id": "67190800-bbfd-4bd3-a616-ec6b57050c96",
|
148 |
+
"metadata": {},
|
149 |
+
"outputs": [
|
150 |
+
{
|
151 |
+
"name": "stdout",
|
152 |
+
"output_type": "stream",
|
153 |
+
"text": [
|
154 |
+
"CPU times: user 4min 22s, sys: 25.6 s, total: 4min 48s\n",
|
155 |
+
"Wall time: 6min 25s\n"
|
156 |
+
]
|
157 |
+
}
|
158 |
+
],
|
159 |
+
"source": [
|
160 |
+
"%%time\n",
|
161 |
+
"\n",
|
162 |
+
"# Run the async main function\n",
|
163 |
+
"asyncio.run(main())"
|
164 |
+
]
|
165 |
+
},
|
166 |
+
{
|
167 |
+
"cell_type": "code",
|
168 |
+
"execution_count": 65,
|
169 |
+
"id": "56bc67d5-fd8d-4dda-9939-111ce484bb3f",
|
170 |
+
"metadata": {},
|
171 |
+
"outputs": [
|
172 |
+
{
|
173 |
+
"data": {
|
174 |
+
"text/plain": [
|
175 |
+
"0"
|
176 |
+
]
|
177 |
+
},
|
178 |
+
"execution_count": 65,
|
179 |
+
"metadata": {},
|
180 |
+
"output_type": "execute_result"
|
181 |
+
}
|
182 |
+
],
|
183 |
+
"source": [
|
184 |
+
"len(errors)"
|
185 |
+
]
|
186 |
+
}
|
187 |
+
],
|
188 |
+
"metadata": {
|
189 |
+
"kernelspec": {
|
190 |
+
"display_name": "Python 3 (ipykernel)",
|
191 |
+
"language": "python",
|
192 |
+
"name": "python3"
|
193 |
+
},
|
194 |
+
"language_info": {
|
195 |
+
"codemirror_mode": {
|
196 |
+
"name": "ipython",
|
197 |
+
"version": 3
|
198 |
+
},
|
199 |
+
"file_extension": ".py",
|
200 |
+
"mimetype": "text/x-python",
|
201 |
+
"name": "python",
|
202 |
+
"nbconvert_exporter": "python",
|
203 |
+
"pygments_lexer": "ipython3",
|
204 |
+
"version": "3.11.6"
|
205 |
+
}
|
206 |
+
},
|
207 |
+
"nbformat": 4,
|
208 |
+
"nbformat_minor": 5
|
209 |
+
}
|