Skip to content

Instantly share code, notes, and snippets.

@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
@jccartwright
jccartwright / gist:5c6f83576eb580df8577
Created April 30, 2015 04:46
print memory usage from servlet
out.println("Free Memory: "+Runtime.getRuntime().freeMemory()+"<br>");
out.println("Max Memory: "+Runtime.getRuntime().maxMemory()+"<br>");
out.println("Total Memory: "+Runtime.getRuntime().totalMemory()+"<br>");
@jccartwright
jccartwright / gist:4fda779549fdfa624872
Last active August 29, 2015 14:20
create a geotiff using OGR
import numpy,os
from osgeo import osr, gdal
nx = 361
ny = 181
output_file = "calibration_1deg.tiff"
driver = gdal.GetDriverByName("GTiff")
dst_ds = driver.Create(output_file, nx, ny, 1, gdal.GDT_Int16)
repositories {
mavenCentral()
}
configurations {
dijits
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.6'
dijits 'commons-io:commons-io:2.4'