Skip to content

Instantly share code, notes, and snippets.

@wongcht
wongcht / gdalcopyproj.py
Last active December 28, 2024 16:40
gdalcopyproj.py for Python 3 and Rasters varying resolution
#!/usr/bin/env python
#******************************************************************************
# $Id$
#
# Name: gdalcopyproj.py
# Project: GDAL Python Interface
# Purpose: Duplicate the geotransform and projection metadata from
# one raster dataset to another, which can be useful after
# performing image manipulations with other software that
# ignores or discards georeferencing metadata.
@wongcht
wongcht / spot_height_gml_to_geojson.py
Created May 4, 2022 10:58
Conversion of GML to GeoJSON for HK Spot Height Data
import glob
import json
from lxml.etree import XMLParser, parse
parser = XMLParser(huge_tree=True)
namespaces = {
"gml": "http://www.opengis.net/gml",
"fme": "http://www.safe.com/gml/fme"}
@wongcht
wongcht / pathfinding_HK_road_network.py
Created May 2, 2022 22:55
Simple Pathfinding algorithm by NetworkX, using HK Road Network Data
from lxml.etree import XMLParser, parse
import networkx as nx
# from collections import defaultdict
DATA_DIRECTORY = ""
i_path = f"{DATA_DIRECTORY}/INTERSECTION.gml"
cl_path = f"{DATA_DIRECTORY}/CENTERLINE.gml"
parser = XMLParser(huge_tree=True)
# use lxml to parse GML
from lxml.etree import XMLParser, parse
PLATE_LINE_PATH = "DTAD_DS_PLATE_LINE.gml" # file path of the direction plate GML
SCALE = 100 # scale for SVG
# namespaces for easier xml data extraction
namespaces = {"core": "http://www.opengis.net/citygml/2.0",
"gen": "http://www.opengis.net/citygml/generics/2.0",
"gml": "http://www.opengis.net/gml"}
@wongcht
wongcht / geojsonPathGeneration.cs
Created April 28, 2022 03:26
Unity 3D Path Generation from 2D GeoJSON file
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json; // This has to be installed to parse json
// the class name should be renamed accordingly
public class PathGenerate : MonoBehaviour
{
// Make a field in Unity Editor to select the geojson file
@wongcht
wongcht / 3d_spatial_data_index.py
Created April 23, 2022 23:36
LandsD 1:1000 Map Sheet Index Query in 2D-list
import csv
# largest grid (1-19) east: 15000m, north: 12000m
# start_coord = (800000, 848000)
# Calculation of starting HK80 Coordinates with known map sheet index
# Divide 4 parts, first part is base coordinate, part2-4 are addition of E/N coordinates
# Each part with value checking
def part1(a):
@wongcht
wongcht / 3d_map_download.py
Created April 20, 2022 22:43
Batch Download 3D Visualisation Map from Hong Kong Lands Department
from urllib.parse import urlparse
import urllib.request
from pathlib import Path
# [1-17] [NW/NE/SW/SE] [1-25] [A/B/C/D] [1-20]
for i in range(1,18):
for j in ['NW', 'NE', 'SW', 'SE']:
for k in range(1, 26):
for l in ['A', 'B', 'C', 'D']:
for m in range(1, 21):