Skip to content

Commit 2c5f234

Browse files
committed
Created a pipeline for translating coordinates from aalesund 110 to standard and creating world files (jgw) for all files in a dataset containing sos and jpgs
1 parent 7d9babe commit 2c5f234

File tree

3 files changed

+602
-61
lines changed

3 files changed

+602
-61
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
/datasets
2+
/dataset
3+
/dataset_with_jgw
24
*.pt
35
*.pth
46
.dev_secret
7+
temp
8+
dataset*
9+
temp*

sos_coordinate_extracting/sos_coordinate_extracting.ipynb

+51-61
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 33,
5+
"execution_count": 2,
66
"metadata": {},
77
"outputs": [
88
{
@@ -57,7 +57,7 @@
5757
},
5858
{
5959
"cell_type": "code",
60-
"execution_count": 12,
60+
"execution_count": 3,
6161
"metadata": {},
6262
"outputs": [
6363
{
@@ -66,7 +66,7 @@
6666
"('32bit', 'WindowsPE')"
6767
]
6868
},
69-
"execution_count": 12,
69+
"execution_count": 3,
7070
"metadata": {},
7171
"output_type": "execute_result"
7272
}
@@ -78,16 +78,18 @@
7878
},
7979
{
8080
"cell_type": "code",
81-
"execution_count": 34,
81+
"execution_count": 8,
8282
"metadata": {},
8383
"outputs": [
8484
{
85-
"name": "stdout",
86-
"output_type": "stream",
87-
"text": [
88-
"Coordinates transformed from Ålesund 110 to EUREF89-32N\n",
89-
"World file created: ../datasets/aalesund/1504200/200.jgw\n",
90-
"Image georeferencing parameters have been written.\n"
85+
"ename": "RuntimeError",
86+
"evalue": "Failed to init transformation",
87+
"output_type": "error",
88+
"traceback": [
89+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
90+
"\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)",
91+
"Cell \u001b[1;32mIn[8], line 50\u001b[0m\n\u001b[0;32m 48\u001b[0m _InitSkTrans(href, ctypes\u001b[38;5;241m.\u001b[39mbyref(sLen), ctypes\u001b[38;5;241m.\u001b[39mbyref(sErr))\n\u001b[0;32m 49\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m sErr\u001b[38;5;241m.\u001b[39mvalue \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m---> 50\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFailed to init transformation\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 52\u001b[0m \u001b[38;5;66;03m# Ålesund-lokal -> EUREF89-32N\u001b[39;00m\n\u001b[0;32m 53\u001b[0m \u001b[38;5;66;03m# According to given info:\u001b[39;00m\n\u001b[0;32m 54\u001b[0m \u001b[38;5;66;03m# slSys1=4; slZone1=1 for Ålesund-lokal\u001b[39;00m\n\u001b[0;32m 55\u001b[0m \u001b[38;5;66;03m# slSys2=7; slZone2=32 for EUREF89-32N\u001b[39;00m\n\u001b[0;32m 56\u001b[0m slSys1 \u001b[38;5;241m=\u001b[39m ctypes\u001b[38;5;241m.\u001b[39mc_short(\u001b[38;5;241m4\u001b[39m)\n",
92+
"\u001b[1;31mRuntimeError\u001b[0m: Failed to init transformation"
9193
]
9294
}
9395
],
@@ -113,8 +115,8 @@
113115
"# If koordsys is 110 (Ålesund), transform to EUREF89-32N using the DLL\n",
114116
"if koordsys == 110:\n",
115117
" # Load and initialize the transformation\n",
116-
" dll_path = r\"C:\\Users\\siver\\ScanAI\\sos_coordinate_extracting\\skt2_1507-1504_1.dll\"\n",
117-
" href_file = r\"C:\\Users\\siver\\ScanAI\\sos_coordinate_extracting\\HREF2018B_NN2000_EUREF89.bin\"\n",
118+
" dll_path = r\"C:\\Users\\SivertEspelandHusebø\\ScanAI\\sos_coordinate_extracting\\skt2_1507-1504_1.dll\"\n",
119+
" href_file = r\"C:\\Users\\SivertEspelandHusebø\\ScanAI\\sos_coordinate_extracting\\HREF2018B_NN2000_EUREF89.bin\"\n",
118120
" if not os.path.exists(dll_path):\n",
119121
" raise FileNotFoundError(\"DLL file not found.\")\n",
120122
" if not os.path.exists(href_file):\n",
@@ -181,7 +183,7 @@
181183
"else:\n",
182184
" print(\"Coordinate system is not Ålesund 110. Using coordinates as is.\")\n",
183185
"\n",
184-
"image_path = \"../datasets/aalesund/1504209/209.jpg\"\n",
186+
"image_path = \"../datasets/aalesund/1504200/200.jpg\"\n",
185187
"img = Image.open(image_path)\n",
186188
"width, height = img.size\n",
187189
"\n",
@@ -206,32 +208,28 @@
206208
},
207209
{
208210
"cell_type": "code",
209-
"execution_count": 1,
211+
"execution_count": 19,
210212
"metadata": {},
211213
"outputs": [
212214
{
213215
"name": "stdout",
214216
"output_type": "stream",
215217
"text": [
216218
"KOORDSYS: 110\n",
217-
"Extracted Coordinates: [(497980.0, 9179.0), (498909.0, 11036.0)]\n",
219+
"Extracted Coordinates: [(497197.0, 16073.0), (498310.0, 16976.0)]\n",
218220
"Sorted Coordinates:\n",
219-
"X_min: 497980.0, X_max: 498909.0\n",
220-
"Y_min: 9179.0, Y_max: 11036.0\n"
221-
]
222-
},
223-
{
224-
"ename": "OSError",
225-
"evalue": "[WinError 193] %1 is not a valid Win32 application",
226-
"output_type": "error",
227-
"traceback": [
228-
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
229-
"\u001b[1;31mOSError\u001b[0m Traceback (most recent call last)",
230-
"Cell \u001b[1;32mIn[1], line 232\u001b[0m\n\u001b[0;32m 229\u001b[0m create_world_file(image_path, easting_min, northing_min, easting_max, northing_max)\n\u001b[0;32m 231\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m--> 232\u001b[0m \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
231-
"Cell \u001b[1;32mIn[1], line 196\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m 194\u001b[0m \u001b[38;5;66;03m# Initialize transformation if KOORDSYS is 110 (Ålesund)\u001b[39;00m\n\u001b[0;32m 195\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m koordsys \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m110\u001b[39m:\n\u001b[1;32m--> 196\u001b[0m trans_dll, _InitSkTrans, _GeoTrans \u001b[38;5;241m=\u001b[39m \u001b[43minitialize_transformation\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdll_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhref_file\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 197\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTransformation DLL initialized successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 199\u001b[0m \u001b[38;5;66;03m# Define system and zone codes\u001b[39;00m\n",
232-
"Cell \u001b[1;32mIn[1], line 63\u001b[0m, in \u001b[0;36minitialize_transformation\u001b[1;34m(dll_path, href_file)\u001b[0m\n\u001b[0;32m 60\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mexists(href_file):\n\u001b[0;32m 61\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mFileNotFoundError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHREF file not found.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m---> 63\u001b[0m trans_dll \u001b[38;5;241m=\u001b[39m \u001b[43mctypes\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCDLL\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdll_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 65\u001b[0m \u001b[38;5;66;03m# Define argument and return types for _InitSkTrans\u001b[39;00m\n\u001b[0;32m 66\u001b[0m _InitSkTrans \u001b[38;5;241m=\u001b[39m trans_dll\u001b[38;5;241m.\u001b[39m_InitSkTrans\n",
233-
"File \u001b[1;32mc:\\Python312\\Lib\\ctypes\\__init__.py:379\u001b[0m, in \u001b[0;36mCDLL.__init__\u001b[1;34m(self, name, mode, handle, use_errno, use_last_error, winmode)\u001b[0m\n\u001b[0;32m 376\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_FuncPtr \u001b[38;5;241m=\u001b[39m _FuncPtr\n\u001b[0;32m 378\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m handle \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m--> 379\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_handle \u001b[38;5;241m=\u001b[39m \u001b[43m_dlopen\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmode\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 380\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 381\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_handle \u001b[38;5;241m=\u001b[39m handle\n",
234-
"\u001b[1;31mOSError\u001b[0m: [WinError 193] %1 is not a valid Win32 application"
221+
"X_min: 497197.0, X_max: 498310.0\n",
222+
"Y_min: 16073.0, Y_max: 16976.0\n",
223+
"Initializing transformation...\n",
224+
"Transformation DLL initialized successfully.\n",
225+
"Coordinates transformed from Ålesund 110 to EUREF89-32N\n",
226+
"Transformed Bottom-Left: (Easting: 364010.2971850267, Northing: 6928533.038357328)\n",
227+
"Transformed Top-Right: (Easting: 364962.85623642476, Northing: 6929603.448842814)\n",
228+
"Pixel Size X: 0.21083644342586796 meters/pixel\n",
229+
"Pixel Size Y: -0.19231234018801652 meters/pixel (negative for north-up)\n",
230+
"Top-Left Pixel Coordinates: (Easting: 364010.2971850267, Northing: 6929603.448842814)\n",
231+
"World file created: ../datasets/aalesund/1504201/201.jgw\n",
232+
"Image georeferencing parameters have been written.\n"
235233
]
236234
}
237235
],
@@ -285,46 +283,38 @@
285283
"def initialize_transformation(dll_path, href_file):\n",
286284
" \"\"\"\n",
287285
" Initializes the coordinate transformation using the provided DLL and HREF file.\n",
288-
"\n",
289-
" Args:\n",
290-
" dll_path (str): Path to the transformation DLL.\n",
291-
" href_file (str): Path to the HREF file.\n",
292-
"\n",
293-
" Returns:\n",
294-
" tuple: (trans_dll, _InitSkTrans, _GeoTrans)\n",
295286
" \"\"\"\n",
296287
" if not os.path.exists(dll_path):\n",
297288
" raise FileNotFoundError(\"DLL file not found.\")\n",
298289
" if not os.path.exists(href_file):\n",
299290
" raise FileNotFoundError(\"HREF file not found.\")\n",
300291
" \n",
301-
" trans_dll = ctypes.CDLL(dll_path)\n",
292+
" try:\n",
293+
" trans_dll = ctypes.CDLL(dll_path)\n",
294+
" except OSError as e:\n",
295+
" raise RuntimeError(f\"Failed to load DLL: {e}\")\n",
302296
"\n",
303-
" # Define argument and return types for _InitSkTrans\n",
297+
" # Define argument and return types\n",
304298
" _InitSkTrans = trans_dll._InitSkTrans\n",
305299
" _InitSkTrans.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_short), ctypes.POINTER(ctypes.c_short)]\n",
306300
" _InitSkTrans.restype = None\n",
307301
"\n",
308-
" # Define argument and return types for _GeoTrans\n",
309-
" _GeoTrans = trans_dll._GeoTrans\n",
310-
" _GeoTrans.argtypes = [\n",
311-
" ctypes.POINTER(ctypes.c_short), ctypes.POINTER(ctypes.c_short),\n",
312-
" ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double),\n",
313-
" ctypes.POINTER(ctypes.c_short), ctypes.POINTER(ctypes.c_short),\n",
314-
" ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double),\n",
315-
" ctypes.POINTER(ctypes.c_short)\n",
316-
" ]\n",
317-
" _GeoTrans.restype = None\n",
318-
"\n",
319-
" # Initialize the transformation\n",
320302
" href = href_file.encode('utf-8')\n",
321303
" sLen = ctypes.c_short(len(href))\n",
322304
" sErr = ctypes.c_short(0)\n",
323-
" _InitSkTrans(href, ctypes.byref(sLen), ctypes.byref(sErr))\n",
305+
"\n",
306+
" print(\"Initializing transformation...\")\n",
307+
" try:\n",
308+
" _InitSkTrans(href, ctypes.byref(sLen), ctypes.byref(sErr))\n",
309+
" except Exception as e:\n",
310+
" print(f\"Error during initialization: {e}\")\n",
311+
" \n",
324312
" if sErr.value != 0:\n",
325-
" raise RuntimeError(\"Failed to initialize transformation.\")\n",
313+
" print(f\"sErr value: {sErr.value}\") # Log the error code\n",
314+
" raise RuntimeError(f\"Failed to initialize transformation. Error Code: {sErr.value}\")\n",
315+
"\n",
316+
" return trans_dll, _InitSkTrans, trans_dll._GeoTrans\n",
326317
"\n",
327-
" return trans_dll, _InitSkTrans, _GeoTrans\n",
328318
"\n",
329319
"def transform_aalesund_to_euref(x, y, _GeoTrans, slSys1, slZone1, slSys2, slZone2):\n",
330320
" \"\"\"\n",
@@ -359,7 +349,7 @@
359349
" )\n",
360350
"\n",
361351
" if slErr.value != 0:\n",
362-
" raise RuntimeError(\"Coordinate transformation failed.\")\n",
352+
" raise RuntimeError(f\"Failed to initialize transformation. Error Code: {sErr.value}\")\n",
363353
"\n",
364354
" # Swap to return (Easting, Northing) if DLL returns (Northing, Easting)\n",
365355
" return y2.value, x2.value\n",
@@ -406,10 +396,10 @@
406396
"\n",
407397
"def main():\n",
408398
" # Define paths\n",
409-
" sos_path = \"../datasets/aalesund/1504209/209.sos\"\n",
410-
" image_path = \"../datasets/aalesund/1504209/209.jpg\"\n",
411-
" dll_path = r\"C:\\Users\\siver\\ScanAI\\sos_coordinate_extracting\\skt2_1507-1504_1.dll\"\n",
412-
" href_file = r\"C:\\Users\\siver\\ScanAI\\sos_coordinate_extracting\\HREF2018B_NN2000_EUREF89.bin\"\n",
399+
" sos_path = \"../datasets/aalesund/1504201/201.sos\"\n",
400+
" image_path = \"../datasets/aalesund/1504201/201.jpg\"\n",
401+
" dll_path = r\"skt2_1507-1504_1.dll\"\n",
402+
" href_file = r\"HREF2018B_NN2000_EUREF89.bin\"\n",
413403
"\n",
414404
" # Parse the SOS file\n",
415405
" koordsys, coords = parse_sos_file(sos_path)\n",
@@ -480,7 +470,7 @@
480470
],
481471
"metadata": {
482472
"kernelspec": {
483-
"display_name": "Python 3.12 (32-bit)",
473+
"display_name": "Python 3.12 (32bit)",
484474
"language": "python",
485475
"name": "py32"
486476
},

0 commit comments

Comments
 (0)