File size: 10,075 Bytes
795da8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "3fbb8cd1-679c-4f8a-8889-43ed516bfedd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Collecting aiofiles\n",
" Obtaining dependency information for aiofiles from https://files.pythonhosted.org/packages/c5/19/5af6804c4cc0fed83f47bff6e413a98a36618e7d40185cd36e69737f3b0e/aiofiles-23.2.1-py3-none-any.whl.metadata\n",
" Downloading aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)\n",
"Collecting jsonlines\n",
" Obtaining dependency information for jsonlines from https://files.pythonhosted.org/packages/f8/62/d9ba6323b9202dd2fe166beab8a86d29465c41a0288cbe229fac60c1ab8d/jsonlines-4.0.0-py3-none-any.whl.metadata\n",
" Downloading jsonlines-4.0.0-py3-none-any.whl.metadata (1.6 kB)\n",
"Requirement already satisfied: attrs>=19.2.0 in /home/appuser/.local/lib/python3.11/site-packages (from jsonlines) (23.1.0)\n",
"Downloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
"Downloading jsonlines-4.0.0-py3-none-any.whl (8.7 kB)\n",
"Installing collected packages: jsonlines, aiofiles\n",
"Successfully installed aiofiles-23.2.1 jsonlines-4.0.0\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"!pip install aiofiles"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5c72c2e7-d825-4a9f-a179-fbbe07c93000",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1684"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"from glob import glob\n",
"import aiofiles\n",
"\n",
"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": 2,
"id": "f6fa4495-d6a3-470f-9a79-3838f0e38794",
"metadata": {},
"outputs": [],
"source": [
"# import json\n",
"\n",
"# decodeerrors = {}\n",
"# deleted = []\n",
"# nocontent = []\n",
"# with_metadata = {}\n",
"\n",
"# async def layers_with_metadata():\n",
"# async def _check(f):\n",
"# delete = False\n",
"# async with aiofiles.open(f, 'r') as infile:\n",
"# content = await infile.read()\n",
"# if content:\n",
"# data = json.loads(content)\n",
"# if 'metadata' not in data:\n",
"# delete = True\n",
"# else:\n",
"# with_metadata[f] = data\n",
"# else:\n",
"# nocontent.append(f)\n",
"# delete = True\n",
"# if delete:\n",
"# deleted.append(f)\n",
"# os.remove(f)\n",
"# return await asyncio.gather(*[_check(f) for f in jsonfiles])\n",
"\n",
"# import asyncio\n",
"# import nest_asyncio\n",
"\n",
"# nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "34514f76-bf06-4793-8085-56dc266c6d04",
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"import json\n",
"\n",
"async def read_data():\n",
" async def _read(f):\n",
" async with aiofiles.open(f, 'r') as infile:\n",
" content = await infile.read()\n",
" return json.loads(content)\n",
" tasks = [_read(f) for f in jsonfiles]\n",
" return await asyncio.gather(*tasks)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2879f4d6-dfc6-42f7-ac33-25457c62c204",
"metadata": {},
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "1b8c5363-774d-4cac-8cb5-a8746f2d113c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2min 41s, sys: 23 s, total: 3min 4s\n",
"Wall time: 2min 50s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"data = asyncio.run(read_data())"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "dee1ba14-ba44-435f-abc1-91f6cbee6153",
"metadata": {},
"outputs": [],
"source": [
"docs = {\n",
" \"servers\": [],\n",
" \"services\": [],\n",
" \"layers\": [],\n",
"}\n",
"\n",
"for server in data:\n",
" server_md = server['metadata']\n",
" server_url = server_md['url']\n",
" server_hash = hash(server_md['url'])\n",
" server_md['hash'] = server_hash\n",
" for a in ('folders', 'services'):\n",
" if a in server_md:\n",
" del server_md[a]\n",
" docs[\"servers\"].append(server_md)\n",
"\n",
" for service in server['services']:\n",
" service_md = service['metadata']\n",
" service_name = service_md['name'] = service['name']\n",
" service_url = service_md['url'] = service['url']\n",
" service_md['server_url'] = server_url\n",
" service_hash = hash(service_url)\n",
" service_md['hash'] = service_hash\n",
" service_md['server_hash'] = server_hash\n",
" layers = service_md['layers']\n",
" del service_md['layers']\n",
" docs['services'].append(service_md)\n",
"\n",
" for layer_md in layers:\n",
" layer_md['server_url'] = server_url\n",
" layer_md['service_name'] = service_name\n",
" layer_md['service_url'] = service_url\n",
" layer_hash = hash(layer_md['url'])\n",
" layer_md['hash'] = layer_hash\n",
" layer_md['server_hash'] = server_hash\n",
" layer_md['service_hash'] = service_hash\n",
" \n",
" docs['layers'].append(layer_md)"
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "072b1b2e-1309-4e9e-96b0-30c665706e90",
"metadata": {},
"outputs": [],
"source": [
"layerdict = {}\n",
"for l in docs['layers']:\n",
" if 'type' in l:\n",
" if l['type'] not in layerdict:\n",
" layerdict[l['type']] = []\n",
" layerdict[l['type']].append(l)"
]
},
{
"cell_type": "code",
"execution_count": 125,
"id": "5824e617-1211-4642-9714-3b5be74885d5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['Raster Layer', 'Group Layer', 'Feature Layer', 'Annotation Layer', 'Annotation SubLayer', 'Mosaic Layer', 'Raster Catalog Layer', 'Terrain Layer', 'Network Analysis Layer', 'Network Dataset Layer', 'Parcel Layer', 'Topology Layer', 'Mosiac Layer', 'Image Service Layer', 'Annotation Sublayer', 'Dimension Layer', 'Tin Layer', 'Table'])"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layerdict.keys()"
]
},
{
"cell_type": "code",
"execution_count": 163,
"id": "30b154d9-5f90-41c6-a004-f19dcd00a132",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 37 ms, sys: 1.28 ms, total: 38.3 ms\n",
"Wall time: 51.8 ms\n"
]
}
],
"source": [
"%%time\n",
"\n",
"server_df = pd.DataFrame(docs['servers']).astype(str).apply(lambda s: s.str.strip().replace('', None)).dropna(axis=1, how='all').astype(str)\n",
"server_df.dropna(axis=1, how='all').to_parquet('servers.parquet')"
]
},
{
"cell_type": "code",
"execution_count": 164,
"id": "10cbce4a-8184-48ec-8bae-6bb3b8395ddf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 34.3 s, sys: 1.94 s, total: 36.3 s\n",
"Wall time: 37.3 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"services_df = pd.DataFrame(docs['services']).astype(str).apply(lambda s: s.str.strip().replace('', None)).dropna(axis=1, how='all').astype(str)\n",
"services_df.astype(str).to_parquet('services.parquet')"
]
},
{
"cell_type": "code",
"execution_count": 158,
"id": "44e8267c-b07d-4b0d-93f3-662fb6659776",
"metadata": {},
"outputs": [],
"source": [
"layer_df = pd.DataFrame(docs['layers'])"
]
},
{
"cell_type": "code",
"execution_count": 165,
"id": "dc247c57-3e17-4cb4-b69b-71ee052fa0ee",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2min 36s, sys: 25.7 s, total: 3min 1s\n",
"Wall time: 3min 10s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"layer_df.astype(str).apply(lambda s: s.str.strip().replace('', None)).dropna(axis=1, how='all').astype(str).to_parquet('layers.parquet')"
]
}
],
"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
}
|