2011年12月31日土曜日

groovyでredmineの指定プロジェクトの情報を取得する

groovyでredmineの指定プロジェクトの情報を取得するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.2')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import groovy.json.*

httpclient = new DefaultHttpClient()
// 指定のプロジェクトの情報を取得する
id = "project1"
method = new HttpGet("http://redmine-server/redmine/projects/${id}.json")
// アカウントページで作成したAPIアクセスキーを設定する
apiKey = "your-api-key"
method.addHeader(new BasicHeader("X-Redmine-API-Key", apiKey))
response = httpclient.execute(method)

json = new JsonSlurper().parseText(response.getEntity().getContent().text)
// 名前
println "name:${json.project.name}"
// 説明
println "description:${json.project.description}"
// 作成日時
println "created_on:${json.project.created_on}"
// 更新日時
println "updated_on:${json.project.updated_on}"


動作環境
groovy 1.8.4, JDK6 Update29, redmine 1.3.0

groovyでdrupalの投稿の一覧を取得する

groovyでdrupalの投稿の一覧を取得するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.2')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import groovy.json.*

httpclient = new DefaultHttpClient()
// 投稿の一覧を取得する
method = new HttpGet("http://localhost/drupal/rest/node.json")
response = httpclient.execute(method)
json = new JsonSlurper().parseText(response.getEntity().getContent().text)
json.each {
  println "題名:${it.title}"
  println "url:${it.uri}"
  println "created:${new Date(Long.parseLong(it.created)*1000)}"
}

動作環境
groovy 1.8.4, JDK7 Update1, drupal 7.1.0, services-7.x-3.1, ctools-7.x-1.0-rc1, spyc-0.5

2011年12月30日金曜日

groovyとoracleでライブラリキャッシュヒット率を取得する

groovyとoracleでライブラリキャッシュヒット率を取得するには、以下のコードを実行します。
import groovy.sql.Sql

sql = Sql.newInstance(
  "jdbc:oracle:thin:@localhost:1521:orcl", 
   "system",
   "manager", 
   "oracle.jdbc.driver.OracleDriver")

query = '''
select
  sum(pins) as total_pins,
  sum(reloads) as total_reloads,
  (1 - sum(reloads) / sum(pins)) * 100 as hitratio
from
  v$librarycache
'''
// ライブラリキャッシュヒット率を表示
println "total_pins, total_reloads, hitratio"
sql.eachRow(query){
  println "${it.total_pins}, ${it.total_reloads}, ${it.hitratio}"
}

動作環境
groovy 1.8.4, JDK6 Update29, Oracle11g R2

2011年12月29日木曜日

groovyとWordpress + JSON API pluginで指定カテゴリの投稿を取得する

groovyとWordpress + JSON API pluginで指定カテゴリの投稿を取得するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.2')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import groovy.json.*

httpclient = new DefaultHttpClient()
// 指定カテゴリーの投稿を取得する
method = new HttpGet("http://localhost/?json=get_category_posts&slug=groovy")
response = httpclient.execute(method)

json = new JsonSlurper().parseText(response.getEntity().getContent().text)
json.posts.each {
  // ID
  println "id:${it.id}"
  // タイトル
  println "title:${it.title}"
  // 日時
  println "date:${it.date}"
  // 内容
  println "content:${it.content}"
}

動作環境
groovy 1.8.4, JDK6 Update29, Wordpress 3.2.1, JSON API plugin 1.0.7

JSON APIのページ
http://wordpress.org/extend/plugins/json-api/

2011年12月28日水曜日

groovyとgeotoolsで指定した地点にXマークを描画する

groovyとgeotoolsで指定した地点にXマークを描画するには、以下のコードを実行します。
import java.awt.*
import java.awt.image.*
import javax.imageio.*
import org.geotools.data.shapefile.*
import org.geotools.factory.*
import org.geotools.feature.*
import org.geotools.feature.simple.*
import org.geotools.filter.*
import org.geotools.map.*
import org.geotools.renderer.lite.*
import org.geotools.styling.*
import org.geotools.geometry.jts.*
import com.vividsolutions.jts.geom.*
import org.geotools.data.*
import org.opengis.feature.type.*

url = new URL("file://C:/share/geotools/world.shp")
shapefile = new ShapefileDataStore(url)

