Snowflake-Labs/jts-udf
Java
Captured source
source ↗Snowflake-Labs/jts-udf
Description: Advanced geospatial functions for Snowflake SQL from the JTS Java library
Language: Java
License: Apache-2.0
Stars: 0
Forks: 0
Open issues: 0
Created: 2026-03-02T06:06:38Z
Pushed: 2026-03-09T22:13:42Z
Default branch: main
Fork: no
Archived: no
README:
User Defined JTS Functions for Snowflake
The Snowflake geography and geometry types have a few dozen functions defined, covering the most frequent use cases. The JTS computational geometry library has many more functions, providing unique geospatial capabilities.
This repository contains the code and instructions to enable several of the unique functions in JTS as Java user-defined functions, bringing the capabilities of the open source JTS library into the Snowflake SQL environment.
[Learn more...](docs/about.md)
Available Functions
- jts.buffer(geom GEOMETRY, radius DOUBLE, quadsegs INT DEFAULT 8) -> GEOMETRY
Build buffer of geometry. Negative radius shrinks output, positive expands. More quad segments for more detail on curves.
- jts.buffersinglesided(geom GEOMETRY, radius DOUBLE) -> GEOMETRY
Build single sided buffer. Negative radius builds to right, positive to left.
- jts.concavehull(geom GEOMETRY, lengthRatio DOUBLE, isHolesAllowed BOOLEAN DEFAULT false) -> GEOMETRY
Returns concave hull. Length ratio from 0 (convex) to 1 (highly concave).
- jts.pointonsurface(geom GEOMETRY) -> GEOMETRY
Returns point guaranteed to be within geometry.
- jts.convexhull(geom GEOMETRY) -> GEOMETRY
Construct convex hull of geometry.
- jts.reduceprecision(geom GEOMETRY, gridSize DOUBLE) -> GEOMETRY
Apply precision grid to geometry, returns valid output.
- jts.isvalid(geom GEOMETRY) -> BOOLEAN
Return true for valid geometry, false for invalid.
- jts.makevalid(geom GEOMETRY) -> GEOMETRY
Repair invalidities in geometry, if there are any.
- jts.makevalid(geom GEOGRAPHY) -> GEOGRAPHY
Repair invalidities in geography, if there are any, using gnomonic projection.
- jts.intersection(geom0 GEOMETRY, geom1 GEOMETRY, gridSize DOUBLE DEFAULT 0) -> GEOMETRY
Calculate intersection of geometries. Optional grid size to force precision grid on output.
- jts.gunion(geom0 GEOMETRY, geom1 GEOMETRY, gridSize DOUBLE DEFAULT 0) -> GEOMETRY
Calculate union of geometries. Optional grid size to force precision grid on output.
- jts.difference(geom0 GEOMETRY, geom1 GEOMETRY, gridSize DOUBLE DEFAULT 0) -> GEOMETRY
Calculate difference of second geometry from first. Optional grid size to force precision grid on output.
- jts.symdifference(geom0 GEOMETRY, geom1 GEOMETRY, gridSize DOUBLE DEFAULT 0) -> GEOMETRY
Calculate symmetric difference of geometries. Optional grid size to force precision grid on output.
- jts.relate(geom0 GEOMETRY, geom1 GEOMETRY) -> GEOMETRY
Calculate full DE9IM relate matrix, flattened to string.
Build / Install
For now this library is available to build and install in your own organization, and is not built out to the marketplace.
Connect to Snowflake
The directions below make use of the snow commandline tool, so install the tool and setup your local login configuration using the Configuring Snowflake CLI and connecting to Snowflake docs.
Install Build Dependencies
To build the Java portions of the project, you will need:
- Java Development Kit (JDK) installation
- You may already have one, type
which javaat the command-line - Maven build tool
To build the SQL portions of the project, you will need:
- Python
- Make
Build and Install the UDF Native App
The JTS UDF is structured as a Snowflake Native App. For this project that means:
- a Java JAR file that contains the JTS code and shims to connect it to Snowflake,
- a SQL file that defines the function names and connects them to the Java code; and,
- some YML manifest files to wrap the whole thing up as an "application package" that the
snowcommandline program can understand.
To build all the artefacts, run:
make app.prepare
This will generate all the necessary Java and SQL code.
To install the application package, run:
# check you have a working connection snow connection list snow connection test # make sure the app passes basic tests snow app validate # copy the app package components into your account # when complete you should be able to see the package # in your account browser snow app deploy # enable the application # when complete you should be able to run JTS functions # in SQL snow app run
Try the JTS Native App
A basic test is if a very simple geometry is valid (points are always valid).
SELECT jts.isvalid(to_geometry('POINT(0 0)'));From there, do something that only JTS can do, like properly clean a figure-8 geometry polygon.
SELECT jts.makevalid(to_geometry('POLYGON((0 0, 0 1, 2 1, 2 2, 1 2, 1 0, 0 0))'));Or clean an invalid geography polygon (the lower edge crosses the upper edge in geographics).
SELECT jts.makevalid(to_geography('POLYGON((0 60, 0 61, 5 60.05, 10 61, 10 60, 0 60))', True));Development Installation
If you are planning to add new functions or work actively on the application, a manual installation might be more effective.
Set your role to SYSADMIN.
-- Create a database for our UDF work CREATE OR REPLACE DATABASE java_udf_db; -- Create a schema within that database CREATE OR REPLACE SCHEMA java_udf_schema;
For working on the app package, installing it piecemeal is more convenient than installing it as an app package.
First, ensure all the components are built.
make dev.prepare
Then create a stage, copy the JAR onto that stage, and then create all the UDFs with reference to that JAR.
# make sure you have a working connection snow connection test # put a stage into the schema you created earlier snow stage create \ --database java_udf_db \ --schema java_udf_schema \ --role sysadmin \ java_udf_stage # copy the jar file to that stage snow stage copy --overwrite ./target/snowflake-jts-1.0.0.jar @java_udf_stage #…
Excerpt shown — open the source for the full document.
Notability
notability 3.0/10Routine new repo from Snowflake Labs for geospatial UDF