This repository was archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.zig
More file actions
56 lines (45 loc) · 1.78 KB
/
Copy pathex1.zig
File metadata and controls
56 lines (45 loc) · 1.78 KB
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
/// Reads two WKT representations and calculates the intersection, prints it out,
/// and cleans up.
///
/// Ported from: src/geos/examples/capi_read.c
const c = @cImport({
@cInclude("zig_handlers.h");
@cInclude("geos_c.h");
});
const std = @import("std");
const handlers = @import("default_handlers");
pub fn main() anyerror!void {
const stdout = std.io.getStdOut().writer();
// Send notice and error messages to our stdout handler
c.initGEOS(handlers.shimNotice, handlers.shimError);
// Clean up the global context
defer c.finishGEOS();
errdefer c.finishGEOS();
// Two squares that overlap
const wkt_a = "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))";
const wkt_b = "POLYGON((5 5, 15 5, 15 15, 5 15, 5 5))";
// Read the WKT into geometry objects
const reader = c.GEOSWKTReader_create();
defer c.GEOSWKTReader_destroy(reader);
const geom_a = c.GEOSWKTReader_read(reader, wkt_a);
defer c.GEOSGeom_destroy(geom_a);
const geom_b = c.GEOSWKTReader_read(reader, wkt_b);
defer c.GEOSGeom_destroy(geom_b);
// Calculate the intersection
const inter = c.GEOSIntersection(geom_a, geom_b);
defer c.GEOSGeom_destroy(inter);
// Convert result to WKT
const writer = c.GEOSWKTWriter_create();
defer c.GEOSWKTWriter_destroy(writer);
// Trim trailing zeros off output
c.GEOSWKTWriter_setTrim(writer, 1);
const wkt_inter = c.GEOSWKTWriter_write(writer, inter);
defer c.GEOSFree(wkt_inter);
// Print answer
try stdout.print("Geometry A: {s}\n", .{wkt_a});
try stdout.print("Geometry B: {s}\n", .{wkt_b});
try stdout.print("Intersection(A, B): {s}\n", .{wkt_inter});
// | Clean up everything we allocated
// | Clean up the global context
// |-> *see zig defer statements above*
}