axiom-data-science/polyline-encoder


Java library for encoding spatial data (JTS geometries and WKT) to Google encoded polylines

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.axiom-data-science:polyline-encoder:'
	}
	dependencies {
		implementation("com.github.axiom-data-science:polyline-encoder:")
	}
	<dependency>
	    <groupId>com.github.axiom-data-science</groupId>
	    <artifactId>polyline-encoder</artifactId>
	    <version></version>
	</dependency>

                            
    libraryDependencies += "com.github.axiom-data-science" % "polyline-encoder" % ""
        
        

                            
    :dependencies [[com.github.axiom-data-science/polyline-encoder ""]]
        
        

Readme


polyline-encoder

This library allows easy conversion of JTS geometries and WKT to Google encoded polylines. It was originally based on an implementation of the encoding algorithm by Mark Rambow, but was extended for additional functionality and Mavenization.

https://developers.google.com/maps/documentation/utilities/polylinealgorithm

Requirements

Java 11+ Maven 3+

Example usage

import com.axiomalaska.polylineencoder.EncodedPolyline;
import com.axiomalaska.polylineencoder.PolylineEncoder;
import com.axiomalaska.polylineencoder.UnsupportedGeometryTypeException;
import com.axiomalaska.polylineencoder.WktUtil;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

public static void jtsExample() throws UnsupportedGeometryTypeException{
    GeometryFactory geomFactory = new GeometryFactory( new PrecisionModel( PrecisionModel.FLOATING ), 4326 );
    Coordinate[] coords = new Coordinate[3];
    coords[0] = new Coordinate( 0, 0 );
    coords[1] = new Coordinate( 1, 1 );
    coords[2] = new Coordinate( 2, 2 );
    LineString jtsLineString = geomFactory.createLineString( coords );

    EncodedPolyline encodedLineString = PolylineEncoder.encode( jtsLineString );    
    System.out.println( encodedLineString.getPoints() );
    System.out.println( encodedLineString.getLevels() );               
}

public static void wktExample() throws UnsupportedGeometryTypeException{
    Geometry wktPolygon = WktUtil.wktToGeom( "POLYGON (( 0 0, 0 5, 5 5, 5 0, 0 0 ))" );
	 
    EncodedPolyline encodedPolygon = PolylineEncoder.encode( wktPolygon );    
    System.out.println( encodedPolygon.getPoints() );
    System.out.println( encodedPolygon.getLevels() );               
}