Skip to content

Instantly share code, notes, and snippets.

@peterstace
Created October 28, 2020 19:19
Show Gist options
  • Save peterstace/c9fc4cd66bc5de50d2a43cd818e7c786 to your computer and use it in GitHub Desktop.
Save peterstace/c9fc4cd66bc5de50d2a43cd818e7c786 to your computer and use it in GitHub Desktop.
Example of how to use GEOS to do a single binary operation
#include "/usr/include/geos_c.h"
#include <stdio.h>
void messageHandler(const char *fmt, ...) {
printf(fmt);
}
int main() {
GEOSContextHandle_t handle = initGEOS_r(messageHandler, messageHandler);
const char *wkt1 = "POLYGON((0.9 0.1,0.9 1,0.901921471959677 1.019509032201613,0.9076120467488714 1.0382683432365092,0.9168530387697456 1.0555570233019602,0.9292893218813453 1.0707106781186548,0.9444429766980398 1.0831469612302547,0.961731656763491 1.0923879532511287,0.9804909677983872 1.0980785280403231,1 1.1,1.0195090322016127 1.0980785280403231,1.038268343236509 1.0923879532511287,1.0555570233019602 1.0831469612302547,1.0707106781186546 1.0707106781186548,1.0831469612302544 1.0555570233019602,1.0923879532511287 1.0382683432365092,1.0980785280403231 1.019509032201613,1.1 1,1.0717462120245884 0.06467514421272098,0.9353248557872769 -0.07174621202458649,0 -0.1,-0.019509032201612955 -0.09807852804032302,-0.03826834323650912 -0.09238795325112863,-0.055557023301960363 -0.08314696123025443,-0.07071067811865482 -0.07071067811865468,-0.08314696123025457 -0.055557023301960155,-0.0923879532511287 -0.03826834323650893,-0.09807852804032306 -0.01950903220161279,-0.1 0.000000000000000012246467991473533,-0.09807852804032305 0.01950903220161282,-0.0923879532511287 0.03826834323650895,-0.08314696123025456 0.055557023301960176,-0.07071067811865482 0.0707106781186547,-0.05555702330196031 0.08314696123025447,-0.0382683432365091 0.09238795325112864,-0.019509032201612972 0.09807852804032302,0 0.1,0.9 0.1))";
GEOSGeometry *g1 = GEOSGeomFromWKT_r(handle, wkt1);
const char *wkt2 = "POLYGON((0.9292893218813453 1.0707106781186548,1.0707106781186548 0.9292893218813453,0.07071067811865475 -0.07071067811865475,-0.07071067811865475 0.07071067811865475,0.9292893218813453 1.0707106781186548))";
GEOSGeometry *g2 = GEOSGeomFromWKT_r(handle, wkt2);
GEOSGeometry *diff = GEOSDifference_r(handle, g1, g2);
GEOSWKTWriter *writer = GEOSWKTWriter_create_r(handle);
char *diffWKT = GEOSWKTWriter_write_r(handle, writer, diff);
printf(diffWKT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment