Skip to content

Instantly share code, notes, and snippets.

@jccartwright
jccartwright / gist:2623428
Created May 6, 2012 17:33
calc points along a line using JTS
# use JTS to find the linear distance along a line a given point falls
@Grab(group='com.vividsolutions',module='jts',version='1.12')
import com.vividsolutions.jts.io.WKTReader
import com.vividsolutions.jts.geom.Geometry
import com.vividsolutions.jts.linearref.LengthIndexedLine
import com.vividsolutions.jts.geom.Coordinate
WKTReader reader = new WKTReader()
Geometry geom = reader.read('LINESTRING(10 10, 30 10)')
@jccartwright
jccartwright / gist:2623506
Created May 6, 2012 17:52
use JTS to densify a line, adding vertices at a specified interval
# densify a line, adding vertices at a specified interval
@Grab(group='com.vividsolutions',module='jts',version='1.12')
import com.vividsolutions.jts.io.WKTReader
import com.vividsolutions.jts.geom.Geometry
import com.vividsolutions.jts.densify.Densifier
import com.vividsolutions.jts.geom.GeometryFactory
import com.vividsolutions.jts.geom.PrecisionModel
PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FIXED)
@jccartwright
jccartwright / gist:a1a963027660811781fd
Created April 30, 2015 04:11
Calculate distance using Spherical Law of Cosines (javascript)
/**
* calculates the distance between two points using the Spherical Law of Cosines.
* returned distance in miles, input points expected to be in signed decimal
* degrees.
* WARNING: returns incorrect values if longitudes in different hemispheres
*/
f8unction geographicalDistance(lon1,lat1,lon2,lat2) {
//convert to radians
var ct = Math.PI/180.0;
lon1 *= ct;
@jccartwright
jccartwright / gist:b128bc0541eb1549515c
Last active August 29, 2015 14:20
handle a WCS 2 multipart response
#!/usr/bin/env groovy
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMultipart
import javax.activation.URLDataSource
def address = "http://example.com/cgi-bin/public/ms/gcv4/F16_2010_blended.lzw.tif?request=GetCoverage&service=WCS&version=1.1.1&COVERAGE=F16_2010_blended.lzw.tif&crs=EPSG:4326&format=geotiff&resx=0.0083333333&resy=0.0083333333&bbox=-95,25,-75,40"
def url = address.toURL()
HttpURLConnection connection = url.openConnection()
URLDataSource dataSource = new URLDataSource(url)
MimeMultipart multipart = new MimeMultipart(dataSource)
def roundDown(Double value, Integer interval) {
if (interval == null || interval <= 0) {
throw new IllegalArgumentException("Interval must be > 0")
}
return Math.floor(value/interval)*interval
}
def Integer roundUp(Double value, Integer interval) {
if (interval == null || interval <= 0) {
@jccartwright
jccartwright / gist:7ca9684983fdf4a005d8
Created April 30, 2015 04:39
clip a geometry in JTS
import com.vividsolutions.jts.geom.Geometry
import com.vividsolutions.jts.io.WKTReader
def WKTReader wktReader = new WKTReader()
def clip = wktReader.read("POLYGON((-10 -10, 10 -10, 10 10, -10 10,-10 -10))")
def line = wktReader.read("MULTILINESTRING((-15 -20, 15 20),(9 -30, 9 30))")
//def line = wktReader.read("MULTILINESTRING((-15 -20, 15 20))")
//def line = wktReader.read("MULTILINESTRING((0 -20, 0 20),(2 -30, 2 30))")
def String getWmsUrl(url) {
def parser = new org.cyberneko.html.parsers.SAXParser()
parser.setFeature('http://xml.org/sax/features/namespaces', false)
def page = new XmlParser(parser).parse("${url}/MapServer?f=html")
def data = page.depthFirst().A.'@href'.grep{ it != null && it.endsWith('WMS') }
if (data) {
return data[0]
} else {
return null
}
import ucar.nc2.dataset.NetcdfDataset
import ucar.nc2.dt.GridCoordSystem
//import org.apache.log4j.*
//Logger log = Logger.getInstance("netcdf")
String filename = "http://motherlode.ucar.edu:8080/thredds/dodsC/fmrc/NCEP/GFS/CONUS_80km/runs/NCEP-GFS-CONUS_80km_RUN_2011-03-20T18:00:00Z.nc"
//http://motherlode.ucar.edu:8080/thredds/dodsC/model/NCEP/GFS/CONUS_80km/GFS_CONUS_80km_20061019_0000.grib1";
NetcdfDataset ncd = null;
try {
import ucar.nc2.NetcdfFile
import ucar.nc2.dataset.NetcdfDataset
String filename = "/Users/jcc/testme.grd";
def NetcdfFile ncfile = null;
try {
ncfile = NetcdfFile.open(filename)
def minZ = ncfile.findVariable("z").findAttribute("actual_range").getValue(0)
def maxZ = ncfile.findVariable("z").findAttribute("actual_range").getValue(1)
@jccartwright
jccartwright / gist:f5931e478beec1dc224f
Created April 30, 2015 04:45
extract points along a line using JTS
import com.vividsolutions.jts.linearref.LengthIndexedLine
import com.vividsolutions.jts.io.WKTReader
def reader = new WKTReader()
def line = reader.read("LINESTRING(10 10, -10 -10)")
def lil = new LengthIndexedLine(line)
def increment = 2