Created
May 23, 2010 19:32
-
-
Save dwins/411190 to your computer and use it in GitHub Desktop.
is this the minimal code to render a shapefile with geotools?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Graphics2D; | |
import java.awt.Rectangle; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.net.URL; | |
import javax.imageio.ImageIO; | |
import org.geotools.data.FeatureSource; | |
import org.geotools.data.shapefile.ShapefileDataStore; | |
import org.geotools.factory.CommonFactoryFinder; | |
import org.geotools.geometry.jts.ReferencedEnvelope; | |
import org.geotools.map.DefaultMapContext; | |
import org.geotools.map.DefaultMapLayer; | |
import org.geotools.map.MapContext; | |
import org.geotools.map.MapLayer; | |
import org.geotools.renderer.lite.StreamingRenderer; | |
import org.geotools.styling.SLDParser; | |
import org.geotools.styling.Style; | |
import org.geotools.styling.StyleFactory; | |
import org.opengis.feature.simple.SimpleFeature; | |
import org.opengis.feature.simple.SimpleFeatureType; | |
/** | |
* Hello world! | |
* | |
*/ | |
public class App { | |
public static void main(String[] args) throws Exception { | |
StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null); | |
SLDParser parser = new SLDParser(styleFactory, new URL("file:///home/dwins/Projects/geoserver/data/release/styles/popshade.sld")); | |
Style style = parser.readXML()[0]; | |
ShapefileDataStore shapefile = | |
new ShapefileDataStore(new URL("file:///home/dwins/Projects/geoserver/data/release/data/shapefiles/states.shp")); | |
FeatureSource<SimpleFeatureType, SimpleFeature> features = | |
shapefile.getFeatureSource(shapefile.getTypeNames()[0]); | |
MapContext context = new DefaultMapContext(new MapLayer[]{ | |
new DefaultMapLayer(features, style) | |
}); | |
BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D graphics = image.createGraphics(); | |
Rectangle screenArea = new Rectangle(0, 0, 300, 300); | |
ReferencedEnvelope mapArea = features.getBounds(); | |
StreamingRenderer renderer = new StreamingRenderer(); | |
renderer.setContext(context); | |
renderer.paint(graphics, screenArea, mapArea); | |
ImageIO.write(image, "png", new File("/home/dwins/states.png")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment