|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +uniffi_example.py |
| 4 | +
|
| 5 | +Example of using the UniFFI-generated Python bindings for the RDP library. |
| 6 | +This demonstrates the simplified interface compared to the manual ctypes FFI. |
| 7 | +
|
| 8 | +To run: |
| 9 | +1. Build the library with Python bindings: cargo build --release --features python-bindings |
| 10 | +2. Ensure the generated Python module is in your path |
| 11 | +3. Run this script: python examples/uniffi_example.py |
| 12 | +""" |
| 13 | + |
| 14 | +import os |
| 15 | +import sys |
| 16 | +from enum import Enum |
| 17 | + |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +# Add the python directory to the path to import the generated bindings |
| 21 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "python")) |
| 22 | + |
| 23 | +try: |
| 24 | + # Import the UniFFI generated module |
| 25 | + import rdp |
| 26 | +except ImportError: |
| 27 | + print( |
| 28 | + "Could not import rdp module. Make sure to build with: cargo build --release --features python-bindings" |
| 29 | + ) |
| 30 | + sys.exit(1) |
| 31 | + |
| 32 | + |
| 33 | +class Algorithm(Enum): |
| 34 | + """Simplification algorithms available.""" |
| 35 | + |
| 36 | + RDP = "rdp" # Ramer-Douglas-Peucker |
| 37 | + VW = "vw" # Visvalingam-Whyatt |
| 38 | + VWP = "vwp" # Visvalingam-Whyatt with topology preservation |
| 39 | + |
| 40 | + |
| 41 | +def simplify_linestring(coords, precision, algorithm=Algorithm.RDP): |
| 42 | + """ |
| 43 | + Simplify a linestring using the specified algorithm. |
| 44 | +
|
| 45 | + Args: |
| 46 | + coords: numpy array of shape (n, 2) or list of [x, y] pairs |
| 47 | + precision: simplification tolerance |
| 48 | + algorithm: Algorithm enum value |
| 49 | +
|
| 50 | + Returns: |
| 51 | + Simplified coordinates as numpy array |
| 52 | + """ |
| 53 | + # Convert to flat list efficiently |
| 54 | + flat = np.asarray(coords, dtype=np.float64).flatten().tolist() |
| 55 | + |
| 56 | + # Map algorithm to corresponding function |
| 57 | + algorithm_map = { |
| 58 | + Algorithm.RDP: rdp.simplify_rdp, |
| 59 | + Algorithm.VW: rdp.simplify_visvalingam, |
| 60 | + Algorithm.VWP: rdp.simplify_visvalingam_preserve, |
| 61 | + } |
| 62 | + |
| 63 | + try: |
| 64 | + simplify_func = algorithm_map[algorithm] |
| 65 | + result = simplify_func(flat, precision) |
| 66 | + except KeyError: |
| 67 | + raise ValueError(f"Unknown algorithm: {algorithm}") |
| 68 | + except rdp.RdpError as e: |
| 69 | + print(f"Error during simplification: {e}") |
| 70 | + raise |
| 71 | + |
| 72 | + # Reshape back to (n, 2) |
| 73 | + return np.array(result).reshape(-1, 2) |
| 74 | + |
| 75 | + |
| 76 | +def simplify_indices(coords, precision, algorithm=Algorithm.RDP): |
| 77 | + """ |
| 78 | + Get indices of points to keep after simplification. |
| 79 | +
|
| 80 | + Args: |
| 81 | + coords: numpy array of shape (n, 2) or list of [x, y] pairs |
| 82 | + precision: simplification tolerance |
| 83 | + algorithm: Algorithm enum value (RDP or VW only) |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Indices of points to keep |
| 87 | + """ |
| 88 | + # Convert to flat list |
| 89 | + flat = np.asarray(coords, dtype=np.float64).flatten().tolist() |
| 90 | + |
| 91 | + # Map algorithm to corresponding function |
| 92 | + algorithm_map = { |
| 93 | + Algorithm.RDP: rdp.simplify_rdp_idx, |
| 94 | + Algorithm.VW: rdp.simplify_visvalingam_idx, |
| 95 | + } |
| 96 | + |
| 97 | + try: |
| 98 | + simplify_func = algorithm_map[algorithm] |
| 99 | + result = simplify_func(flat, precision) |
| 100 | + except KeyError: |
| 101 | + raise ValueError(f"Algorithm {algorithm} does not support index output") |
| 102 | + except rdp.RdpError as e: |
| 103 | + print(f"Error during simplification: {e}") |
| 104 | + raise |
| 105 | + |
| 106 | + return np.array(result, dtype=np.uint64) |
| 107 | + |
| 108 | + |
| 109 | +def main(): |
| 110 | + # Example 1: RDP simplification |
| 111 | + print("Example 1: Ramer-Douglas-Peucker Simplification") |
| 112 | + print("-" * 50) |
| 113 | + |
| 114 | + coords_rdp = np.array( |
| 115 | + [[0.0, 0.0], [5.0, 4.0], [11.0, 5.5], [17.3, 3.2], [27.8, 0.1]] |
| 116 | + ) |
| 117 | + |
| 118 | + print(f"Original points ({len(coords_rdp)} points):") |
| 119 | + print(coords_rdp) |
| 120 | + |
| 121 | + simplified_rdp = simplify_linestring(coords_rdp, 1.0, Algorithm.RDP) |
| 122 | + print(f"\nSimplified with RDP (tolerance=1.0, {len(simplified_rdp)} points):") |
| 123 | + print(simplified_rdp) |
| 124 | + |
| 125 | + indices_rdp = simplify_indices(coords_rdp, 1.0, Algorithm.RDP) |
| 126 | + print(f"\nIndices of kept points: {indices_rdp}") |
| 127 | + |
| 128 | + # Example 2: Visvalingam-Whyatt simplification |
| 129 | + print("\n\nExample 2: Visvalingam-Whyatt Simplification") |
| 130 | + print("-" * 50) |
| 131 | + |
| 132 | + coords_vw = np.array( |
| 133 | + [[5.0, 2.0], [3.0, 8.0], [6.0, 20.0], [7.0, 25.0], [10.0, 10.0]] |
| 134 | + ) |
| 135 | + |
| 136 | + print(f"Original points ({len(coords_vw)} points):") |
| 137 | + print(coords_vw) |
| 138 | + |
| 139 | + simplified_vw = simplify_linestring(coords_vw, 30.0, Algorithm.VW) |
| 140 | + print(f"\nSimplified with VW (epsilon=30.0, {len(simplified_vw)} points):") |
| 141 | + print(simplified_vw) |
| 142 | + |
| 143 | + indices_vw = simplify_indices(coords_vw, 30.0, Algorithm.VW) |
| 144 | + print(f"\nIndices of kept points: {indices_vw}") |
| 145 | + |
| 146 | + # Example 3: Topology-preserving Visvalingam-Whyatt |
| 147 | + print("\n\nExample 3: Topology-Preserving Visvalingam-Whyatt") |
| 148 | + print("-" * 50) |
| 149 | + |
| 150 | + simplified_vwp = simplify_linestring(coords_vw, 30.0, Algorithm.VWP) |
| 151 | + print(f"Simplified with VWP (epsilon=30.0, {len(simplified_vwp)} points):") |
| 152 | + print(simplified_vwp) |
| 153 | + |
| 154 | + # Example 4: Error handling |
| 155 | + print("\n\nExample 4: Error Handling") |
| 156 | + print("-" * 50) |
| 157 | + |
| 158 | + try: |
| 159 | + # Try with invalid input (odd number of coordinates) |
| 160 | + invalid_coords = [1.0, 2.0, 3.0] |
| 161 | + rdp.simplify_rdp(invalid_coords, 1.0) |
| 162 | + except (rdp.RdpError, rdp.InternalError) as e: |
| 163 | + print(f"Caught expected error for invalid input: {e}") |
| 164 | + |
| 165 | + try: |
| 166 | + # Try with empty input |
| 167 | + empty_coords = [] |
| 168 | + rdp.simplify_rdp(empty_coords, 1.0) |
| 169 | + except (rdp.RdpError, rdp.InternalError) as e: |
| 170 | + print(f"Caught expected error for empty input: {e}") |
| 171 | + |
| 172 | + print('"All examples completed successfully!') |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + main() |
0 commit comments