fs = shapefile.getFeatureSource()
schema = fs.getSchema()
crs = schema.getGeometryDescriptor().getCoordinateReferenceSystem()
map = new DefaultMapContext([] as MapLayer[], crs)

ff = FilterFactoryFinder.createFilterFactory()
sf = CommonFactoryFinder.getStyleFactory()

// フィルタを作成
filter = ff.createCompareFilter(FilterType.COMPARE_EQUALS)
filter.addLeftValue(ff.createAttributeExpression("NAME"))
filter.addRightValue(ff.createLiteralExpression("JAPAN"))
fs = fs.getFeatures(filter)

// ポリゴンの線の色1
stroke1 = sf.createStroke(
  ff.literal(new Color(0x59, 0x58, 0x55)),
  ff.literal(1)
)

// ポリゴンの塗りつぶし色1
fill1 = sf.createFill(
  ff.literal(new Color(0xD2, 0xDB, 0xD5))
)
sym1 = sf.createPolygonSymbolizer(stroke1, fill1, null)

// フィルタに一致する場合のルールを作成
rule1 = sf.createRule()
rule1.symbolizers().add(sym1)

fts = sf.createFeatureTypeStyle([rule1] as Rule[])
style1 = sf.createStyle()
style1.featureTypeStyles().add(fts)

// レイヤーとして追加
map.addLayer(new FeatureLayer(fs, style1))


// ------------------------------------------
// ポイント用のレイヤーを作成
// ポイント用のタイプを作成
pointtype = DataUtilities.createType(
  "Location", 
  "the_geom:Point," + 
  "name:String"
)
sfb = new SimpleFeatureBuilder(pointtype)

// 福岡を示すポイントを作成
// 経度
longitude = 130.23193359375d
// 緯度
latitude = 33.61919376817004d
gf = JTSFactoryFinder.getGeometryFactory(null)
point = gf.createPoint(new Coordinate(longitude, latitude))
sfb.add(point)
sfb.add("fukuoka")

col = FeatureCollections.newCollection()
feature1 = sfb.buildFeature(null)
col.add(feature1)

// マーカーの線
stroke2 = sf.createStroke(
  ff.literal(new Color(0xC8, 0x46, 0x63)),
  ff.literal(1)
)
// マーカーの塗りつぶし色
fill2 = sf.createFill(ff.literal(new Color(0xff, 0xC8, 0x61)))

// マーカーの形
mark = sf.getXMark()
mark.setFill(fill2)
mark.setStroke(stroke2)

graphic = sf.createDefaultGraphic()
graphic.graphicalSymbols().clear()
graphic.graphicalSymbols().add(mark)
graphic.setSize(ff.literal(18))

GeometryDescriptor geomDesc = fs.getSchema().getGeometryDescriptor()
geometryAttributeName = geomDesc.getLocalName()
sym2 = sf.createPointSymbolizer(graphic, geometryAttributeName)

rule2 = sf.createRule()
rule2.symbolizers().add(sym2)
fts2 = sf.createFeatureTypeStyle([rule2] as Rule[])
style2 = sf.createStyle()
style2.featureTypeStyles().add(fts2)
// レイヤーとして追加
map.addLayer(new FeatureLayer(col, style2))

// レンダリング
renderer = new StreamingRenderer()
renderer.setContext(map)

width = 400
bounds = map.getLayerBounds()
rect = new Rectangle(0, 0, width, 
  (int)(width * bounds.getHeight() / bounds.getWidth()))

image = new BufferedImage((int)rect.width, (int)rect.height, 
  BufferedImage.TYPE_INT_RGB)
gr = image.createGraphics()
gr.setPaint(Color.WHITE)
gr.fill(rect)
renderer.paint(gr, rect, bounds)

ImageIO.write(image, "jpeg", new File("fukuoka.jpg"))

出力画像

※世界地図のシェイプファイルは以下からダウンロード
World map for APRS
http://aprsworld.net/gisdata/world/

※日本地図は国土地理院のウェブサイトからダウンロードできます。
ダウンロード・利用規約などは以下を参照。
http://www1.gsi.go.jp/geowww/globalmap-gsi/download/index.html

※.groovy/libからGROOVY_HOME/libにgeotools-2.7.3-bin.zipに
含まれるjarをコピーする

