Skip to content

Instantly share code, notes, and snippets.

@peteroupc
Last active April 20, 2017 03:38
Show Gist options
  • Save peteroupc/7fd0f0a570147b43e09550b146ee93da to your computer and use it in GitHub Desktop.
Save peteroupc/7fd0f0a570147b43e09550b146ee93da to your computer and use it in GitHub Desktop.
Generates polygon WKT from an array of latitude/longitude coordinates
/*
Any copyright to this file is released to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
If you like this, you should donate
to Peter O. at:
http://peteroupc.github.io/
*/
public static String polygonWkt(double[] latlon){
if(latlon.length%2 != 0)
throw new IllegalArgumentException("latlon length is not even");
StringBuilder builder=new StringBuilder();
builder.append("POLYGON((");
for(int i=0;i<latlon.length;i+=2){
if(i>0){
builder.append(",");
}
builder.append(latlon[i]+" "+latlon[i+1]);
}
builder.append("))");
return builder.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment