我正在使用Geotools来显示班加罗尔的路线图

时间:2012-05-04 14:09:53

标签: java geotools

我想知道如何突出显示任何特定的道路。例如,我想为id = 1的道路黄色着色。我正在使用Java来显示地图。

1 个答案:

答案 0 :(得分: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;
}