動作環境
groovy 1.8.2, JDK6 Update27, GeoTools 2.7.3

関連情報
・GeoToolsのウェブサイト
http://geotools.org/

2011年12月26日月曜日

groovyとoracleでバッファキャッシュヒット率を取得する

groovyとoracleでバッファキャッシュヒット率を取得するには、以下のコードを実行します。
import groovy.sql.Sql

sql = Sql.newInstance(
  "jdbc:oracle:thin:@localhost:1521:orcl", 
   "system",
   "manager", 
   "oracle.jdbc.driver.OracleDriver")

query = '''
select 
  name, 
  physical_reads,  -- physical reads統計
  db_block_gets,  -- database blocks gotten統計
  consistent_gets,  -- consistent gets統計
  (1-(physical_reads/(db_block_gets + consistent_gets)))*100 as hit_ratio
from
  v$buffer_pool_statistics
'''
// バッファキャッシュヒット率を表示
println "name, physical_reads, db_block_gets, consistent_gets, hit_ratio"
sql.eachRow(query){
  println "${it.name}, ${it.physical_reads}, ${it.db_block_gets}, " +
    "${it.consistent_gets}, ${it.hit_ratio}}"
}

動作環境
groovy 1.8.4, JDK6 Update29, Oracle11g R2

2011年12月25日日曜日

groovyとWordpress + JSON API pluginで指定IDの固定ページの情報を取得する

groovyとWordpress + JSON API pluginで指定IDの固定ページの情報を取得するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.2')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import groovy.json.*

httpclient = new DefaultHttpClient()
// 指定IDの固定ページを取得する
method = new HttpGet("http://localhost/?json=get_page&id=22")
response = httpclient.execute(method)

json = new JsonSlurper().parseText(response.getEntity().getContent().text)
// タイトル
println "title:${json.page.title}"
// URL
println "url:${json.page.url}"
// 日時
println "date:${json.page.date}"
// 内容
println "content:${json.page.content}"

動作環境
groovy 1.8.4, JDK6 Update29, Wordpress 3.2.1, JSON API plugin 1.0.7

JSON APIのページ
http://wordpress.org/extend/plugins/json-api/

2011年12月24日土曜日

groovyとoracleでデータファイルごとのI/O回数・ブロック数を取得する

groovyとoracleでデータファイルごとのI/O回数・ブロック数を取得するには、以下のコードを実行します。
import groovy.sql.Sql

sql = Sql.newInstance(
  "jdbc:oracle:thin:@localhost:1521:orcl", 
   "system",
   "manager", 
   "oracle.jdbc.driver.OracleDriver")

query = '''
select
  to_char(current_timestamp, 'yyyy/mm/dd hh24:mi:ss') as ct,
  vt.name as tablespace_name,
  vd.name as filename,
  vfs.phyrds,  -- 実行された物理読取り数
  vfs.phywrts,  -- DBWRに書込みが必要となった回数
  vfs.phyblkrd,  -- 読み込まれた物理ブロックの数
  vfs.phyblkwrt  -- ディスクに書き込まれたブロックの数
from
  v$filestat vfs
  join v$datafile vd
    on (vfs.file# = vd.file#)
  join v$tablespace vt
    on (vd.ts# = vt.ts#)
'''
// ファイル毎のI/Oを表示
println "tablespace_name, filename, phyrds, phywrts, phyblkrd, phyblkwrt"
sql.eachRow(query){
  println "${it.ct}, ${it.tablespace_name}, ${it.filename}, " +
    "${it.phyrds}, ${it.phywrts}, " + 
    "${it.phyblkrd}, ${it.phyblkwrt}"
}

動作環境
groovy 1.8.4, JDK6 Update29, Oracle11g R2

2011年12月22日木曜日

groovyとgeotoolsで指定した地点に三角マークを描画する

groovyとgeotoolsで指定した地点に三角マークを描画するには、以下のコードを実行します。
import java.awt.*
import java.awt.image.*
import javax.imageio.*
import org.geotools.data.shapefile.*
import org.geotools.factory.*
import org.geotools.feature.*
import org.geotools.feature.simple.*
import org.geotools.filter.*
import org.geotools.map.*
import org.geotools.renderer.lite.*
import org.geotools.styling.*
import org.geotools.geometry.jts.*
import com.vividsolutions.jts.geom.*
import org.geotools.data.*
import org.opengis.feature.type.*

url = new URL("file://C:/share/geotools/world.shp")
shapefile = new ShapefileDataStore(url)

fs = shapefile.getFeatureSource()
schema = fs.getSchema()
crs = schema.getGeometryDescriptor().getCoordinateReferenceSystem()
map = new DefaultMapContext([] as MapLayer[], crs)

ff = FilterFactoryFinder.createFilterFactory()
sf = CommonFactoryFinder.getStyleFactory()

// フィルタを作成
filter = ff.createCompareFilter(FilterType.COMPARE_EQUALS)
filter.addLeftValue(ff.createAttributeExpression("NAME"))
filter.addRightValue(ff.createLiteralExpression("JAPAN"))
fs = fs.getFeatures(filter)

// ポリゴンの線の色1
stroke1 = sf.createStroke(
  ff.literal(new Color(0x59, 0x58, 0x55)),
  ff.literal(1)
)

// ポリゴンの塗りつぶし色1
fill1 = sf.createFill(
  ff.literal(new Color(0xD2, 0xDB, 0xD5))
)
sym1 = sf.createPolygonSymbolizer(stroke1, fill1, null)

// フィルタに一致する場合のルールを作成
rule1 = sf.createRule()
rule1.symbolizers().add(sym1)

fts = sf.createFeatureTypeStyle([rule1] as Rule[])
style1 = sf.createStyle()
style1.featureTypeStyles().add(fts)

// レイヤーとして追加
map.addLayer(new FeatureLayer(fs, style1))


// ------------------------------------------
// ポイント用のレイヤーを作成
// ポイント用のタイプを作成
pointtype = DataUtilities.createType(
  "Location", 
  "the_geom:Point," + 
  "name:String"
)
sfb = new SimpleFeatureBuilder(pointtype)

// 名古屋を示すポイントを作成
// 経度
longitude = 136.6864013671875
// 緯度
latitude = 35.26580442886754d
gf = JTSFactoryFinder.getGeometryFactory(null)
point = gf.createPoint(new Coordinate(longitude, latitude))
sfb.add(point)
sfb.add("nagoya")

col = FeatureCollections.newCollection()
feature1 = sfb.buildFeature(null)
col.add(feature1)

// マーカーの線
stroke2 = sf.createStroke(
  ff.literal(new Color(0xC8, 0x46, 0x63)),
  ff.literal(1)
)
// マーカーの塗りつぶし色
fill2 = sf.createFill(ff.literal(new Color(0xff, 0xC8, 0x61)))

// マーカーの形
mark = sf.getTriangleMark()
mark.setFill(fill2)
mark.setStroke(stroke2)

graphic = sf.createDefaultGraphic()
graphic.graphicalSymbols().clear()
graphic.graphicalSymbols().add(mark)
graphic.setSize(ff.literal(18))

GeometryDescriptor geomDesc = fs.getSchema().getGeometryDescriptor()
geometryAttributeName = geomDesc.getLocalName()
sym2 = sf.createPointSymbolizer(graphic, geometryAttributeName)

rule2 = sf.createRule()
rule2.symbolizers().add(sym2)
fts2 = sf.createFeatureTypeStyle([rule2] as Rule[])
style2 = sf.createStyle()
style2.featureTypeStyles().add(fts2)
// レイヤーとして追加
map.addLayer(new FeatureLayer(col, style2))

// レンダリング
renderer = new StreamingRenderer()
renderer.setContext(map)

width = 400
bounds = map.getLayerBounds()
rect = new Rectangle(0, 0, width, 
  (int)(width * bounds.getHeight() / bounds.getWidth()))

image = new BufferedImage((int)rect.width, (int)rect.height, 
  BufferedImage.TYPE_INT_RGB)
gr = image.createGraphics()
gr.setPaint(Color.WHITE)
gr.fill(rect)
renderer.paint(gr, rect, bounds)

ImageIO.write(image, "jpeg", new File("nagoya.jpg"))

出力画像

※世界地図のシェイプファイルは以下からダウンロード
World map for APRS
http://aprsworld.net/gisdata/world/

※日本地図は国土地理院のウェブサイトからダウンロードできます。
ダウンロード・利用規約などは以下を参照。
http://www1.gsi.go.jp/geowww/globalmap-gsi/download/index.html

※.groovy/libからGROOVY_HOME/libにgeotools-2.7.3-bin.zipに
含まれるjarをコピーする

動作環境
groovy 1.8.2, JDK6 Update27, GeoTools 2.7.3

関連情報
・GeoToolsのウェブサイト
http://geotools.org/

2011年12月20日火曜日

groovyとgeotoolsでアンチエイリアスを指定する

groovyとgeotoolsでアンチエイリアスを指定するには、以下のコードを実行します。
import java.awt.*
import java.awt.image.*
import javax.imageio.*
import org.geotools.data.shapefile.*
import org.geotools.map.*
import org.geotools.renderer.lite.*
import org.geotools.styling.*

url = new URL("file://C:/share/geotools/world.shp")
shapefile = new ShapefileDataStore(url)

fs = shapefile.getFeatureSource()
schema = fs.getSchema()
crs = schema.getGeometryDescriptor().getCoordinateReferenceSystem()
map = new DefaultMapContext([] as MapLayer[], crs)

// スタイルを作成
style = SLD.createSimpleStyle(schema)
map.addLayer(new FeatureLayer(fs, style))

// レンダリング
renderer = new StreamingRenderer()
renderer.setContext(map)
// アンチエイリアスを指定
rh = new RenderingHints(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON
)
renderer.setJava2DHints(rh)

width = 400
bounds = map.getMaxBounds()
rect = new Rectangle(0, 0, width, 
  (int)(width * bounds.getHeight() / bounds.getWidth()))

image = new BufferedImage((int)rect.width, (int)rect.height, 
  BufferedImage.TYPE_INT_RGB)
gr = image.createGraphics()
gr.setPaint(Color.WHITE)
gr.fill(rect)
renderer.paint(gr, rect, bounds)

ImageIO.write(image, "jpeg", new File("world_antialias.jpg"))

出力画像

※世界地図のシェイプファイルは以下からダウンロード
World map for APRS
http://aprsworld.net/gisdata/world/

※日本地図は国土地理院のウェブサイトからダウンロードできます。
ダウンロード・利用規約などは以下を参照。
http://www1.gsi.go.jp/geowww/globalmap-gsi/download/index.html

※.groovy/libからGROOVY_HOME/libにgeotools-2.7.3-bin.zipに
含まれるjarをコピーする

動作環境
groovy 1.8.2, JDK6 Update27, GeoTools 2.7.3

関連情報
・GeoToolsのウェブサイト
http://geotools.org/

2011年12月19日月曜日

groovyとWordpress + JSON API pluginで指定IDの記事を取得する

groovyとWordpress + JSON API pluginで指定IDの記事を取得するには、以下のコードを実行します。
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.2')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import groovy.json.*

httpclient = new DefaultHttpClient()
// 指定IDの記事を取得する
method = new HttpGet("http://localhost/?json=get_post&id=3")
response = httpclient.execute(method)

json = new JsonSlurper().parseText(response.getEntity().getContent().text)
// タイトル
println "title:${json.post.title}"
// 日時
println "date:${json.post.date}"
// 内容
println "content:${json.post.content}"

動作環境
groovy 1.8.4, JDK6 Update29, Wordpress 3.2.1, JSON API plugin 1.0.7

JSON APIのページ
http://wordpress.org/extend/plugins/json-api/

2011年12月18日日曜日

groovyとgeotoolsで指定した地点に星マークを描画する

groovyとgeotoolsで指定した地点に星マークを描画するには、以下のコードを実行します。
import java.awt.*
import java.awt.image.*
import javax.imageio.*
import org.geotools.data.shapefile.*
import org.geotools.factory.*
import org.geotools.feature.*
import org.geotools.feature.simple.*
import org.geotools.filter.*
import org.geotools.map.*
import org.geotools.renderer.lite.*
import org.geotools.styling.*
import org.geotools.geometry.jts.*
import com.vividsolutions.jts.geom.*
import org.geotools.data.*
import org.opengis.feature.type.*

url = new URL("file://C:/share/geotools/world.shp")
shapefile = new ShapefileDataStore(url)

fs = shapefile.getFeatureSource()
schema = fs.getSchema()
crs = schema.getGeometryDescriptor().getCoordinateReferenceSystem()
map = new DefaultMapContext([] as MapLayer[], crs)

