I want to know how to highlight any specific road. For example i want to color a road yellow that has id=1. I am using Java to display the map.
Asked
Active
Viewed 834 times
-4
-
1You posted no code, we have no idea how far you have progressed. I'd love to give an answer like "In line 14, insert this statement: ...", but that's not happening. – f1sh May 04 '12 at 14:23
-
I am new to GeoTools. I have a program that reads the shape file and displays the map on the screen – Sandeep May 05 '12 at 05:28
1 Answers
2
I have found how to highlight the road which has ID=1. Steps that I followed are:
//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;
}

Brendan Green
- 11,676
- 5
- 44
- 76

Sandeep
- 106
- 1
- 4
-
here the constants SELECTED_COLOR,LINE_COLOUR, FILL_COLOUR are of type Color from java.awt package – Sandeep May 13 '12 at 06:19