特定の道路を強調表示する方法を知りたいです。たとえば、id=1の道路を黄色に着色したいとします。私はJavaを使用して地図を表示しています。
質問する
827 次
1 に答える
2
ID=1 の道路を強調表示する方法を見つけました。私が従った手順は次のとおりです。
//create a filter object
Filter filter;
//create a datastore object from .shp file
FileDataStore store= FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource=store.getFeatureSource();
//I am using CQL query to select the road that is ID=1
filter=CQL.toFilter("ID=1");
//create a SimpleFeatureCollection object for the filtered features
SimpleFeatureCollection fc=featureSource.getFeatures(filter);
//create a feature iterator to traverse through the selected features
SimpleFeatureIterator iter=fc.features();
//create a Set object to store the featureIdentifiers.
Set<FeatureId> IDs=new HashSet<FeatureId>();
//add the selected features to IDs
try{
while(iter.hasNext()){
SimpleFeature f=iter.next();
IDs.add(f.getIdentifier());
System.out.println(" "+f.getIdentifier());
}
}
finally{
iter.close();
}
//create style object to store style of selected features
Style style=createSelectedStyle(IDs);
MapContext map=new DefaultMapContext();
//show the map
map.addLayer(featureSource,style);
JMapFrame.showMap(map);
//defining the createSelectedStyle method
private Style createSelectedStyle(Set<FeatureId> IDs) {
Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR);
selectedRule.setFilter(ff.id(IDs));
Rule otherRule = createRule(LINE_COLOUR, FILL_COLOUR);
otherRule.setElseFilter(true);
FeatureTypeStyle fts = sf.createFeatureTypeStyle();
fts.rules().add(selectedRule);
fts.rules().add(otherRule);
Style style2 = sf.createStyle();
style2.featureTypeStyles().add(fts);
return style2;
}
//defining the createRule method
private Rule createRule(Color outlineColor, Color fillColor) {
Symbolizer symbolizer = null;
Fill fill = null;//not required if working with line
Stroke stroke = sf.createStroke(ff.literal(outlineColor), ff.literal(LINE_WIDTH));
symbolizer = sf.createLineSymbolizer(stroke, "the_geom");
Rule rule = sf.createRule();
rule.symbolizers().add(symbolizer);
return rule;
}
于 2012-05-13T06:15:37.247 に答える