ff = FilterFactoryFinder.createFilterFactory()
sf = CommonFactoryFinder.getStyleFactory()

// フィルタを作成
filter = ff.createCompareFilter(FilterType.COMPARE_EQUALS)
filter.addLeftValue(ff.createAttributeExpression("NAME"))
filter.addRightValue(ff.createLiteralExpression("JAPAN"))
fs = fs.getFeatures(filter)

// ポリゴンの線の色1
stroke1 = sf.createStroke(
  ff.literal(new Color(0x59, 0x58, 0x55)),
  ff.literal(1)
)

// ポリゴンの塗りつぶし色1
fill1 = sf.createFill(
  ff.literal(new Color(0xD2, 0xDB, 0xD5))
)
sym1 = sf.createPolygonSymbolizer(stroke1, fill1, null)

// フィルタに一致する場合のルールを作成
rule1 = sf.createRule()
rule1.symbolizers().add(sym1)

fts = sf.createFeatureTypeStyle([rule1] as Rule[])
style1 = sf.createStyle()
style1.featureTypeStyles().add(fts)

// レイヤーとして追加
map.addLayer(new FeatureLayer(fs, style1))


// ------------------------------------------
// ポイント用のレイヤーを作成
// ポイント用のタイプを作成
pointtype = DataUtilities.createType(
  "Location", 
  "the_geom:Point," + 
  "name:String"
)
sfb = new SimpleFeatureBuilder(pointtype)

// 金沢を示すポイントを作成
// 経度
longitude = 136.27166748046875d
// 緯度
latitude = 36.578041001498704d
gf = JTSFactoryFinder.getGeometryFactory(null)
point = gf.createPoint(new Coordinate(longitude, latitude))
sfb.add(point)
sfb.add("kanazawa")

col = FeatureCollections.newCollection()
feature1 = sfb.buildFeature(null)
col.add(feature1)

// マーカーの線
stroke2 = sf.createStroke(
  ff.literal(new Color(0xC8, 0x46, 0x63)),
  ff.literal(1)
)
// マーカーの塗りつぶし色
fill2 = sf.createFill(ff.literal(new Color(0xff, 0xC8, 0x61)))

// マーカーの形
mark = sf.getStarMark()
mark.setFill(fill2)
mark.setStroke(stroke2)

graphic = sf.createDefaultGraphic()
graphic.graphicalSymbols().clear()
graphic.graphicalSymbols().add(mark)
graphic.setSize(ff.literal(20))

GeometryDescriptor geomDesc = fs.getSchema().getGeometryDescriptor()
geometryAttributeName = geomDesc.getLocalName()
sym2 = sf.createPointSymbolizer(graphic, geometryAttributeName)

rule2 = sf.createRule()
rule2.symbolizers().add(sym2)
fts2 = sf.createFeatureTypeStyle([rule2] as Rule[])
style2 = sf.createStyle()
style2.featureTypeStyles().add(fts2)
// レイヤーとして追加
map.addLayer(new FeatureLayer(col, style2))

// レンダリング
renderer = new StreamingRenderer()
renderer.setContext(map)

width = 400
bounds = map.getLayerBounds()
rect = new Rectangle(0, 0, width, 
  (int)(width * bounds.getHeight() / bounds.getWidth()))

image = new BufferedImage((int)rect.width, (int)rect.height, 
  BufferedImage.TYPE_INT_RGB)
gr = image.createGraphics()
gr.setPaint(Color.WHITE)
gr.fill(rect)
renderer.paint(gr, rect, bounds)

ImageIO.write(image, "jpeg", new File("kanazawa.jpg"))

出力画像

※世界地図のシェイプファイルは以下からダウンロード
World map for APRS
http://aprsworld.net/gisdata/world/

※日本地図は国土地理院のウェブサイトからダウンロードできます。
ダウンロード・利用規約などは以下を参照。
http://www1.gsi.go.jp/geowww/globalmap-gsi/download/index.html

※.groovy/libからGROOVY_HOME/libにgeotools-2.7.3-bin.zipに
含まれるjarをコピーする

動作環境
groovy 1.8.2, JDK6 Update27, GeoTools 2.7.3

関連情報
・GeoToolsのウェブサイト
http://geotools.org/