import java.awt.*; import java.awt.image.*; import java.io.*; import java.net.*; import javax.imageio.*; import org.geotools.data.shapefile.*; import org.geotools.data.simple.*; import org.opengis.filter.*; import org.geotools.factory.*; import org.geotools.geometry.jts.*; import org.geotools.map.*; import org.geotools.renderer.lite.*; import org.geotools.styling.*; import org.opengis.feature.simple.*; import org.opengis.referencing.crs.*; public class Geotools5 { public static void main(String args[]) throws Exception { URL url = new URL("file://C:/share/geotools/world.shp"); ShapefileDataStore shapefile = new ShapefileDataStore(url); SimpleFeatureSource fs = shapefile.getFeatureSource(); // 国の名前でフィルタリング FilterFactory ff = CommonFactoryFinder.getFilterFactory2(null); Filter filter = ff.equals( ff.property("NAME"), ff.literal("JAPAN") ); SimpleFeatureCollection sfc = fs.getFeatures(filter); SimpleFeatureType schema = sfc.getSchema(); CoordinateReferenceSystem crs = schema.getGeometryDescriptor().getCoordinateReferenceSystem(); MapLayer layers[] = {}; DefaultMapContext map = new DefaultMapContext(layers, crs); //スタイルを作成 StyleFactory sf = CommonFactoryFinder.getStyleFactory(null); // ポリゴンの線の色 org.geotools.styling.Stroke stroke = sf.createStroke( ff.literal(new Color(47, 184, 27)), ff.literal(2) ); // ポリゴンの塗りつぶし色 org.geotools.styling.Fill fill = sf.createFill( ff.literal(new Color(133, 255, 7)) ); PolygonSymbolizer sym = sf.createPolygonSymbolizer(stroke, fill, null); Rule rule = sf.createRule(); rule.symbolizers().add(sym); Rule rules[] = {rule}; FeatureTypeStyle fts = sf.createFeatureTypeStyle(rules); Style style = sf.createStyle(); style.featureTypeStyles().add(fts); map.addLayer(new FeatureLayer(sfc, style)); // レンダリング StreamingRenderer renderer = new StreamingRenderer(); renderer.setContext(map); int width = 400; ReferencedEnvelope bounds = map.getMaxBounds(); Rectangle rect = new Rectangle(0, 0, width, (int)(width * bounds.getHeight() / bounds.getWidth())); BufferedImage image = new BufferedImage((int)rect.width, (int)rect.height, BufferedImage.TYPE_INT_RGB); Graphics2D gr = image.createGraphics(); gr.setPaint(Color.WHITE); gr.fill(rect); renderer.paint(gr, rect, bounds); ImageIO.write(image, "jpeg", new File("polygon.jpg")); } }出力画像
動作環境
JDK7 Update1, geotools 2.7.4
関連情報
・GeoToolsのウェブサイト
http://geotools.org/
※世界地図のシェイプファイルは以下からダウンロード
World map for APRS
http://aprsworld.net/gisdata/world/
No comments:
Post a Comment