Posts

Showing posts from January, 2022

Extract PDF content (from CHP) with Python

import re import sys import urllib.request import pdfplumber import pandas as pd if (len(sys.argv) < 2):     print("\n\nSyntax: python extract_pdf.py date_string (e.g. 20220124) \n\n")     exit() def main():         date_string  = sys.argv[1] # e.g. 20220124     pdf_file_name = f"ctn_{date_string}.pdf" # PDF from CHP: https://www.chp.gov.hk/files/pdf/ctn_20220124.pdf     #download_pdf( pdf_file_name )     extract_pdf( pdf_file_name ) def extract_pdf( pdf_file_name ):     with pdfplumber.open( f"./pdf/{pdf_file_name}" ) as pdf:         for page in pdf.pages:             #print(page)             for table in page.extract_tables():                 df = pd.DataFrame(table[1:], columns=table[0])                 for index, row in df.iterrows():...

Display cx_oracle error indicating which rows are affected

cursor.executemany("SQL EXEC STATEMENT", data, batcherrors=True ) for error in cursor.getbatcherrors() :     print("Error", error.message, "at row offset", error.offset) 

Calculate the viewing location in Cesium

 Here I summarise two approach for calculating the viewing location in Cesium viewer: Method 1 (using Camera.computeViewRectangle() method)  var projection = new Cesium.WebMercatorProjection(); var rect = new Cesium.Rectangle() viewer.camera.computeViewRectangle(projection.ellipsoid, rect) let rect_x = (rect.east - rect.west)/2 + rect.west; let rect_y = (rect.north - rect.south)/2 + rect.south; var rect_lon = Cesium.Math.toDegrees(rect_y); var rect_lat = Cesium.Math.toDegrees(rect_x); console.log(rect_lat, rect_lon) Note: The computeViewRectangle( ) could be very large when the view pitch is very flat, resulting in a very far point being calculated. Method 2 (using  Camera.computeViewRectangle()  method)  var cc = document.getElementById("cesiumContainer"); var screenCenterPt = new Cesium.Cartesian2(cc.offsetWidth/2, cc.offsetHeight/2); var pick = viewer.scene.globe.pick(viewer.camera.getPickRay(screenCenterPt), viewer.scene); if (pick) {   var geoPt = vie...

DOS batch file: Expand * into list of filenames in command line

expand-wildcard.bat @echo off setlocal EnableDelayedExpansion set params=command for %%a in ("%~1") do (     set params=!params! %%a ) echo %params% %params%   Then, running the following command: C:\>expand-wildcard filename* Will be executed as following: C:\>command filename1 filename2 filename3 filename4 ..