使用GEOTools对图层的渲染
地图最重要的是其精确性,其次就是表现力了。对图层设置不同的渲染方案,会使地图更为容易理解、获取跟多信息,但是非常可惜的是GEOTools提供的渲染能力很弱,其提供的渲染方案为map.addLayer(featureSource,
style),我们需要做的是获取style,获取style的方法大概有以下几种:
StyleLab_StylePane me = new
StyleLab_StylePane();
me.displayShapefile();
{
return;
}
FileDataStore store =
FileDataStoreFinder.getDataStore(file);
FeatureSource featureSource =
store.getFeatureSource();//typename
MapContext map = new DefaultMapContext();
map.setTitle(“样式”);
SimpleFeatureType schema =
(SimpleFeatureType)featureSource.getSchema();
Style style = JSimpleStyleDialog.showDialog(null,
schema);
map.addLayer(featureSource, style);
JMapFrame.showMap(map);
filterFactory.literal(Color.red),
filterFactory.literal(1),
filterFactory.literal(0.5));
filterFactory.literal(Color.CYAN),
filterFactory.literal(0.5));
PolygonSymbolizer sym =
styleFactory.createPolygonSymbolizer(stroke, fill, null);
rule.symbolizers().add(sym);
FeatureTypeStyle fts =
styleFactory.createFeatureTypeStyle(new Rule[]{rule});
style.featureTypeStyles().add(fts);
style),我们需要做的是获取style,获取style的方法大概有以下几种:
1、使用JSimpleStyleDialog
2、使用SLD文件
3、自己创建一个Style类
第一种方式很简单,完整的代码如下所示:
StyleLab_StylePane me = new
StyleLab_StylePane();
me.displayShapefile();
}
private void displayShapefile() throws
Exception
Exception
{
File filepath=new
File(“E:\\workspace”);
File(“E:\\workspace”);
//file获取的是一个完整路径
File file =
JFileDataStoreChooser.showOpenFile(“shp”, filepath, null);
JFileDataStoreChooser.showOpenFile(“shp”, filepath, null);
if
(file == null)
(file == null)
{
return;
}
FileDataStore store =
FileDataStoreFinder.getDataStore(file);
String
typename=store.getTypeNames()[0];//这里的typename并没有起到作用
typename=store.getTypeNames()[0];//这里的typename并没有起到作用
FeatureSource featureSource =
store.getFeatureSource();//typename
//
MapContext
MapContext
MapContext map = new DefaultMapContext();
map.setTitle(“样式”);
SimpleFeatureType schema =
(SimpleFeatureType)featureSource.getSchema();
Style style = JSimpleStyleDialog.showDialog(null,
schema);
map.addLayer(featureSource, style);
JMapFrame.showMap(map);
可以看到,前面都是读取SHP文件的代码,真正使用的Style的就两行,因为JSimpleStyleDialog已经为我们完成了大量的工作。而自己定义一个Style的大概方法则如下:
private Style
createPolygonStyle()
createPolygonStyle()
{
//
createStroke用于创建线样式
createStroke用于创建线样式
Stroke
stroke = styleFactory.createStroke(
stroke = styleFactory.createStroke(
filterFactory.literal(Color.red),
filterFactory.literal(1),
filterFactory.literal(0.5));
//
createFill用于创建面样式
createFill用于创建面样式
Fill
fill = styleFactory.createFill(
fill = styleFactory.createFill(
filterFactory.literal(Color.CYAN),
filterFactory.literal(0.5));
PolygonSymbolizer sym =
styleFactory.createPolygonSymbolizer(stroke, fill, null);
Rule
rule = styleFactory.createRule();
rule = styleFactory.createRule();
rule.symbolizers().add(sym);
FeatureTypeStyle fts =
styleFactory.createFeatureTypeStyle(new Rule[]{rule});
Style
style = styleFactory.createStyle();
style = styleFactory.createStyle();
style.featureTypeStyles().add(fts);
return
style;
style;
}
当然,这里的一些方法被重载后除了颜色、线条粗细、透明度外还会拥有更多的样式设置参数,不过整体来说表表现力还是略渣。
转载自:https://blog.csdn.net/SCNU_Arain/article/details/84879718